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

Revision 311, 17.9 KB checked in by niltonneto, 16 years ago (diff)

Correções críticas para funcionalidades já existentes.

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