source: trunk/instant_messenger/js/jabber.js @ 305

Revision 305, 15.8 KB checked in by niltonneto, 16 years ago (diff)

Correçoes

Line 
1(function()
2{
3        var _conn;
4        var _xtools;
5        var _window;
6        var _ldap;
7
8        var _menu = new ShowHidden(1500);
9
10        var _vcards = [];
11        var _info = [];
12
13        var _win_position = 30;
14
15        function _next_position()
16        {
17                return (_win_position += 30);
18        }
19
20        function _chat(_pJid)
21        {
22                var _win_pos = _next_position();
23                var _win_chat = _window.load('chat' + _pJid);
24                        _win_chat.title('.::Expresso Messenger::.');
25                        _win_chat.size(310);
26                        _win_chat.content(_xtools.parse('chat.xsl'));
27                        _win_chat.position(_win_pos, _win_pos);
28                        _win_chat.wc.style.padding = "3px";
29
30                _ldap.photo(
31                        _pJid.substr(0,_pJid.indexOf('@')),
32                        _win_chat.wc.childNodes[2]
33                );
34
35                var _iframe = top.document.createElement('iframe');
36                        _iframe.id = 'iframe_' + _pJid;
37
38                _win_chat.wc.childNodes[2].appendChild(_iframe);
39
40                _iframe.style.width = '250px';
41                _iframe.style.height = '100%';
42                _iframe.style.border = '0';
43                _iframe.style.margin = '0';
44
45                var _contentWindow = _iframe.contentWindow;
46
47                _iframe = _contentWindow.document;
48                _iframe.write('<html><body style="padding:0;margin:0;"></body></html>');
49                _iframe.close();
50                _iframe.designMode = "On";
51
52                _contentWindow.focus();
53
54                _config(
55                        _win_chat.wc.childNodes[1].childNodes[5],
56                        'onclick',
57                        function()
58                        {
59                                _info[_pJid] = true;
60                                _vcard(_pJid);
61                        }
62                );
63
64                _config(
65                        _win_chat.wc.childNodes[1].lastChild.previousSibling,
66                        'onclick',
67                        function()
68                        {
69                                _sendMessage(
70                                        _pJid,
71                                        _iframe,
72                                        _win_chat.wc.firstChild
73                                );
74                                _iframe.body.innerHTML = '';
75                                _contentWindow.focus();
76                        }
77                );
78
79                function _execKeyAction(e)
80                {
81                        switch ( e.keyCode )
82                        {
83                                case 13 :
84                                        if ( !(e.shiftKey) )
85                                        {
86                                                if ( e.type.indexOf('keydown') != -1 )
87                                                        _sendMessage(
88                                                                _pJid,
89                                                                _iframe,
90                                                                _win_chat.wc.firstChild
91                                                        );
92                                                else
93                                                        _iframe.body.innerHTML = '';
94
95                                                return false;
96                                        }
97                                break;
98                                case 27 :
99                                        if ( e.type.indexOf('keyup') != -1 )
100                                                _win_chat.button(_win_chat.bc);
101                                break;
102                        }
103                }
104                _config(_contentWindow.document, 'onkeydown', _execKeyAction);
105                _config(_contentWindow.document, 'onkeyup', _execKeyAction);
106
107                if ( !(_vcards[_pJid]) )
108                        _conn.go('$this.Ujabber.getVcard','jid='+_pJid);
109                else
110                        _readVCard(_vcards[_pJid]);
111
112                return _win_chat;
113        }
114
115        function _config(pObj, pEvent, pHandler)
116        {
117                if ( typeof pObj == 'object' )
118                {
119                        if ( pEvent.substring(0, 2) == 'on' )
120                                pEvent = pEvent.substring(2, pEvent.length);
121
122                        if ( pObj.addEventListener )
123                                pObj.addEventListener(pEvent, pHandler, false);
124                        else if ( pObj.attachEvent )
125                                pObj.attachEvent('on' + pEvent, pHandler);
126                }
127        }
128
129        function _openChat(el)
130        {
131                var id = el.id;
132
133                id = id.substr(id.indexOf('_') + 1);
134
135                if ( id.indexOf('/') > -1 )
136                        id = id.substr(0, id.indexOf('/'));
137
138                var _win = _window.get('chat' + id);
139                if ( !_win )
140                        _chat(id);
141                else
142                {
143                        _win.wm.style.display = 'block';
144                        _win.focus();
145                        _win.wc.childNodes[2].firstChild.contentWindow.focus();
146                }
147        }
148
149        function _readContacts(_pContacts)
150        {
151                var _win_contact = _window.load('contact_list');
152                        _win_contact.title('.::Expresso Messenger::.');
153                        _win_contact.size(170, 350);
154                        _win_contact.position(30, 30, true);
155                        _win_contact.bc.style.display = 'none';
156                        _win_contact.wm.style.display = 'none';
157
158                var _params = {
159                        'lang1':imGetLang('nickname'),
160                        'lang2':imGetLang('add contact'),
161                        'lang3':imGetLang('preferences'),
162                        'nickname':imGetLang('username')
163                };
164                _win_contact.content(
165                        _xtools.parse(_xtools.xml('layer'),'layer.xsl', _params)
166                        + _xtools.parse(_pContacts, 'contacts.xsl', {'path':path_im})
167                );
168
169                _config(_win_contact.wc.firstChild.childNodes[5], 'onclick', _ldap.add);
170
171                var _contact_list = top.document.getElementById('im_contact_list');
172                if ( _contact_list )
173                {
174                        _conn.go('$this.Ujabber.setStatus');
175                        _ldap.photo(
176                                'im_avatar',
177                                _win_contact.wc.firstChild
178                        );
179                        _conn.go('$this.Ujabber.getVcard','jid=this');
180                        function _click(e)
181                        {
182                                var el = ( e.target ) ? e.target : e.srcElement;
183                                //alert(e.button);
184                                if ( (e.button == 0 && !document.all) || (e.button == 1 && document.all) )
185                                        _openChat(el);
186                                else
187                                {
188                                        var _options = [
189                                                imGetLang("Nickname"),
190                                                imGetLang("Group"),
191                                                imGetLang("Remove"),
192                                                imGetLang("It requisition permission"),
193                                                imGetLang("See Info")
194                                        ];
195
196                                        var _style_text = 'background: no-repeat url(' + path_im
197                                                                + 'templates/default/images/group_close.gif);'
198                                                                + 'padding:2px 4px 2px 16px;';
199
200                                        var _xml = _xtools.xml('menu');
201                                        var _option;
202                                        var _item;
203                                        var _action;
204                                        var _style;
205
206                                        for ( var i in _options )
207                                        {
208                                                _option = _xml.createElement('option');
209
210                                                _item = _xml.createElement('item');
211                                                _item.appendChild(_xml.createTextNode(_options[i]));
212
213                                                _style = _xml.createElement('style');
214                                                _style.appendChild(_xml.createTextNode(_style_text));
215
216                                                _option.appendChild(_item);
217                                                _option.appendChild(_style);
218
219                                                _xml.documentElement.appendChild(_option);
220                                        }
221
222                                        var _sub_menu = top.document.getElementById('sub_' + el.getAttribute('jid'));
223                                                _sub_menu.innerHTML = _xtools.parse(_xml, 'options.xsl');
224                                                _menu.action('onmouseover', 'onmouseout', _sub_menu);
225
226                                        _sub_menu = _sub_menu.firstChild;
227                                        _sub_menu.childNodes[0].style.display = 'none';
228                                        _sub_menu.childNodes[1].style.display = 'none';
229                                        _sub_menu.childNodes[3].style.display = 'none';
230                                        _config(
231                                                _sub_menu.childNodes[2],
232                                                'onclick',
233                                                function(e)
234                                                {
235                                                        var el = ( e.target ) ? e.target : e.srcElement;
236                                                        var jid = el.parentNode.parentNode.id.substr(4);
237                                                        _conn.go('$this.Ujabber.removeContact','jid='+jid);
238                                                }
239                                        );
240                                        _config(
241                                                _sub_menu.childNodes[4],
242                                                'onclick',
243                                                function()
244                                                {
245                                                        _info[el.getAttribute('jid')] = true;
246                                                        _vcard(el.getAttribute('jid'));
247                                                }
248                                        );
249                                }
250                        }
251                        function _hover(e)
252                        {
253                                var el = ( e.target ) ? e.target : e.srcElement;
254                                var color = "#fff";
255                                if ( e.type.indexOf('mouseover') != -1 )
256                                        color = "#b4cfe5";
257                                el.style.backgroundColor = color;
258                        }
259                        var _contact = _contact_list.firstChild;
260                        while ( _contact )
261                        {
262                                if ( _contact.getAttribute('jid') )
263                                {
264                                        //_config(_contact, 'onmousedown', _click);
265                                        _config(_contact, 'onmouseup', _click);
266                                        _config(_contact, 'onmouseover', _hover);
267                                        _config(_contact, 'onmouseout', _hover);
268                                }
269                                _contact = _contact.nextSibling;
270                        }
271                }
272        }
273
274        function _vcard(_pJid)
275        {
276                if ( _vcards[_pJid] )
277                {
278                        if ( _info[_pJid] )
279                                delete _info[_pJid];
280
281                        var _win_vcard;
282                        if ( !(_win_vcard = _window.get('vcard_' + _pJid)) )
283                        {
284                                var _win_pos = _next_position();
285
286                                _win_vcard = _window.load('vcard_' + _pJid);
287                                _win_vcard.title('.:: Informações do Contato ::.');
288                                _win_vcard.size(300);
289                                _win_vcard.position(_win_pos, _win_pos);
290
291                                _win_vcard.wc.style.padding = "3px";
292                                _win_vcard.bx.style.display = "none";
293
294                                var _params = {
295                                        'fullname' : imGetLang('Full Name'),
296                                        'nickname' : imGetLang('Nickname'),
297                                        'organization' : imGetLang('Organization'),
298                                        'sector' : imGetLang('Sector'),
299                                        'role' : imGetLang('Role'),
300                                        'birthday' : imGetLang('Birthday')
301                                };
302
303                                _win_vcard.content(
304                                        _xtools.parse(
305                                                _vcards[_pJid],
306                                                'vcard_contacts.xsl',
307                                                _params
308                                        )
309                                );
310                        }
311                        else
312                                _win_vcard.focus();
313                }
314                else
315                        _conn.go('$this.Ujabber.getVcard', 'jid='+_pJid);
316        }
317
318        function _readVCard(_pVCard)
319        {
320                _vcards[_pVCard.getAttribute('from')] = _pVCard;
321
322                switch ( _pVCard.getAttribute('id') )
323                {
324                        case "vCard_user" :
325                                var _layer_nickname = top.document.getElementById('im_layer_nickname');
326                                var _nickname = _pVCard.getElementsByTagName('NICKNAME');
327
328                                if ( _nickname.length && _layer_nickname )
329                                        _layer_nickname.innerHTML = _nickname[0].firstChild.nodeValue;
330                        break;
331                        case "vCard" :
332                                if ( _info[_pVCard.getAttribute('from')] )
333                                        _vcard(_pVCard.getAttribute('from'));
334                        break;
335                }
336        }
337
338        function _readIq(pIq)
339        {
340                switch ( pIq.getAttribute('id') )
341                {
342                        case 'contacts' :
343                                _readContacts(pIq);
344                        break;
345                        case 'vCard' :
346                        case 'vCard_user' :
347                                _readVCard(pIq);
348                        break;
349                        default : //alert('readIq : ' + pIq.getAttribute('id'));
350                }
351        }
352
353        function _readMessage(_pMessage)
354        {
355                var _from = _pMessage.getAttribute('from');
356
357                if ( _from.indexOf('/') > -1 )
358                        _from = _from.substr(0, _from.indexOf('/'));
359
360                var _win = _window.get('chat' + _from);
361
362                if ( _win && _pMessage.getElementsByTagName('composing').length > 0 )
363                        _win.wc.childNodes[1].lastChild.style.display = 'block';
364
365                if ( _pMessage.getElementsByTagName('body').length > 0 )
366                {
367                        if ( !_win )
368                                _win = _chat(_from);
369
370                        var _history = _win.wc.firstChild;
371
372                        var _nickname;
373                        if ( _vcards[_from] )
374                        {
375                                _nickname = _vcards[_from].getElementsByTagName('NICKNAME');
376                                if ( _nickname.length )
377                                        _nickname = _nickname[0].firstChild.nodeValue;
378                                else
379                                        _nickname = _from;
380                        }
381                        else
382                                _nickname = _from;
383
384                        var _msg = _xtools.parse(_pMessage, 'message_new.xsl', {'nickname':_nickname});
385
386                        if ( _history.scrollHeight == (_history.scrollTop + _history.clientHeight) )
387                        {
388                                _history.innerHTML += _msg;
389                                _history.scrollTop = _history.scrollHeight;
390                        }
391                        else
392                                // this "else" is to prevent the creation of a flag and the need to use another conditional
393                                _history.innerHTML += _msg;
394
395                        _win.wc.childNodes[1].lastChild.style.display = 'none';
396                }
397
398                if ( _win && _pMessage.getElementsByTagName('paused').length > 0 )
399                        _win.wc.childNodes[1].lastChild.style.display = 'none';
400        }
401
402        function _readPresence(_pPresence)
403        {
404                var _from = _pPresence.getAttribute('from');
405                if ( _from.indexOf('/') > -1 )
406                        _from = _from.substr(0, _from.indexOf('/'));
407
408                var _presence_type = _pPresence.getAttribute('type');
409                var _img_status_contact = top.document.getElementById('contact_' + _from);
410                var _mensagem_status_contact = top.document.getElementById('status_' + _from);
411                if ( _mensagem_status_contact )
412                {
413                        _mensagem_status_contact.innerHTML = '';
414                        _mensagem_status_contact.style.display = 'none';
415                }
416
417                if ( _presence_type )
418                {
419                        switch( _presence_type )
420                        {
421                                case 'available' :
422                                case 'unavailable' :
423                                        if ( _img_status_contact )
424                                                _img_status_contact.style.backgroundImage = 'url(' + eval('im_' + _presence_type + '.src') + ')';
425                                break;
426                                case 'subscribe' :
427                                        if ( top.document.getElementById('contact_' + _from) )
428                                        {
429                                                _conn.go('$this.Ujabber.subscription', 'jid=' + _from + '&type=subscribed' );
430                                        }
431                                        else
432                                        {
433                                                if ( !_window.get('subscription' + _from) )
434                                                {
435                                                        var _win_pos = _next_position();
436                                                        var _win_subscription = _window.load('subscription' + _from);
437                                                                _win_subscription.title('.::Expresso Messenger::.');
438                                                                _win_subscription.position(_win_pos, _win_pos);
439                                                                _win_subscription.size(200);
440                                                                _win_subscription.wc.style.padding = "3px";
441                                                                _win_subscription.bc.style.display = 'none';
442                                                                _win_subscription.bx.style.display = 'none';
443                                                                _win_subscription.bz.style.display = 'none';
444
445                                                        var _params = {
446                                                                'jid' : _from,
447                                                                'lang1' : 'deseja adiciona-lo como contato.',
448                                                                'lang2' : imGetLang('nickname'),
449                                                                'lang3' : imGetLang('group'),
450                                                                'lang4' : imGetLang('allow'),
451                                                                'lang5' : imGetLang('deny')
452                                                        };
453
454                                                        _win_subscription.content(
455                                                                _xtools.parse(_xtools.xml('subscribe'), 'subscribe.xsl', _params)
456                                                        );
457
458                                                        _win_subscription.wc.childNodes[2].style.margin = '5px';
459                                                        _win_subscription.wc.childNodes[3].style.margin = '5px';
460
461                                                        _config(
462                                                                _win_subscription.wc.childNodes[2],
463                                                                'onclick',
464                                                                function()
465                                                                {
466                                                                        _win_subscription.wc.childNodes[1].style.display = 'block';
467                                                                        _win_subscription.size(450);
468                                                                        _config(
469                                                                                _win_subscription.wc.childNodes[2],
470                                                                                'onclick',
471                                                                                function()
472                                                                                {
473
474                                                                                        var _status = top.document.getElementById('im_status_add_' + _from);
475                                                                                        var _name = top.document.getElementById('im_name_' + _from);
476                                                                                        var _group = top.document.getElementById('im_group_' + _from);
477
478                                                                                        _name.value = _name.value.replace(/^( )*|( )*$/g, '');
479                                                                                        _group.value = _group.value.replace(/^( )*|( )*$/g, '');
480
481                                                                                        if ( !(_name.value) || !(_group.value) )
482                                                                                        {
483                                                                                                _status.innerHTML = '<br/>O campo destacado em vermelho é obrigatório!<br/>';
484
485                                                                                                if ( !(_name.value) )
486                                                                                                {
487                                                                                                        _name.previousSibling.style.color = "#f00";
488                                                                                                        _status.innerHTML += '<br/>** Informe um "NOME" para o contato.';
489                                                                                                }
490
491                                                                                                if ( !(_group.value) )
492                                                                                                {
493                                                                                                        _group.previousSibling.style.color = "#f00";
494                                                                                                        _status.innerHTML += '<br/>** Informe um "GRUPO" para o contato.';
495                                                                                                }
496                                                                                        }
497                                                                                        else
498                                                                                        {
499                                                                                                _status.innerHTML = '<br/>Adicionado contanto, aguarde.<br/>';
500
501                                                                                                _conn.go(
502                                                                                                        '$this.Ujabber.allowContact',
503                                                                                                        function()
504                                                                                                        {
505                                                                                                                _status.innerHTML = '<br/>Contato autorizado com "SUCESSO"!<br/>';
506                                                                                                                _win_subscription.button(_win_subscription.bc);
507                                                                                                        },
508                                                                                                        'uid='+_from.substr(0,_from.indexOf('@'))+'&name='+_name.value+'&group='+_group.value
509                                                                                                );
510
511                                                                                                _name.value = '';
512                                                                                                _group.value = '';
513                                                                                        }
514                                                                                }
515                                                                        );
516                                                                }
517                                                        );
518                                                }
519                                        }
520                                break;
521                                case 'unsubscribe' :
522                                case 'unsubscribed' :
523                                        _conn.go('$this.Ujabber.removeContact','jid=' + _from);
524                                break;
525                                default : //alert('readPresence : ' + _presence_type);
526                        }
527                }
528                else
529                {
530                        var _node = _pPresence.firstChild;
531                        while( _node )
532                        {
533                                if ( _node.hasChildNodes() )
534                                {
535                                        switch ( _node.nodeName )
536                                        {
537                                                case "show" :
538                                                        if ( _img_status_contact )
539                                                                _img_status_contact.style.backgroundImage = 'url(' + eval('im_' + _node.firstChild.nodeValue + '.src') + ')';
540                                                break;
541                                                case "status" :
542                                                        if ( _mensagem_status_contact )
543                                                        {
544                                                                if ( _node.firstChild.nodeValue )
545                                                                        _mensagem_status_contact.innerHTML = _node.firstChild.nodeValue;
546                                                                _mensagem_status_contact.style.display = 'block';
547                                                        }
548                                                break;
549                                                default :
550                                                        if ( _img_status_contact )
551                                                                _img_status_contact.style.backgroundImage = 'url(' + im_available.src + ')';
552                                        }
553                                }
554                                _node = _node.nextSibling;
555                        }
556                }
557        }
558
559        function _sendMessage(_pJid, _pMessage, _pHistory)
560        {
561                var m1 = _pMessage = _pMessage.body.innerHTML;
562                var m2 = _pMessage = _pMessage.replace(/(&nbsp;)+|( )+/ig, ' ');
563                var m3 = _pMessage = _pMessage.replace(/(<br[^>]*>)+/ig, '<br/>');
564                var m4 = _pMessage = _pMessage.replace(/^( )+|( )+$|^(<br\/>)|(<br\/>)$/g, '');
565
566                //_pMessage = _pMessage.replace(/( )+(<br[^>]*>)+/g, '<br/>');
567                //_pMessage = _pMessage.replace(/(<br[^>]*>)*( )+(<br[^>]*>)+/g, '');
568                //_pMessage = _pMessage.replace(/^(<br[^>]*>)*|(<br[^>]*>)*$/g, '');
569
570                if ( _pMessage != '' )
571                {
572                        _conn.go('$this.Ujabber.sendMessage', "to=" + _pJid + "&body=" + escape(_pMessage));
573
574                        if ( _pHistory.scrollHeight == (_pHistory.scrollTop + _pHistory.clientHeight) )
575                        {
576                                _pHistory.innerHTML += '<b>' + imGetLang('says') + ':</b><br/>' + _pMessage + '<br/><br/>';
577                            _pHistory.scrollTop = _pHistory.scrollHeight;
578                        }
579                        else
580                        // this "else" is to prevent the creation of a flag and the need to use another conditional
581                                _pHistory.innerHTML += '<b>' + imGetLang('says') + ':</b><br/>' + _pMessage + '<br/><br/>';
582                }
583        }
584
585        function _action(_pAction)
586        {
587                //alert ( typeof pAction + "\n\n" + pAction.nodeName )
588                if ( typeof _pAction == 'object' && _pAction.nodeName )
589                {
590                        switch ( _pAction.nodeName )
591                        {
592                                case 'iq' :
593                                        _readIq(_pAction);
594                                break;
595                                case 'message' :
596                                        _readMessage(_pAction);
597                                break;
598                                case 'presence' :
599                                        _readPresence(_pAction);
600                                break;
601                                //default : alert('action : ' + _pAction.nodeName);
602                        }
603                }
604        }
605
606        function Jabber()
607        {
608                _conn = arguments[0];
609                _xtools = arguments[1];
610                _window = arguments[2];
611                _ldap = arguments[3];
612        }
613
614        Jabber.prototype.action = _action;
615        window.Jabber = Jabber;
616}
617)();
Note: See TracBrowser for help on using the repository browser.