-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMMM-XKCD.js
108 lines (93 loc) · 3.13 KB
/
MMM-XKCD.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
/* Magic Mirror
* Module: MMM-XKCD
*
* By jupadin
* MIT Licensed.
*/
Module.register("MMM-XKCD", {
// Default module config.
defaults: {
header: "xkcd",
dailyJSONURL: "https://xkcd.com/info.0.json",
updateInterval: 10 * 60 * 60 * 1000, // 10 hours
grayScale: false,
invertColors: false,
limitComicWidth: 400,
limitComicHeight: 0,
randomComic: false,
alwaysRandom: false,
showTitle: true,
},
// Define start sequence.
start: function() {
Log.info("Starting module: " + this.name);
this.dailyComic = "";
this.dailyComicTitle = "";
this.animationSpeed = 2000;
this.scrollProgress = 0;
this.loaded = false;
this.sendSocketNotification("SET_CONFIG", this.config);
},
// Define required styles.
getStyles: function() {
return ["MMM-XKCD.css"];
},
// Define required scripts.
getScripts: function() {
return ["moment.js"];
},
// Define header.
getHeader: function() {
if (this.config.showTitle && !this.dailyComicTitle == "") {
return this.config.header + " - " + this.dailyComicTitle;
} else {
return this.config.header;
}
},
// Override dom geneartor.
getDom: function() {
var wrapper = document.createElement("div");
wrapper.id = "wrapper";
if (!this.loaded) {
wrapper.innerHTML = "Loading...";
wrapper.className = "light small dimmed";
return wrapper;
}
var comicWrapper = document.createElement("div");
comicWrapper.id = "comicWrapper";
var comic = document.createElement("img");
comic.id = "comic"
comic.src = this.dailyComic;
if (this.config.grayScale || this.config.invertColors) {
comic.setAttribute("style", "-webkit-filter: " +
(this.config.grayScale ? "grayscale(100%) " : "") +
(this.config.invertColors ? "invert(100%) " : "") +
";");
}
comic.config = this.config;
// Limit width or height of comic on load
comic.onload = function() {
const width = this.width;
if (this.config.limitComicHeight > 0) {
comic.style.height = this.config.limitComicHeight + "px";
comic.style.width = "auto";
} else if (this.config.limitComicWidth > 0 && width > this.config.limitComicWidth) {
comic.style.width = this.config.limitComicWidth + "px";
comic.style.height = "auto";
}
};
comicWrapper.appendChild(comic);
wrapper.appendChild(comicWrapper);
return wrapper;
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === "COMIC") {
this.loaded = true;
this.dailyComic = payload.img;
// this.dailyComicTitle = payload.safe_title;
this.dailyComicTitle = payload.title;
this.updateDom(this.animationSpeed);
}
}
});