Mercurial > ambnews-firefox
comparison 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 |
comparison
equal
deleted
inserted
replaced
| 9:61687c1c0ce8 | 10:f07bf657f483 |
|---|---|
| 2 var Cc = Components.classes; | 2 var Cc = Components.classes; |
| 3 var Ci = Components.interfaces; | 3 var Ci = Components.interfaces; |
| 4 var historyService = Cc["@mozilla.org/browser/nav-history-service;1"] | 4 var historyService = Cc["@mozilla.org/browser/nav-history-service;1"] |
| 5 .getService(Ci.nsINavHistoryService); | 5 .getService(Ci.nsINavHistoryService); |
| 6 | 6 |
| 7 // no query parameters will get all history | 7 // No query parameters will get all history. |
| 8 // XXX default sorting is... ? | |
| 9 var options = historyService.getNewQueryOptions(); | 8 var options = historyService.getNewQueryOptions(); |
| 10 | 9 |
| 11 options.resultType = options.RESULTS_AS_URI; | 10 options.resultType = options.RESULTS_AS_URI; |
| 12 options.sortingMode = options.SORT_BY_VISITCOUNT_DESCENDING; | 11 options.sortingMode = options.SORT_BY_VISITCOUNT_DESCENDING; |
| 13 | 12 |
| 14 // no query parameters will return everything | 13 // No query parameters will return everything. |
| 15 var query = historyService.getNewQuery(); | 14 var query = historyService.getNewQuery(); |
| 16 query.annotation = "ambnews/feed"; | 15 query.annotation = "ambnews/feed"; |
| 17 | 16 |
| 18 // execute the query | 17 // Execute the query. |
| 19 var result = historyService.executeQuery(query, options); | 18 var result = historyService.executeQuery(query, options); |
| 20 | 19 |
| 21 var root = result.root; | 20 var root = result.root; |
| 22 root.containerOpen = true; | 21 root.containerOpen = true; |
| 23 | 22 |
| 23 // Index of the feed number that we're looking at. | |
| 24 var childNum = 0; | 24 var childNum = 0; |
| 25 | |
| 26 // Number of feeds we've shown so far. | |
| 25 var childrenShown = 0; | 27 var childrenShown = 0; |
| 28 | |
| 29 // Maximum numer of feeds we'll show. | |
| 26 var maxChildrenToShow = 20; | 30 var maxChildrenToShow = 20; |
| 31 | |
| 32 // Hashtable that keeps track of what feeds we've shown so far, | |
| 33 // holding both URLs and feed titles. The keys for the values are | |
| 34 // irrelevant; we're just using a hashtable for quick lookup. | |
| 27 var shownFeeds = {}; | 35 var shownFeeds = {}; |
| 28 | 36 |
| 37 // TODO: Consider using JS 1.7 generators/coroutines instead of | |
| 38 // the following closures. | |
| 39 | |
| 40 // This function fetches the next feed and then displays it. | |
| 29 function getNextFeed() { | 41 function getNextFeed() { |
| 30 if (childrenShown == maxChildrenToShow || | 42 if (childrenShown == maxChildrenToShow || |
| 31 childNum == root.childCount) { | 43 childNum == root.childCount) { |
| 44 // We're done! Close our query result container. | |
| 32 root.containerOpen = false; | 45 root.containerOpen = false; |
| 33 } else { | 46 } else { |
| 34 var annSvc = AmbNews.__getAnnSvc(); | 47 var annSvc = AmbNews.__getAnnSvc(); |
| 35 var child = root.getChild(childNum); | 48 var child = root.getChild(childNum); |
| 36 var feedUri = annSvc.getPageAnnotation(AmbNews.url(child.uri), | 49 var feedUri = annSvc.getPageAnnotation(AmbNews.url(child.uri), |
| 37 "ambnews/feed"); | 50 "ambnews/feed"); |
| 38 childNum++; | 51 childNum++; |
| 39 if (feedUri in shownFeeds) { | 52 if (feedUri in shownFeeds) { |
| 53 // We've already shown this feed, skip to the next one. | |
| 40 getNextFeed(); | 54 getNextFeed(); |
| 41 } else { | 55 } else { |
| 42 shownFeeds[feedUri] = true; | 56 shownFeeds[feedUri] = true; |
| 43 childrenShown++; | 57 childrenShown++; |
| 44 AmbNews.getFeed(feedUri, onFeed); | 58 AmbNews.getFeed(feedUri, displayFeed); |
| 45 } | 59 } |
| 46 } | 60 } |
| 47 } | 61 } |
| 48 | 62 |
| 49 function onFeed(feed) { | 63 // This function displays the given feed. |
| 50 var text = ""; | 64 function displayFeed(feed) { |
| 65 if (feed && feed.title.text in shownFeeds) | |
| 66 // We've already shown this feed, skip to the next one. | |
| 67 feed = null; | |
| 51 if (feed) { | 68 if (feed) { |
| 69 shownFeeds[feed.title.text] = true; | |
| 70 var text = ""; | |
| 52 var entriesToShow = 4; | 71 var entriesToShow = 4; |
| 72 var useAuthor = false; | |
| 53 if (feed.entries.length < entriesToShow) | 73 if (feed.entries.length < entriesToShow) |
| 54 entriesToShow = feed.entries.length; | 74 entriesToShow = feed.entries.length; |
| 75 if (entriesToShow > 1 && | |
| 76 feed.entries[0].title.text == feed.entries[1].title.text) | |
| 77 // The feed's entries all have the same title, so display | |
| 78 // author information instead. This is used for Vanilla | |
| 79 // thread feeds. | |
| 80 useAuthor = true; | |
| 55 var feedLink = feed.link ? feed.link.spec : ""; | 81 var feedLink = feed.link ? feed.link.spec : ""; |
| 56 text += ('<div class="feed"><div class="feed-title"><a ' + | 82 text += ('<div class="feed"><div class="feed-title"><a ' + |
| 57 'href="' + feedLink + '">' + | 83 'href="' + feedLink + '">' + |
| 58 feed.title.text + '</a></div><ul>'); | 84 feed.title.text + '</a></div><ul>'); |
| 59 for (var i = 0; i < entriesToShow; i++) { | 85 for (var i = 0; i < entriesToShow; i++) { |
| 60 var entryTitle = feed.entries[i].title.text; | 86 var entryTitle = feed.entries[i].title.text; |
| 87 if (useAuthor) { | |
| 88 // TODO: Gross, XPCOM cruft. This should be passed to our | |
| 89 // function as a normal JS Array. | |
| 90 entryTitle = feed.entries[i].authors.queryElementAt( | |
| 91 0, | |
| 92 Components.interfaces.nsIFeedPerson | |
| 93 ).name; | |
| 94 } | |
| 61 if (entryTitle.length > 100) { | 95 if (entryTitle.length > 100) { |
| 62 entryTitle = entryTitle.slice(0, 100) + '\u2026'; | 96 entryTitle = entryTitle.slice(0, 100) + '\u2026'; |
| 63 } | 97 } |
| 64 text += ('<li class="feed-entry"> \u00b7 <a href="' + | 98 text += ('<li class="feed-entry"> \u00b7 <a href="' + |
| 65 feed.entries[i].link.spec + | 99 feed.entries[i].link.spec + |
| 66 '">' + | 100 '">' + |
| 67 entryTitle + '</a></li>'); | 101 entryTitle + '</a></li>'); |
| 68 } | 102 } |
| 69 text += '</ul></div>'; | 103 text += '</ul></div>'; |
| 70 } else | 104 $(text).hide().fadeIn("slow", getNextFeed).appendTo("#content"); |
| 71 text = "An error occurred."; | 105 } else { |
| 72 //$("#content").append(text); | 106 // We're skipping this feed, so just process the next one. |
| 73 $(text).hide().fadeIn("slow", getNextFeed).appendTo("#content"); | 107 window.setTimeout(getNextFeed, 10); |
| 108 } | |
| 74 } | 109 } |
| 75 | 110 |
| 76 getNextFeed(); | 111 getNextFeed(); |
| 77 } | 112 } |
| 78 | 113 |
