-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-Dictons.js
193 lines (168 loc) · 5.7 KB
/
MMM-Dictons.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
//MMM-Dictons.js:
Module.register("MMM-Dictons",{
// Default module config.
defaults: {
// all => select all sayings except sayings linked to date (default)
// day => select sayings linked to date
// <theme> => theme named saying list in config.sayings
select: "all",
sayings : { "all" : [
"A beautiful thing is never perfect.",
"Beauty is in the eye of the beholder.",
"Beauty is only skin deep.",
"Butterflies come to pretty flowers.",
"Do not dissect a rainbow. ",
"Don't gild the lily.",
"Everything is lovely when the geese honk high.",
"Music has charms to soothe a savage beast.",
"See life through an artist's eye.",
"Stop and smell the roses.",
"The eyes are the windows of the soul.",
"Everything has beauty, but not everyone sees it.",
"Beauty is power; a smile is its sword.",
"You can't buy love.",
"Love is like the wind, you can't see it but you can feel it.",
"Where there is love there is life.",
"A morning without coffee is like sleep.",
"Decaffeinated coffee is the devil's blend.",
"I'd rather take coffee than compliments just now.",
"Coffee and love are best when they are hot.",
"Every pot will find its lid.",
"If it were not for hope, the heart would break.",
"Where there's life, there's hope."
] },
updateInterval: 10000,
fadeSpeed: 4000,
language: "en"
},
requiresVersion: "2.1.0",
file2load: "dictonsEN.json",
// Define required scripts.
// moment.js to get current day values
getScripts: function() {
return ["moment.js"];
},
getTranslations: function() {
return {
fr: "translations/fr.json",
en: "translations/en.json"
}
},
// start loading sayings from file and set update interval
start: function() {
Log.info(this.name + " - Starting module...");
// Set locale.
moment.locale(this.config.language);
if (this.config.language != null) {
this.file2load = "dictons/" + this.translate("FILE");
Log.info(this.name + " - loading file : " + this.file2load);
this.loadFile((response) => {
this.config.sayings = JSON.parse(response);
});
}
// Schedule update timer.
var self = this;
setInterval(function() {
self.updateDom(self.config.fadeSpeed);
}, this.config.updateInterval);
},
// Override dom generator.
getDom: function() {
Log.log(this.name + " - getDom method called ");
var wrapper = document.createElement("div");
wrapper.innerHTML = this.getRandomSaying();;
return wrapper;
},
// Module's notifications
notificationReceived: function(notification, payload, sender) {
if (sender) {
Log.log(this.name + " - received a module notification: " + notification + " from sender: " + sender.name);
} else {
Log.log(this.name + " - received a system notification: " + notification);
}
},
/* loadFile(callback)
* Retrieve a file from the local filesystem
*/
loadFile: function(callback) {
Log.log(this.name + " - loading file using XMLHttpRequest : " + this.file2load);
var xobj = new XMLHttpRequest();
xobj.overrideMimeType("application/json");
xobj.open("GET", this.file(this.file2load), true);
xobj.onreadystatechange = function() {
if (xobj.readyState == 4 && xobj.status == "200") {
callback(xobj.responseText);
}
};
xobj.send(null);
},
/*
* get Saying to be displayed
*/
getRandomSaying: function() {
var select = this.config.select ;
var saying = this.translate("LOAD");
var selection = [];
var totalLength = 0;
if (/^day,.*/.test(select) || /,day$/.test(select) || /,day,/.test(select) || /^day$/.test(select)) {
// display only day's saying
Log.log(this.name + " - Display day");
if (typeof this.config.sayings["day"] != 'undefined') {
Log.log(this.name + " - Month : " + (moment().month()+1) + " / Day of month : " + moment().date());
moment().month()
var idx = Math.floor(Math.random() * this.config.sayings["day"][(moment().month()+1).toString()][moment().date().toString()].length);
Log.log(this.name + " - index : " + idx);
saying = this.config.sayings["day"][(moment().month()+1).toString()][moment().date().toString()][idx];
} else {
saying = this.translate("NOSAYING");
}
} else {
if (/^all,.*/.test(select) || /,all$/.test(select) || /,all,/.test(select) || /^all$/.test(select)) {
// display all except day's sayings
Log.log(this.name + " - Display all");
for (var theme in this.config.sayings) {
if (theme != "day") {
var rec = {};
rec.theme = theme;
rec.length = this.config.sayings[theme].length;
rec.start = totalLength + 1;
totalLength = totalLength + rec.length;
rec.end = totalLength;
selection.push(rec);
}
}
} else {
// display only theme in 'config.select' list
Log.log(this.name + " - Display themes");
var selectors = this.config.select.split(",");
for (var theme in this.config.sayings) {
if (theme != "day") {
for (j=0; j<selectors.length; j++) {
if (theme === selectors[j]) {
var rec = {};
rec.theme = theme;
rec.length = this.config.sayings[theme].length;
rec.start = totalLength + 1;
totalLength = totalLength + rec.length;
rec.end = totalLength;
selection.push(rec);
}
}
}
}
}
}
if (totalLength > 0) {
var idx = Math.floor(Math.random() * totalLength) + 1;
Log.log(this.name + " - length = " + totalLength + " random number : " + idx);
for (i=0; i < selection.length; i++) {
if ((selection[i].start <= idx) && (selection[i].end >= idx)) {
saying = this.config.sayings[selection[i].theme][idx - selection[i].start];
break;
}
}
}
return saying;
}
}
);