Allow the subscription to multiple news feeds in one newsfeed instance.

This commit is contained in:
Michael Teeuw
2016-04-20 14:39:38 +02:00
parent 3b1f1a41dc
commit fd500ac411
5 changed files with 192 additions and 45 deletions

View File

@@ -11,13 +11,19 @@ Module.register("newsfeed",{
// Default module config.
defaults: {
feedUrl: "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml",
feeds: [
{
title: "New York Times",
url: "http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml",
encoding: "UTF-8" //ISO-8859-1
}
],
showSourceTitle: true,
showPublishDate: true,
showDescription: false,
reloadInterval: 5 * 60 * 1000, // every 5 minutes
updateInterval: 10 * 1000,
animationSpeed: 2.5 * 1000,
encoding: "UTF-8" //ISO-8859-1
},
// Define required scripts.
@@ -36,21 +42,20 @@ Module.register("newsfeed",{
this.loaded = false;
this.activeItem = 0;
this.fetchNews();
this.registerFeeds();
},
// 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.generateFeed(payload);
this.loaded = true;
if (!this.loaded) {
this.scheduleUpdateInterval();
}
this.loaded = true;
}
},
@@ -58,12 +63,11 @@ Module.register("newsfeed",{
getDom: function() {
var wrapper = document.createElement("div");
// wrapper.className = "small";
// for (var n in this.newsItems) {
// var item = this.newsItems[n];
// wrapper.innerHTML += item.title + '<br>';
// }
// return wrapper;
if (this.config.feedUrl) {
wrapper.className = "small bright";
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;
@@ -71,12 +75,16 @@ Module.register("newsfeed",{
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);
if (this.config.showSourceTitle || this.config.showPublishDate) {
var sourceAndTimestamp = document.createElement("div");
sourceAndTimestamp.className = "light small 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);
}
var title = document.createElement("div");
@@ -99,16 +107,78 @@ Module.register("newsfeed",{
return wrapper;
},
/* fetchNews(compliments)
* Requests new data from news proxy.
/* registerFeeds()
* registers the feeds to be used by the backend.
*/
fetchNews: function() {
Log.log("Add news feed to fetcher: " + this.config.feedUrl);
this.sendSocketNotification("ADD_FEED", {
url: this.config.feedUrl,
reloadInterval: this.config.reloadInterval,
encoding: this.config.encoding
registerFeeds: function() {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
this.sendSocketNotification("ADD_FEED", {
feed: feed,
config: this.config
});
}
},
/* registerFeeds()
* Generate an ordered list of items for this configured module.
*
* attribute feeds object - An object with feeds returned by the nod helper.
*/
generateFeed: function(feeds) {
var newsItems = [];
for (var feed in feeds) {
var feedItems = feeds[feed];
if (this.subscribedToFeed(feed)) {
for (var i in feedItems) {
var item = feedItems[i];
item.sourceTitle = this.titleForFeed(feed);
newsItems.push(item);
}
}
}
newsItems.sort(function(a,b) {
var dateA = new Date(a.pubdate);
var dateB = new Date(b.pubdate);
return dateB - dateA;
});
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: function(feedUrl) {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
if (feed.url === feedUrl) {
return true;
}
}
return false;
},
/* subscribedToFeed(feedUrl)
* Returns title for a specific feed Url.
*
* attribute feedUrl string - Url of the feed to check.
*
* returns string
*/
titleForFeed: function(feedUrl) {
for (var f in this.config.feeds) {
var feed = this.config.feeds[f];
if (feed.url === feedUrl) {
return feed.title || '';
}
}
return '';
},
/* scheduleUpdateInterval()