Allow for successive growing of "unread_articles".
Move helper function adjacent to method which uses it.
This commit is contained in:
parent
124f6ac7ad
commit
ef96c04946
2 changed files with 37 additions and 41 deletions
69
js/App.js
69
js/App.js
|
@ -227,46 +227,52 @@ App.prototype.validate = function(articles) {
|
||||||
return false;
|
return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Utility function to toggle feed content visibility
|
||||||
|
function toggleFeed(feedid) {
|
||||||
|
var e = document.getElementById("feed" + feedid.toString());
|
||||||
|
if (e.style.display == "none") {
|
||||||
|
e.style.display = "";
|
||||||
|
} else {
|
||||||
|
e.style.display = "none";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
App.prototype.populateList = function() {
|
App.prototype.populateList = function() {
|
||||||
|
|
||||||
// First pull all articles together from each distinct feed
|
// Tabulate all articles, grouped by feed.
|
||||||
var ua = this.unread_articles;
|
// Note that this.unread_articles[] can be growing even
|
||||||
var newarts = [], byfeed = [];
|
// as our user reads, so we have to leave it alone and
|
||||||
while (ua.length > 0) {
|
// just point to the appropriate articles in situ.
|
||||||
article = ua[0];
|
const ua = this.unread_articles, ual = ua.length;
|
||||||
|
const byfeed = {}, feeds = [];
|
||||||
|
for (let x = 0; x < ual; ++x) {
|
||||||
|
const article = ua[x];
|
||||||
|
|
||||||
// Next feed ID
|
// Add this article to an existing feed list, or
|
||||||
var fid = article.feed_id;
|
// or start one for this feed ID.
|
||||||
// Here's all the articles under that ID
|
const fid = article.feed_id;
|
||||||
var feeds =
|
if (fid in byfeed) {
|
||||||
ua.filter( function(a) { return a.feed_id == fid; } );
|
byfeed[fid].push(x);
|
||||||
// Keep a list of them so it's easy to tabulate
|
} else {
|
||||||
byfeed.push(feeds);
|
byfeed[fid] = [x];
|
||||||
|
feeds.push( {"fid": fid, "name": article.feed_title} );
|
||||||
// Add them on to the new unread_articles, so they're
|
}
|
||||||
// in order.
|
|
||||||
newarts = newarts.concat(feeds);
|
|
||||||
|
|
||||||
// Trim them off the old, unsorted article list
|
|
||||||
ua = ua.filter( function(a) { return a.feed_id != fid; } );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Make the reordered article list the "official" one
|
|
||||||
ua = this.unread_articles = newarts;
|
|
||||||
|
|
||||||
// Now build the article list; it's a <ul> of feeds,
|
// Now build the article list; it's a <ul> of feeds,
|
||||||
// then sub-<ul> of articles in that feed
|
// then sub-<ul> of articles in that feed
|
||||||
var html_str = "";
|
let html_str = "";
|
||||||
var artidx = 0;
|
for (let x = 0; x < feeds.length; ++x) {
|
||||||
for (i = 0; i < byfeed.length; ++i) {
|
const xs = x.toString();
|
||||||
var feed = byfeed[i];
|
const f = feeds[x];
|
||||||
|
const feed = byfeed[f.fid];
|
||||||
html_str += '<li><span' +
|
html_str += '<li><span' +
|
||||||
' onclick="return(toggleFeed(' + i.toString() + '));"' +
|
' onclick="return(toggleFeed(' + xs + '));"' +
|
||||||
'>' + feed[0].feed_title + '</span>';
|
'>' + f.name + '</span>';
|
||||||
var feedid = '"feed' + i.toString() + '"';
|
const feedid = '"feed' + xs + '"';
|
||||||
html_str += '<ul id=' + feedid + ' style="display: none;">'
|
html_str += '<ul id=' + feedid + ' style="display: none;">'
|
||||||
for (var j = 0; j < feed.length; ++j) {
|
for (let artidx of byfeed[f.fid]) {
|
||||||
article = ua[artidx];
|
const article = ua[artidx];
|
||||||
html_str += "<li"+ (article.unread ?
|
html_str += "<li"+ (article.unread ?
|
||||||
" class='unread'" : "") +">";
|
" class='unread'" : "") +">";
|
||||||
html_str += "<a href='#full-" + artidx + "'>";
|
html_str += "<a href='#full-" + artidx + "'>";
|
||||||
|
@ -276,7 +282,6 @@ App.prototype.populateList = function() {
|
||||||
article.excerpt + "</p>";
|
article.excerpt + "</p>";
|
||||||
}
|
}
|
||||||
html_str += "</a></li>";
|
html_str += "</a></li>";
|
||||||
artidx += 1;
|
|
||||||
}
|
}
|
||||||
html_str += "</ul></li>";
|
html_str += "</ul></li>";
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,15 +8,6 @@ function debug(obj) {
|
||||||
alert(obj)
|
alert(obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleFeed(feedid) {
|
|
||||||
var e = document.getElementById("feed" + feedid.toString());
|
|
||||||
if (e.style.display == "none") {
|
|
||||||
e.style.display = "";
|
|
||||||
} else {
|
|
||||||
e.style.display = "none";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function $(obj) {
|
function $(obj) {
|
||||||
if(typeof obj == "string") return document.querySelector(obj);
|
if(typeof obj == "string") return document.querySelector(obj);
|
||||||
else return obj;
|
else return obj;
|
||||||
|
|
Reference in a new issue