1 | /* |
---|
2 | This program is distributed under the terms of the MIT license. |
---|
3 | Please see the LICENSE file for details. |
---|
4 | |
---|
5 | Copyright 2008 Michael Garvin |
---|
6 | */ |
---|
7 | /*TODO dump / very loose roadmap |
---|
8 | --0.4 |
---|
9 | add chats to json store |
---|
10 | Mouseover status messages in roster |
---|
11 | HTML in messages (xslt?) |
---|
12 | Select presence status/message |
---|
13 | Optional user-specified resource |
---|
14 | Loglevel select on login instead of check box |
---|
15 | vcard support http://xmpp.org/extensions/xep-0153.html |
---|
16 | Notifications of closed chats |
---|
17 | Notifications of typing |
---|
18 | --0.5 |
---|
19 | roster management |
---|
20 | figure out how we want to handle presence from our own jid (and transports) |
---|
21 | roster sorting by presence / offline roster capablility |
---|
22 | auto-subscribe vs prompted subscribe based on config option |
---|
23 | make sure makeChat() et al. can handle empty resources |
---|
24 | (offline chat capabilities) |
---|
25 | --1.0 (or whenever someone submits better .css) |
---|
26 | layout overhaul |
---|
27 | code cleanup (like checking for excessive function lengths) |
---|
28 | roster versioning http://xmpp.org/extensions/attic/xep-0237-0.1.html |
---|
29 | make sure onload bootstrapping actually preserves existing onloads |
---|
30 | */ |
---|
31 | var TROPHYIM_BOSH_SERVICE = '/proxy/xmpp-httpbind'; //Change to suit |
---|
32 | var TROPHYIM_LOG_LINES = 200; |
---|
33 | var TROPHYIM_LOGLEVEL = 0; //0=debug, 1=info, 2=warn, 3=error, 4=fatal |
---|
34 | var TROPHYIM_VERSION = "0.3"; |
---|
35 | //Uncomment to make session reattachment work |
---|
36 | //var TROPHYIM_JSON_STORE = "json_store.php"; |
---|
37 | |
---|
38 | /** File: trophyimclient.js |
---|
39 | * A JavaScript front-end for strophe.js |
---|
40 | * |
---|
41 | * This is a JavaScript library that runs on top of strophe.js. All that |
---|
42 | * is required is that your page have a <div> element with an id of |
---|
43 | * 'trophyimclient', and that your page does not explicitly set an onload |
---|
44 | * event in its <body> tag. In that case you need to append TrophyIM.load() |
---|
45 | * to it. |
---|
46 | * |
---|
47 | * The style of the client can be conrolled via trophyim.css, which is |
---|
48 | * auto-included by the client. |
---|
49 | */ |
---|
50 | |
---|
51 | /** Object: HTMLSnippets |
---|
52 | * |
---|
53 | * This is the repository for all the html snippets that TrophyIM uses |
---|
54 | * |
---|
55 | */ |
---|
56 | HTMLSnippets = { |
---|
57 | |
---|
58 | chatArea : loadIM.HTMLSnippets.chatArea( ), |
---|
59 | |
---|
60 | chatBox : loadIM.HTMLSnippets.chatBox( ), |
---|
61 | |
---|
62 | chatTab : loadIM.HTMLSnippets.chatTab( ), |
---|
63 | |
---|
64 | //loginPage : loadIM.HTMLSnippets.loginPage( ), |
---|
65 | |
---|
66 | loggingDiv : loadIM.HTMLSnippets.loggingDiv( ), |
---|
67 | |
---|
68 | rosterDiv : loadIM.HTMLSnippets.rosterDiv( ), |
---|
69 | |
---|
70 | rosterGroup : loadIM.HTMLSnippets.rosterGroup( ), |
---|
71 | |
---|
72 | rosterItem : loadIM.HTMLSnippets.rosterItem( ), |
---|
73 | |
---|
74 | statusDiv : loadIM.HTMLSnippets.statusDiv( ), |
---|
75 | }; |
---|
76 | |
---|
77 | /** Object: DOMObjects |
---|
78 | * This class contains builders for all the DOM objects needed by TrophyIM |
---|
79 | */ |
---|
80 | DOMObjects = { |
---|
81 | /** Function: xmlParse |
---|
82 | * Cross-browser alternative to using innerHTML |
---|
83 | * Parses given string, returns valid DOM HTML object |
---|
84 | * |
---|
85 | * Parameters: |
---|
86 | * (String) xml - the xml string to parse |
---|
87 | */ |
---|
88 | xmlParse : function(xmlString) { |
---|
89 | var xmlObj = this.xmlRender(xmlString); |
---|
90 | if(xmlObj) { |
---|
91 | try { //Firefox, Gecko, etc |
---|
92 | if (this.processor == undefined) { |
---|
93 | this.processor = new XSLTProcessor(); |
---|
94 | this.processor.importStylesheet(this.xmlRender( |
---|
95 | '<xsl:stylesheet version="1.0"\ |
---|
96 | xmlns:xsl="http://www.w3.org/1999/XSL/Transform">\ |
---|
97 | <xsl:output method="html" indent="yes"/><xsl:template\ |
---|
98 | match="@*|node()"><xsl:copy><xsl:copy-of\ |
---|
99 | select="@*|node()"/></xsl:copy></xsl:template>\ |
---|
100 | </xsl:stylesheet>')); |
---|
101 | } |
---|
102 | var htmlObj = |
---|
103 | this.processor.transformToDocument(xmlObj).documentElement; |
---|
104 | //Safari has a quirk where it wraps dom elements in <html><body> |
---|
105 | if (htmlObj.tagName.toLowerCase() == 'html') { |
---|
106 | htmlObj = htmlObj.firstChild.firstChild; |
---|
107 | } |
---|
108 | return document.importNode(htmlObj, true); |
---|
109 | } catch(e) { |
---|
110 | try { //IE is so very very special |
---|
111 | var htmlObj = document.importNode(xmlObj.documentElement, true); |
---|
112 | if (htmlObj.tagName.toLowerCase() == "div") { |
---|
113 | var div_wrapper = document.createElement('div'); |
---|
114 | div_wrapper.appendChild(htmlObj); |
---|
115 | if(div_wrapper.innerHTML) { |
---|
116 | div_wrapper.innerHTML = div_wrapper.innerHTML; |
---|
117 | } |
---|
118 | htmlObj = div_wrapper.firstChild; |
---|
119 | } |
---|
120 | return htmlObj; |
---|
121 | } catch(e) { |
---|
122 | alert( |
---|
123 | "TrophyIM Error: Cannot add html to page" + e.message); |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | }, |
---|
128 | /** Function: xmlRender |
---|
129 | * Uses browser-specific methods to turn given string into xml object |
---|
130 | * |
---|
131 | * Parameters: |
---|
132 | * (String) xml - the xml string to parse |
---|
133 | */ |
---|
134 | xmlRender : function(xmlString) { |
---|
135 | try {//IE |
---|
136 | var renderObj = new ActiveXObject("Microsoft.XMLDOM"); |
---|
137 | renderObj.async="false"; |
---|
138 | if(xmlString) { |
---|
139 | renderObj.loadXML(xmlString); |
---|
140 | } |
---|
141 | } catch (e) { |
---|
142 | try { //Firefox, Gecko, etc |
---|
143 | if (this.parser == undefined) { |
---|
144 | this.parser = new DOMParser(); |
---|
145 | } |
---|
146 | var renderObj = this.parser.parseFromString(xmlString, |
---|
147 | "application/xml"); |
---|
148 | } catch(e) { |
---|
149 | alert("TrophyIM Error: Cannot create new html for page"); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | return renderObj; |
---|
154 | }, |
---|
155 | /** Function: getHTML |
---|
156 | * Returns named HTML snippet as DOM object |
---|
157 | * |
---|
158 | * Parameters: |
---|
159 | * (String) name - name of HTML snippet to retrieve (see HTMLSnippets |
---|
160 | * object) |
---|
161 | */ |
---|
162 | getHTML : function(page) { |
---|
163 | //alert("Page : \n" + page); |
---|
164 | return this.xmlParse(HTMLSnippets[page]); |
---|
165 | }, |
---|
166 | /** Function: getScript |
---|
167 | * Returns script object with src to given script |
---|
168 | * |
---|
169 | * Parameters: |
---|
170 | * (String) script - name of script to put in src attribute of script |
---|
171 | * element |
---|
172 | */ |
---|
173 | getScript : function(script) { |
---|
174 | var newscript = document.createElement('script'); |
---|
175 | newscript.setAttribute('src', script); |
---|
176 | newscript.setAttribute('type', 'text/javascript'); |
---|
177 | return newscript; |
---|
178 | } |
---|
179 | }; |
---|
180 | |
---|
181 | /** Object: TrophyIM |
---|
182 | * |
---|
183 | * This is the actual TrophyIM application. It searches for the |
---|
184 | * 'trophyimclient' element and inserts itself into that. |
---|
185 | */ |
---|
186 | TrophyIM = { |
---|
187 | /** Constants: |
---|
188 | * |
---|
189 | * (Boolean) stale_roster - roster is stale and needs to be rewritten. |
---|
190 | */ |
---|
191 | constants : {stale_roster: false}, |
---|
192 | /** Object: chatHistory |
---|
193 | * |
---|
194 | * Stores chat history (last 10 message) and current presence of active |
---|
195 | * chat tabs. Indexed by jid. |
---|
196 | */ |
---|
197 | chatHistory : {}, |
---|
198 | /** Object: activeChats |
---|
199 | * |
---|
200 | * This object stores the currently active chats. |
---|
201 | */ |
---|
202 | activeChats : {current: null, divs: {}}, |
---|
203 | /** Function: setCookie |
---|
204 | * |
---|
205 | * Sets cookie name/value pair. Date and path are auto-selected. |
---|
206 | * |
---|
207 | * Parameters: |
---|
208 | * (String) name - the name of the cookie variable |
---|
209 | * (String) value - the value of the cookie variable |
---|
210 | */ |
---|
211 | setCookie : function(name, value) { |
---|
212 | var expire = new Date(); |
---|
213 | expire.setDate(expire.getDate() + 365); |
---|
214 | document.cookie = name + "=" + value + "; expires=" + expire.toGMTString(); |
---|
215 | }, |
---|
216 | /** Function: delCookie |
---|
217 | * |
---|
218 | * Deletes cookie |
---|
219 | * |
---|
220 | * Parameters: |
---|
221 | * (String) name) - the name of the cookie to delete |
---|
222 | */ |
---|
223 | delCookie : function(name) { |
---|
224 | var expire = new Date(); |
---|
225 | expire.setDate(expire.getDate() - 365); |
---|
226 | document.cookie = name + "= ; expires=" + expire.toGMTString(); |
---|
227 | delete TrophyIM.cookies[name]; |
---|
228 | }, |
---|
229 | /** Function: getCookies |
---|
230 | * |
---|
231 | * Retrieves all trophyim cookies into an indexed object. Inteneded to be |
---|
232 | * called once, at which time the app refers to the returned object instead |
---|
233 | * of re-parsing the cookie string every time. |
---|
234 | * |
---|
235 | * Each cookie is also re-applied so as to refresh the expiry date. |
---|
236 | */ |
---|
237 | getCookies : function() { |
---|
238 | var cObj = {}; |
---|
239 | var cookies = document.cookie.split(';'); |
---|
240 | for (var c in cookies) { |
---|
241 | while (cookies[c].charAt(0)==' ') { |
---|
242 | cookies[c] = cookies[c].substring(1,cookies[c].length); |
---|
243 | } |
---|
244 | if (cookies[c].substr(0, 8) == "trophyim") { |
---|
245 | var nvpair = cookies[c].split("=", 2); |
---|
246 | cObj[nvpair[0]] = nvpair[1]; |
---|
247 | TrophyIM.setCookie(nvpair[0], nvpair[1]); |
---|
248 | } |
---|
249 | } |
---|
250 | return cObj; |
---|
251 | }, |
---|
252 | /** Function: load |
---|
253 | * |
---|
254 | * This function searches for the trophyimclient div and loads the client |
---|
255 | * into it. |
---|
256 | */ |
---|
257 | load : function() |
---|
258 | { |
---|
259 | TrophyIM.cookies = TrophyIM.getCookies(); |
---|
260 | |
---|
261 | TrophyIM.client_div = document.getElementById('trophyimclient'); |
---|
262 | |
---|
263 | //Load other .js scripts needed |
---|
264 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/strophe.js')); |
---|
265 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/md5.js')); |
---|
266 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/sha1.js')); |
---|
267 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/b64.js')); |
---|
268 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('js/json2.js')); //Keep this script last |
---|
269 | |
---|
270 | //Wait a second to give scripts time to load |
---|
271 | setTimeout("TrophyIM.showLogin()", 500); |
---|
272 | |
---|
273 | /*TrophyIM.cookies = TrophyIM.getCookies(); |
---|
274 | var client_div = document.getElementById('trophyimclient'); |
---|
275 | if (client_div) |
---|
276 | { |
---|
277 | TrophyIM.client_div = client_div; |
---|
278 | |
---|
279 | //load .css |
---|
280 | //document.getElementsByTagName('head')[0].appendChild(DOMObjects.getHTML('cssLink')); |
---|
281 | //DOMObjects.getHTML('cssLink'); |
---|
282 | |
---|
283 | //Load other .js scripts needed |
---|
284 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/strophe.js')); |
---|
285 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/md5.js')); |
---|
286 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/sha1.js')); |
---|
287 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('strophejs/b64.js')); |
---|
288 | document.getElementsByTagName('head')[0].appendChild(DOMObjects.getScript('js/json2.js')); //Keep this script last |
---|
289 | |
---|
290 | //Wait a second to give scripts time to load |
---|
291 | setTimeout("TrophyIM.showLogin()", 500); |
---|
292 | } else { |
---|
293 | alert("Cannot load TrophyIM client.\nClient div not found."); |
---|
294 | }*/ |
---|
295 | }, |
---|
296 | /** Function: storeData |
---|
297 | * |
---|
298 | * Store all our data in the JSONStore, if it is active |
---|
299 | */ |
---|
300 | storeData : function() { |
---|
301 | if (TrophyIM.connection && TrophyIM.connection.connected) { |
---|
302 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
303 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
304 | TrophyIM.rosterObj.save(); |
---|
305 | } |
---|
306 | }, |
---|
307 | /** Function: showlogin |
---|
308 | * |
---|
309 | * This function clears out the IM box and either redisplays the login |
---|
310 | * page, or re-attaches to Strophe, preserving the logging div if it |
---|
311 | * exists, or creating a new one of we are re-attaching. |
---|
312 | */ |
---|
313 | showLogin : function() { |
---|
314 | //JSON is the last script to load, so we wait on it |
---|
315 | //Added Strophe check too because of bug where it's sometimes missing |
---|
316 | if (typeof(JSON) != undefined && typeof(Strophe) != undefined) |
---|
317 | { |
---|
318 | TrophyIM.JSONStore = new TrophyIMJSONStore(); |
---|
319 | |
---|
320 | if ( TrophyIM.JSONStore.store_working && TrophyIM.cookies['trophyim_bosh_xid'] ) |
---|
321 | { |
---|
322 | var xids = TrophyIM.cookies['trophyim_bosh_xid'].split("|"); |
---|
323 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
324 | TrophyIM.constants.stale_roster = true; |
---|
325 | if (TrophyIM.cookies['trophyimloglevel']) |
---|
326 | { |
---|
327 | TrophyIM.client_div.appendChild(DOMObjects.getHTML('loggingDiv')); |
---|
328 | TrophyIM.logging_div = document.getElementById('trophyimlog'); |
---|
329 | } |
---|
330 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
331 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
332 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
333 | Strophe.log = TrophyIM.log; |
---|
334 | Strophe.info('Attempting Strophe attach.'); |
---|
335 | TrophyIM.connection.attach(xids[0], xids[1], xids[2], TrophyIM.onConnect); |
---|
336 | TrophyIM.onConnect(Strophe.Status.CONNECTED); |
---|
337 | } |
---|
338 | else |
---|
339 | { |
---|
340 | var logging_div = TrophyIM.clearClient(); |
---|
341 | loadIM.HTMLSnippets.loginPage(); |
---|
342 | } |
---|
343 | } |
---|
344 | else |
---|
345 | { |
---|
346 | setTimeout("TrophyIM.showLogin()", 500); |
---|
347 | } |
---|
348 | }, |
---|
349 | /** Function: log |
---|
350 | * |
---|
351 | * This function logs the given message in the trophyimlog div |
---|
352 | * |
---|
353 | * Parameter: (String) msg - the message to log |
---|
354 | */ |
---|
355 | |
---|
356 | log : function(level, msg) |
---|
357 | { |
---|
358 | if (TrophyIM.logging_div && level >= TROPHYIM_LOGLEVEL) { |
---|
359 | while(TrophyIM.logging_div.childNodes.length > TROPHYIM_LOG_LINES) { |
---|
360 | TrophyIM.logging_div.removeChild( |
---|
361 | TrophyIM.logging_div.firstChild); |
---|
362 | } |
---|
363 | var msg_div = document.createElement('div'); |
---|
364 | msg_div.className = 'trophyimlogitem'; |
---|
365 | msg_div.appendChild(document.createTextNode(msg)); |
---|
366 | TrophyIM.logging_div.appendChild(msg_div); |
---|
367 | TrophyIM.logging_div.scrollTop = TrophyIM.logging_div.scrollHeight; |
---|
368 | } |
---|
369 | }, |
---|
370 | /** Function: rawInput |
---|
371 | * |
---|
372 | * This logs the packets actually recieved by strophe at the debug level |
---|
373 | */ |
---|
374 | rawInput : function (data) { |
---|
375 | Strophe.debug("RECV: " + data); |
---|
376 | }, |
---|
377 | /** Function: rawInput |
---|
378 | * |
---|
379 | * This logs the packets actually recieved by strophe at the debug level |
---|
380 | */ |
---|
381 | rawOutput : function (data) { |
---|
382 | Strophe.debug("SEND: " + data); |
---|
383 | }, |
---|
384 | /** Function: login |
---|
385 | * |
---|
386 | * This function logs into server using information given on login page. |
---|
387 | * Since the login page is where the logging checkbox is, it makes or |
---|
388 | * removes the logging div and cookie accordingly. |
---|
389 | * |
---|
390 | */ |
---|
391 | login : function() { |
---|
392 | if (document.getElementById('trophyimloglevel').checked) { |
---|
393 | TrophyIM.setCookie('trophyimloglevel', 1); |
---|
394 | if (!document.getElementById('trophyimlog')) { |
---|
395 | TrophyIM.client_div.appendChild(DOMObjects.getHTML('loggingDiv')); |
---|
396 | TrophyIM.logging_div = document.getElementById('trophyimlog'); |
---|
397 | } |
---|
398 | } else { |
---|
399 | TrophyIM.delCookie('trophyimloglevel'); |
---|
400 | if (document.getElementById('trophyimlog')) { |
---|
401 | TrophyIM.client_div.removeChild(document.getElementById( |
---|
402 | 'trophyimlog')); |
---|
403 | TrophyIM.logging_div = null; |
---|
404 | } |
---|
405 | } |
---|
406 | if (TrophyIM.JSONStore.store_working) { //In case they never logged out |
---|
407 | TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', |
---|
408 | 'chat_history']); |
---|
409 | } |
---|
410 | TrophyIM.connection = new Strophe.Connection(TROPHYIM_BOSH_SERVICE); |
---|
411 | TrophyIM.connection.rawInput = TrophyIM.rawInput; |
---|
412 | TrophyIM.connection.rawOutput = TrophyIM.rawOutput; |
---|
413 | Strophe.log = TrophyIM.log; |
---|
414 | var barejid = document.getElementById('trophyimjid').value |
---|
415 | var fulljid = barejid + '/TrophyIM'; |
---|
416 | TrophyIM.setCookie('trophyimjid', barejid); |
---|
417 | var password = document.getElementById('trophyimpass').value; |
---|
418 | var button = document.getElementById('trophyimconnect'); |
---|
419 | if (button.value == 'connect') { |
---|
420 | button.value = 'disconnect'; |
---|
421 | TrophyIM.connection.connect(fulljid, password, TrophyIM.onConnect); |
---|
422 | } else { |
---|
423 | button.value = 'connect'; |
---|
424 | TrophyIM.connection.disconnect(); |
---|
425 | } |
---|
426 | |
---|
427 | }, |
---|
428 | /** Function: login |
---|
429 | * |
---|
430 | * Logs into fresh session through Strophe, purging any old data. |
---|
431 | */ |
---|
432 | logout : function() |
---|
433 | { |
---|
434 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
435 | delete TrophyIM['cookies']['trophyim_bosh_xid']; |
---|
436 | if (TrophyIM.JSONStore.store_working) |
---|
437 | { |
---|
438 | TrophyIM.JSONStore.delData(['groups','roster', 'active_chat', 'chat_history']); |
---|
439 | } |
---|
440 | |
---|
441 | for (var chat in TrophyIM.activeChats['divs']) |
---|
442 | { |
---|
443 | delete TrophyIM.activeChats['divs'][chat]; |
---|
444 | } |
---|
445 | |
---|
446 | TrophyIM.activeChats = {current: null, divs: {}}, |
---|
447 | TrophyIM.connection.disconnect(); |
---|
448 | TrophyIM.showLogin(); |
---|
449 | |
---|
450 | }, |
---|
451 | /** Function onConnect |
---|
452 | * |
---|
453 | * Callback given to Strophe upon connection to BOSH proxy. |
---|
454 | */ |
---|
455 | onConnect : function(status) { |
---|
456 | if (status == Strophe.Status.CONNECTING) { |
---|
457 | Strophe.info('Strophe is connecting.'); |
---|
458 | } else if (status == Strophe.Status.CONNFAIL) { |
---|
459 | Strophe.info('Strophe failed to connect.'); |
---|
460 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
461 | TrophyIM.showLogin(); |
---|
462 | } else if (status == Strophe.Status.DISCONNECTING) { |
---|
463 | Strophe.info('Strophe is disconnecting.'); |
---|
464 | } else if (status == Strophe.Status.DISCONNECTED) { |
---|
465 | Strophe.info('Strophe is disconnected.'); |
---|
466 | TrophyIM.delCookie('trophyim_bosh_xid'); |
---|
467 | TrophyIM.showLogin(); |
---|
468 | } else if (status == Strophe.Status.CONNECTED) { |
---|
469 | Strophe.info('Strophe is connected.'); |
---|
470 | TrophyIM.showClient(); |
---|
471 | } |
---|
472 | }, |
---|
473 | |
---|
474 | /** Function: showClient |
---|
475 | * |
---|
476 | * This clears out the main div and puts in the main client. It also |
---|
477 | * registers all the handlers for Strophe to call in the client. |
---|
478 | */ |
---|
479 | showClient : function() |
---|
480 | { |
---|
481 | TrophyIM.setCookie('trophyim_bosh_xid', TrophyIM.connection.jid + "|" + |
---|
482 | TrophyIM.connection.sid + "|" + TrophyIM.connection.rid); |
---|
483 | var logging_div = TrophyIM.clearClient(); |
---|
484 | |
---|
485 | loadIM.HTMLSnippets.rosterDiv(); |
---|
486 | loadIM.HTMLSnippets.chatArea(); |
---|
487 | |
---|
488 | //TrophyIM.client_div.appendChild(DOMObjects.getHTML('rosterDiv')); |
---|
489 | //TrophyIM.client_div.appendChild(DOMObjects.getHTML('chatArea')); |
---|
490 | //TrophyIM.client_div.appendChild(DOMObjects.getHTML('statusDiv')); |
---|
491 | |
---|
492 | if(logging_div) |
---|
493 | { |
---|
494 | TrophyIM.client_div.appendChild(logging_div); |
---|
495 | TrophyIM.logging_div = document.getElementById('trophyimlog'); |
---|
496 | } |
---|
497 | TrophyIM.rosterObj = new TrophyIMRoster(); |
---|
498 | TrophyIM.connection.addHandler(TrophyIM.onVersion, Strophe.NS.VERSION, |
---|
499 | 'iq', null, null, null); |
---|
500 | TrophyIM.connection.addHandler(TrophyIM.onRoster, Strophe.NS.ROSTER, |
---|
501 | 'iq', null, null, null); |
---|
502 | TrophyIM.connection.addHandler(TrophyIM.onPresence, null, 'presence', |
---|
503 | null, null, null); |
---|
504 | TrophyIM.connection.addHandler(TrophyIM.onMessage, null, 'message', |
---|
505 | null, null, null); |
---|
506 | //Get roster then announce presence. |
---|
507 | TrophyIM.connection.send($iq({type: 'get', xmlns: Strophe.NS.CLIENT}).c( |
---|
508 | 'query', {xmlns: Strophe.NS.ROSTER}).tree()); |
---|
509 | TrophyIM.connection.send($pres().tree()); |
---|
510 | TrophyIM.renderChats(); |
---|
511 | setTimeout("TrophyIM.renderRoster()", 1000); |
---|
512 | }, |
---|
513 | |
---|
514 | /** Function: clearClient |
---|
515 | * |
---|
516 | * Clears out client div, preserving and returning existing logging_div if |
---|
517 | * one exists |
---|
518 | */ |
---|
519 | clearClient : function() { |
---|
520 | if(TrophyIM.logging_div) { |
---|
521 | var logging_div = TrophyIM.client_div.removeChild( |
---|
522 | document.getElementById('trophyimlog')); |
---|
523 | } else { |
---|
524 | var logging_div = null; |
---|
525 | } |
---|
526 | while(TrophyIM.client_div.childNodes.length > 0) { |
---|
527 | TrophyIM.client_div.removeChild(TrophyIM.client_div.firstChild); |
---|
528 | } |
---|
529 | return logging_div; |
---|
530 | }, |
---|
531 | /** Function: onVersion |
---|
532 | * |
---|
533 | * jabber:iq:version query handler |
---|
534 | */ |
---|
535 | onVersion : function(msg) { |
---|
536 | Strophe.debug("Version handler"); |
---|
537 | if (msg.getAttribute('type') == 'get') { |
---|
538 | var from = msg.getAttribute('from'); |
---|
539 | var to = msg.getAttribute('to'); |
---|
540 | var id = msg.getAttribute('id'); |
---|
541 | var reply = $iq({type: 'result', to: from, from: to, id: id}).c('query', |
---|
542 | {name: "TrophyIM", version: TROPHYIM_VERSION, os: |
---|
543 | "Javascript-capable browser"}); |
---|
544 | TrophyIM.connection.send(reply.tree()); |
---|
545 | } |
---|
546 | return true; |
---|
547 | }, |
---|
548 | /** Function: onRoster |
---|
549 | * |
---|
550 | * Roster iq handler |
---|
551 | */ |
---|
552 | onRoster : function(msg) { |
---|
553 | Strophe.debug("Roster handler"); |
---|
554 | var roster_items = msg.firstChild.getElementsByTagName('item'); |
---|
555 | for (var i = 0; i < roster_items.length; i++) { |
---|
556 | var groups = roster_items[i].getElementsByTagName('group'); |
---|
557 | var group_array = new Array(); |
---|
558 | for (var g = 0; g < groups.length; g++) { |
---|
559 | group_array[group_array.length] = |
---|
560 | groups[g].firstChild.nodeValue; |
---|
561 | } |
---|
562 | TrophyIM.rosterObj.addContact(roster_items[i].getAttribute('jid'), |
---|
563 | roster_items[i].getAttribute('subscription'), |
---|
564 | roster_items[i].getAttribute('name'), group_array); |
---|
565 | } |
---|
566 | if (msg.getAttribute('type') == 'set') { |
---|
567 | TrophyIM.connection.send($iq({type: 'reply', id: |
---|
568 | msg.getAttribute('id'), to: msg.getAttribute('from')}).tree()); |
---|
569 | } |
---|
570 | return true; |
---|
571 | }, |
---|
572 | /** Function: onPresence |
---|
573 | * |
---|
574 | * Presence handler |
---|
575 | */ |
---|
576 | onPresence : function(msg) { |
---|
577 | Strophe.debug("Presence handler"); |
---|
578 | var type = msg.getAttribute('type') ? msg.getAttribute('type') : |
---|
579 | 'available'; |
---|
580 | var show = msg.getElementsByTagName('show').length ? Strophe.getText( |
---|
581 | msg.getElementsByTagName('show')[0]) : type; |
---|
582 | var status = msg.getElementsByTagName('status').length ? Strophe.getText( |
---|
583 | msg.getElementsByTagName('status')[0]) : ''; |
---|
584 | var priority = msg.getElementsByTagName('priority').length ? parseInt( |
---|
585 | Strophe.getText(msg.getElementsByTagName('priority')[0])) : 0; |
---|
586 | TrophyIM.rosterObj.setPresence(msg.getAttribute('from'), priority, |
---|
587 | show, status); |
---|
588 | return true; |
---|
589 | }, |
---|
590 | /** Function: onMessage |
---|
591 | * |
---|
592 | * Message handler |
---|
593 | */ |
---|
594 | onMessage : function(msg) { |
---|
595 | Strophe.debug("Message handler"); |
---|
596 | var from = msg.getAttribute('from'); |
---|
597 | var type = msg.getAttribute('type'); |
---|
598 | var elems = msg.getElementsByTagName('body'); |
---|
599 | |
---|
600 | if ((type == 'chat' || type == 'normal') && elems.length > 0) { |
---|
601 | var barejid = Strophe.getBareJidFromJid(from); |
---|
602 | var jid_lower = barejid.toLowerCase(); |
---|
603 | var contact = TrophyIM.rosterObj.roster[barejid.toLowerCase()]['contact']; |
---|
604 | if (contact) { //Do we know you? |
---|
605 | if (contact['name'] != null) { |
---|
606 | message = contact['name'] + " (" + barejid + "): "; |
---|
607 | } else { |
---|
608 | message = contact['jid'] + ": "; |
---|
609 | } |
---|
610 | message += Strophe.getText(elems[0]); |
---|
611 | TrophyIM.makeChat(from); //Make sure we have a chat window |
---|
612 | TrophyIM.addMessage(message, jid_lower); |
---|
613 | |
---|
614 | /* |
---|
615 | if (TrophyIM.activeChats['current'] != jid_lower) { |
---|
616 | TrophyIM.activeChats['divs'][jid_lower][ |
---|
617 | 'tab'].className = "trophyimchattab trophyimchattab_a"; |
---|
618 | TrophyIM.setTabPresence(from, |
---|
619 | TrophyIM.activeChats['divs'][jid_lower]['tab']); |
---|
620 | } |
---|
621 | */ |
---|
622 | } |
---|
623 | } |
---|
624 | return true; |
---|
625 | }, |
---|
626 | /** Function: makeChat |
---|
627 | * |
---|
628 | * Make sure chat window to given fulljid exists, switching chat context to |
---|
629 | * given resource |
---|
630 | */ |
---|
631 | makeChat : function(fulljid) |
---|
632 | { |
---|
633 | |
---|
634 | var barjid = Strophe.getBareJidFromJid(fulljid); |
---|
635 | |
---|
636 | var winChatBox = |
---|
637 | { |
---|
638 | id_window : "window_chat_area_" + barjid, |
---|
639 | width : 387, |
---|
640 | height : 355, |
---|
641 | top : 100, |
---|
642 | left : 400, |
---|
643 | draggable : true, |
---|
644 | visible : "display", |
---|
645 | resizable : true, |
---|
646 | zindex : loadIM.getZIndex(), |
---|
647 | title : barjid.substring(0, barjid.indexOf('@')), |
---|
648 | closeAction : "hidden", |
---|
649 | content : loadIM.parse("chat_box","chatBox.xsl") |
---|
650 | } |
---|
651 | |
---|
652 | _winBuild(winChatBox) |
---|
653 | |
---|
654 | /* |
---|
655 | * Codigo Original |
---|
656 | * |
---|
657 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
658 | if (!TrophyIM.activeChats['divs'][barejid]) { |
---|
659 | var chat_tabs = document.getElementById('trophyimchattabs'); |
---|
660 | var chat_tab = DOMObjects.getHTML('chatTab'); |
---|
661 | var chat_box = DOMObjects.getHTML('chatBox'); |
---|
662 | var contact = TrophyIM.rosterObj.getContact(barejid); |
---|
663 | var tab_name = (contact['name'] != null) ? contact['name'] : barejid; |
---|
664 | chat_tab.className = "trophyimchattab trophyimchattab_a"; |
---|
665 | getElementsByClassName('trophyimchattabjid', 'div', chat_tab)[0].appendChild(document.createTextNode(barejid)); |
---|
666 | getElementsByClassName('trophyimchattabname', 'div', chat_tab)[0].appendChild(document.createTextNode(tab_name)); |
---|
667 | chat_tab = chat_tabs.appendChild(chat_tab); |
---|
668 | TrophyIM.activeChats['divs'][barejid] = {jid:fulljid, tab:chat_tab, box:chat_box}; |
---|
669 | |
---|
670 | if (!TrophyIM.activeChats['current']) { //We're the first |
---|
671 | TrophyIM.activeChats['current'] = barejid; |
---|
672 | document.getElementById('trophyimchat').appendChild(chat_box); |
---|
673 | TrophyIM.activeChats['divs'][barejid]['box'] = chat_box; |
---|
674 | TrophyIM.activeChats['divs'][barejid]['tab'].className = |
---|
675 | "trophyimchattab trophyimchattab_f"; |
---|
676 | } |
---|
677 | if (!TrophyIM.chatHistory[barejid.toLowerCase()]) { |
---|
678 | TrophyIM.chatHistory[barejid.toLowerCase()] = {resource: null, |
---|
679 | history: new Array()}; |
---|
680 | } |
---|
681 | TrophyIM.setTabPresence(fulljid, chat_tab); |
---|
682 | } |
---|
683 | TrophyIM.activeChats['divs'][barejid.toLowerCase()]['resource'] = |
---|
684 | Strophe.getResourceFromJid(fulljid); |
---|
685 | TrophyIM.chatHistory[barejid.toLowerCase()]['resource'] = |
---|
686 | Strophe.getResourceFromJid(fulljid);*/ |
---|
687 | |
---|
688 | }, |
---|
689 | /** Function showChat |
---|
690 | * |
---|
691 | * Make chat box to given barejid active |
---|
692 | */ |
---|
693 | showChat : function(barejid) { |
---|
694 | if (TrophyIM.activeChats['current'] && |
---|
695 | TrophyIM.activeChats['current'] != barejid) { |
---|
696 | var chat_area = document.getElementById('trophyimchat'); |
---|
697 | var active_divs = |
---|
698 | TrophyIM.activeChats['divs'][TrophyIM.activeChats['current']]; |
---|
699 | active_divs['box'] = |
---|
700 | chat_area.removeChild(getElementsByClassName('trophyimchatbox', |
---|
701 | 'div', chat_area)[0].parentNode); |
---|
702 | active_divs['tab'].className = "trophyimchattab trophyimchattab_b"; |
---|
703 | TrophyIM.setTabPresence(TrophyIM.activeChats['current'], |
---|
704 | active_divs['tab']); |
---|
705 | TrophyIM.activeChats['divs'][barejid]['box'] = |
---|
706 | chat_area.appendChild(TrophyIM.activeChats['divs'][barejid]['box']); |
---|
707 | TrophyIM.activeChats['current'] = barejid; |
---|
708 | TrophyIM.activeChats['divs'][barejid]['tab'].className = |
---|
709 | "trophyimchattab trophyimchattab_f"; |
---|
710 | TrophyIM.setTabPresence(barejid, |
---|
711 | TrophyIM.activeChats['divs'][barejid]['tab']); |
---|
712 | getElementsByClassName('trophyimchatinput', null, |
---|
713 | TrophyIM.activeChats['divs'][barejid]['box'])[0].focus(); |
---|
714 | } |
---|
715 | }, |
---|
716 | /** Function: setTabPresence |
---|
717 | * |
---|
718 | * Applies appropriate class to tab div based on presence |
---|
719 | * |
---|
720 | * Parameters: |
---|
721 | * (String) jid - jid to check presence for |
---|
722 | * (String) tab_div - tab div element to alter class on |
---|
723 | */ |
---|
724 | setTabPresence : function(jid, tab_div) { |
---|
725 | var presence = TrophyIM.rosterObj.getPresence(jid); |
---|
726 | tab_div.className = tab_div.className.replace(" trophyimchattab_av", ""); |
---|
727 | tab_div.className = tab_div.className.replace(" trophyimchattab_aw", ""); |
---|
728 | tab_div.className = tab_div.className.replace(" trophyimchattab_off", ""); |
---|
729 | if (presence) { |
---|
730 | if (presence['show'] == "chat" || presence['show'] == "available") { |
---|
731 | tab_div.className += " trophyimchattab_av"; |
---|
732 | } else { |
---|
733 | tab_div.className += " trophyimchattab_aw"; |
---|
734 | } |
---|
735 | } else { |
---|
736 | tab_div.className += " trophyimchattab_off"; |
---|
737 | } |
---|
738 | }, |
---|
739 | /** Function: addMessage |
---|
740 | * |
---|
741 | * Adds message to chat box, and history |
---|
742 | * |
---|
743 | * Parameters: |
---|
744 | * (string) msg - the message to add |
---|
745 | * (string) jid - the jid of chat box to add the message to. |
---|
746 | */ |
---|
747 | addMessage : function(msg, jid) |
---|
748 | { |
---|
749 | alert(jid + " : " + msg); |
---|
750 | |
---|
751 | /* |
---|
752 | * Codigo Original Comentado |
---|
753 | * |
---|
754 | |
---|
755 | var chat_box = getElementsByClassName('trophyimchatbox', 'div', |
---|
756 | TrophyIM.activeChats['divs'][jid]['box'])[0]; |
---|
757 | var msg_div = document.createElement('div'); |
---|
758 | msg_div.className = 'trophyimchatmessage'; |
---|
759 | msg_div.appendChild(document.createTextNode(msg)); |
---|
760 | chat_box.appendChild(msg_div); |
---|
761 | chat_box.scrollTop = chat_box.scrollHeight; |
---|
762 | if (TrophyIM.chatHistory[jid]['history'].length > 10) { |
---|
763 | TrophyIM.chatHistory[jid]['history'].shift(); |
---|
764 | } |
---|
765 | TrophyIM.chatHistory[jid]['history'][ |
---|
766 | TrophyIM.chatHistory[jid]['history'].length] = msg; |
---|
767 | */ |
---|
768 | }, |
---|
769 | /** Function: renderRoster |
---|
770 | * |
---|
771 | * Renders roster, looking only for jids flagged by setPresence as having |
---|
772 | * changed. |
---|
773 | */ |
---|
774 | renderRoster : function() { |
---|
775 | if (TrophyIM.rosterObj.changes.length > 0) { |
---|
776 | var roster_div = document.getElementById('trophyimroster'); |
---|
777 | if(roster_div) { |
---|
778 | var groups = new Array(); |
---|
779 | for (var group in TrophyIM.rosterObj.groups) { |
---|
780 | groups[groups.length] = group; |
---|
781 | } |
---|
782 | groups.sort(); |
---|
783 | var group_divs = getElementsByClassName('trophyimrostergroup', |
---|
784 | null, roster_div); |
---|
785 | for (var g = 0; g < group_divs.length; g++) { |
---|
786 | var group_name = getElementsByClassName('trophyimrosterlabel', |
---|
787 | null, group_divs[g])[0].firstChild.nodeValue; |
---|
788 | while (group_name > groups[0]) { |
---|
789 | var new_group = TrophyIM.renderGroup(groups[0], roster_div); |
---|
790 | new_group = roster_div.insertBefore(new_group, |
---|
791 | group_divs[g]); |
---|
792 | if (TrophyIM.rosterObj.groupHasChanges(groups[0])) { |
---|
793 | TrophyIM.renderGroupUsers(new_group, groups[0], |
---|
794 | TrophyIM.rosterObj.changes.slice()); |
---|
795 | } |
---|
796 | groups.shift(); |
---|
797 | } |
---|
798 | if (group_name == groups[0]) { |
---|
799 | groups.shift(); |
---|
800 | } |
---|
801 | if (TrophyIM.rosterObj.groupHasChanges(group_name)) { |
---|
802 | TrophyIM.renderGroupUsers(group_divs[g], group_name, |
---|
803 | TrophyIM.rosterObj.changes.slice()); |
---|
804 | } |
---|
805 | } |
---|
806 | while (groups.length) { |
---|
807 | var group_name = groups.shift(); |
---|
808 | var new_group = TrophyIM.renderGroup(group_name, |
---|
809 | roster_div); |
---|
810 | new_group = roster_div.appendChild(new_group); |
---|
811 | if (TrophyIM.rosterObj.groupHasChanges(group_name)) { |
---|
812 | TrophyIM.renderGroupUsers(new_group, group_name, |
---|
813 | TrophyIM.rosterObj.changes.slice()); |
---|
814 | } |
---|
815 | } |
---|
816 | } |
---|
817 | TrophyIM.rosterObj.changes = new Array(); |
---|
818 | TrophyIM.constants.stale_roster = false; |
---|
819 | } |
---|
820 | setTimeout("TrophyIM.renderRoster()", 1000); |
---|
821 | }, |
---|
822 | /** Function: renderChats |
---|
823 | * |
---|
824 | * Renders chats found in TrophyIM.chatHistory. Called upon page reload. |
---|
825 | * Waits for stale_roster flag to clear before trying to run, so that the |
---|
826 | * roster exists |
---|
827 | */ |
---|
828 | renderChats : function() { |
---|
829 | if (TrophyIM.constants.stale_roster == false) { |
---|
830 | if(TrophyIM.JSONStore.store_working) { |
---|
831 | var data = TrophyIM.JSONStore.getData(['chat_history', |
---|
832 | 'active_chat']); |
---|
833 | if (data['active_chat']) { |
---|
834 | for (var jid in data['chat_history']) { |
---|
835 | fulljid = jid + "/" + data['chat_history'][jid]['resource']; |
---|
836 | Strophe.info("Makechat " + fulljid); |
---|
837 | TrophyIM.makeChat(fulljid); |
---|
838 | for (var h = 0; h < |
---|
839 | data['chat_history'][jid]['history'].length; h++) { |
---|
840 | TrophyIM.addMessage(data['chat_history'][jid]['history'][h], |
---|
841 | jid); |
---|
842 | } |
---|
843 | } |
---|
844 | TrophyIM.chat_history = data['chat_history']; |
---|
845 | TrophyIM.showChat(data['active_chat']); |
---|
846 | } |
---|
847 | } |
---|
848 | } else { |
---|
849 | setTimeout("TrophyIM.renderChats()", 1000); |
---|
850 | } |
---|
851 | }, |
---|
852 | /** Function: renderGroup |
---|
853 | * |
---|
854 | * Renders actual group label in roster |
---|
855 | * |
---|
856 | * Parameters: |
---|
857 | * (String) group - name of group to render |
---|
858 | * (DOM) roster_div - roster div |
---|
859 | * |
---|
860 | * Returns: |
---|
861 | * DOM group div to append into roster |
---|
862 | */ |
---|
863 | renderGroup : function(group, roster_div) { |
---|
864 | var new_group = DOMObjects.getHTML('rosterGroup'); |
---|
865 | var label_div = getElementsByClassName( 'trophyimrosterlabel', null, |
---|
866 | new_group)[0]; |
---|
867 | label_div.appendChild(document.createTextNode(group)); |
---|
868 | new_group.appendChild(label_div); |
---|
869 | return new_group; |
---|
870 | }, |
---|
871 | /** Function: renderGroupUsers |
---|
872 | * |
---|
873 | * Re-renders user entries in given group div based on status of roster |
---|
874 | * |
---|
875 | * Parameter: (Array) changes - jids with changes in the roster. Note: |
---|
876 | * renderGroupUsers will clobber this. |
---|
877 | */ |
---|
878 | renderGroupUsers : function(group_div, group_name, changes) { |
---|
879 | var group_members = TrophyIM.rosterObj.groups[group_name]; |
---|
880 | var member_divs = getElementsByClassName('trophyimrosteritem', null, |
---|
881 | group_div); |
---|
882 | for (var m = 0; m < member_divs.length; m++) { |
---|
883 | member_jid = getElementsByClassName('trophyimrosterjid', null, |
---|
884 | member_divs[m])[0].firstChild.nodeValue; |
---|
885 | if (member_jid > changes[0]) { |
---|
886 | if (changes[0] in group_members) { |
---|
887 | var new_presence = TrophyIM.rosterObj.getPresence( |
---|
888 | changes[0]); |
---|
889 | if(new_presence) { |
---|
890 | var new_member = DOMObjects.getHTML('rosterItem'); |
---|
891 | var new_contact = |
---|
892 | TrophyIM.rosterObj.getContact(changes[0]); |
---|
893 | getElementsByClassName('trophyimrosterjid', null, |
---|
894 | new_member)[0].appendChild(document.createTextNode( |
---|
895 | changes[0])); |
---|
896 | var new_name = (new_contact['name'] != null) ? |
---|
897 | new_contact['name'] : changes[0]; |
---|
898 | getElementsByClassName('trophyimrostername', null, |
---|
899 | new_member)[0].appendChild(document.createTextNode( |
---|
900 | new_name)); |
---|
901 | group_div.insertBefore(new_member, member_divs[m]); |
---|
902 | if (new_presence['show'] == "available" || |
---|
903 | new_presence['show'] == "chat") { |
---|
904 | new_member.className = |
---|
905 | "trophyimrosteritem trophyimrosteritem_av"; |
---|
906 | } else { |
---|
907 | new_member.className = |
---|
908 | "trophyimrosteritem trophyimrosteritem_aw"; |
---|
909 | } |
---|
910 | } else { |
---|
911 | //show offline contacts |
---|
912 | } |
---|
913 | } |
---|
914 | changes.shift(); |
---|
915 | } else if (member_jid == changes[0]) { |
---|
916 | member_presence = TrophyIM.rosterObj.getPresence(member_jid); |
---|
917 | if(member_presence) { |
---|
918 | if (member_presence['show'] == "available" || |
---|
919 | member_presence['show'] == "chat") { |
---|
920 | member_divs[m].className = |
---|
921 | "trophyimrosteritem trophyimrosteritem_av"; |
---|
922 | } else { |
---|
923 | member_divs[m].className = |
---|
924 | "trophyimrosteritem trophyimrosteritem_aw"; |
---|
925 | } |
---|
926 | } else { |
---|
927 | //show offline status |
---|
928 | group_div.removeChild(member_divs[m]); |
---|
929 | } |
---|
930 | changes.shift(); |
---|
931 | } |
---|
932 | } |
---|
933 | while (changes.length > 0) { |
---|
934 | if (changes[0] in group_members) { |
---|
935 | var new_presence = TrophyIM.rosterObj.getPresence(changes[0]); |
---|
936 | if(new_presence) { |
---|
937 | var new_member = DOMObjects.getHTML('rosterItem'); |
---|
938 | var new_contact = |
---|
939 | TrophyIM.rosterObj.getContact(changes[0]); |
---|
940 | getElementsByClassName('trophyimrosterjid', null, |
---|
941 | new_member)[0].appendChild(document.createTextNode( |
---|
942 | changes[0])); |
---|
943 | var new_name = (new_contact['name'] != null) ? |
---|
944 | new_contact['name'] : changes[0]; |
---|
945 | getElementsByClassName('trophyimrostername', null, |
---|
946 | new_member)[0].appendChild(document.createTextNode( |
---|
947 | new_name)); |
---|
948 | group_div.appendChild(new_member); |
---|
949 | if (new_presence['show'] == "available" || |
---|
950 | new_presence['show'] == "chat") { |
---|
951 | new_member.className = |
---|
952 | "trophyimrosteritem trophyimrosteritem_av"; |
---|
953 | } else { |
---|
954 | new_member.className = |
---|
955 | "trophyimrosteritem trophyimrosteritem_aw"; |
---|
956 | } |
---|
957 | } else { |
---|
958 | //show offline |
---|
959 | } |
---|
960 | } |
---|
961 | changes.shift(); |
---|
962 | } |
---|
963 | }, |
---|
964 | /** Function: rosterClick |
---|
965 | * |
---|
966 | * Handles actions when a roster item is clicked |
---|
967 | */ |
---|
968 | rosterClick : function(roster_item) { |
---|
969 | var barejid = getElementsByClassName('trophyimrosterjid', null, |
---|
970 | roster_item)[0].firstChild.nodeValue; |
---|
971 | var presence = TrophyIM.rosterObj.getPresence(barejid); |
---|
972 | if (presence && presence['resource']) { |
---|
973 | var fulljid = barejid + "/" + presence['resource']; |
---|
974 | } else { |
---|
975 | var fulljid = barejid; |
---|
976 | } |
---|
977 | TrophyIM.makeChat(fulljid); |
---|
978 | TrophyIM.showChat(barejid); |
---|
979 | }, |
---|
980 | /** Function: tabClick |
---|
981 | * |
---|
982 | * Handles actions when a chat tab is clicked |
---|
983 | */ |
---|
984 | tabClick : function(tab_item) { |
---|
985 | var barejid = getElementsByClassName('trophyimchattabjid', null, |
---|
986 | tab_item)[0].firstChild.nodeValue; |
---|
987 | if (TrophyIM.activeChats['divs'][barejid]) { |
---|
988 | TrophyIM.showChat(barejid); |
---|
989 | } |
---|
990 | }, |
---|
991 | /** Function: tabClose |
---|
992 | * |
---|
993 | * Closes chat tab |
---|
994 | */ |
---|
995 | tabClose : function(tab_item) { |
---|
996 | var barejid = getElementsByClassName('trophyimchattabjid', null, |
---|
997 | tab_item.parentNode)[0].firstChild.nodeValue; |
---|
998 | if (TrophyIM.activeChats['current'] == barejid) { |
---|
999 | if (tab_item.parentNode.nextSibling) { |
---|
1000 | TrophyIM.showChat(getElementsByClassName('trophyimchattabjid', |
---|
1001 | null, tab_item.parentNode.nextSibling)[0].firstChild.nodeValue); |
---|
1002 | } else if (tab_item.parentNode.previousSibling) { |
---|
1003 | TrophyIM.showChat(getElementsByClassName('trophyimchattabjid', |
---|
1004 | null, tab_item.parentNode.previousSibling)[0].firstChild.nodeValue); |
---|
1005 | } else { //no other active chat |
---|
1006 | document.getElementById('trophyimchat').removeChild( |
---|
1007 | getElementsByClassName('trophyimchatbox')[0].parentNode); |
---|
1008 | delete TrophyIM.activeChats['current']; |
---|
1009 | } |
---|
1010 | } |
---|
1011 | delete TrophyIM.activeChats['divs'][barejid]; |
---|
1012 | delete TrophyIM.chatHistory[barejid]; |
---|
1013 | //delete tab |
---|
1014 | tab_item.parentNode.parentNode.removeChild(tab_item.parentNode); |
---|
1015 | }, |
---|
1016 | /** Function: sendMessage |
---|
1017 | * |
---|
1018 | * Send message from chat input to user |
---|
1019 | */ |
---|
1020 | sendMessage : function(chat_box) { |
---|
1021 | var message_input = |
---|
1022 | getElementsByClassName('trophyimchatinput', null, |
---|
1023 | chat_box.parentNode)[0]; |
---|
1024 | var active_jid = TrophyIM.activeChats['current']; |
---|
1025 | if(TrophyIM.activeChats['current']) { |
---|
1026 | var active_chat = |
---|
1027 | TrophyIM.activeChats['divs'][TrophyIM.activeChats['current']]; |
---|
1028 | var to = TrophyIM.activeChats['current']; |
---|
1029 | if (active_chat['resource']) { |
---|
1030 | to += "/" + active_chat['resource']; |
---|
1031 | } |
---|
1032 | TrophyIM.connection.send($msg({to: to, from: |
---|
1033 | TrophyIM.connection.jid, type: 'chat'}).c('body').t( |
---|
1034 | message_input.value).tree()); |
---|
1035 | TrophyIM.addMessage("Me:\n" + message_input.value, |
---|
1036 | TrophyIM.activeChats['current']); |
---|
1037 | } |
---|
1038 | message_input.value = ''; |
---|
1039 | message_input.focus(); |
---|
1040 | } |
---|
1041 | }; |
---|
1042 | |
---|
1043 | /** Class: TrophyIMRoster |
---|
1044 | * |
---|
1045 | * |
---|
1046 | * This object stores the roster and presence info for the TrophyIMClient |
---|
1047 | * |
---|
1048 | * roster[jid_lower]['contact'] |
---|
1049 | * roster[jid_lower]['presence'][resource] |
---|
1050 | */ |
---|
1051 | function TrophyIMRoster() { |
---|
1052 | /** Constants: internal arrays |
---|
1053 | * (Object) roster - the actual roster/presence information |
---|
1054 | * (Object) groups - list of current groups in the roster |
---|
1055 | * (Array) changes - array of jids with presence changes |
---|
1056 | */ |
---|
1057 | if (TrophyIM.JSONStore.store_working) { |
---|
1058 | var data = TrophyIM.JSONStore.getData(['roster', 'groups']); |
---|
1059 | this.roster = (data['roster'] != null) ? data['roster'] : {}; |
---|
1060 | this.groups = (data['groups'] != null) ? data['groups'] : {}; |
---|
1061 | } else { |
---|
1062 | this.roster = {}; |
---|
1063 | this.groups = {}; |
---|
1064 | } |
---|
1065 | this.changes = new Array(); |
---|
1066 | if (TrophyIM.constants.stale_roster) { |
---|
1067 | for (var jid in this.roster) { |
---|
1068 | this.changes[this.changes.length] = jid; |
---|
1069 | } |
---|
1070 | } |
---|
1071 | /** Function: addContact |
---|
1072 | * |
---|
1073 | * Adds given contact to roster |
---|
1074 | * |
---|
1075 | * Parameters: |
---|
1076 | * (String) jid - bare jid |
---|
1077 | * (String) subscription - subscription attribute for contact |
---|
1078 | * (String) name - name attribute for contact |
---|
1079 | * (Array) groups - array of groups contact is member of |
---|
1080 | */ |
---|
1081 | this.addContact = function(jid, subscription, name, groups) { |
---|
1082 | var contact = {jid:jid, subscription:subscription, name:name, groups:groups} |
---|
1083 | var jid_lower = jid.toLowerCase(); |
---|
1084 | if (this.roster[jid_lower]) { |
---|
1085 | this.roster[jid_lower]['contact'] = contact; |
---|
1086 | } else { |
---|
1087 | this.roster[jid_lower] = {contact:contact}; |
---|
1088 | } |
---|
1089 | groups = groups ? groups : ['']; |
---|
1090 | for (var g = 0; g < groups.length; g++) { |
---|
1091 | if (!this.groups[groups[g]]) { |
---|
1092 | this.groups[groups[g]] = {}; |
---|
1093 | } |
---|
1094 | this.groups[groups[g]][jid_lower] = jid_lower; |
---|
1095 | } |
---|
1096 | } |
---|
1097 | /** Function: getContact |
---|
1098 | * |
---|
1099 | * Returns contact entry for given jid |
---|
1100 | * |
---|
1101 | * Parameter: (String) jid - jid to return |
---|
1102 | */ |
---|
1103 | this.getContact = function(jid) { |
---|
1104 | if (this.roster[jid.toLowerCase()]) { |
---|
1105 | return this.roster[jid.toLowerCase()]['contact']; |
---|
1106 | } |
---|
1107 | } |
---|
1108 | /** Function: setPresence |
---|
1109 | * |
---|
1110 | * Sets presence |
---|
1111 | * |
---|
1112 | * Parameters: |
---|
1113 | * (String) fulljid: full jid with presence |
---|
1114 | * (Integer) priority: priority attribute from presence |
---|
1115 | * (String) show: show attribute from presence |
---|
1116 | * (String) status: status attribute from presence |
---|
1117 | */ |
---|
1118 | this.setPresence = function(fulljid, priority, show, status) { |
---|
1119 | var barejid = Strophe.getBareJidFromJid(fulljid); |
---|
1120 | var resource = Strophe.getResourceFromJid(fulljid); |
---|
1121 | var jid_lower = barejid.toLowerCase(); |
---|
1122 | if(show != 'unavailable') { |
---|
1123 | if (!this.roster[jid_lower]) { |
---|
1124 | this.addContact(barejid, 'not-in-roster'); |
---|
1125 | } |
---|
1126 | var presence = { |
---|
1127 | resource:resource, priority:priority, show:show, status:status |
---|
1128 | } |
---|
1129 | if (!this.roster[jid_lower]['presence']) { |
---|
1130 | this.roster[jid_lower]['presence'] = {} |
---|
1131 | } |
---|
1132 | this.roster[jid_lower]['presence'][resource] = presence |
---|
1133 | } else if (this.roster[jid_lower] && this.roster[jid_lower]['presence'] |
---|
1134 | && this.roster[jid_lower]['presence'][resource]) { |
---|
1135 | delete this.roster[jid_lower]['presence'][resource]; |
---|
1136 | } |
---|
1137 | this.addChange(jid_lower); |
---|
1138 | if (TrophyIM.activeChats['divs'][jid_lower]) { |
---|
1139 | TrophyIM.setTabPresence(jid_lower, |
---|
1140 | TrophyIM.activeChats['divs'][jid_lower]['tab']); |
---|
1141 | } |
---|
1142 | } |
---|
1143 | /** Function: addChange |
---|
1144 | * |
---|
1145 | * Adds given jid to this.changes, keeping this.changes sorted and |
---|
1146 | * preventing duplicates. |
---|
1147 | * |
---|
1148 | * Parameters |
---|
1149 | * (String) jid : jid to add to this.changes |
---|
1150 | */ |
---|
1151 | this.addChange = function(jid) { |
---|
1152 | for (var c = 0; c < this.changes.length; c++) { |
---|
1153 | if (this.changes[c] == jid) { |
---|
1154 | return; |
---|
1155 | } |
---|
1156 | } |
---|
1157 | this.changes[this.changes.length] = jid; |
---|
1158 | this.changes.sort(); |
---|
1159 | } |
---|
1160 | /** Function: getPresence |
---|
1161 | * |
---|
1162 | * Returns best presence for given jid as Array(resource, priority, show, |
---|
1163 | * status) |
---|
1164 | * |
---|
1165 | * Parameter: (String) fulljid - jid to return best presence for |
---|
1166 | */ |
---|
1167 | this.getPresence = function(fulljid) { |
---|
1168 | var jid = Strophe.getBareJidFromJid(fulljid); |
---|
1169 | var current = null; |
---|
1170 | if (this.roster[jid.toLowerCase()] && |
---|
1171 | this.roster[jid.toLowerCase()]['presence']) { |
---|
1172 | for (var resource in this.roster[jid.toLowerCase()]['presence']) { |
---|
1173 | var presence = this.roster[jid.toLowerCase()]['presence'][resource]; |
---|
1174 | if (current == null) { |
---|
1175 | current = presence |
---|
1176 | } else { |
---|
1177 | if(presence['priority'] > current['priority'] && ((presence['show'] == "chat" |
---|
1178 | || presence['show'] == "available") || (current['show'] != "chat" || |
---|
1179 | current['show'] != "available"))) { |
---|
1180 | current = presence |
---|
1181 | } |
---|
1182 | } |
---|
1183 | } |
---|
1184 | } |
---|
1185 | return current; |
---|
1186 | } |
---|
1187 | /** Function: groupHasChanges |
---|
1188 | * |
---|
1189 | * Returns true if current group has members in this.changes |
---|
1190 | * |
---|
1191 | * Parameters: |
---|
1192 | * (String) group - name of group to check |
---|
1193 | */ |
---|
1194 | this.groupHasChanges = function(group) { |
---|
1195 | for (var c = 0; c < this.changes.length; c++) { |
---|
1196 | if (this.groups[group][this.changes[c]]) { |
---|
1197 | return true; |
---|
1198 | } |
---|
1199 | } |
---|
1200 | return false; |
---|
1201 | } |
---|
1202 | /** Fuction: save |
---|
1203 | * |
---|
1204 | * Saves roster data to JSON store |
---|
1205 | */ |
---|
1206 | this.save = function() { |
---|
1207 | if (TrophyIM.JSONStore.store_working) { |
---|
1208 | TrophyIM.JSONStore.setData({roster:this.roster, |
---|
1209 | groups:this.groups, active_chat:TrophyIM.activeChats['current'], |
---|
1210 | chat_history:TrophyIM.chatHistory}); |
---|
1211 | } |
---|
1212 | } |
---|
1213 | } |
---|
1214 | /** Class: TrophyIMJSONStore |
---|
1215 | * |
---|
1216 | * |
---|
1217 | * This object is the mechanism by which TrophyIM stores and retrieves its |
---|
1218 | * variables from the url provided by TROPHYIM_JSON_STORE |
---|
1219 | * |
---|
1220 | */ |
---|
1221 | function TrophyIMJSONStore() { |
---|
1222 | this.store_working = false; |
---|
1223 | /** Function _newXHR |
---|
1224 | * |
---|
1225 | * Set up new cross-browser xmlhttprequest object |
---|
1226 | * |
---|
1227 | * Parameters: |
---|
1228 | * (function) handler = what to set onreadystatechange to |
---|
1229 | */ |
---|
1230 | this._newXHR = function (handler) { |
---|
1231 | var xhr = null; |
---|
1232 | if (window.XMLHttpRequest) { |
---|
1233 | xhr = new XMLHttpRequest(); |
---|
1234 | if (xhr.overrideMimeType) { |
---|
1235 | xhr.overrideMimeType("text/xml"); |
---|
1236 | } |
---|
1237 | } else if (window.ActiveXObject) { |
---|
1238 | xhr = new ActiveXObject("Microsoft.XMLHTTP"); |
---|
1239 | } |
---|
1240 | return xhr; |
---|
1241 | } |
---|
1242 | /** Function getData |
---|
1243 | * Gets data from JSONStore |
---|
1244 | * |
---|
1245 | * Parameters: |
---|
1246 | * (Array) vars = Variables to get from JSON store |
---|
1247 | * |
---|
1248 | * Returns: |
---|
1249 | * Object with variables indexed by names given in parameter 'vars' |
---|
1250 | */ |
---|
1251 | this.getData = function(vars) { |
---|
1252 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
1253 | Strophe.debug("Retrieving JSONStore data"); |
---|
1254 | var xhr = this._newXHR(); |
---|
1255 | var getdata = "get=" + vars.join(","); |
---|
1256 | try { |
---|
1257 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
1258 | } catch (e) { |
---|
1259 | Strophe.error("JSONStore open failed."); |
---|
1260 | return false; |
---|
1261 | } |
---|
1262 | xhr.setRequestHeader('Content-type', |
---|
1263 | 'application/x-www-form-urlencoded'); |
---|
1264 | xhr.setRequestHeader('Content-length', getdata.length); |
---|
1265 | xhr.send(getdata); |
---|
1266 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
1267 | try { |
---|
1268 | var dataObj = JSON.parse(xhr.responseText); |
---|
1269 | return this.emptyFix(dataObj); |
---|
1270 | } catch(e) { |
---|
1271 | Strophe.error("Could not parse JSONStore response" + |
---|
1272 | xhr.responseText); |
---|
1273 | return false; |
---|
1274 | } |
---|
1275 | } else { |
---|
1276 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
1277 | return false; |
---|
1278 | } |
---|
1279 | } |
---|
1280 | } |
---|
1281 | /** Function emptyFix |
---|
1282 | * Fix for bugs in external JSON implementations such as |
---|
1283 | * http://bugs.php.net/bug.php?id=41504. |
---|
1284 | * A.K.A. Don't use PHP, people. |
---|
1285 | */ |
---|
1286 | this.emptyFix = function(obj) { |
---|
1287 | if (typeof(obj) == "object") { |
---|
1288 | for (var i in obj) { |
---|
1289 | if (i == '_empty_') { |
---|
1290 | obj[""] = this.emptyFix(obj['_empty_']); |
---|
1291 | delete obj['_empty_']; |
---|
1292 | } else { |
---|
1293 | obj[i] = this.emptyFix(obj[i]); |
---|
1294 | } |
---|
1295 | } |
---|
1296 | } |
---|
1297 | return obj |
---|
1298 | } |
---|
1299 | /** Function delData |
---|
1300 | * Deletes data from JSONStore |
---|
1301 | * |
---|
1302 | * Parameters: |
---|
1303 | * (Array) vars = Variables to delete from JSON store |
---|
1304 | * |
---|
1305 | * Returns: |
---|
1306 | * Status of delete attempt. |
---|
1307 | */ |
---|
1308 | this.delData = function(vars) { |
---|
1309 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
1310 | Strophe.debug("Retrieving JSONStore data"); |
---|
1311 | var xhr = this._newXHR(); |
---|
1312 | var deldata = "del=" + vars.join(","); |
---|
1313 | try { |
---|
1314 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
1315 | } catch (e) { |
---|
1316 | Strophe.error("JSONStore open failed."); |
---|
1317 | return false; |
---|
1318 | } |
---|
1319 | xhr.setRequestHeader('Content-type', |
---|
1320 | 'application/x-www-form-urlencoded'); |
---|
1321 | xhr.setRequestHeader('Content-length', deldata.length); |
---|
1322 | xhr.send(deldata); |
---|
1323 | if (xhr.readyState == 4 && xhr.status == 200) { |
---|
1324 | try { |
---|
1325 | var dataObj = JSON.parse(xhr.responseText); |
---|
1326 | return dataObj; |
---|
1327 | } catch(e) { |
---|
1328 | Strophe.error("Could not parse JSONStore response"); |
---|
1329 | return false; |
---|
1330 | } |
---|
1331 | } else { |
---|
1332 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
1333 | return false; |
---|
1334 | } |
---|
1335 | } |
---|
1336 | } |
---|
1337 | /** Function setData |
---|
1338 | * Stores data in JSONStore, overwriting values if they exist |
---|
1339 | * |
---|
1340 | * Parameters: |
---|
1341 | * (Object) vars : Object containing named vars to store ({name: value, |
---|
1342 | * othername: othervalue}) |
---|
1343 | * |
---|
1344 | * Returns: |
---|
1345 | * Status of storage attempt |
---|
1346 | */ |
---|
1347 | this.setData = function(vars) { |
---|
1348 | if (typeof(TROPHYIM_JSON_STORE) != undefined) { |
---|
1349 | Strophe.debug("Storing JSONStore data"); |
---|
1350 | var senddata = "set=" + JSON.stringify(vars); |
---|
1351 | var xhr = this._newXHR(); |
---|
1352 | try { |
---|
1353 | xhr.open("POST", TROPHYIM_JSON_STORE, false); |
---|
1354 | } catch (e) { |
---|
1355 | Strophe.error("JSONStore open failed."); |
---|
1356 | return false; |
---|
1357 | } |
---|
1358 | xhr.setRequestHeader('Content-type', |
---|
1359 | 'application/x-www-form-urlencoded'); |
---|
1360 | xhr.setRequestHeader('Content-length', senddata.length); |
---|
1361 | xhr.send(senddata); |
---|
1362 | if (xhr.readyState == 4 && xhr.status == 200 && xhr.responseText == |
---|
1363 | "OK") { |
---|
1364 | return true; |
---|
1365 | } else { |
---|
1366 | Strophe.error("JSONStore open failed. Status: " + xhr.status); |
---|
1367 | return false; |
---|
1368 | } |
---|
1369 | } |
---|
1370 | } |
---|
1371 | var testData = true; |
---|
1372 | if (this.setData({testData:testData})) { |
---|
1373 | var testResult = this.getData(['testData']); |
---|
1374 | if (testResult && testResult['testData'] == true) { |
---|
1375 | this.store_working = true; |
---|
1376 | } |
---|
1377 | } |
---|
1378 | } |
---|
1379 | /** Constants: Node types |
---|
1380 | * |
---|
1381 | * Implementations of constants that IE doesn't have, but we need. |
---|
1382 | */ |
---|
1383 | if (document.ELEMENT_NODE == null) { |
---|
1384 | document.ELEMENT_NODE = 1; |
---|
1385 | document.ATTRIBUTE_NODE = 2; |
---|
1386 | document.TEXT_NODE = 3; |
---|
1387 | document.CDATA_SECTION_NODE = 4; |
---|
1388 | document.ENTITY_REFERENCE_NODE = 5; |
---|
1389 | document.ENTITY_NODE = 6; |
---|
1390 | document.PROCESSING_INSTRUCTION_NODE = 7; |
---|
1391 | document.COMMENT_NODE = 8; |
---|
1392 | document.DOCUMENT_NODE = 9; |
---|
1393 | document.DOCUMENT_TYPE_NODE = 10; |
---|
1394 | document.DOCUMENT_FRAGMENT_NODE = 11; |
---|
1395 | document.NOTATION_NODE = 12; |
---|
1396 | } |
---|
1397 | |
---|
1398 | /** Function: importNode |
---|
1399 | * |
---|
1400 | * document.importNode implementation for IE, which doesn't have importNode |
---|
1401 | * |
---|
1402 | * Parameters: |
---|
1403 | * (Object) node - dom object |
---|
1404 | * (Boolean) allChildren - import node's children too |
---|
1405 | */ |
---|
1406 | if (!document.importNode) { |
---|
1407 | document.importNode = function(node, allChildren) { |
---|
1408 | switch (node.nodeType) { |
---|
1409 | case document.ELEMENT_NODE: |
---|
1410 | var newNode = document.createElement(node.nodeName); |
---|
1411 | if (node.attributes && node.attributes.length > 0) { |
---|
1412 | for(var i = 0; i < node.attributes.length; i++) { |
---|
1413 | newNode.setAttribute(node.attributes[i].nodeName, |
---|
1414 | node.getAttribute(node.attributes[i].nodeName)); |
---|
1415 | } |
---|
1416 | } |
---|
1417 | if (allChildren && node.childNodes && |
---|
1418 | node.childNodes.length > 0) { |
---|
1419 | for (var i = 0; i < node.childNodes.length; i++) { |
---|
1420 | newNode.appendChild(document.importNode( |
---|
1421 | node.childNodes[i], allChildren)); |
---|
1422 | } |
---|
1423 | } |
---|
1424 | return newNode; |
---|
1425 | break; |
---|
1426 | case document.TEXT_NODE: |
---|
1427 | case document.CDATA_SECTION_NODE: |
---|
1428 | case document.COMMENT_NODE: |
---|
1429 | return document.createTextNode(node.nodeValue); |
---|
1430 | break; |
---|
1431 | } |
---|
1432 | }; |
---|
1433 | } |
---|
1434 | |
---|
1435 | /** Function: getElementsByClassName |
---|
1436 | * |
---|
1437 | * DOMObject.getElementsByClassName implementation for browsers that don't |
---|
1438 | * support it yet. |
---|
1439 | * |
---|
1440 | * Developed by Robert Nyman, http://www.robertnyman.com |
---|
1441 | * Code/licensing: http://code.google.com/p/getelementsbyclassname/ |
---|
1442 | */ |
---|
1443 | var getElementsByClassName = function (className, tag, elm){ |
---|
1444 | if (document.getElementsByClassName) { |
---|
1445 | getElementsByClassName = function (className, tag, elm) { |
---|
1446 | elm = elm || document; |
---|
1447 | var elements = elm.getElementsByClassName(className), |
---|
1448 | nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null, |
---|
1449 | returnElements = [], |
---|
1450 | current; |
---|
1451 | for(var i=0, il=elements.length; i<il; i+=1){ |
---|
1452 | current = elements[i]; |
---|
1453 | if(!nodeName || nodeName.test(current.nodeName)) { |
---|
1454 | returnElements.push(current); |
---|
1455 | } |
---|
1456 | } |
---|
1457 | return returnElements; |
---|
1458 | }; |
---|
1459 | } else if (document.evaluate) { |
---|
1460 | getElementsByClassName = function (className, tag, elm) { |
---|
1461 | tag = tag || "*"; |
---|
1462 | elm = elm || document; |
---|
1463 | var classes = className.split(" "), |
---|
1464 | classesToCheck = "", |
---|
1465 | xhtmlNamespace = "http://www.w3.org/1999/xhtml", |
---|
1466 | namespaceResolver = (document.documentElement.namespaceURI === |
---|
1467 | xhtmlNamespace)? xhtmlNamespace : null, |
---|
1468 | returnElements = [], |
---|
1469 | elements, |
---|
1470 | node; |
---|
1471 | for(var j=0, jl=classes.length; j<jl; j+=1){ |
---|
1472 | classesToCheck += "[contains(concat(' ', @class, ' '), ' " + |
---|
1473 | classes[j] + " ')]"; |
---|
1474 | } |
---|
1475 | try { |
---|
1476 | elements = document.evaluate(".//" + tag + classesToCheck, |
---|
1477 | elm, namespaceResolver, 0, null); |
---|
1478 | } catch (e) { |
---|
1479 | elements = document.evaluate(".//" + tag + classesToCheck, |
---|
1480 | elm, null, 0, null); |
---|
1481 | } |
---|
1482 | while ((node = elements.iterateNext())) { |
---|
1483 | returnElements.push(node); |
---|
1484 | } |
---|
1485 | return returnElements; |
---|
1486 | }; |
---|
1487 | } else { |
---|
1488 | getElementsByClassName = function (className, tag, elm) { |
---|
1489 | tag = tag || "*"; |
---|
1490 | elm = elm || document; |
---|
1491 | var classes = className.split(" "), |
---|
1492 | classesToCheck = [], |
---|
1493 | elements = (tag === "*" && elm.all)? elm.all : |
---|
1494 | elm.getElementsByTagName(tag), |
---|
1495 | current, |
---|
1496 | returnElements = [], |
---|
1497 | match; |
---|
1498 | for(var k=0, kl=classes.length; k<kl; k+=1){ |
---|
1499 | classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + |
---|
1500 | "(\\s|$)")); |
---|
1501 | } |
---|
1502 | for(var l=0, ll=elements.length; l<ll; l+=1){ |
---|
1503 | current = elements[l]; |
---|
1504 | match = false; |
---|
1505 | for(var m=0, ml=classesToCheck.length; m<ml; m+=1){ |
---|
1506 | match = classesToCheck[m].test(current.className); |
---|
1507 | if (!match) { |
---|
1508 | break; |
---|
1509 | } |
---|
1510 | } |
---|
1511 | if (match) { |
---|
1512 | returnElements.push(current); |
---|
1513 | } |
---|
1514 | } |
---|
1515 | return returnElements; |
---|
1516 | }; |
---|
1517 | } |
---|
1518 | return getElementsByClassName(className, tag, elm); |
---|
1519 | }; |
---|
1520 | |
---|
1521 | /** |
---|
1522 | * |
---|
1523 | * Bootstrap self into window.onload and window.onunload |
---|
1524 | */ |
---|
1525 | var oldonload = window.onload; |
---|
1526 | window.onload = function() { |
---|
1527 | if(oldonload) { |
---|
1528 | oldonload(); |
---|
1529 | } |
---|
1530 | TrophyIM.load(); |
---|
1531 | }; |
---|
1532 | var oldonunload = window.onunload; |
---|
1533 | window.onunload = function() { |
---|
1534 | if(oldonunload) { |
---|
1535 | oldonunload(); |
---|
1536 | } |
---|
1537 | TrophyIM.storeData(); |
---|
1538 | } |
---|