-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-shabbat.js
163 lines (131 loc) · 4.44 KB
/
MMM-shabbat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/* Magic Mirror
* Module: Pi-Hole Stats
*
* By Sheya Bernstein https://github.com/sheyabernstein/MMM-shabbat
* MIT Licensed.
*/
Module.register("MMM-shabbat", {
// Default module config.
defaults: {
minutesBefore: "18",
minutesAfter: "50",
ashkenaz: true,
latitude: "",
longitude: "",
tzid: "",
updateInterval: 30 * 60 * 1000, // every thirty minutes
animationSpeed: 1000,
retryDelay: 2500,
initialLoadDelay: 0,
modulesHidden: false, // don't change
},
// Define required scripts.
getScrips: function() {
return ["moment.min.js"];
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.items = [];
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
var requiredConfigs = ["latitude", "longitude", "tzid"]
for (var i in requiredConfigs) {
req = requiredConfigs[i]
if (this.config[req] === "") {
wrapper.innerHTML = "Please set the correct <i>" + req + "</i> in the config for module " + this.name + ".";
wrapper.className = "dimmed light small";
return wrapper;
}
}
if (!this.loaded) {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "dimmed light";
return wrapper;
}
var events = {};
for (var i in this.items) {
var item = this.items[i];
var date = moment(item["date"]).calendar().split(" at")[0];
var title = item["title"];
if (date === "Saturday") {date = "Shabbos";}
if(events.hasOwnProperty(date)) {
events[date].push(title);
}
else {
events[date] = [title];
}
}
eventKeys = Object.keys(events).slice(0, 3);
for (var i in eventKeys) {
var day = eventKeys[i];
var dayEvents = events[eventKeys[i]];
if (dayEvents) {
dateEl = document.createElement("div");
dateEl.className = "xsmall light";
dateEl.innerHTML = day;
wrapper.appendChild(dateEl);
for (var e in dayEvents) {
eventEl = document.createElement("div");
eventEl.className = "small";
eventEl.innerHTML = dayEvents[e];
eventEl.style["text-indent"] = "1em";
wrapper.appendChild(eventEl);
}
}
}
return wrapper;
},
updateTimes: function() {
var self = this;
var url = self.makeURL();
var retry = true;
var timesRequest = new XMLHttpRequest();
timesRequest.open("GET", url, true);
timesRequest.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status === 200) {
self.processTimes(JSON.parse(this.response));
} else {
Log.error(self.name + ": Could not load shabbat updateTimes.");
}
if (retry) {
self.scheduleUpdate((self.loaded) ? -1 : self.config.retryDelay);
}
}
};
timesRequest.send();
},
scheduleUpdate: function(delay) {
var nextLoad = this.config.updateInterval;
if (typeof delay !== "undefined" && delay >= 0) {
nextLoad = delay;
}
var self = this
setTimeout(function() {
self.updateTimes();
}, nextLoad);
},
makeURL: function() {
var c = this.config
var ashkenaz = "on"
if (!c.ashkenaz) {
ashkenaz = "off"
}
var url = "https://www.hebcal.com/shabbat/?cfg=json&b=" + c.minutesBefore + "&m=" + c.minutesAfter + "&a=" + ashkenaz + "&geo=pos&latitude=" + c.latitude + "&longitude=" + c.longitude + "&tzid=" + c.tzid;
return url
},
processTimes: function(data) {
if (!data || !data['items']) {
// Did not receive usable new data.
return;
}
this.items = data['items']
this.loaded = true;
this.updateDom(this.config.animationSpeed);
}
})