view daily-edition.js @ 49:13565a96f005

added cache.manifest, curr issue can be changed via hash as well as query args now (though w/ hash, page needs to be manually reloaded).
author Atul Varma <varmaa@toolness.com>
date Thu, 12 Aug 2010 04:21:13 +0000
parents d79bf2ee77f5
children f5e2b64dfbca
line wrap: on
line source

$(window).ready(
  function() {
    var req = new XMLHttpRequest();
    var jsonFile = 'daily-edition.json';

    var matches = location.search.match(/\?issue=([0-9]+)/);
    if (!matches)
      matches = location.hash.match(/\#issue=([0-9]+)/);
    if (matches)
      jsonFile = 'issue-' + parseInt(matches[1]) + '.json';

    req.open('GET', jsonFile);
    req.overrideMimeType('text/plain');
    req.addEventListener(
      "load",
      function(event) {
        var info;

        if (window.JSON)
          info = JSON.parse(req.responseText);
        else
          info = eval("(" + req.responseText + ");");

        req = null;

        $("#issue-no").text(info.id + 1);
        $("#pub-date").text(info.pubDate.join("."));

        info.authors.forEach(
          function(author) {
            if (author in info.articles)
              info.articles[author].forEach(
                function(article) {
                  var div = $("#templates .article").clone();
                  var date = article.pubDate.join(".");
                  div.find(".title .link").html(article.title);
                  div.find(".title .link").attr("href", article.url);
                  div.find(".author").text(author);

                  var html = [];
                  article.content.forEach(
                    function(content) {
                      if (content.type == "text/html")
                        html.push(content.value);
                    });

                  if (html.length > 0) {
                    var content = div.find(".content");
                    content.html(html[0]);
                    if (content.get(0).firstChild.nodeType == 3)
                      // The child isn't wrapped in a containing block
                      // element, so do that to ensure it has some
                      // padding from everything around it.
                      content.html("<p>" + html[0] + "</p>");
                  } else
                    console.warn("no html content for", article,
                                 "by", author);
                  div.find(".date").text(date);
                  $("#articles").append(div);
                });
          });

        $("#container").fadeIn("fast");
      },
      false
    );
    req.send(null);
  });