forked from nischi/MMM-News-QR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMMM-News-QR.js
107 lines (92 loc) · 2.87 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
/* Magic Mirror
* Module: MMM-News-QR
*
* By Thierry Nischelwitzer http://nischi.ch
* MIT Licensed.
*/
"use strict";
Module.register("MMM-News-QR", {
defaults: {
// possible values (polling, push)
// push only works with MagicMirror 2.8+ and broadcastNewsFeeds activated
updateType: "push",
// only needed if updateType is polling
interval: 2000,
animationSpeed: 2500,
colorDark: "#fff",
colorLight: "#000",
imageSize: 150
},
text: "",
getStyles: function () {
return ["MMM-News-QR.css"];
},
getScripts: function() {
return [this.file("node_modules/qrcode/build/qrcode.min.js")];
},
start: function () {
this.config = Object.assign({}, this.defaults, this.config);
Log.log("Starting module: " + this.name);
},
notificationReceived: function (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"
) {
var _self = this;
// this.sendNotification('ARTICLE_INFO_REQUEST');
setInterval(function () {
_self.sendNotification("ARTICLE_INFO_REQUEST");
}, this.config.interval);
}
},
handleNews: function (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: async function () {
const wrapperEl = document.createElement("div");
wrapperEl.classList.add("qrcode");
if (this.text !== "") {
const qrcodeEl = document.createElement("div");
await QRCode.toCanvas(this.text, {
width: this.config.imageSize,
colorDark: this.config.colorDark,
colorLight: this.config.colorLight,
errorCorrectionLevel: 'H',
}, function (error, qrcodeCanvas) {
if (error) console.error(error);
qrcodeEl.appendChild(qrcodeCanvas);
});
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;
}
});