-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMMM-DateCounter.js
200 lines (168 loc) · 6.15 KB
/
MMM-DateCounter.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* Magic Mirror
* Module: Date Counter
*
* By Jesse Alltop
*/
Module.register("MMM-DateCounter", {
//Module config defaults
defaults: {
updateInterval: 1000,
remoteFile: null,
eventDate: "2020-07-15",
eventStartTime: "0:00",
eventTitle: "Happy Birthday!",
showLongCountdown: true,
longCountdownFormat: "year,month,week,day,hour,minute,second",
longCountdownAutoSize: true,
// icon: "/modules/MMM-DateCounter/Flat_tick_icon.svg"
// icon: "https://upload.wikimedia.org/wikipedia/commons/7/73/Flat_tick_icon.svg"
icon: null,
hideOnExpiration: false,
showOnOneLine: false
},
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
getStyles: function () {
return ["MMM-DateCounter.css"];
},
// Define start sequence.
start: function () {
Log.info("Starting module: " + this.name);
var self = this;
if (this.config.remoteFile !== null) {
this.eventFile(function (response) {
self.config.dateCounter = JSON.parse(response);
self.updateDom();
});
}
// Schedule update timer.
setInterval(function () {
self.updateDom();
}, this.config.updateInterval);
},
eventFile: function (callback) {
var xobj = new XMLHttpRequest(),
isRemote = this.config.remoteFile.indexOf("http://") === 0 || this.config.remoteFile.indexOf("https://") === 0,
path = isRemote ? this.config.remoteFile : this.file(this.config.remoteFile);
xobj.overrideMimeType("application/json");
xobj.open("GET", path, true);
xobj.onreadystatechange = function () {
if (xobj.readyState === 4 && xobj.status === 200) {
callback(xobj.responseText);
}
};
xobj.send(null);
},
// Override dom generator.
getDom: function () {
var wrapper = document.createElement("div");
wrapper.id = this.config.classes ? this.config.classes : "DATE_COUNTER";
if(this.config.hideOnExpiration && !this.isFutureDate(moment(this.config.eventDate + "T" + this.config.eventStartTime, "YYYY-MM-DDThh:mm"))) {
wrapper.hidden = true;
return wrapper;
}
// get the eventTitle text
var titleDiv = document.createElement("div");
titleDiv.id = "DATE_COUNTER_TITLE";
var eventTitleText = this.config.eventTitle;
titleDiv.appendChild(document.createTextNode(eventTitleText));
//get the countdown to date
var countdownTextDiv = document.createElement("div");
countdownTextDiv.id = "DATE_COUNTER_COUNTDOWN_TEXT";
var remainingTime;
if (this.config.showLongCountdown) {
remainingTime = this.getDuration(moment(this.config.eventDate + "T" + this.config.eventStartTime, "YYYY-MM-DDThh:mm"));
} else {
remainingTime = moment(this.config.eventDate + "T" + this.config.eventStartTime, "YYYY-MM-DDThh:mm").fromNow();
}
countdownTextDiv.appendChild(document.createTextNode(remainingTime));
if (this.config.longCountdownAutoSize) {
var count = (remainingTime.match(/,/g) || []).length;
var newFontSize = 20 - (count);
countdownTextDiv.innerHTML = "<span style=\"font-size:" + newFontSize + "pt\">" + remainingTime + "</span>";
}
var event = document.createElement("div");
event.id = "DATE_COUNTER_TEXT_GROUP";
// process all the parts of the compliment text
event.appendChild(titleDiv);
//add line
var hr = document.createElement("hr");
hr.classList.add("date_counter_hr")
event.appendChild(hr);
//add differnece between current time and event date/time
event.appendChild(countdownTextDiv);
wrapper.appendChild(event);
if (this.config.icon !== null) {
var iconDiv = document.createElement("div");
iconDiv.id = "DATE_COUNTER_ICON";
var icon = document.createElement("img");
icon.src = this.config.icon;
iconDiv.appendChild(icon);
wrapper.appendChild(iconDiv);
wrapper.style.display = "grid";
}
return wrapper;
},
isFutureDate: function(time) {
return (moment().diff(time) < 0);
},
getDuration: function (time) {
var units = new Map();
if (this.isFutureDate(time)) {
this.updateMapAndRemoveUnitFuture(units, "year", "years", time);
this.updateMapAndRemoveUnitFuture(units, "month", "months", time);
this.updateMapAndRemoveUnitFuture(units, "week", "weeks", time);
this.updateMapAndRemoveUnitFuture(units, "day", "days", time);
this.updateMapAndRemoveUnitFuture(units, "hour", "hours", time);
this.updateMapAndRemoveUnitFuture(units, "minute", "minutes", time);
this.updateMapAndRemoveUnitFuture(units, "second", "seconds", time);
} else {
this.updateMapAndRemoveUnitPast(units, "year", "years", time);
this.updateMapAndRemoveUnitPast(units, "month", "months", time);
this.updateMapAndRemoveUnitPast(units, "week", "weeks", time);
this.updateMapAndRemoveUnitPast(units, "day", "days", time);
this.updateMapAndRemoveUnitPast(units, "hour", "hours", time);
this.updateMapAndRemoveUnitPast(units, "minute", "minutes", time);
this.updateMapAndRemoveUnitPast(units, "second", "seconds", time);
}
units.forEach(this.buildDurationString);
return this.buildCountdownDisplay(units);
},
updateMapAndRemoveUnitFuture: function (map, key, unit, time) {
if (this.config.longCountdownFormat.toLowerCase().indexOf(key) === -1) {
console.log(key + " was not setup in the config");
return;
}
map.set(key, Math.abs(moment().diff(time, unit, false)));
time = time.subtract(map.get(key), unit);
},
updateMapAndRemoveUnitPast: function (map, key, unit, time) {
if (this.config.longCountdownFormat.toLowerCase().indexOf(key) === -1) {
console.log(key + " was not setup in the config");
return;
}
map.set(key, moment().diff(time, unit, false));
time = time.add(map.get(key), unit);
},
buildCountdownDisplay(units) {
var displayText = "";
for (const [key, value] of units.entries()) {
if (key === "second" || value !== 0) {
displayText = displayText.concat(displayText.length === 0 ? "" : ", ", value, " ", key, value > 1 ? "s" : "");
}
}
if(this.config.showOnOneLine) {
displayText = displayText.split(/,/).reduce((a, b, i) => i % 2 == 0 ? a + ", " + b : a + "," + b);
} else {
displayText = displayText.split(/,/).reduce((a, b, i) => i % 2 == 0 ? a + ",\n" + b : a + "," + b);
}
return displayText;
},
buildDurationString: function (value, key) {
if (value !== 0) {
return value + key + value > 1 ? "s" : "";
}
}
});