source: sandbox/jabberit_messenger/trophy_expresso/js/loadIM.js @ 2599

Revision 2599, 18.7 KB checked in by alexandrecorreia, 14 years ago (diff)

Ticket #986 - Notificacao de nova mensagem e posicionamento das janelas.

  • Property svn:executable set to *
Line 
1(function()
2{
3        var fullName            = "";
4        var path                        = "";
5        var addUser                     = null;
6        var conn                        = null;
7        var selectEditable      = null;
8        var showhidden          = null;
9        var timeoutId           = null
10        var userCurrent         = null;
11        var Xtools                      = null;
12        var zIndex                      = 9001;
13       
14        // Images
15        var add_user = new Image();
16        add_user.src = path_jabberit + 'templates/default/images/adduser_azul.png';
17
18        var arrow_down = new Image();
19        arrow_down.src = path_jabberit + 'templates/default/images/arrow_down.gif';
20
21        var arrow_right = new Image();
22        arrow_right.src = path_jabberit + 'templates/default/images/arrow_right.gif';
23       
24        function actionButton()
25        {
26                if( arguments.length > 0 )
27                {
28                        var e           = arguments[0];
29                        var jid         = arguments[1];
30                        var index       = arguments[2];
31                        var coord       = null;
32               
33                        if ( !e )
34                                var e = window.event;
35
36                        var _X = e.clientX + document.body.scrollLeft - document.body.clientLeft;
37                        var _Y = e.clientY + document.body.scrollTop  - document.body.clientTop;
38                               
39                        coord = { X : _X, Y : _Y };
40                       
41                        window.document.oncontextmenu = function()
42                        {
43                                return false;
44                        };
45       
46                        if ( ( e.which && e.which > 1 ) || ( e.button && e.button > 1 ) )
47                        {
48                                optionsItensContact( jid, index, coord );
49                        }
50                        else
51                        {
52                                if ( e.target.id )
53                                        TrophyIM.rosterClick(jid);
54                        }
55                }
56        }
57
58        function addContact()
59        {
60                if( arguments.length > 0 )
61                        addUser.add();
62                else
63                        addUser.show();
64        }
65       
66        function addIcon()
67        {
68                var StatusBar = getElement('divStatusBar');
69
70                /**
71                 * @quando estiver habilitada a opção fora de escritório nos filtros.
72                 */
73               
74                if( div_write_msg = getElement('em_div_write_msg') )
75                {               
76                        var StatusBarIM = getElement('JabberMessenger');
77                                StatusBarIM.style.paddingLeft = '33px';
78                       
79                        div_write_msg.parentNode.insertBefore( StatusBarIM, div_write_msg );
80                       
81                        return;                 
82                }
83               
84                if( StatusBar )
85                {
86                        StatusBar.style.paddingLeft = '33px';
87                       
88                        var _div = document.createElement('div');
89                                _div.appendChild(StatusBar.parentNode.removeChild(StatusBar.previousSibling));
90                       
91                                StatusBar.parentNode.insertBefore( _div, StatusBar);
92                       
93                        var divJabber = document.createElement('div');
94                                divJabber.setAttribute('id', 'JabberMessenger');
95                       
96                        var _fastMenu = top.document.createElement('div');
97                                _fastMenu.setAttribute('id', 'fast_menu_jabber_expresso');
98                                _fastMenu.style.background              = 'no-repeat';
99                                _fastMenu.style.backgroundImage = 'url(' + arrow_down.src + ')';
100                                _fastMenu.style.float                   = 'left';
101                                _fastMenu.style.height                  = '15px';
102                                _fastMenu.style.left                    = '7px';
103                                _fastMenu.style.margin                  = '8 0 0 10px';
104                                _fastMenu.style.padding                 = '0px';
105                                _fastMenu.style.position                = 'absolute';
106                                _fastMenu.style.width                   = '15px';
107                                _fastMenu.style.cursor                  = 'pointer';
108
109                        divJabber.insertBefore( _fastMenu, divJabber.firstChild );
110                       
111                        // Add event onclick element _fastMenu
112                        configEvents( _fastMenu, 'onclick', function(){ fastMenu(_fastMenu); });
113
114                        var _statusJabber = top.document.createElement('div');
115                                _statusJabber.setAttribute('id','status_jabber_expresso');
116                                _statusJabber.style.background          = 'no-repeat';
117                                _statusJabber.style.backgroundImage = 'url(' + add_user.src +')';
118                                _statusJabber.style.float                       = 'left';
119                                _statusJabber.style.height                      = '18px';
120                                _statusJabber.style.left                        = '19px';
121                                _statusJabber.style.margin                      = '0 0 0 10px';
122                                _statusJabber.style.padding                     = '0px';
123                                _statusJabber.style.position            = 'absolute';
124                                _statusJabber.style.width                       = '18px';
125                                _statusJabber.style.cursor                      = 'pointer';
126                                _statusJabber.style.zindex                      = '999999';
127                       
128                        divJabber.insertBefore( _statusJabber, divJabber.firstChild );
129                       
130                        StatusBar.insertBefore( divJabber, StatusBar.firstChild );
131
132                        // Add event onclick element _statusJabber
133                        configEvents( _statusJabber, 'onclick', function(){ TrophyIM.load();});
134                }
135        }
136
137        function configEvents(pObj, pEvent, pHandler)
138        {
139                if ( typeof pObj == 'object' )
140                {
141                        if ( pEvent.substring(0, 2) == 'on' )
142                                pEvent = pEvent.substring(2, pEvent.length);
143
144                        if ( pObj.addEventListener )
145                                pObj.addEventListener(pEvent, pHandler, false);
146                        else if ( pObj.attachEvent )
147                                pObj.attachEvent('on' + pEvent, pHandler);
148                }
149        }
150       
151        function fastMenu()
152        {
153                if( arguments.length > 0 )
154                {
155                        var element = arguments[0];
156                       
157                        if( showhidden == null )
158                                showhidden = new ShowHidden(300);
159
160                        var _options = [
161                                                ['Adicionar Contato', 'loadIM.addContact();' ],
162                                                ['Ajuda', 'alert("2")' ],
163                                                ['Preferências', 'alert("3")'],
164                                                   ];
165
166                        var _itens = "";
167                       
168                        for( var i in _options )
169                        {
170                                _itens += '<img src="'+arrow_right.src+'"/>';
171                                _itens += '<span style="cursor:pointer; margin:3px;" onclick='+_options[i][1]+'>';
172                                _itens += _options[i][0] + '</span><br/>';
173                        }
174                       
175                        var _optionsItens = document.createElement("div");
176                                _optionsItens.setAttribute("style", "margin: 20px 0px 0px -4px;");
177                                _optionsItens.className         = "x-menu";
178                                _optionsItens.style.zIndex      = '999999';
179                                _optionsItens.innerHTML         = _itens; 
180                                                                                 
181                                showhidden.action('onmouseover', 'onmouseout', _optionsItens);
182                               
183                        element.parentNode.onmouseout   = function(){ showhidden.hiddenObject(false); };
184                        element.parentNode.onmouseover  = function(){ showhidden.hiddenObject(true); };
185                        element.parentNode.appendChild( _optionsItens );
186                       
187                }
188        }
189       
190        function getElement( elementId )
191        {
192                return document.getElementById( elementId );
193        }
194       
195        function getPhotoUser( jid )
196        {
197                var _divPhoto = getElement( jid + '__photo' );
198
199                if( _divPhoto.style.backgroundImage.indexOf('photo.png') > 0 )
200                {
201                        var _imgUser  = path_jabberit + 'inc/WebService.php?' + Date.parse( new Date );
202                                _imgUser += '&photo_ldap=' + jid;
203
204                        _divPhoto.style.backgroundImage = 'url(' + _imgUser + ')';
205                }
206        }
207       
208        function getUserCurrent()
209        {
210                return userCurrent;
211        }
212       
213        function getZindex()
214        {
215                return zIndex++;
216        }
217       
218        function groupsHidden()
219        {
220                if( arguments.length > 0 )
221                {
222                        var _element = arguments[0];
223                                _element.style.background = "url('"+path_jabberit+"templates/default/images/arrow_right.gif')no-repeat center left";
224                                _element.onclick = function(){ groupsVisible(_element);};
225                               
226                                // Hidden all
227                                var _elementNext = _element.nextSibling;
228                               
229                                while( _elementNext )
230                                {       
231                                        _elementNext.style.display = "none";
232                                        _elementNext = _elementNext.nextSibling;
233                                }
234                }
235        }
236       
237        function groupsVisible()
238        {
239                if( arguments.length > 0 )
240                {
241                        var _element = arguments[0];
242                                _element.style.background = "url('"+path_jabberit+"templates/default/images/arrow_down.gif')no-repeat center left";
243                                _element.onclick = function(){ groupsHidden(_element);};
244
245                                // Display all
246                                var _elementNext = _element.nextSibling;
247                               
248                                while( _elementNext )
249                                {       
250                                        _elementNext.style.display = "block";
251                                        _elementNext = _elementNext.nextSibling;
252                                }
253                }
254        }
255       
256        function keyPressSearch()
257        {
258                if( arguments.length > 0 )
259                {
260                        var ev          = arguments[0];
261                        var element     = arguments[1];
262       
263                        if ( ev.keyCode == 13 )
264                                if( element.value.length >= 3 )
265                                        searchUser( element.value );   
266                                else
267                                        alert('Your search argument must be longer than 3 characters.');
268                }
269        }
270
271       
272        function loginPage()
273        {
274                var winLoginPage =
275                {       
276                         id_window              : "window_login_page",
277                         width                  : 260,
278                         height                 : 150,
279                         top                    : 100,
280                         left                   : 400,
281                         draggable              : true,
282                         visible                : "display",
283                         resizable              : true,
284                         zindex                 : zIndex++,
285                         title                  : "Login - Expresso Messenger",
286                         closeAction    : "remove",
287                         content                : Xtools.parse(Xtools.xml("login_page"), "loginPage.xsl")       
288                };
289
290                _winBuild( winLoginPage );
291        }
292
293        function loadScripts(pFiles)
294        {
295                // Load JavaScript
296                var loadJavaScript = function(pJs)
297                {
298                        var newScript = document.createElement("script");
299                                newScript.setAttribute("type", "text/javascript");
300                                newScript.setAttribute("src", pJs );
301                               
302                        return newScript;
303                };
304               
305                // Load CSS
306                var loadStyleSheet = function(pCss)
307                {
308                        var newStyle = document.createElement("link");
309                                newStyle.setAttribute("rel", "stylesheet");
310                                newStyle.setAttribute("type", "text/css");
311                                newStyle.setAttribute("href", pCss);
312                               
313                        return newStyle;
314                };
315               
316                for(var i = 0; i < pFiles.length; i++)
317                {
318                        if( pFiles[i].indexOf(".js") > -1 )
319                                document.getElementsByTagName("head")[0].appendChild(loadJavaScript(pFiles[i]));
320                               
321                        if( pFiles[i].indexOf(".css") > -1 )
322                                document.getElementsByTagName("head")[0].appendChild(loadStyleSheet(pFiles[i]));
323                }
324        }
325       
326        function notificationNewMessage()
327        {
328                var oldTitle    = document.title;
329                var newTitle    = " ## NOVA MENSAGEM ## ";
330                var idWindow    = arguments[1];
331               
332                if( timeoutId == null)
333                {
334                        timeoutId = setInterval(function()
335                        {
336                                document.title = document.title == newTitle ? ' ' : newTitle;
337                               
338                        }, 1000);
339                       
340                        configEvents( window, 'onclick', function()
341                        {
342                                clearInterval(timeoutId);
343                                document.title  = oldTitle;
344                                timeoutId               = null;
345                        });
346                }
347        }
348
349        function optionsItensContact()
350        {
351                if( arguments.length > 0 )
352                {
353                        var jid         = arguments[0];
354                        var index       = arguments[1];
355                        var coord       = arguments[2];
356                        var element = getElement('itenContact_' + jid + '_' + index );
357
358                       
359                        if( showhidden == null )
360                                showhidden = new ShowHidden(300);
361
362                        var _options = [
363                                                ['Autorizar', 'loadIM.setAutorization(\''+jid+'\',\''+index+'\')'],
364                                                ['Remover', 'loadIM.removeContact(\''+jid+'\',\''+index+'\')'],
365                                                ['Renomear', 'loadIM.renameContact(\''+jid+'\',\''+index+'\')'],
366                                                ['Trocar grupo', 'loadIM.renameGroup(\''+jid+'\',\''+index+'\')'],
367                                                   ];
368
369                        var _itens = "";
370                       
371                        for( var i in _options )
372                        {
373                                _itens += '<img src="'+arrow_right.src+'"/>';
374                                _itens += '<span style="cursor:pointer;margin:3px;font-weight:normal;" onclick='+_options[i][1]+'>';
375                                _itens += _options[i][0] + '</span><br/>';
376                        }
377                       
378                        var _optionsItens = document.createElement("div");
379                                _optionsItens.className         = "x-menu";
380                                _optionsItens.style.top         = coord.Y;
381                                _optionsItens.style.left        = ( coord.X - element.offsetLeft );
382                                _optionsItens.style.zIndex      = getZindex();
383                                _optionsItens.innerHTML         = _itens; 
384                                _optionsItens.onclick           = function(){ showhidden.hiddenObject(false); };
385                                _optionsItens.onmouseout        = function(){ showhidden.hiddenObject(false); };       
386                                _optionsItens.onmouseover       = function(){ showhidden.hiddenObject(true); };
387                               
388                                showhidden.action('onmouseover', 'onmouseout', _optionsItens);
389                               
390                        window.document.body.appendChild(_optionsItens);
391                       
392                       
393                        setTimeout(function()
394                        {
395                                window.document.oncontextmenu = function()
396                                {
397                                        return true;
398                                };
399                               
400                        },500);
401                }
402        }
403
404        function parse()
405        {
406                if( arguments.length == 2 )
407                        return Xtools.parse(Xtools.xml(arguments[0]), arguments[1] );
408               
409                if( arguments.length === 3 )
410                        return Xtools.parse(Xtools.xml(arguments[0]), arguments[1], arguments[2] );
411        }
412       
413        function removeContact(jid, index)
414        {
415                setTimeout(function()
416                {
417                        TrophyIM.removeContact(jid, index);
418                       
419                },300);
420        }
421       
422        function removeElement( )
423        {
424                if( arguments.length > 0 )
425                {
426                        var _element = arguments[0]
427                        var _parent  = _element.parentNode;
428       
429                        _parent.removeChild( _element );
430                }
431        }
432       
433        function removeGroup()
434        {
435                var _parent = arguments[0];
436               
437                if( _parent.childNodes.length <= 2 )
438                        _parent.parentNode.removeChild(_parent);
439        }
440       
441        function renameContact()
442        {
443                if( arguments.length > 0 )
444                {
445                        var _jid        = arguments[0];
446                        var _index      = arguments[1];
447                       
448                        TrophyIM.renameContact( _jid, _index );
449                }
450        }
451       
452        function renameGroup()
453        {
454                if( arguments.length > 0 )
455                {
456                        var _jid        = arguments[0];
457                        var _index      = arguments[1];
458                       
459                        TrophyIM.renameGroup( _jid , _index );
460                }
461        }
462       
463        function rosterDiv()
464        {
465                var paramListContact =
466                {
467                        'idUser'                : getUserCurrent().jid,
468                        'full_name'             : (( fullName.length < 25 ) ? fullName : ( fullName.substring( 0, 25) + "...")),
469                        'path_jabberit' : path_jabberit,
470                        'zIndex_'               : zIndex++
471                };
472               
473                var winRosterDiv =
474                {
475                         id_window              : "window_Roster_im",
476                         width                  : 250,
477                         height                 : 410,
478                         top                    : 50,
479                         left                   : 50,
480                         draggable              : true,
481                         visible                : "display",
482                         resizable              : true,
483                         zindex                 : zIndex++,
484                         title                  : "Contatos - Expresso Messenger",
485                         closeAction    : "hidden",
486                         content                : Xtools.parse(Xtools.xml("contacts_list"),"contactsList.xsl", paramListContact)       
487                };
488               
489                _winBuild( winRosterDiv );
490
491                // Photo User
492                getPhotoUser(getUserCurrent().jid);
493        }
494
495        function searchUser()
496        {
497                var _input      = getElement('search_user_jabber');
498               
499                if( _input.value.length >= 3 )
500                        addUser.search();
501                else
502                        alert('Your search argument must be longer than 3 characters.');
503        }
504       
505        function setAutorization(jidTo, indexTo)
506        {
507        var divItenContact = null;
508       
509        if( ( divItenContact = getElement('itenContact_' + jidTo + '_' + indexTo )))
510        {       
511                var subscription = divItenContact.getAttribute('subscription');
512               
513                switch(subscription)
514                {
515                        case 'from':
516                               
517                                TrophyIM.setAutorization( jidTo, this.getUserCurrent().jid, 'subscribe');
518                                break;
519
520                        case 'none' :
521                       
522                                TrophyIM.setAutorization( jidTo, this.getUserCurrent().jid, 'subscribe');
523                                TrophyIM.setAutorization( jidTo, this.getUserCurrent().jid, 'subscribed');
524                               
525                                break;
526
527                        case 'not-in-roster':
528                                       
529                                setTimeout(function()
530                                        {
531                                        var _add = "";
532                               
533                                        if( ( _add = confirm("Deseja adicionar o usuario!")) )         
534                                        {
535                                                addUser.add( jidTo, indexTo );
536                                        }
537                                       
538                                        },100);
539                               
540                                        break;
541                                       
542                        case 'to' :
543                               
544                                TrophyIM.setAutorization( jidTo, this.getUserCurrent().jid, 'subscribed');
545                                       
546                        case 'subscribe' :
547                               
548                                TrophyIM.setAutorization( jidTo, this.getUserCurrent().jid, 'subscribe');
549                               
550                                        break;
551                }
552        }       
553        }
554       
555        function setPresence()
556        {
557                if( arguments.length > 0 )
558                {
559                        var element = arguments[0];
560                       
561                        if( showhidden == null )
562                                showhidden = new ShowHidden(300);
563                       
564                        var _status = [
565                                               ['Afastado', 'away', '<img src="'+path_jabberit+'templates/default/images/away.gif" />'],
566                                               ['Disponível', 'available', '<img src="'+path_jabberit+'templates/default/images/available.gif" />'],
567                                               ['Livre p/ Conversa', 'chat', '<img src="'+path_jabberit+'templates/default/images/chat.gif" />'],
568                                               ['Não Disponível', 'xa', '<img src="'+path_jabberit+'templates/default/images/xa.gif" />'],
569                                               ['Ocupado', 'dnd', '<img src="'+path_jabberit+'templates/default/images/dnd.gif" />'],
570                                               ['Mensagem de Status...', 'status', '<img src="'+path_jabberit+'templates/default/images/message_normal.gif" />']                                               
571                                          ];
572                       
573                        var _itens = "";
574                       
575                        for( var i in _status )
576                        {
577                                _itens += '<span style="cursor:pointer;" onclick="TrophyIM.setPresence(\''+_status[i][1]+'\'); loadIM.setStatusJabber(\''+_status[i][0]+'\',\''+_status[i][1]+'\');">';
578                                _itens += _status[i][2]+ "<span style='margin:3px;'>" + _status[i][0] + "</span></span><br/>";
579                        }
580                       
581                        var _statusItens = document.createElement("div");
582                                _statusItens.setAttribute("style", "margin: 62px 0px 0px 76px;");
583                                _statusItens.className          = "x-menu";
584                                _statusItens.style.zIndex       = '99999';
585                                _statusItens.innerHTML          = _itens; 
586                                _statusItens.onclick            = function(){ showhidden.hiddenObject(false); };
587                                                                                 
588                                showhidden.action('onmouseover', 'onmouseout', _statusItens);
589                               
590                        element.parentNode.onmouseout   = function(){ showhidden.hiddenObject(false); };
591                        element.parentNode.onmouseover  = function(){ showhidden.hiddenObject(true); };
592                        element.parentNode.appendChild(_statusItens);
593                }
594        }
595
596        function setSelectEditable(element)
597        {
598                if( getElement('selectBox0') == null )
599                        selectEditable.create(element);
600        }
601
602        function setStatusJabber()
603        {
604                if( arguments.length > 0 )
605                {
606                        if( arguments[1] != 'status' )
607                        {
608                                var _text       = arguments[0];
609                                var _img        = arguments[1];
610                               
611                                getElement('statusJabberImg').setAttribute("style", "background-image: url('"+path_jabberit+"templates/default/images/"+_img+".gif'); margin-left: 13px;width:15px; height:15px;");
612                                getElement('statusJabberText').innerHTML = _text;
613                                getElement('status_jabber_expresso').style.background = "url('"+path_jabberit+"templates/default/images/"+_img+".gif') no-repeat";
614                        }
615                }       
616        }
617       
618        function setUserCurrent( _jid , _password )
619        {
620                userCurrent =
621                {
622                        'jid'           : _jid,
623                        'password'      : _password
624                }
625        }
626
627        function loadIM()
628        {
629                if( arguments.length > 0 )
630                {
631                        var files = [
632                                                path_jabberit + 'js/connector.js',
633                                                path_jabberit + 'js/xtools.js',
634                                                path_jabberit + 'js/dragdrop.js',
635                                                path_jabberit + 'js/makeW.js',
636                                                path_jabberit + 'js/show_hidden.js',
637                                                path_jabberit + 'js/trophyim.js',
638                                                path_jabberit + 'js/AddUser.js',
639                                                path_jabberit + 'js/SelectEditable.js',
640                                                path_jabberit + 'templates/default/css/button.css',
641                                                path_jabberit + 'templates/default/css/common.css',
642                                                path_jabberit + 'templates/default/css/selectEditableStyle.css',
643                                                path_jabberit + 'templates/default/css/' + theme_jabberit
644                                    ];
645                        // FullName
646                        fullName = arguments[0];
647                       
648                        loadScripts(files);
649                       
650                        // User & Password Jabber;
651                        setUserCurrent( arguments[1], arguments[2]);
652                       
653                        setTimeout(function()
654                        {
655                                // Object Xtools       
656                                if( Xtools == null )
657                                        Xtools = new xtools(path_jabberit);
658                               
659                                // Object Conector
660                                if( conn == null )
661                                        conn = new AjaxConnector(path_jabberit);
662                               
663                                // Object Add User
664                                if( addUser == null )
665                                        addUser = new addUserIM(Xtools, conn);
666
667                               
668                                // Object SelectEditable
669                                if( selectEditable == null )
670                                        selectEditable = new SelectEditable();
671                                       
672                                // Add Jabber in StatusBar;
673                                addIcon();
674                               
675                        }, 1000);
676                }
677        }
678       
679        loadIM.prototype.addIcon                        = addIcon;
680        loadIM.prototype.actionButton           = actionButton;
681        loadIM.prototype.addContact                     = addContact;
682        loadIM.prototype.configEvents           = configEvents;
683        loadIM.prototype.getPhotoUser           = getPhotoUser;
684        loadIM.prototype.getUserCurrent         = getUserCurrent;
685        loadIM.prototype.getZIndex                      = getZindex;
686        loadIM.prototype.groupsHidden           = groupsHidden;
687        loadIM.prototype.groupsVisible          = groupsVisible;
688        loadIM.prototype.keyPressSearch         = keyPressSearch;       
689        loadIM.prototype.loginPage                      = loginPage;
690        loadIM.prototype.notification           = notificationNewMessage;
691        loadIM.prototype.parse                          = parse;
692        loadIM.prototype.searchUser                     = searchUser
693        loadIM.prototype.setAutorization        = setAutorization;
694        loadIM.prototype.setPresence            = setPresence;
695        loadIM.prototype.setStatusJabber        = setStatusJabber;
696        loadIM.prototype.setSelectEditable      = setSelectEditable;
697        loadIM.prototype.removeContact          = removeContact;
698        loadIM.prototype.removeElement          = removeElement;
699        loadIM.prototype.removeGroup            = removeGroup;
700        loadIM.prototype.renameContact          = renameContact;
701        loadIM.prototype.renameGroup            = renameGroup;
702        loadIM.prototype.rosterDiv                      = rosterDiv;
703       
704        window.loadIM = loadIM;
705       
706})();
Note: See TracBrowser for help on using the repository browser.