Mercurial > eldonilo > blog
annotate atom.js @ 8:461a24a5a788
Silent errors when testing if we are server- or client-side.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Wed, 02 Nov 2011 09:48:28 -0700 |
parents | 7ab6b48122af |
children | 360186772aa3 |
rev | line source |
---|---|
0 | 1 'use strict'; |
2 | |
8
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
3 if (!document) |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
4 var document = {}; |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
5 |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
6 if (!document.createElementNS) |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
7 document.createElementNS = function(ns, name) { |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
8 return new Element(name, {xmlns: ns}); |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
9 }; |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
10 |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
11 if (!document.createTextNode) |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
12 document.createTextNode = function(text) { |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
13 return text; |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
14 }; |
461a24a5a788
Silent errors when testing if we are server- or client-side.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
7
diff
changeset
|
15 |
0 | 16 parsers[ns.atom] = function(id, xml) { |
17 var toDate = function(atom) { | |
18 if (!atom) | |
5
4d7a67349089
Finish the changing of name to Eldonilo.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
19 return new Date; |
0 | 20 |
21 var last = atom.getChild('updated'); | |
22 if (!last) { | |
23 last = atom.getChild('published'); | |
24 if (!last) | |
5
4d7a67349089
Finish the changing of name to Eldonilo.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
25 return new Date; |
0 | 26 } |
27 | |
28 // var d = new Date(last); // FIXME: don't work in obsolete browsers | |
5
4d7a67349089
Finish the changing of name to Eldonilo.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
29 var d = new Date; |
0 | 30 d.set8601(last); |
31 | |
32 return d; | |
33 }; | |
34 | |
35 var toHTML = function(atom, date) { | |
36 var article = document.createElementNS(ns.xhtml, 'article'); | |
37 | |
5
4d7a67349089
Finish the changing of name to Eldonilo.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
38 article.setAttributeNS(ns.e, 'id', id); |
0 | 39 var d8601 = date.to8601(); |
5
4d7a67349089
Finish the changing of name to Eldonilo.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
2
diff
changeset
|
40 article.setAttributeNS(ns.e, 'date', d8601); |
0 | 41 |
42 var aside = document.createElementNS(ns.xhtml, 'aside'); | |
43 article.appendChild(aside); | |
44 | |
45 var atomTitle = atom.getChild('title'); | |
46 if (atomTitle) { | |
47 var title = document.createElementNS(ns.xhtml, 'h2'); | |
48 title.appendChild(document.createTextNode(atomTitle)); | |
49 article.appendChild(title); | |
50 } | |
51 | |
52 var footer = document.createElementNS(ns.xhtml, 'footer'); | |
53 | |
54 var atomAuthor = atom.getChild('author', ns.atom); | |
55 if (atomAuthor) { | |
56 var atomName = atomAuthor.getChild('name'); | |
57 | |
58 var atomURI = atomAuthor.getChild('uri'); | |
59 var cite = document.createElementNS(ns.xhtml, 'cite'); | |
60 if (atomURI) { | |
61 var a = document.createElementNS(ns.xhtml, 'a'); | |
62 a.href = atomURI; | |
63 var atomJID = new JID; | |
64 atomJID.uri = atomURI; | |
65 | |
66 a.appendChild(document.createTextNode(atomName? atomName: atomJID.bare)); | |
67 cite.appendChild(a); | |
68 | |
69 var img = document.createElementNS(ns.xhtml, 'img'); | |
1
82905edac9d8
Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
70 img.src = 'http://eldonilo.linkmauve.fr/avatar/' + atomJID.bare; |
0 | 71 aside.appendChild(img); |
72 } else | |
73 cite.appendChild(document.createTextNode(atomName)); | |
74 | |
75 footer.appendChild(document.createTextNode('By ')); | |
76 footer.appendChild(cite); | |
77 | |
78 var atomEmail = atomAuthor.getChild('email'); | |
79 if (atomEmail) { | |
80 footer.appendChild(document.createTextNode(' (')); | |
81 var a = document.createElementNS(ns.xhtml, 'a'); | |
2
eb5b6d3ab09b
Better import of ns and jid; fix email and missing ns.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
82 a.href = 'mailto:' + atomEmail; |
0 | 83 a.appendChild(document.createTextNode('email')); |
84 footer.appendChild(a); | |
85 footer.appendChild(document.createTextNode(')')); | |
86 } | |
87 | |
88 article.appendChild(footer); | |
89 } | |
90 | |
91 footer.innerHTML += ', <time datetime="' + d8601 + '">' + date.getRelative() + '</time>'; | |
92 | |
93 var atomSummary = atom.getChild('summary'); | |
94 if (atomSummary) { | |
95 var p = document.createElementNS(ns.xhtml, 'p'); | |
96 p.appendChild(document.createTextNode(atomSummary)); | |
97 article.appendChild(p); | |
98 } | |
99 | |
7
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
100 var atomContent = atom.getChild('content'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
101 if (atomContent) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
102 var contentType = atomContent.getAttribute('type'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
103 if (/^text$/.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
104 var p = document.createElementNS(ns.xhtml, 'p'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
105 p.appendChild(document.createTextNode(atomContent.getText())); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
106 article.appendChild(p); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
107 } else if (/^html$/.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
108 article.insertAdjacentHTML('beforeend', atomContent.getText()); // FIXME: could be not-well-formed. |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
109 } else if (/^xhtml$/.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
110 // TODO: use a better xml2json lib that allow to json2xml |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
111 /*var div = atomContent.getChild(); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
112 article.appendChild(div.innerXml());*/ |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
113 } else { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
114 var contentSrc = atomContent.getAttribute('src'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
115 if (contentSrc) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
116 if (/^image\//.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
117 var img = document.createElementNS(ns.xhtml, 'img'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
118 img.setAttributeNS(null, 'src', contentSrc); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
119 article.appendChild(img); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
120 } else if (/^audio\//.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
121 var audio = document.createElementNS(ns.xhtml, 'audio'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
122 audio.setAttributeNS(null, 'src', contentSrc); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
123 audio.setAttributeNS(null, 'controls', ''); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
124 article.appendChild(audio); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
125 } else if (/^video\//.test(contentType)) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
126 var video = document.createElementNS(ns.xhtml, 'video'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
127 video.setAttributeNS(null, 'src', contentSrc); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
128 video.setAttributeNS(null, 'controls', ''); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
129 article.appendChild(video); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
130 } else { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
131 var a = document.createElementNS(ns.xhtml, 'a'); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
132 a.setAttributeNS(null, 'href', contentSrc); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
133 article.appendChild(a); |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
134 } |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
135 } |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
136 } |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
137 } |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5
diff
changeset
|
138 |
0 | 139 var atomLinks = atom.getChildren('link'); |
140 for (var i in atomLinks) { | |
141 var atomLink = atomLinks[i]; | |
142 | |
143 if (atomLink['@rel'] !== 'replies') | |
144 continue; | |
145 | |
146 if (atomLink['@title'] !== 'comments') | |
147 continue; | |
148 | |
149 var href = new JID; | |
150 href.uri = atomLink['@href']; | |
151 | |
152 article.innerHTML += '<a href="?jid=' + href.bare + ';node=' + href.query.node + ';comments=' + params.jid + '/' + params.node + '">Comments !</a>'; | |
153 } | |
154 | |
155 return article; | |
156 }; | |
157 | |
158 this.xml = xml; | |
159 this.date = toDate(xml); | |
160 this.html = toHTML(xml, this.date); | |
161 } |