view ambnews/content/blank.js @ 22:808c3edb1085 default tip

Updated update.rdf.
author Atul Varma <varmaa@toolness.com>
date Sun, 19 Jul 2009 17:57:43 -0700
parents aa3ccc022d07
children
line wrap: on
line source

/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is Ambient News.
 *
 * The Initial Developer of the Original Code is Mozilla.
 * Portions created by the Initial Developer are Copyright (C) 2007
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *  Atul Varma <avarma@mozilla.com>
 *
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 *
 * ***** END LICENSE BLOCK ***** */

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);
      }
    }
  }

  function displayFeed(feed) {
    try {
      tryToDisplayFeed(feed);
    } catch (e) {
      Components.utils.reportError(e);
      window.setTimeout(getNextFeed, 10);
    }
  }

  // This function displays the given feed.
  function tryToDisplayFeed(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);