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