-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMMM-JokeAPI.js
58 lines (51 loc) · 1.7 KB
/
MMM-JokeAPI.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
Module.register("MMM-JokeAPI", {
defaults: {
category: "Programming",
fetchInterval: 10 * 1000
},
getStyles() {
return [
this.file('style.css')
]
},
joke: null,
notificationReceived(notification, payload, sender) {
if (notification === 'MODULE_DOM_CREATED') {
this.getJoke();
setInterval(() => {
this.getJoke()
}, this.config.fetchInterval);
}
},
getDom() {
const wrapper = document.createElement("div");
if(this.joke === null) return wrapper;
this.setupHTMLStructure(wrapper);
return wrapper;
},
setupHTMLStructure(wrapper) {
if (this.joke.type === 'single') {
const joke = document.createElement("h1");
joke.className = "bright medium light fadeInJoke";
joke.innerHTML = this.joke.joke;
wrapper.appendChild(joke);
} else if (this.joke.type === 'twopart') {
const setup = document.createElement("h1");
setup.className = "bright medium light no-wrap fadeInJoke";
setup.innerHTML = this.joke.setup;
wrapper.appendChild(setup);
const punchline = document.createElement("h2");
punchline.className = "bright small light fadeInPunchline";
punchline.innerHTML = this.joke.delivery;
wrapper.appendChild(punchline);
}
},
getJoke() {
fetch(`https://sv443.net/jokeapi/v2/joke/${this.config.category}`).then((response) => {
response.json().then((joke) => {
this.joke = joke;
this.updateDom();
});
});
}
});