Mercurial > eldonilo > blog
annotate blog.js @ 15:5149a856d9dd default tip
Fix hybrid mode.
author | Emmanuel Gil Peyrot <linkmauve@linkmauve.fr> |
---|---|
date | Thu, 03 Nov 2011 17:28:49 -0700 |
parents | 161d4ea1c3f8 |
children |
rev | line source |
---|---|
0 | 1 //'use strict'; |
2 | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
3 const WS_SERVICE = 'ws://plugsbee.com:5280/'; |
0 | 4 var conn = null; |
5 var jid = 'blog@linkmauve.fr'; // FIXME: Strophe should accept anonymous connections. | |
6 var password = 'blog'; | |
7 var service = 'psgxs.linkmauve.fr'; | |
8 var node = 'blog'; | |
9 var re = true; | |
10 | |
11 var params = (function() { | |
12 var vars = {}; | |
13 var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split(';'); | |
14 | |
15 for(var i = 0; i < hashes.length; i++) { | |
16 var s = hashes[i].indexOf('='); | |
17 var key = hashes[i].substring(0, s); | |
18 var value = hashes[i].substring(s+1); | |
19 vars[key] = value; | |
20 } | |
21 | |
22 return vars; | |
23 })(); | |
24 | |
25 if (params.jid) | |
26 service = params.jid; | |
27 | |
28 if (params.node) | |
29 node = params.node; | |
30 | |
31 var received = {}; | |
32 var messages = document.getElementById('messages'); | |
33 | |
7
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
34 var updateMessage = function(name, id) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
35 var html = function(name, id) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
36 return received[name][id].html; |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
37 } |
0 | 38 |
7
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
39 var date = function(name, id) { |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
40 return received[name][id].date; |
7ab6b48122af
Add support for content in Atom and improve published display.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
4
diff
changeset
|
41 } |
0 | 42 |
43 var divs = messages.getElementsByTagNameNS(ns.xhtml, 'div'); | |
44 var container = null; | |
45 | |
46 for (var i in divs) { | |
47 var div = divs[i] | |
48 if (typeof div != 'object') | |
49 continue; | |
50 | |
1
82905edac9d8
Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
51 if (div.getAttributeNS(ns.e, 'jid') === name) { |
0 | 52 container = div; |
53 break; | |
54 } | |
55 } | |
56 | |
57 if (!container) { | |
58 var container = document.createElementNS(ns.xhtml, 'div'); | |
1
82905edac9d8
Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
59 container.setAttributeNS(ns.e, 'jid', name); |
0 | 60 messages.appendChild(container); |
61 } | |
62 | |
63 var articles = container.getElementsByTagNameNS(ns.xhtml, 'article'); | |
64 for (var i in articles) { | |
65 var article = articles[i]; | |
66 if (typeof article != 'object') | |
67 continue; | |
68 | |
1
82905edac9d8
Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
69 if (article.getAttributeNS(ns.e, 'id') === id) { |
0 | 70 container.replaceChild(html(name, id), article); |
71 return; | |
72 } | |
73 } | |
74 | |
75 var article = html(name, id); | |
76 | |
77 if (!container.firstChild) | |
78 container.appendChild(article); | |
79 else { | |
80 var d = date(name, id); | |
81 var toInsert; | |
82 for (var i in articles) { | |
83 var a = articles[i]; | |
84 if (typeof a != 'object') | |
85 continue; | |
86 | |
87 var ad = new Date(); | |
1
82905edac9d8
Fix some things.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
0
diff
changeset
|
88 ad.set8601(a.getAttributeNS(ns.e, 'date')); |
0 | 89 |
90 if (ad < d) { | |
91 toInsert = a; | |
92 break; | |
93 } | |
94 } | |
95 | |
96 if (toInsert) | |
97 container.insertBefore(article, toInsert); | |
98 else | |
99 container.appendChild(article); | |
100 } | |
101 } | |
102 | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
103 var convert = function(item) { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
104 if (item.ns in parsers) |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
105 return new parsers[item.ns](item); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
106 return new parsers[''](item); |
0 | 107 }; |
108 | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
109 var onMessages = function(stanza) { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
110 var name = stanza.service+'/'+stanza.node; |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
111 if (!received[name]) |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
112 received[name] = {}; |
0 | 113 |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
114 var r = received[name]; |
0 | 115 |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
116 for (var id in stanza.items) { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
117 r[id] = convert(stanza.items[id]); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
118 updateMessage(name, id); |
0 | 119 } |
120 } | |
121 | |
122 var onInfo = function(stanza) { | |
4
f630f4b90564
Remove the old log system and use console.log instead.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
123 console.log('info'); // TODO! |
0 | 124 /*var query = stanza.getElementsByTagNameNS(ns.info, 'query')[0]; |
125 var x = query.getElementsByTagNameNS(ns.data, 'x')[0]; | |
126 var form = forms.parse(x);*/ | |
127 } | |
128 | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
129 function onConnect() { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
130 conn.send('<presence/>'); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
131 // TODO: verify the subscription. |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
132 //conn.pubsubSubscribe(service, node); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
133 if (params.no === 'server') { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
134 conn.pubsubItems(service, node, onMessages); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
135 conn.discoInfo(service, node, onInfo); |
0 | 136 } |
137 | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
138 /*if (status == Strophe.Status.CONNFAIL) { |
4
f630f4b90564
Remove the old log system and use console.log instead.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
139 console.log('Failed to connect.'); |
0 | 140 } else if (status == Strophe.Status.DISCONNECTING) { |
4
f630f4b90564
Remove the old log system and use console.log instead.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
141 console.log('Disconnecting.'); |
0 | 142 } else if (status == Strophe.Status.DISCONNECTED) { |
4
f630f4b90564
Remove the old log system and use console.log instead.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1
diff
changeset
|
143 console.log('Disconnected.'); |
0 | 144 if (re) |
145 conn.connect(jid, password, onConnect); | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
146 }*/ |
0 | 147 } |
148 | |
149 window.addEventListener('load', function () { | |
13
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
150 conn = new XMPP(WS_SERVICE); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
151 |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
152 // Debug |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
153 conn.on('DOMOutput', function(stanza) { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
154 console.log('out:', stanza); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
155 }); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
156 conn.on('DOMInput', function(stanza) { |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
157 console.log('in:', stanza); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
158 }); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
159 |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
160 conn.on('connected', onConnect); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
161 conn.on('message', onMessages); |
161d4ea1c3f8
Migration of the client-side to XMPP.js instead of Strophe.js.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
9
diff
changeset
|
162 conn.connect(jid, password); |
0 | 163 }, false); |
164 | |
165 window.addEventListener('unload', function (e) { | |
166 re = false; | |
167 conn.disconnect(); | |
168 e.preventDefault(); | |
169 }, false); |