Allow for successive growing of "unread_articles".

Move helper function adjacent to method which uses it.
This commit is contained in:
Andy Valencia 2017-01-29 09:03:51 -08:00
parent 124f6ac7ad
commit ef96c04946
2 changed files with 37 additions and 41 deletions

View file

@ -227,46 +227,52 @@ App.prototype.validate = function(articles) {
return false; return false;
}; };
App.prototype.populateList = function() { // Utility function to toggle feed content visibility
function toggleFeed(feedid) {
// First pull all articles together from each distinct feed var e = document.getElementById("feed" + feedid.toString());
var ua = this.unread_articles; if (e.style.display == "none") {
var newarts = [], byfeed = []; e.style.display = "";
while (ua.length > 0) { } else {
article = ua[0]; e.style.display = "none";
}
// Next feed ID
var fid = article.feed_id;
// Here's all the articles under that ID
var feeds =
ua.filter( function(a) { return a.feed_id == fid; } );
// Keep a list of them so it's easy to tabulate
byfeed.push(feeds);
// 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 App.prototype.populateList = function() {
ua = this.unread_articles = newarts;
// Tabulate all articles, grouped by feed.
// Note that this.unread_articles[] can be growing even
// as our user reads, so we have to leave it alone and
// just point to the appropriate articles in situ.
const ua = this.unread_articles, ual = ua.length;
const byfeed = {}, feeds = [];
for (let x = 0; x < ual; ++x) {
const article = ua[x];
// Add this article to an existing feed list, or
// or start one for this feed ID.
const fid = article.feed_id;
if (fid in byfeed) {
byfeed[fid].push(x);
} else {
byfeed[fid] = [x];
feeds.push( {"fid": fid, "name": article.feed_title} );
}
}
// 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>";
} }

View file

@ -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;