miniflux-legacy/assets/js/feed.js

66 lines
2.3 KiB
JavaScript
Raw Normal View History

2013-10-15 04:38:07 +02:00
Miniflux.Feed = (function() {
// List of subscriptions
var feeds = [];
// List of feeds currently updating
var queue = [];
// Number of concurrent requests when updating all feeds
var queue_length = 5;
// Update the items unread/total count for the feed
function updateItemsCounter(feed_id, counts)
{
var container = document.getElementById("items-count-" + feed_id);
2014-12-03 10:02:16 +01:00
if (container) container.innerHTML = counts["items_unread"] + "/" + counts['items_total'];
}
2013-10-15 04:38:07 +02:00
return {
Update: function(feed_id, callback) {
var container = document.getElementById("items-count-" + feed_id);
if (! container) return;
2013-10-15 04:38:07 +02:00
//add data-feed-id to article element and couse the first h2
container.parentNode.className = "loading-icon";
2013-10-15 04:38:07 +02:00
var request = new XMLHttpRequest();
request.onload = function() {
container.parentNode.className = "";
2013-10-15 04:38:07 +02:00
var response = JSON.parse(this.responseText);
if (response.result) updateItemsCounter(feed_id, response.items_count);
if (callback) callback(response);
2013-10-15 04:38:07 +02:00
};
request.open("POST", "?action=refresh-feed&feed_id=" + feed_id, true);
request.send();
},
UpdateAll: function() {
var links = document.getElementsByTagName("a");
2013-10-15 04:38:07 +02:00
for (var i = 0, ilen = links.length; i < ilen; i++) {
var feed_id = links[i].getAttribute('data-feed-id');
if (feed_id) feeds.push(parseInt(feed_id));
}
2013-10-15 04:38:07 +02:00
var interval = setInterval(function() {
while (feeds.length > 0 && queue.length < queue_length) {
var feed_id = feeds.shift();
queue.push(feed_id);
Miniflux.Feed.Update(feed_id, function(response) {
var index = queue.indexOf(response.feed_id);
if (index >= 0) queue.splice(index, 1);
if (feeds.length === 0 && queue.length === 0) {
2013-10-15 04:38:07 +02:00
clearInterval(interval);
window.location.href = "?action=unread";
}
});
}
}, 100);
}
};
})();