-
Notifications
You must be signed in to change notification settings - Fork 0
/
MMM-WOTD.js
160 lines (135 loc) · 5.33 KB
/
MMM-WOTD.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
Module.register("MMM-WOTD", {
defaults: {
updateInterval: 86400000,
retryDelay: 5000,
rotateInterval: 1000 * 60,
showExamples: true,
showExampleTranslations: true,
language: ["spanish"] //spanish or portuguese
},
start: function () {
Log.info(`Starting module: ${this.name}`);
suspended = false;
this.timer = null;
this.index = 0;
this.apiData = null;
this.getData();
this.scheduleUpdate();
},
stop: function () {
Log.info('Stopping module ' + this.name);
},
resume: function () {
Log.info('Resuming module ' + this.name);
Log.debug('with config: ' + JSON.stringify(this.config));
this.suspended = false;
this.draw(this.apiData)
},
suspend: function () {
Log.info('Suspending module ' + this.name);
this.suspended = true;
},
// getHeader: function () {
// return `Word of the Day`
// },
getData: function () {
this.sendSocketNotification("WOTD_GET_DATA", this.config)
},
scheduleUpdate: function (delay) {
var nextUpdate = this.config.updateInterval
if (typeof delay != "undefined" && delay >= 0) {
nextUpdate = delay
}
var self = this;
setInterval(function () {
self.getData();
}, nextUpdate)
},
socketNotificationReceived: function (notification, payload) {
if (notification === "WOTD_DATA") {
this.apiData = payload;
this.draw(this.apiData);
} else {
Log.log("No Data Received");
return;
}
},
draw: function (payload) {
clearTimeout(this.timer);
wrapper = this.wrapper
this.timer = null;
payload = this.apiData
var t = payload[this.index]
if (payload.length > 1) {
var mylang = document.getElementById("wotd-lang")
var wotdword = document.getElementById("wotd-word")
var translation = document.getElementById("wotd-translation")
var wr = document.getElementById("wotd-wr")
var we = document.getElementById("wotd-we")
var we2 = document.getElementById("wotd-we2")
mylang.innerHTML = "Language - " + `${t.language}`.toUpperCase()
wotdword.innerHTML = "Word - " + `${t.data[0].word}`.toUpperCase()
translation.innerHTML = "Meaning - " + (t.data[0].translation ? `${t.data[0].translation}`.toUpperCase() : "")
wr.innerHTML = `${t.data[0].examples.wordex}`
we.innerHTML = (t.data[0].examples.wordextr ? `${t.data[0].examples.wordextr}` : "")
we2.innerHTML = (t.data[0].examples.wordextr2 ? `${t.data[0].examples.wordextr2}` : "")
setTimeout(() => {
}, 900)
this.timer = setTimeout(() => {
this.index++
if (this.index >= this.apiData.length) this.index = 0
this.draw()
}, this.config.rotateInterval)
} else {
var mylang = document.getElementById("wotd-lang")
var wotdword = document.getElementById("wotd-word")
var translation = document.getElementById("wotd-translation")
var wr = document.getElementById("wotd-wr")
var we = document.getElementById("wotd-we")
var we2 = document.getElementById("wotd-we2")
mylang.innerHTML = "Language - " + `${t.language}`.toUpperCase()
wotdword.innerHTML = "Word - " + `${t.data[0].word}`.toUpperCase()
translation.innerHTML = "Meaning - " + `${t.data[0].translation}`.toUpperCase()
wr.innerHTML = `- ${t.data[0].examples.wordex}`
we.innerHTML = (t.data[0].examples.wordextr ? `${t.data[0].examples.wordextr}` : "")
we2.innerHTML = (t.data[0].examples.wordextr2 ? `${t.data[0].examples.wordextr2}` : "")
}
return wrapper
},
getDom: function () {
const wrapper = document.createElement("div");
wrapper.id = "wotd"
const lang = document.createElement("div");
lang.id = "wotd-lang"
lang.className = "bright small"
wrapper.appendChild(lang);
const word = document.createElement("div");
word.id = "wotd-word"
word.className = "bold bright medium"
wrapper.appendChild(word);
const translation = document.createElement("div")
translation.id = "wotd-translation"
translation.className = "medium"
wrapper.appendChild(translation);
if (this.config.showExamples) {
const list = document.createElement('div')
list.className = "small"
const listItem = document.createElement('div')
const wr = document.createElement('div')
wr.id = "wotd-wr"
listItem.appendChild(wr)
if (this.config.showExampleTranslations) {
const we = document.createElement('div')
we.id = "wotd-we"
listItem.appendChild(we)
const we2 = document.createElement('div')
we2.id = "wotd-we2"
listItem.appendChild(we2)
}
list.appendChild(listItem)
wrapper.appendChild(list)
}
this.wrapper = wrapper
return this.wrapper
}
})