source: trunk/instant_messenger/js/client.js @ 323

Revision 323, 7.3 KB checked in by niltonneto, 16 years ago (diff)
  • Property svn:executable set to *
Line 
1(function()
2{
3
4        var _cookies = new IMCookies;
5        var _running = _cookies.get('IM_running');
6        if ( !_running )
7        {
8                _cookies.set('IM_running', 'true');
9
10                function _config(pObj, pEvent, pHandler)
11                {
12                        if ( typeof pObj == 'object' )
13                        {
14                                if ( pEvent.substring(0, 2) == 'on' )
15                                        pEvent = pEvent.substring(2, pEvent.length);
16
17                                if ( pObj.addEventListener )
18                                        pObj.addEventListener(pEvent, pHandler, false);
19                                else if ( pObj.attachEvent )
20                                        pObj.attachEvent('on' + pEvent, pHandler);
21                        }
22                }
23
24                _config(window, 'onbeforeunload',
25                        function()
26                        {
27                                var _running = _cookies.get('IM_running');
28                                _cookies.clear('IM_running');
29                        }
30                );
31
32                if ( !(top.document.getElementById('instant_messenger_content')) )
33                {
34                        var _im_content = top.document.createElement('div');
35                        _im_content.setAttribute('id', 'instant_messenger_content');
36                        _im_content = top.document.body.appendChild(_im_content);
37                }
38
39                var buffer = '';
40                var _disconnected = false;
41
42                var _conn = new IMConnector(path_im);
43                var _xtools = new XTools(path_im);
44                var _window = new Windows(_xtools, _im_content);
45                var _ldap = new InstantMessengerLDAP(_conn, _xtools, _window);
46                var _showhidden = new ShowHidden(1500);
47                var _jabber = new Jabber(_conn, _xtools, _window, _ldap, _showhidden, _cookies);
48
49                // XSL preload
50                var _xsl_preload = [
51                        'window.xsl',
52                        'layer.xsl',
53                        'contacts.xsl',
54                        'chat.xsl',
55                        'options.xsl'
56                ];
57                var _xsl_preload_legth = _xsl_preload.length;
58                for ( var i = 0; i < _xsl_preload_legth; i++ )
59                        _xtools.load(_xsl_preload[i]);
60
61                function _stream(data)
62                {
63                        data = data.replace(/^ | $/, '');
64
65                        if ( !(data.length) )
66                                return false;
67
68                        if ( data.indexOf('disconnected') != -1 || data.indexOf('</stream:stream>') != -1 )
69                                _disconnect();
70                        else
71                        {
72                                if ( data.lastIndexOf('>') != data.length - 1)
73                                        buffer += data + ' ';
74                                else
75                                {
76                                        if ( buffer.length )
77                                        {
78                                                data = buffer + data;
79                                                buffer = '';
80                                        }
81
82                                        // with xmlns, occurs problems during parse between
83                                        // xml and xsl, and for that reason it was removed
84                                        data = data.replace(/ xmlns='.*?'/g, '');
85                                        data = '<handler>' + data + '</handler>';
86
87                                        var _xml = _xtools.convert(data);
88
89                                        if ( _xml )
90                                        {
91                                                var _node = _xml.documentElement.firstChild;
92
93                                                while ( _node )
94                                                {
95                                                        if ( _node.nodeType == 1 )
96                                                                _jabber.action(_node);
97                                                        //else
98                                                        //      alert('name : ' + _node.nodeName +
99                                                        //                      "\n\nhas children:" + _node.hasChildNodes() +
100                                                        //                      "\nvalue\n\n" + _node.nodeValue);
101
102                                                        _node = _node.nextSibling;
103                                                }
104                                        }
105                                }
106                        }
107                }
108
109                var _count_reconnect = 0;
110                function _request()
111                {
112                        if ( !_disconnected )
113                                return _conn.go(
114                                        '$this.Ujabber.listen',
115                                        {'stream':_stream, 'request':_wait},
116                                        'classCostructor=read'
117                                );
118                        else
119                                setTimeout(_reconnect, (++_count_reconnect * 2000));
120                }
121
122                function _wait()
123                {
124                        setTimeout(_request, 3000);
125                }
126
127                function _reconnect()
128                {
129                        _disconnected = false;
130                        _request();
131                }
132
133                function _disconnect()
134                {
135                        _disconnected = true;
136                        _conn.abort();
137                        _loading();
138                        var _status = top.document.getElementById('im_status');
139                        if ( _status )
140                                _status.style.backgroundImage = 'url(' + im_unavailable.src + ')';
141
142                        var _win_contacts = _window.get('contact_list');
143                        if ( _win_contacts )
144                                _win_contacts.icon(im_unavailable.src);
145                }
146
147                function _contacts()
148                {
149                        if ( !_disconnected )
150                        {
151                                _conn.go('$this.Ujabber.getContacts');
152                                setTimeout(function()
153                                {
154                                        var _contact_list = top.document.getElementById('im_contact_list');
155                                        if ( !_contact_list )
156                                                _contacts();
157                                }, 5000);
158                        }
159                }
160
161                function _loading()
162                {
163                        var _win_contact = _window.get('contact_list');
164                        if ( !_win_contact )
165                        {
166                                _win_contact = _window.load('contact_list');
167                                _win_contact.hidden();
168                                _win_contact.title('.::Expresso Messenger::.');
169                                _win_contact.size(170, 350);
170                                _win_contact.position(30, 30, true);
171                                _win_contact.icon(im_unavailable.src);
172                                _win_contact.bc.style.display = 'none';
173                        }
174
175                        _win_contact.loading();
176                }
177
178                function _showContacts()
179                {
180                        var _win = _window.get('contact_list');
181                        if ( _win )
182                                if ( _win.wm.style.display == 'none' )
183                                        _win.wm.style.display = 'block';
184                                else
185                                        _win.wm.style.display = 'none';
186                }
187
188                _loading();
189
190                var local;
191                if ( (local = top.document.getElementById('user_info')) )
192                {
193                        local.firstChild.style.marginLeft = '30px';
194
195                        var _status = top.document.createElement('div');
196                                _status.setAttribute('id', 'im_status');
197                                _status.style.height = '15px';
198                                _status.style.margin = '0 0 0 10px';
199                                _status.style.padding = '0px';
200                                _status.style.width = '15px';
201                                _status.style.background = 'no-repeat';
202                                _status.style.backgroundImage = 'url(' + im_unavailable.src + ')';
203                                _status.style.float = 'left';
204                                _status.style.position = 'absolute';
205
206                        local.insertBefore(_status, local.firstChild);
207
208                        _config(_status, 'onclick', _showContacts);
209
210                        var _menu;
211                                _menu = top.document.createElement('span');
212                                _menu.setAttribute('id','fast_menu_im');
213                                _menu.style.backgroundColor = 'b4cfe5';
214                                _menu.style.display = 'none';
215                                _menu.style.margin = '15px 0 0 -15px';
216                                _menu.style.position = 'absolute';
217                                _menu.style.zIndex = '99999';
218                        local.insertBefore(_menu, local.firstChild);
219
220                        var _menu_img = top.document.createElement('div');
221                                _menu_img.style.background = 'no-repeat';
222                                _menu_img.style.backgroundImage = 'url(' + im_fast_menu.src + ')';
223                                _menu_img.style.height = '10px';
224                                _menu_img.style.margin = '0px';
225                                _menu_img.style.padding = '0px';
226                                _menu_img.style.position = 'absolute';
227                                _menu_img.style.width = '10px';
228                                _menu_img.style.float = 'left';
229                        local.insertBefore(_menu_img, local.firstChild);
230
231                        _config(
232                                _menu_img,
233                                'onclick',
234                                function()
235                                {
236                                        var _options = [
237                                                'available',
238                                                'away',
239                                                'dnd',
240                                                'unavailable'
241                                        ];
242
243                                        var _style_text = 'cursor: pointer; padding:2px 4px 2px 20px; background: no-repeat ';
244
245                                        var _xml = _xtools.xml('menu');
246                                        var _option;
247                                        var _item;
248                                        var _action;
249                                        var _style;
250
251                                        for ( var i in _options )
252                                        {
253                                                _option = _xml.createElement('option');
254
255                                                _item = _xml.createElement('item');
256                                                _item.appendChild(_xml.createTextNode(imGetLang(_options[i])));
257
258                                                _style = _xml.createElement('style');
259                                                _style.appendChild(_xml.createTextNode(_style_text + 'url(' + eval('im_' + _options[i] + '.src') + ')'));
260
261                                                _option.appendChild(_item);
262                                                _option.appendChild(_style);
263
264                                                _xml.documentElement.appendChild(_option);
265                                        }
266
267                                        _menu.innerHTML = _xtools.parse(_xml, 'options.xsl');
268                                        _showhidden.action('onmouseover', 'onmouseout', _menu);
269
270                                        _item = _menu.firstChild;
271
272                                        for ( var i in _options )
273                                        {
274                                                _item.childNodes[i].setAttribute('presence', _options[i]);
275                                                _config(
276                                                        _item.childNodes[i],
277                                                        'onclick',
278                                                        function(e)
279                                                        {
280                                                                var el = ( e.target ) ? e.target : e.srcElement;
281                                                                var _presence = el.getAttribute('presence');
282                                                                _jabber.setPresence(_presence);
283                                                        }
284                                                );
285                                        }
286                                }
287                        );
288                }
289
290                function _check_connection()
291                {
292                        if ( !_disconnected )
293                                _conn.go('t.checkConnection');
294                }
295
296                function Client()
297                {
298                        _request();
299                        setTimeout(_contacts, 3000);
300                        window.setInterval(_check_connection, 20000);
301                }
302
303                window.InstantMessengerClient = Client;
304        }
305}
306)();
Note: See TracBrowser for help on using the repository browser.