Michael Teeuw 899d05bc32 Add server (web/socket), create socket system, better helper loader.
- The Magic Mirror is now hosted via a express server, allowing you to
load it from an external client (for debugging.)
- It now includes a socket system to communicate between the
node_helper and the client module.
- node_helpers are now only loaded if the module is configured in the
config.
2016-03-30 12:20:46 +02:00

124 lines
2.8 KiB
JavaScript

/* global Module */
/* Magic Mirror
* Module: NewsFeed
*
* By Michael Teeuw http://michaelteeuw.nl
* MIT Licensed.
*/
Module.create({
// Default module config.
defaults: {
feedUrl: 'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml',
showPublishDate: true,
reloadInterval: 10 * 60 * 1000, // every 10 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);
}
});