-
Notifications
You must be signed in to change notification settings - Fork 2
/
MMM-kudos.js
212 lines (184 loc) · 4.98 KB
/
MMM-kudos.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
201
202
203
204
205
206
207
208
209
210
211
212
/* global Module */
/* Magic Mirror
* Module: MMM-kudos
*
* By Thomas Mohaupt
* MIT Licensed.
*/
Module.register("MMM-kudos", {
defaults: {
kudos: {
anytime: ["Und jetzt einen Kaffee!"],
morning: ["Guten Morgen, Sonnenschein!"],
lunch: ["Mahlzeit!"],
afternoon: ["Schon Feierabend?"],
evening: ["Eine Augenweide!"],
night: ["Schlaf schön!"],
"....-01-01": [
"Gesundes Neues Jahr!",
"Neue Vorsätze für das neue Jahr gefasst?",
],
},
fadeSpeed: 4000,
hourmap: {
5: "morning",
11: "lunch",
15: "afternoon",
19: "evening",
23: "night",
},
remoteFile: null,
random: true,
shrinkLimit: 35,
updateInterval: 30000,
},
lastIndexUsed: -1,
// Set currentweather from module
currentWeatherType: "",
requiresVersion: "2.1.0", // Required version of MagicMirror
// Define required scripts.
getScripts: function () {
return ["moment.js"];
},
// Define start sequence.
start: async function () {
Log.info(`Starting module: ${this.name}`);
this.lastKudoIndex = -1;
if (this.config.remoteFile !== null) {
const response = await this.loadKudoFile();
this.config.kudos = JSON.parse(response);
this.updateDom();
}
// set subset at begin of day to same as end of day
this.config.hourmap[0] = this.config.hourmap[0]
? this.config.hourmap[0]
: this.config.hourmap[this.hourKey(24)];
// Schedule update timer.
setInterval(() => {
this.updateDom(this.config.fadeSpeed);
}, this.config.updateInterval);
},
/**
* Generate a random index for a list of kudos.
* @param {string[]} kudos Array with kudos.
* @returns {number} a random index of given array
*/
randomIndex: function (kudos) {
if (kudos.length === 1) {
return 0;
}
const generate = function () {
return Math.floor(Math.random() * kudos.length);
};
let kudoIndex = generate();
while (kudoIndex === this.lastKudoIndex) {
kudoIndex = generate();
}
this.lastKudoIndex = kudoIndex;
return kudoIndex;
},
hourKey: function (hour) {
while (!(hour in this.config.hourmap) || hour < 0) {
hour = hour - 1;
}
return hour;
},
/**
* Retrieve an array of kudos for the time of the day.
* @returns {string[]} array with kudos for the time of the day.
*/
kudoArray: function () {
const hour = moment().hour();
const date = moment().format("YYYY-MM-DD");
let kudos = [];
var hourkey = this.hourKey(hour);
if (hourkey > -1) {
kudos = [...this.config.kudos[this.config.hourmap[hourkey]]];
}
// Add kudos based on weather
if (this.currentWeatherType in this.config.kudos) {
Array.prototype.push.apply(
kudos,
this.config.kudos[this.currentWeatherType]
);
}
// Add kudos for anytime
Array.prototype.push.apply(kudos, this.config.kudos.anytime);
// Add kudos for special days
for (let entry in this.config.kudos) {
if (new RegExp(entry).test(date)) {
Array.prototype.push.apply(kudos, this.config.kudos[entry]);
}
}
return kudos;
},
/**
* Retrieve a file from the local filesystem
* @returns {Promise} Resolved when the file is loaded
*/
loadKudoFile: async function () {
const isRemote =
this.config.remoteFile.indexOf("http://") === 0 ||
this.config.remoteFile.indexOf("https://") === 0,
url = isRemote
? this.config.remoteFile
: this.file(this.config.remoteFile);
const response = await fetch(url);
return await response.text();
},
/**
* Retrieve a random kudo.
* @returns {string} a kudo
*/
getRandomKudo: function () {
// get the current time of day kudos list
const kudos = this.kudoArray();
// variable for index to next message to display
let index;
// are we randomizing
if (this.config.random) {
// yes
index = this.randomIndex(kudos);
} else {
// no, sequential
// if doing sequential, don't fall off the end
index = this.lastIndexUsed >= kudos.length - 1 ? 0 : ++this.lastIndexUsed;
}
return kudos[index] || "";
},
// Override dom generator.
getDom: function () {
const wrapper = document.createElement("div");
wrapper.className = this.config.classes
? this.config.classes
: "thin xlarge bright pre-line";
// get the kudo text
const kudoText = this.getRandomKudo();
// split it into parts on newline text
const parts = kudoText.split("\n");
// create a span to hold the kudo
const kudo = document.createElement("span");
// process all the parts of the kudo text
for (const part of parts) {
if (part !== "") {
// create a text element for each part
kudo.appendChild(document.createTextNode(part));
// add a break
kudo.appendChild(document.createElement("BR"));
}
}
// only add kudo to wrapper if there is actual text in there
if (kudo.children.length > 0) {
// remove the last break
kudo.lastElementChild.remove();
wrapper.appendChild(kudo);
}
return wrapper;
},
// Override notification handler.
notificationReceived: function (notification, payload, sender) {
if (notification === "CURRENTWEATHER_TYPE") {
this.currentWeatherType = payload.type;
}
},
});