Mercurial > daily-edition
annotate daily-edition.js @ 56:35edfd76a3f7
added localStorage-based caching for latest issue.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 13 Aug 2010 06:04:28 +0000 |
parents | 995aaa2f079a |
children | 182af9ac76fb |
rev | line source |
---|---|
52 | 1 function isUrlSafe(url) { |
2 if (typeof(url) != "string") | |
3 return false; | |
4 return (url.match("^https?://") != null); | |
5 } | |
6 | |
53 | 7 var imgRemover = html.makeHtmlSanitizer( |
8 function(tagName, attribs) { | |
9 if (/^img$/i.test(tagName)) | |
10 return null; | |
11 return attribs; | |
12 }); | |
13 | |
52 | 14 function safeHtml(html) { |
53 | 15 var out = []; |
16 imgRemover(html, out); | |
17 html = out.join(''); | |
52 | 18 return html_sanitize( |
19 html, | |
20 function urlPolicy(url) { | |
21 return isUrlSafe(url) ? url : null; | |
22 }); | |
23 } | |
24 | |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
25 const DEFAULT_JSON_FILE = 'daily-edition.json'; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
26 var jsonFile = DEFAULT_JSON_FILE; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
27 |
55
995aaa2f079a
Added error feedback display.
Atul Varma <varmaa@toolness.com>
parents:
53
diff
changeset
|
28 function showError() { |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
29 var json; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
30 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
31 if (jsonFile == DEFAULT_JSON_FILE) |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
32 json = window.localStorage[DEFAULT_JSON_FILE]; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
33 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
34 if (json) |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
35 showContent(json); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
36 else { |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
37 $("#error").show(); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
38 $("#container").fadeIn("fast"); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
39 } |
55
995aaa2f079a
Added error feedback display.
Atul Varma <varmaa@toolness.com>
parents:
53
diff
changeset
|
40 } |
995aaa2f079a
Added error feedback display.
Atul Varma <varmaa@toolness.com>
parents:
53
diff
changeset
|
41 |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
42 function showContent(json) { |
47
d79bf2ee77f5
just use eval() if JSON isn't available.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
43 if (window.JSON) |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
44 info = JSON.parse(json); |
47
d79bf2ee77f5
just use eval() if JSON isn't available.
Atul Varma <varmaa@toolness.com>
parents:
45
diff
changeset
|
45 else |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
46 info = eval("(" + json + ");"); |
13
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
47 |
31
a4df72a6d8a1
added header to html view, changed article by-lines
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
48 $("#issue-no").text(info.id + 1); |
a4df72a6d8a1
added header to html view, changed article by-lines
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
49 $("#pub-date").text(info.pubDate.join(".")); |
a4df72a6d8a1
added header to html view, changed article by-lines
Atul Varma <varmaa@toolness.com>
parents:
13
diff
changeset
|
50 |
13
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
51 info.authors.forEach( |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
52 function(author) { |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
53 if (author in info.articles) |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
54 info.articles[author].forEach( |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
55 function(article) { |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
56 var div = $("#templates .article").clone(); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
57 var date = article.pubDate.join("."); |
52 | 58 var title = safeHtml(article.title); |
59 div.find(".title .link").html(title); | |
13
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
60 div.find(".title .link").attr("href", article.url); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
61 div.find(".author").text(author); |
35
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
62 |
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
63 var html = []; |
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
64 article.content.forEach( |
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
65 function(content) { |
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
66 if (content.type == "text/html") |
52 | 67 html.push(safeHtml(content.value)); |
35
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
68 }); |
6495497409dc
fixed js code to work in safari
Atul Varma <varmaa@toolness.com>
parents:
33
diff
changeset
|
69 |
32
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
70 if (html.length > 0) { |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
71 var content = div.find(".content"); |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
72 content.html(html[0]); |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
73 if (content.get(0).firstChild.nodeType == 3) |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
74 // The child isn't wrapped in a containing block |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
75 // element, so do that to ensure it has some |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
76 // padding from everything around it. |
c0e3b6984986
normalized html output a bit
Atul Varma <varmaa@toolness.com>
parents:
31
diff
changeset
|
77 content.html("<p>" + html[0] + "</p>"); |
52 | 78 } |
13
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
79 div.find(".date").text(date); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
80 $("#articles").append(div); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
81 }); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
82 }); |
33 | 83 |
84 $("#container").fadeIn("fast"); | |
56
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
85 } |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
86 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
87 $(window).ready( |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
88 function() { |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
89 var req = new XMLHttpRequest(); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
90 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
91 var matches = location.search.match(/\?issue=([0-9]+)/); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
92 if (!matches) |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
93 matches = location.hash.match(/\#issue=([0-9]+)/); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
94 if (matches) |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
95 jsonFile = 'issue-' + parseInt(matches[1]) + '.json'; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
96 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
97 req.open('GET', jsonFile); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
98 req.overrideMimeType('text/plain'); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
99 req.addEventListener("error", showError, false); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
100 req.addEventListener( |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
101 "load", |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
102 function(event) { |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
103 var info; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
104 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
105 if (req.status != 200) { |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
106 showError(); |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
107 return; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
108 } |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
109 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
110 if (jsonFile == DEFAULT_JSON_FILE) |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
111 window.localStorage[DEFAULT_JSON_FILE] = req.responseText; |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
112 |
35edfd76a3f7
added localStorage-based caching for latest issue.
Atul Varma <varmaa@toolness.com>
parents:
55
diff
changeset
|
113 showContent(req.responseText); |
13
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
114 }, |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
115 false |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
116 ); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
117 req.send(null); |
69fd13a4aef4
Added an html viewer and a publish_edition.py script that generates the JSON which the html viewer reads.
Atul Varma <varmaa@toolness.com>
parents:
diff
changeset
|
118 }); |