-
Notifications
You must be signed in to change notification settings - Fork 3
/
MMM-EmbedURL.js
419 lines (358 loc) · 12.2 KB
/
MMM-EmbedURL.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/* MagicMirror²
* Module: MMM-EmbedURL
*
* By Tom Hirschberger
* MIT Licensed.
*/
Module.register("MMM-EmbedURL", {
defaults: {
basicElementType: "div", // this module uses a lot of wrappers and basic elements. This option decides about the basic element (div or span)
embedElementType: "iframe", // the elements can either be embeded as iframe webview
updateInterval: 60, // how often should the module be refreshed
animationSpeed: 500, // use this animation speed if the dom objects of the module gets updated
positions: "tie",
attributes: [
"frameborder=0"
],
imgDecodeCheckInterval: -1,
updateDomOnResume: true
},
suspend() {
const self = this
self.resetTimer(-1, false)
},
resume() {
const self = this
self.resetTimer(self.config.updateInterval, self.config.updateDomOnResume)
},
getScripts() {
return [this.file("node_modules/@iconify/iconify/dist/iconify.min.js")]
},
getStyles() {
return ["font-awesome.css", "embedURL.css"]
},
/*
** creates html objects based on a given string
** see: https://stackoverflow.com/questions/494143/creating-a-new-dom-element-from-an-html-string-using-built-in-dom-methods-or-pro/35385518#35385518
*/
htmlToElement(theString) {
const template = document.createElement("template")
theString = theString.trim() // Never return a text node of whitespace as the result
template.innerHTML = theString
return template.content.firstChild
},
getTitleElement(subConfig, additionalClasses) {
const self = this
if (subConfig != null) {
const titleElement = document.createElement(self.config.basicElementType)
if (Array.isArray(subConfig)) {
titleElement.classList.add("titleWrapper")
let idx = 0
for (const curTitle of subConfig) {
const curTitleElement = document.createElement(self.config.basicElementType)
curTitleElement.appendChild(self.htmlToElement(String(curTitle)))
curTitleElement.classList.add("title")
curTitleElement.classList.add(`title${idx}`)
additionalClasses.forEach(element => curTitleElement.classList.add(element))
titleElement.appendChild(curTitleElement)
idx += 1
}
} else {
titleElement.classList.add("title")
titleElement.appendChild(self.htmlToElement(String(subConfig)))
}
additionalClasses.forEach(element => titleElement.classList.add(element))
return titleElement
} else {
return null
}
},
getFontIconElement(subConfig, additionalClasses) {
const self = this
// Log.log("Trying to get font icon element")
if (subConfig != null) {
// Log.log("subConfig != null")
let fontIconElement = null
if (Array.isArray(subConfig)) {
fontIconElement = document.createElement(self.config.basicElementType)
fontIconElement.classList.add("iconWrapper")
let idx = 0
for (const curIcon of subConfig) {
let curIconElement
if (curIcon.startsWith("fa ")) {
curIconElement = document.createElement("i")
curIcon.split(" ").forEach(element => curIconElement.classList.add(element))
curIconElement.setAttribute("aria-hidden", "true")
} else {
curIconElement = document.createElement("span")
curIconElement.classList.add("iconify-inline")
curIconElement.setAttribute("data-icon", curIcon)
}
curIconElement.classList.add("fontIcon")
curIconElement.classList.add(`fontIcon${idx}`)
additionalClasses.forEach(element => curIconElement.classList.add(element))
fontIconElement.appendChild(curIconElement)
idx += 1
}
} else {
if (subConfig.startsWith("fa ")) {
fontIconElement = document.createElement("i")
subConfig.split(" ").forEach(element => fontIconElement.classList.add(element))
fontIconElement.setAttribute("aria-hidden", "true")
} else {
fontIconElement = document.createElement("span")
fontIconElement.classList.add("iconify-inline")
fontIconElement.setAttribute("data-icon", subConfig)
}
fontIconElement.classList.add("fontIcon")
}
additionalClasses.forEach(element => fontIconElement.classList.add(element))
return fontIconElement
} else {
return null
}
},
getImgIconElement(subConfig, additionalClasses) {
const self = this
if (subConfig != null) {
let imgIconElement = null
if (Array.isArray(subConfig)) {
imgIconElement = document.createElement(self.config.basicElementType)
imgIconElement.classList.add("iconWrapper")
let idx = 0
for (const curIcon of subConfig) {
const curIconElement = document.createElement("img")
imgIconElement.setAttribute("src", curIcon)
curIconElement.classList.add("imgIcon")
curIconElement.classList.add(`imgIcon${idx}`)
if (subConfig.endsWith(".svg")) {
curIconElement.classList.add("svgIcon")
}
additionalClasses.forEach(element => curIconElement.classList.add(element))
imgIconElement.appendChild(curIconElement)
idx += 1
}
} else {
imgIconElement = document.createElement("img")
imgIconElement.setAttribute("src", subConfig)
imgIconElement.classList.add("imgIcon")
if (subConfig.endsWith(".svg")) {
imgIconElement.classList.add("svgIcon")
}
}
additionalClasses.forEach(element => imgIconElement.classList.add(element))
return imgIconElement
} else {
return null
}
},
getEmbedElement(subConfig, additionalClasses, attributes, embedElementType, appendTimestamp, imgDecodeCheckInterval) {
const self = this
if (subConfig != null) {
const embedElement = document.createElement(embedElementType)
if ((embedElementType === "img") && (imgDecodeCheckInterval > 0)) {
self.imgs.push([embedElement, imgDecodeCheckInterval])
}
if ((typeof appendTimestamp !== "undefined") && appendTimestamp) {
const url = new URL(subConfig)
url.searchParams.append("timestamp", Math.floor(Date.now() / 1000))
embedElement.setAttribute("src", url)
} else {
embedElement.setAttribute("src", subConfig)
}
if (attributes != null) {
// Log.log(JSON.stringify(attributes))
for (const curAttribute of attributes) {
if (curAttribute.indexOf("=") > 0) {
const attArray = curAttribute.split("=")
const key = attArray[0]
let value = ""
if (typeof attArray[1] !== "undefined") {
value = attArray[1]
}
embedElement.setAttribute(key.trim(), value.trim())
} else {
embedElement.setAttribute(curAttribute.trim(), "")
}
}
}
embedElement.classList.add("embeded")
additionalClasses.forEach(element => embedElement.classList.add(element))
return embedElement
} else {
return null
}
},
getWrapperElement(subConfig, fallbackPositions, fallbackAttributes, fallbackEmbedElementType, fallbackAppendTimestamp, fallbackImgDecodeCheckInterval, depth = 0) {
if (subConfig != null) {
const self = this
if ((subConfig.profiles || null) != null) {
if (!subConfig.profiles.includes(self.currentProfile)) {
return null
}
}
const classes = []
const subClasses = subConfig.classes || null
if (subClasses != null) {
subClasses.split(" ").forEach(curClass => classes.push(curClass))
}
let positions = subConfig.positions || null
if (positions == null) {
positions = fallbackPositions
}
let attributes = subConfig.attributes || null
if (attributes == null) {
attributes = fallbackAttributes
}
let embedElementType = subConfig.embedElementType || null
if (embedElementType == null) {
embedElementType = fallbackEmbedElementType
}
let appendTimestamp
if (typeof subConfig.appendTimestamp !== "undefined") {
appendTimestamp = subConfig.appendTimestamp
} else {
appendTimestamp = fallbackAppendTimestamp
}
let imgDecodeCheckInterval
if (typeof subConfig.imgDecodeCheckInterval !== "undefined") {
imgDecodeCheckInterval = subConfig.imgDecodeCheckInterval * 1000
} else {
imgDecodeCheckInterval = fallbackImgDecodeCheckInterval
}
const wrapper = document.createElement(self.config.basicElementType)
wrapper.classList.add("embededWrapper")
wrapper.classList.add(`embededWrapper${depth}`)
classes.forEach(element => wrapper.classList.add(element))
const titleElement = self.getTitleElement(subConfig.title || null, classes)
const fontIconElement = self.getFontIconElement(subConfig.fontIcon || null, classes)
const imgIconElement = self.getImgIconElement(subConfig.imgIcon || null, classes)
const embedConfig = subConfig.embed || null
let embedElement = null
if (embedConfig != null) {
if (Array.isArray(embedConfig)) {
embedElement = document.createElement(self.config.basicElementType)
embedElement.classList.add("embededSubWrapper")
embedElement.classList.add(`embededSubWrapper${depth}`)
classes.forEach(element => embedElement.classList.add(element))
for (let idx = 0; idx < embedConfig.length; idx++) {
const curEmbed = embedConfig[idx]
let curEmbedElement = null
if (typeof curEmbed === "string") {
curEmbedElement = self.getEmbedElement(curEmbed, classes, attributes, embedElementType, appendTimestamp, imgDecodeCheckInterval)
} else {
curEmbedElement = self.getWrapperElement(curEmbed || null, positions, attributes, embedElementType, appendTimestamp, imgDecodeCheckInterval, depth + 1)
}
if (curEmbedElement != null) {
embedElement.appendChild(curEmbedElement)
}
}
} else {
embedElement = self.getEmbedElement(embedConfig, classes, attributes, embedElementType, appendTimestamp, imgDecodeCheckInterval)
}
}
let atLeastOneAdded = false
const curWrapper = wrapper
let iconElement = fontIconElement
if (imgIconElement !== null) {
iconElement = imgIconElement
}
for (const posChar of positions) {
if (posChar === "t") {
if (titleElement != null) {
atLeastOneAdded = true
curWrapper.appendChild(titleElement)
}
} else if (posChar === "i") {
if (iconElement != null) {
atLeastOneAdded = true
curWrapper.appendChild(iconElement)
}
} else if (posChar === "e") {
if (embedElement != null) {
atLeastOneAdded = true
curWrapper.appendChild(embedElement)
}
} else {
Log.log("UNKNOWN CHARACTER")
}
}
if (atLeastOneAdded) {
return wrapper
} else {
return null
}
} else {
return null
}
},
getDom() {
const self = this
console.log(self.name+": UPDATE DOM")
for (let imgIdx = 0; imgIdx < self.imgsTimeouts.length; imgIdx++) {
clearTimeout(self.imgsTimeouts[imgIdx])
}
self.imgsTimeouts = []
self.imgs = []
let wrapper = self.getWrapperElement(self.config, self.config.positions, self.config.attributes, self.config.embedElementType, self.config.imgDecodeCheckInterval * 1000, 0)
if (wrapper == null) {
wrapper = document.createElement(self.config.basicElementType)
}
wrapper.classList.add("embed")
wrapper.classList.add("rootWrapper")
for (let imgIdx = 0; imgIdx < self.imgs.length; imgIdx++) {
self.checkImgSrc(imgIdx)
}
return wrapper
},
async checkImgSrc(imgIdx) {
const self = this
const imgElement = self.imgs[imgIdx][0]
try {
await imgElement.decode()
} catch {
Log.log(`Image with idx: ${imgIdx} has an undecodeable URL. Refreshing it!`)
const src = imgElement.src
imgElement.src = ""
imgElement.src = src
}
self.imgsTimeouts[imgIdx] = setTimeout(() => {
self.checkImgSrc(imgIdx)
}, self.imgs[imgIdx][1])
},
start() {
const self = this
self.currentProfile = null
self.imgs = []
self.imgsTimeouts = []
const curClasses = []
const classConfig = self.config.classes || null
if (classConfig !== null) {
classConfig.split(" ").forEach(curClass => curClasses.push(curClass))
}
self.classes = curClasses
self.sendSocketNotification("CONFIG", self.config)
self.resetTimer(self.config.updateInterval, true)
},
resetTimer(interval, updateDom) {
const self = this
if (self.refreshTimer) {
clearTimeout(self.refreshTimer)
self.refreshTimer = null
}
if (interval > 0) {
self.refreshTimer = setTimeout(() => {
self.resetTimer(interval, true)
}, interval * 1000)
if ((!self.hidden) && updateDom) {
self.updateDom(self.config.animationSpeed)
}
}
},
notificationReceived(notification, payload) {
const self = this
if (notification === "CHANGED_PROFILE") {
self.currentProfile = payload.to
}
}
})