view daily-edition.js @ 45:8cb5e556960b

Added an optional querystring param, '?issue=<number>', allowing browser to pass in the issue # to view
author Atul Varma <varmaa@toolness.com>
date Sun, 28 Mar 2010 22:37:49 +0000
parents 6495497409dc
children d79bf2ee77f5
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)
      jsonFile = 'issue-' + parseInt(matches[1]) + '.json';

    req.open('GET', jsonFile);
    req.overrideMimeType('text/plain');
    req.addEventListener(
      "load",
      function(event) {
        var info = JSON.parse(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);
  });