view planet.js @ 35:722b7bdf055a

Renamed 'days-ago' to 'time-ago'.
author Atul Varma <varmaa@toolness.com>
date Tue, 03 Mar 2009 10:25:33 -0800
parents e51c8e11d202
children 35deb244a49c
line wrap: on
line source

// = Planet Ubiquity Code =
//
// This is the JavaScript source code for the Planet Ubiquity Redesign.

var Planet = {
  // The base URL for where Ubiquity buildbot information is located.
  BUILDBOT_BASE: "http://ubiquity.mozilla.com/buildbot/",

  // The base URL for where Ubiquity's main HG repository is located.
  HG_BASE: "https://ubiquity.mozilla.com/hg/ubiquity-firefox/"
};

Planet.getBuildInfo = function getBuildInfo(page) {
  var builds = [];
  $("li", page).each(
    function(i) {
      var revRegexp = /.*rev=\[([0-9a-f]+)\].*/;
      builds.push({rev: $(this).text().match(revRegexp)[1],
                   href: (Planet.BUILDBOT_BASE +
                          $("a:not(:first)", this).attr("href")),
                   isSuccessful: $(".success", this).length > 0
                  });
    }
  );
  return builds;
};

Planet.mashupBuildbotWithHgData = function mashup(hgLog, buildbotPage) {
  var builds = Planet.getBuildInfo(buildbotPage);
  builds.forEach(
    function(build) {
      var revUrl = Planet.HG_BASE + "rev/" + build.rev;
      var revSelector = ("a[href='" + revUrl + "']");
      var link = jQuery(revSelector, hgLog);
      var className = "build-errors";
      var title = "This build may have errors.";
      if (build.isSuccessful) {
        className = "build-success";
        title = "This build passed all tests.";
      }
      var status = jQuery('<span></span>');
      status.addClass(className);
      status.addClass("build");
      link.before(status);
      status.wrap('<a title="' + title +
                  '" href="' + build.href + '"></a>');
    });
};

Planet.processHgFeed = function processHgFeed(feed, content, cb) {
  Planet.processBlogFeed(
    feed,
    content,
    function() {
      jQuery.get(
        ("http://about-mozilla.appspot.com/?url=" +
         Planet.BUILDBOT_BASE + "one_line_per_build&jsonp=?"),
        null,
        function(result) {
          Planet.mashupBuildbotWithHgData(content, $(result.data));
          cb();
        },
      "jsonp"
      );
    });
};

Planet.processBlogFeed = function processBlogFeed(feed, content, cb) {
  jQuery.each(
    feed.entries,
    function(i) {
      var item = $('<div class="blog-item">' +
                   '<span class="title"></span> ' +
                   '<span class="author"></span></div>');
      $('.title', item).html(this.title);
      var linkTitle = "";
      if (this.contentSnippet)
        linkTitle =this.contentSnippet ;
      var link = $('<a title="' + linkTitle + '" href="' +
                   this.link + '"></a>');
      link.attr("target", "_blank");
      $('.title', item).wrap(link);
      if (this.author) {
        var author = this.author;
        var authorFilters = [
            // Filter out e.g. "Robert Jones <robert@jones.com>"
            /(.+)\s*&lt;.+&gt;/,
            // Filter out e.g. "rob...@jones.com (Robert Jones)"
            /.+\.\.\.@.+\s+\((.+)\)/
        ];
        jQuery.each(
          authorFilters,
          function() {
            var match = author.match(this);
            if (match)
              author = match[1];
          });
        $('.author', item).html('by ' + author);
      }
      $(item).attr("published", this.publishedDate);
      content.append(item);
    });
  cb();
};

Planet.FEEDS = [
  {name: "Blogs",
   url: "http://ubiquity.mozilla.com/planet/?feed=rss2",
   processFeed: Planet.processBlogFeed,
   entries: 10},
  {name: "Bugs",
   url: ("https://ubiquity.mozilla.com/trac/timeline?ticket=on" +
         "&milestone=on&wiki=on&max=50&daysback=90&format=rss"),
   processFeed: Planet.processBlogFeed,
   entries: 10},
  {name: "Code",
   url: Planet.HG_BASE + "rss-log",
   processFeed: Planet.processHgFeed,
   entries: 15},
  {name: "Discussions",
   url: ("http://groups.google.com/group/ubiquity-firefox/feed/" +
         "rss_v2_0_msgs.xml"),
   processFeed: Planet.processBlogFeed,
   entries: 30},
  {name: "Support",
   url: ("http://getsatisfaction.com/mozilla/products/mozilla_ubiquity.rss?" +
         "sort=recently_created"),
   processFeed: Planet.processBlogFeed,
   entries: 10}
];

Planet.doneLoadingFeeds = function doneLoadingFeeds() {
  function fixHeights(timeAgo) {
    var tallestHeight = 0;
    var elements = $(".time-ago-" + timeAgo);
    elements.each(
      function(i) {
        var height = $(this).height();
        if (height > tallestHeight)
          tallestHeight = height;
      });
    elements.height(tallestHeight);
  }

  $("#issue").fadeIn();
  for (var i = 0; i < 10; i++)
    fixHeights(i);
  var entries = $("#issue .entry");
  $(document.body).width(entries.outerWidth() * entries.length);
};

Planet.splitByDate = function splitByDate(rawContent, content) {
  var now = new Date();
  $(".blog-item", rawContent).each(
    function(i) {
      var pub = new Date($(this).attr("published"));
      var msAgo = now - pub;
      var hoursAgo = msAgo / (1000 * 60 * 60);
      var timeAgo = Math.floor(hoursAgo / 8);
      if (timeAgo < 0)
        timeAgo = 0;
      if (timeAgo > 9)
        timeAgo = 9;
      var div = $(".time-ago-" + timeAgo, content);
      div.append(this);
    });
};

Planet.showFeed = function showFeed(feedInfo, cb) {
  var entry = $('<div class="entry"></div>');
  var headline = $('<div class="headline"></div>');
  headline.text(feedInfo.name);
  entry.append(headline);
  $("#body").append(entry);

  var feed = new google.feeds.Feed(feedInfo.url);
  feed.setNumEntries(feedInfo.entries);
  feed.includeHistoricalEntries();
  feed.load(
    function(result) {
      var rawContent = $('<div class="content"></div>');
      var content = $('<div class="content"></div>');

      for (var i = 0; i < 10; i++)
        content.append('<div class="time-ago-' + i + '"></div>');

      feedInfo.processFeed(
        result.feed,
        rawContent,
        function() {
          Planet.splitByDate(rawContent, content);
          entry.append(content);
          cb();
        });
    });
};

google.load("feeds", "1");
google.setOnLoadCallback(
  function() {
    var feedsLeftToLoad = Planet.FEEDS.length;
    jQuery.each(
      Planet.FEEDS,
      function(i) {
        Planet.showFeed(this,
                        function() {
                          feedsLeftToLoad--;
                          if (!feedsLeftToLoad) {
                            Planet.doneLoadingFeeds();
                          }
                        });
      }
    );
  });