Michael Teeuw 5eed80c28e Squashed commit of the following:
commit e38dd346d9f796807ea71035c18e3533ec245ba6
Author: Michael Teeuw <michael@xonaymedia.nl>
Date:   Sat Apr 2 19:17:30 2016 +0200

    Add the possibility to set the maximum number of days.

commit 6f5c86775b708d19d3798267ffd23e491a1d2c62
Author: Sam Vendittelli <sam.vendittelli@hotmail.com>
Date:   Sat Apr 2 06:27:44 2016 +0100

    Fixed cursor appearing in margin

    Cursor was appearing in the margin so moved `cursor: none` property to html.

commit 576c668d84b34b8ad7a0fd51b146fde60f721682
Author: Domi-G <lessuseguy+githubdomig@gmail.com>
Date:   Fri Apr 1 22:52:32 2016 +0200

    Huge cleanup of white space
2016-04-03 19:52:13 +02:00

122 lines
2.8 KiB
JavaScript

/* global Module */
/* Magic Mirror
* Module: NewsFeed
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.register('newsfeed',{
// Default module config.
defaults: {
feedUrl: 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml',
showPublishDate: true,
reloadInterval: 5 * 60 * 1000, // every 5 minutes
updateInterval: 7.5 * 1000,
animationSpeed: 2.5 * 1000,
},
// Define required scripts.
getScripts: function() {
return ['moment.js'];
},
// Define start sequence.
start: function() {
Log.info('Starting module: ' + this.name);
// Set locale.
moment.locale(config.language);
this.newsItems = [];
this.loaded = false;
this.activeItem = 0;
this.fetchNews();
},
// Override socket notification handler.
socketNotificationReceived: function(notification, payload) {
if (notification === 'NEWS_ITEMS') {
if (payload.url === this.config.feedUrl) {
this.newsItems = payload.items;
if (!this.loaded) {
this.scheduleUpdateInterval();
}
this.loaded = true;
}
}
},
// Override dom generator.
getDom: function() {
var wrapper = document.createElement("div");
if (this.activeItem >= this.newsItems.length) {
this.activeItem = 0;
}
if (this.newsItems.length > 0) {
if (this.config.showPublishDate) {
var timestamp = document.createElement("div");
timestamp.className = "light small dimmed";
timestamp.innerHTML = this.capitalizeFirstLetter(moment(new Date(this.newsItems[this.activeItem].pubdate)).fromNow() + ':');
//timestamp.innerHTML = this.config.feedUrl;
wrapper.appendChild(timestamp);
}
var title = document.createElement("div");
title.className = "bright medium light";
title.innerHTML = this.newsItems[this.activeItem].title;
wrapper.appendChild(title);
} else {
wrapper.innerHTML = "Loading news ...";
wrapper.className = "small dimmed";
}
return wrapper;
},
/* fetchNews(compliments)
* Requests new data from news proxy.
*/
fetchNews: function() {
Log.log('Add news feed to fetcher: ' + this.config.feedUrl);
this.sendSocketNotification('ADD_FEED', {
url: this.config.feedUrl,
reloadInterval: this.config.reloadInterval
});
},
/* scheduleUpdateInterval()
* Schedule visual update.
*/
scheduleUpdateInterval: function() {
var self = this;
self.updateDom(self.config.animationSpeed);
setInterval(function() {
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: function(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
});