-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMMM-EOL.js
229 lines (192 loc) · 7.77 KB
/
MMM-EOL.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
/* Magic Mirror
* Module: MMM-EOL
*
* By Mykle1
*
*/
Module.register("MMM-EOL", {
// Module config defaults. // Make all changes in your config.js file
defaults: {
lifeForm: "", // See Life Form list below
scrollDes: "", // yes = scroll description, no = static description
dLength: 400, // Length of descriptive text
useHeader: true, // false if you don't want a header
header: "", // Change in config file. useHeader must be true
maxWidth: "300px",
animationSpeed: 2000, // fade speed
initialLoadDelay: 3250,
retryDelay: 2500,
rotateInterval: 1 * 60 * 1000, // 5 minutes
updateInterval: 5 * 60 * 1000, //
lifeFormArray: {
"Tigers": "328674",
"Hummingbirds": "8021",
"Lions": "328672",
"Jaguar": "328606",
"Leopard": "328673",
"Cheetah": "328680",
"Fox": "19076",
"Deaths-head Moth": "50688",
"Great White Shark": "213726",
"Seals": "7666",
"Mosquitos": "473",
"Venus Flytrap": "71355",
"Cardinals": "19590",
"Humpback Whale": "328575",
"Praying Mantis": "487055",
"Dragonfly": "42274802",
"Tarantulas": "170",
"Bats": "7631",
"Bears": "7664",
"Cicadas": "2645413",
"Striped Bass": "211032",
"Alligators": "796029",
"Crocodiles": "1739",
"Snakes": "2815988",
"Cats": "7674",
"Birds": "695",
"Eagles": "8016",
"Bald Eagle": "1049121",
"Cactus": "4228",
"Anemones and Corals": "1746",
"Crabs, Lobsters, and Shrimps": "1183",
"Sequoia": "42665",
"Sea Horse": "218966",
"Hominidae": "1653",
"Primates": "1645",
"Otters": "328044",
"Raccoons": "328598",
}
},
getStyles: function() {
return ["MMM-EOL.css"];
},
start: function() {
Log.info("Starting module: " + this.name);
requiresVersion: "2.1.0",
// Set locale.
this.url = "http://eol.org/api/pages/1.0.json?batch=false&id=" + this.config.lifeFormArray[this.config.lifeForm] + "&images_per_page=75&subjects=all&details=true&references=false&taxonomy=false&vetted=1&language=en";
this.EOL = [];
this.activeItem = 0;
this.rotateInterval = null;
this.scheduleUpdate();
},
getDom: function() {
var wrapper = document.createElement("div");
wrapper.className = "wrapper";
wrapper.style.maxWidth = this.config.maxWidth;
if (!this.loaded) {
wrapper.innerHTML = "Life as we know it . . .";
wrapper.classList.add("bright", "light", "small");
return wrapper;
}
if (this.config.useHeader != false) {
var header = document.createElement("header");
header.classList.add("xsmall", "bright", "header");
header.innerHTML = this.config.header;
wrapper.appendChild(header);
}
// Rotating my data
var EOL = this.EOL;
var EOLKeys = Object.keys(this.EOL);
if (EOLKeys.length > 0) {
if (this.activeItem >= EOLKeys.length) {
this.activeItem = 0;
}
var EOL = this.EOL[EOLKeys[this.activeItem]];
// console.log(EOL); // for checking
// scientificName
var scientificName = document.createElement("div");
scientificName.classList.add("small", "bright", "scientificName");
// console.log(EOL);
scientificName.innerHTML = this.SCI;
wrapper.appendChild(scientificName);
// title
var title = document.createElement("div");
title.classList.add("xsmall", "bright", "title");
if (EOL.title == undefined || EOL.title == "") {
title.innerHTML = "Life is beautiful!";
wrapper.appendChild(title);
} else
title.innerHTML = this.sTrim(EOL.title, 40, ' ', ' ...');
wrapper.appendChild(title);
// picture
var img = document.createElement("img");
img.classList.add("photo");
var anything = EOL.eolMediaURL;
if (anything != 'undefined'||undefined || null){
img.src = EOL.eolMediaURL;
}
img.onerror = function(event_object) { // This function replaces broken image
var img_with_error = event_object.currentTarget // This function replaces broken image
img_with_error.src= "modules/MMM-EOL/images/darwin.jpg" // This is the path to the replacement image
}
wrapper.appendChild(img);
} // End of rotation
// description NOT IN ROTATION
if (this.config.scrollDes != "no") {
var description = document.createElement("div");
description.classList.add("small", "bright", "description");
description.innerHTML = '<marquee behavior="scroll" direction ="left" scrollamount="5">' + this.DES.replace(/<(?:.|\n)*?>/gm, '') + '</marquee>';
wrapper.appendChild(description);
} else {
var description = document.createElement("div");
description.classList.add("xsmall", "bright", "description");
description.innerHTML = this.sTrim(this.DES, this.config.dLength, ' ', ' ...');
wrapper.appendChild(description);
}
return wrapper;
},
///// Add this function to the modules you want to control with voice //////
notificationReceived: function(notification, payload) {
if (notification === 'HIDE_DARWIN') {
this.hide(1000);
// this.updateDom(300);
} else if (notification === 'SHOW_DARWIN') {
this.show(1000);
// this.updateDom(300);
}
},
processEOL: function(data) {
// this.today = data.Today;
this.EOL = data.dataObjects;
console.log(this.EOL);
this.SCI = data.scientificName;
this.DES = data.dataObjects[1].description;
this.loaded = true;
},
sTrim: function(str, length, delim, appendix) {
if (str.length <= length) return str;
var trimmedStr = str.substr(0, length + delim.length);
var lastDelimIndex = trimmedStr.lastIndexOf(delim);
if (lastDelimIndex >= 0) trimmedStr = trimmedStr.substr(0, lastDelimIndex);
if (trimmedStr) trimmedStr += appendix;
return trimmedStr;
},
scheduleCarousel: function() {
console.log("Carousel of EOL fucktion!");
this.rotateInterval = setInterval(() => {
this.activeItem++;
this.updateDom(this.config.animationSpeed);
}, this.config.rotateInterval);
},
scheduleUpdate: function() {
setInterval(() => {
this.getEOL();
}, this.config.updateInterval);
this.getEOL(this.config.initialLoadDelay);
},
getEOL: function() {
this.sendSocketNotification('GET_EOL', this.url);
},
socketNotificationReceived: function(notification, payload) {
if (notification === "EOL_RESULT") {
this.processEOL(payload);
if (this.rotateInterval == null) {
this.scheduleCarousel();
}
this.updateDom(this.config.animationSpeed);
}
this.updateDom(this.config.initialLoadDelay);
},
});