-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMMM-NewsFeedTicker.js
484 lines (429 loc) · 15.4 KB
/
MMM-NewsFeedTicker.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/* Module: NewsFeedTicker
* version 1.2.0
*
* ((modification of the default newsfeed module
* By Michael Teeuw https://michaelteeuw.nl
* MIT Licensed.))
*
* Modified by Jim Hallock (justjim1220@gmail.com)
*
* thanks to @sdetweil, @Sean, @cowboysdude, & @Mykle1 for all their help
* and for their patience in teaching me how to do this.
*
* Brought to you by the makers of Cheyenne Cigars
* and my very own homemade Southern Sweet Tea.
*
*/
Module.register("MMM-NewsFeedTicker", {
// Default module config.
defaults: {
feeds: [
{
title: "BBC World News",
url: "https://feeds.bbci.co.uk/news/world/rss.xml",
customLogo: "bbc_news_120x60.gif"
}
],
showMarquee: true,
showIcon: true,
showSourceTitle: false,
showPublishDate: false,
showDescription: false,
wrapTitle: false,
wrapDescription: false,
truncDescription: false,
lengthDescription: 4000,
hideLoading: false,
reloadInterval: 60 * 60 * 1000, // every 60 minutes
updateInterval: 60 * 1000, // every 60 seconds
animationSpeed: 2 * 1000,
maxNewsItems: 5, // 0 for unlimited
ignoreOldItems: true,
ignoreOlderThan: 2 * 24 * 60 * 60 * 1000, // 2 days
removeStartTags: "both",
removeEndTags: "both",
startTags: [],
endTags: [],
prohibitedWords: [],
scrollLength: "100%",
logFeedWarnings: false,
encoding: "UTF-8" // ISO-8859-1
},
requiresversion: "2.1.0",
// Define required scripts.
getScripts () {
return ["moment.js"];
},
getStyles () {
return ["MMM-NewsFeedTicker.css"];
},
// Define required translations.
getTranslations () {
// The translations for the default modules are defined in the core translation files.
// Therefor we can just return false. Otherwise we should have returned a dictionary.
// If you're trying to build your own module including translations, check out the documentation.
return false;
},
// Define start sequence.
start () {
Log.info(`Starting module: ${this.name}`);
// Set locale.
moment.locale(config.language);
this.newsItems = [];
this.loaded = false;
this.activeItem = 0;
this.scrollPosition = 0;
this.registerFeeds();
"use strict";
this.isShowingDescription = this.config.showDescription;
},
// Override socket notification handler.
socketNotificationReceived (notification, payload) {
if (notification === "NEWS_ITEMS") {
this.generateFeed(payload);
if (!this.loaded) {
this.scheduleUpdateInterval();
}
this.loaded = true;
}
},
// Override dom generator.
getDom () {
const wrapper = document.createElement("div");
if (this.config.feedUrl) {
wrapper.className = "bold normal";
wrapper.innerHTML = "The configuration options for the newsfeed module have changed.<br>Please check the documentation.";
return wrapper;
}
if (this.activeItem >= this.newsItems.length) {
this.activeItem = 0;
}
if (this.newsItems.length > 0) {
// this.config.showFullArticle is a run-time configuration, triggered by optional notifications
if (!this.config.showFullArticle && (this.config.showSourceTitle || this.config.showPublishDate)) {
const sourceAndTimestamp = document.createElement("div");
// sourceAndTimestamp.className = "bold large dimmed";
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "") {
sourceAndTimestamp.innerHTML = this.newsItems[this.activeItem].sourceTitle;
}
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "" && this.config.showPublishDate) {
sourceAndTimestamp.innerHTML += " ";
}
if (this.config.showPublishDate) {
sourceAndTimestamp.innerHTML += moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow();
}
if (this.config.showSourceTitle && this.newsItems[this.activeItem].sourceTitle !== "" || this.config.showPublishDate) {
sourceAndTimestamp.innerHTML += ":";
}
wrapper.appendChild(sourceAndTimestamp);
}
// Remove selected tags from the beginning of rss feed items (title or description)
if (this.config.removeStartTags == "title" || this.config.removeStartTags == "both") {
for (f = 0; f < this.config.startTags.length; f++) {
if (this.newsItems[this.activeItem].title.slice(0, this.config.startTags[f].length) == this.config.startTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(this.config.startTags[f].length, this.newsItems[this.activeItem].title.length);
}
}
}
if (this.config.removeStartTags == "description" || this.config.removeStartTags == "both") {
if (this.config.showDescription) {
for (f = 0; f < this.config.startTags.length; f++) {
if (this.newsItems[this.activeItem].description.slice(0, this.config.startTags[f].length) == this.config.startTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].description.slice(this.config.startTags[f].length, this.newsItems[this.activeItem].description.length);
}
}
}
}
// Remove selected tags from the end of rss feed items (title or description)
if (this.config.removeEndTags) {
for (f = 0; f < this.config.endTags.length; f++) {
if (this.newsItems[this.activeItem].title.slice(-this.config.endTags[f].length) == this.config.endTags[f]) {
this.newsItems[this.activeItem].title = this.newsItems[this.activeItem].title.slice(0, -this.config.endTags[f].length);
}
}
if (this.config.showDescription) {
for (f = 0; f < this.config.endTags.length; f++) {
if (this.newsItems[this.activeItem].description.slice(-this.config.endTags[f].length) == this.config.endTags[f]) {
this.newsItems[this.activeItem].description = this.newsItems[this.activeItem].description.slice(0, -this.config.endTags[f].length);
}
}
}
}
if (this.config.showSourceTicle) {
const title = document.createElement("div");
title.className = `bright medium light${!this.config.wrapTitle
? " no-wrap"
: ""}`;
title.innerHTML = this.newsItems[this.activeItem].title;
wrapper.appendChild(title);
}
if (this.isShowingDescription) {
const description = document.createElement("div");
description.className = `light${!this.config.wrapDescription
? " no-wrap"
: ""}`;
const txtDesc = this.newsItems[this.activeItem].description;
description.innerHTML = this.config.truncDescription
? txtDesc.length > this.config.lengthDescription
? `${txtDesc.substring(0, this.config.lengthDescription)}...`
: txtDesc
: txtDesc;
wrapper.appendChild(description);
}
if (this.config.showMarquee) {
// if icon requested
if (this.config.showIcon) {
const image = document.createElement("img");
image.className = "image";
// display it as requested
image.src = this.newsItems[this.activeItem].logo;
wrapper.appendChild(image);
}
const tickerBody = document.createElement("div");
tickerBody.className = "tickerbody";
const headline = document.createElement("span");
headline.className = "headline";
tickerBody.style.animationDuration = `${Math.round(this.config.updateInterval / 1000)}s`;
tickerBody.addEventListener("animationend", () => {
headline.innerHTML = "";
this.scheduleUpdateInterval();
}, false);
headline.innerHTML = `<font color= #ffaa00>${moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow()}: ` + `</font>${this.newsItems[this.activeItem].title} || ${this.newsItems[this.activeItem].description}`;
function calcSpeed (speed) {
// Time = Distance/Speed
let spanSelector = document.querySelectorAll("headline"),
i;
for (i = 0; i < spanSelector.length; i++) {
const spanLength = spanSelector[i].offsetWidth,
timeTaken = spanLength / speed;
spanSelector[i].style.animationDuration = `${timeTaken}s`;
}
}
calcSpeed(100);
tickerBody.appendChild(headline);
wrapper.appendChild(tickerBody);
}
if (this.config.showFullArticle) {
const fullArticle = document.createElement("iframe");
fullArticle.className = "";
fullArticle.style.width = "100%";
// very large height value to allow scrolling
fullArticle.height = "3000px";
fullArticle.style.height = "3000px";
fullArticle.style.top = "0";
fullArticle.style.left = "0";
fullArticle.style.border = "none";
fullArticle.src = typeof this.newsItems[this.activeItem].url === "string"
? this.newsItems[this.activeItem].url
: this.newsItems[this.activeItem].url.href;
fullArticle.style.zIndex = 1;
wrapper.appendChild(fullArticle);
}
if (this.config.hideLoading) {
this.show();
}
} else if (this.config.hideLoading) {
this.hide();
} else {
wrapper.innerHTML = this.translate("LOADING");
wrapper.className = "medium normal bold";
}
return wrapper;
},
/* registerFeeds()
* registers the feeds to be used by the backend.
*/
registerFeeds () {
for (const f in this.config.feeds) {
const feed = this.config.feeds[f];
if (feed.customLogo !== undefined) {
feed.customLogo = `${this.data.path}pics/${feed.customLogo}`;
}
this.sendSocketNotification("ADD_FEED", {
feed,
config: this.config
});
}
},
/* generateFeed()
* Generate an ordered list of items for this configured module.
*
* attribute feeds object - An object with feeds returned by the node helper.
*/
generateFeed (feeds) {
let newsItems = [];
for (const feed in feeds) {
const feedItems = feeds[feed];
if (this.subscribedToFeed(feed)) {
for (const i in feedItems) {
const item = feedItems[i];
item.sourceTitle = this.titleForFeed(feed);
if (!(this.config.ignoreOldItems && Date.now() - new Date(item.pubdate) > this.config.ignoreOlderThan)) {
newsItems.push(item);
}
}
}
}
newsItems.sort((a, b) => {
const dateA = new Date(a.pubdate);
const dateB = new Date(b.pubdate);
return dateB - dateA;
});
if (this.config.maxNewsItems > 0) {
newsItems = newsItems.slice(0, this.config.maxNewsItems);
}
if (this.config.prohibitedWords.length > 0) {
newsItems = newsItems.filter(function (value) {
for (let i = 0; i < this.config.prohibitedWords.length; i++) {
if (value.title.toLowerCase().indexOf(this.config.prohibitedWords[i].toLowerCase()) > -1) {
return false;
}
}
return true;
}, this);
}
this.newsItems = newsItems;
},
/* subscribedToFeed(feedUrl)
* Check if this module is configured to show this feed.
*
* attribute feedUrl string - Url of the feed to check.
*
* returns bool
*/
subscribedToFeed (feedUrl) {
for (const f in this.config.feeds) {
const feed = this.config.feeds[f];
if (feed.url === feedUrl) {
return true;
}
}
return false;
},
/* titleForFeed(feedUrl)
* Returns title for a specific feed Url.
*
* attribute feedUrl string - Url of the feed to check.
*
* returns string
*/
titleForFeed (feedUrl) {
for (const f in this.config.feeds) {
const feed = this.config.feeds[f];
if (feed.url === feedUrl) {
return feed.title || "";
}
}
return "";
},
imageForFeed (feedUrl) {
for (const f in this.config.feeds) {
const feed = this.config.feeds[f];
if (feed.url === feedUrl) {
return feed.image || feed.icon || feed.logo || feed.svg || this.feed_var_in_function.icon || "";
}
}
return "";
},
/* scheduleUpdateInterval()
* Schedule visual update.
*/
scheduleUpdateInterval () {
const self = this;
self.updateDom(self.config.animationSpeed);
timer = setInterval(() => {
self.activeItem++;
self.updateDom(self.config.animationSpeed);
}, this.config.updateInterval);
},
/* capitalizeFirstLetter(string)
* Capitalizes the first character of a string.
*
* argument string string - Input string.
*
* return string - Capitalized output string.
*/
capitalizeFirstLetter (string) {
return string.charAt(0).toUpperCase() + string.slice(1);
},
resetDescrOrFullArticleAndTimer () {
this.isShowingDescription = this.config.showDescription;
this.config.showFullArticle = false;
this.scrollPosition = 0;
// reset bottom bar alignment
document.getElementsByClassName("region top bar")[0].style.top = "0";
document.getElementsByClassName("region top bar")[0].style.top = "inherit";
if (!timer) {
this.scheduleUpdateInterval();
}
},
notificationReceived (notification, payload, sender) {
Log.info(`${this.name} - received notification: ${notification}`);
if (notification == "ARTICLE_NEXT") {
var before = this.activeItem;
this.activeItem++;
if (this.activeItem >= this.newsItems.length) {
this.activeItem = 0;
}
this.resetDescrOrFullArticleAndTimer();
Log.info(`${this.name} - going from article #${before} to #${this.activeItem} (of ${this.newsItems.length})`);
this.updateDom(100);
} else if (notification == "ARTICLE_PREVIOUS") {
var before = this.activeItem;
this.activeItem--;
if (this.activeItem < 0) {
this.activeItem = this.newsItems.length - 1;
}
this.resetDescrOrFullArticleAndTimer();
Log.info(`${this.name} - going from article #${before} to #${this.activeItem} (of ${this.newsItems.length})`);
this.updateDom(100);
}
// if "more details" is received the first time: show article summary, on second time show full article
else if (notification == "ARTICLE_MORE_DETAILS") {
// full article is already showing, so scrolling down
if (this.config.showFullArticle == true) {
this.scrollPosition += this.config.scrollLength;
window.scrollTo(0, this.scrollPosition);
Log.info(`${this.name} - scrolling left`);
Log.info(`${this.name} - ARTICLE_MORE_DETAILS, scroll position: ${this.config.scrollLength}`);
} else {
this.showFullArticle();
}
} else if (notification == "ARTICLE_SCROLL_UP") {
if (this.config.showFullArticle == true) {
this.scrollPosition -= this.config.scrollLength;
window.scrollTo(0, this.scrollPosition);
Log.info(`${this.name} - scrolling up`);
Log.info(`${this.name} - ARTICLE_SCROLL_UP, scroll position: ${this.config.scrollLength}`);
}
} else if (notification == "ARTICLE_LESS_DETAILS") {
this.resetDescrOrFullArticleAndTimer();
Log.info(`${this.name} - showing only article titles again`);
this.updateDom(100);
} else if (notification === "ARTICLE_TOGGLE_FULL") {
if (this.config.showFullArticle) {
this.activeItem++;
this.resetDescrOrFullArticleAndTimer();
} else {
this.showFullArticle();
}
} else {
Log.info(`${this.name} - unknown notification, ignoring: ${notification}`);
}
},
showFullArticle () {
this.isShowingDescription = !this.isShowingDescription;
this.config.showFullArticle = !this.isShowingDescription;
// make bottom bar align to top to allow scrolling
if (this.config.showFullArticle === true) {
document.getElementsByClassName("region top bar")[0].style.bottom = "inherit";
document.getElementsByClassName("region top bar")[0].style.top = "-90px";
}
clearInterval(timer);
timer = null;
Log.info(`${this.name} - showing ${this.isShowingDescription}`
? "article description"
: "full article");
this.updateDom(100);
}
});