-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathMMM-News-QR.js
110 lines (94 loc) · 2.89 KB
/
MMM-News-QR.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
/* MagicMirror²
* Module: MMM-News-QR
*
* By Thierry Nischelwitzer http://nischi.ch
* MIT Licensed.
*/
Module.register("MMM-News-QR", {
defaults: {
updateType : "push", // possible values (polling, push)
interval : 2000, // only needed if updateType is polling
animationSpeed : 2500,
colorDark : "#fff",
colorLight : "#000",
imageSize : 150
},
text: "",
getStyles () {
return ["MMM-News-QR.css"];
},
getScripts () {
return [this.file("node_modules/qrcode/build/qrcode.js")];
},
start () {
this.config = { ...this.defaults,
...this.config};
Log.log(`Starting module: ${this.name}`);
},
notificationReceived (notification, payload, sender) {
if (notification === "ARTICLE_INFO_RESPONSE") {
this.handleNews(payload);
}
if (notification === "NEWS_FEED" && this.config.updateType === "push") {
// if newsmodule feed news, read the information and show QR
this.sendNotification("ARTICLE_INFO_REQUEST");
}
if (notification === "DOM_OBJECTS_CREATED" && this.config.updateType === "polling") {
const _self = this;
// this.sendNotification('ARTICLE_INFO_REQUEST');
setInterval(() => {
_self.sendNotification("ARTICLE_INFO_REQUEST");
}, this.config.interval);
}
},
handleNews (news) {
/*
Example from the Newsfeed module, thats in the news object (payload)
{
title: this.newsItems[this.activeItem].title,
source: this.newsItems[this.activeItem].sourceTitle,
date: this.newsItems[this.activeItem].pubdate,
desc: this.newsItems[this.activeItem].description,
url: this.getActiveItemURL()
}*/
if (news.url !== this.text) {
this.text = news.url;
this.updateDom(this.config.animationSpeed);
}
},
getDom () {
const wrapperEl = document.createElement("div");
wrapperEl.classList.add("qrcode");
if (this.text !== "") {
const qrcodeEl = document.createElement("canvas");
const options = {
width: this.config.imageSize,
color: {
dark: this.config.colorDark,
light: this.config.colorLight
},
errorCorrectionLevel: "H"
};
QRCode.toCanvas(
qrcodeEl,
this.text,
options,
(error) => {
if (error) { Log.error(`${this.name}: Error creating QRCode: ${error}`); }
Log.log(`${this.name}: successfully created QRCode.`);
}
);
const imageEl = document.createElement("div");
imageEl.classList.add("qrcode__image");
imageEl.appendChild(qrcodeEl);
wrapperEl.appendChild(imageEl);
if (this.config.showRaw) {
const textEl = document.createElement("div");
textEl.classList.add("qrcode__text");
textEl.innerHTML = this.config.text;
wrapperEl.appendChild(textEl);
}
}
return wrapperEl;
}
});