Mercurial > ambnews-firefox
view ambnews/content/blank.js @ 10:f07bf657f483
Display of Vanilla forum threads is better now; fixed some display bugs; updated TODOs; documented more code.
| author | Atul Varma <varmaa@toolness.com> |
|---|---|
| date | Fri, 22 Aug 2008 10:02:50 -0700 |
| parents | ec86d1e59d44 |
| children | f259d52d361e |
line wrap: on
line source
function loadNews() { var Cc = Components.classes; var Ci = Components.interfaces; var historyService = Cc["@mozilla.org/browser/nav-history-service;1"] .getService(Ci.nsINavHistoryService); // No query parameters will get all history. var options = historyService.getNewQueryOptions(); options.resultType = options.RESULTS_AS_URI; options.sortingMode = options.SORT_BY_VISITCOUNT_DESCENDING; // No query parameters will return everything. var query = historyService.getNewQuery(); query.annotation = "ambnews/feed"; // Execute the query. var result = historyService.executeQuery(query, options); var root = result.root; root.containerOpen = true; // Index of the feed number that we're looking at. var childNum = 0; // Number of feeds we've shown so far. var childrenShown = 0; // Maximum numer of feeds we'll show. var maxChildrenToShow = 20; // Hashtable that keeps track of what feeds we've shown so far, // holding both URLs and feed titles. The keys for the values are // irrelevant; we're just using a hashtable for quick lookup. var shownFeeds = {}; // TODO: Consider using JS 1.7 generators/coroutines instead of // the following closures. // This function fetches the next feed and then displays it. function getNextFeed() { if (childrenShown == maxChildrenToShow || childNum == root.childCount) { // We're done! Close our query result container. root.containerOpen = false; } else { var annSvc = AmbNews.__getAnnSvc(); var child = root.getChild(childNum); var feedUri = annSvc.getPageAnnotation(AmbNews.url(child.uri), "ambnews/feed"); childNum++; if (feedUri in shownFeeds) { // We've already shown this feed, skip to the next one. getNextFeed(); } else { shownFeeds[feedUri] = true; childrenShown++; AmbNews.getFeed(feedUri, displayFeed); } } } // This function displays the given feed. function displayFeed(feed) { if (feed && feed.title.text in shownFeeds) // We've already shown this feed, skip to the next one. feed = null; if (feed) { shownFeeds[feed.title.text] = true; var text = ""; var entriesToShow = 4; var useAuthor = false; if (feed.entries.length < entriesToShow) entriesToShow = feed.entries.length; if (entriesToShow > 1 && feed.entries[0].title.text == feed.entries[1].title.text) // The feed's entries all have the same title, so display // author information instead. This is used for Vanilla // thread feeds. useAuthor = true; var feedLink = feed.link ? feed.link.spec : ""; text += ('<div class="feed"><div class="feed-title"><a ' + 'href="' + feedLink + '">' + feed.title.text + '</a></div><ul>'); for (var i = 0; i < entriesToShow; i++) { var entryTitle = feed.entries[i].title.text; if (useAuthor) { // TODO: Gross, XPCOM cruft. This should be passed to our // function as a normal JS Array. entryTitle = feed.entries[i].authors.queryElementAt( 0, Components.interfaces.nsIFeedPerson ).name; } if (entryTitle.length > 100) { entryTitle = entryTitle.slice(0, 100) + '\u2026'; } text += ('<li class="feed-entry"> \u00b7 <a href="' + feed.entries[i].link.spec + '">' + entryTitle + '</a></li>'); } text += '</ul></div>'; $(text).hide().fadeIn("slow", getNextFeed).appendTo("#content"); } else { // We're skipping this feed, so just process the next one. window.setTimeout(getNextFeed, 10); } } getNextFeed(); } $(window).ready(loadNews);
