1 | (function() |
---|
2 | { |
---|
3 | function _config(pObj, pEvent, pHandler) |
---|
4 | { |
---|
5 | if ( typeof pObj == 'object' ) |
---|
6 | { |
---|
7 | if ( pEvent.substring(0, 2) == 'on' ) |
---|
8 | pEvent = pEvent.substring(2, pEvent.length); |
---|
9 | |
---|
10 | if ( pObj.addEventListener ) |
---|
11 | pObj.addEventListener(pEvent, pHandler, false); |
---|
12 | else if ( pObj.attachEvent ) |
---|
13 | pObj.attachEvent('on' + pEvent, pHandler); |
---|
14 | } |
---|
15 | } |
---|
16 | |
---|
17 | if ( !(top.document.getElementById('instant_messenger_content')) ) |
---|
18 | { |
---|
19 | var _im_content = top.document.createElement('div'); |
---|
20 | _im_content.setAttribute('id', 'instant_messenger_content'); |
---|
21 | _im_content = top.document.body.appendChild(_im_content); |
---|
22 | } |
---|
23 | |
---|
24 | var buffer = ''; |
---|
25 | var _disconnected = false; |
---|
26 | |
---|
27 | var _conn = new IMConnector(path_im); |
---|
28 | var _xtools = new XTools(path_im); |
---|
29 | var _window = new Windows(_xtools, _im_content); |
---|
30 | var _ldap = new InstantMessengerLDAP(_conn, _xtools, _window); |
---|
31 | var _showhidden = new ShowHidden(1500); |
---|
32 | var _jabber = new Jabber(_conn, _xtools, _window, _ldap, _showhidden); |
---|
33 | |
---|
34 | // XSL preload |
---|
35 | var _xsl_preload = ['window.xsl','layer.xsl','contacts.xsl', 'chat.xsl']; |
---|
36 | var _xsl_preload_legth = _xsl_preload.length; |
---|
37 | for ( var i = 0; i < _xsl_preload_legth; i++ ) |
---|
38 | _xtools.load(_xsl_preload[i]); |
---|
39 | |
---|
40 | var local; |
---|
41 | if ( (local = top.document.getElementById('user_info')) ) |
---|
42 | { |
---|
43 | local.firstChild.style.marginLeft = '30px'; |
---|
44 | |
---|
45 | var _status = top.document.createElement('div'); |
---|
46 | _status.setAttribute('id', 'im_status'); |
---|
47 | _status.style.height = '15px'; |
---|
48 | _status.style.margin = '0 0 0 10px'; |
---|
49 | _status.style.padding = '0px'; |
---|
50 | _status.style.width = '15px'; |
---|
51 | _status.style.background = 'no-repeat'; |
---|
52 | _status.style.backgroundImage = 'url(' + im_unavailable.src + ')'; |
---|
53 | _status.style.float = 'left'; |
---|
54 | _status.style.position = 'absolute'; |
---|
55 | |
---|
56 | local.insertBefore(_status, local.firstChild); |
---|
57 | |
---|
58 | _config(_status, 'onclick', _contacts); |
---|
59 | |
---|
60 | var _menu; |
---|
61 | _menu = top.document.createElement('span'); |
---|
62 | _menu.setAttribute('id','fast_menu_im'); |
---|
63 | _menu.style.backgroundColor = 'b4cfe5'; |
---|
64 | _menu.style.display = 'none'; |
---|
65 | _menu.style.margin = '15px 0 0 -15px'; |
---|
66 | _menu.style.position = 'absolute'; |
---|
67 | _menu.style.zIndex = '99999'; |
---|
68 | local.insertBefore(_menu, local.firstChild); |
---|
69 | |
---|
70 | var _menu_img = top.document.createElement('div'); |
---|
71 | _menu_img.style.background = 'no-repeat'; |
---|
72 | _menu_img.style.backgroundImage = 'url(' + im_fast_menu.src + ')'; |
---|
73 | _menu_img.style.height = '10px'; |
---|
74 | _menu_img.style.margin = '0px'; |
---|
75 | _menu_img.style.padding = '0px'; |
---|
76 | _menu_img.style.position = 'absolute'; |
---|
77 | _menu_img.style.width = '10px'; |
---|
78 | _menu_img.style.float = 'left'; |
---|
79 | local.insertBefore(_menu_img, local.firstChild); |
---|
80 | |
---|
81 | _config( |
---|
82 | _menu_img, |
---|
83 | 'onclick', |
---|
84 | function() |
---|
85 | { |
---|
86 | var _options = [ |
---|
87 | 'available', |
---|
88 | 'away', |
---|
89 | 'dnd', |
---|
90 | 'unavailable' |
---|
91 | ]; |
---|
92 | |
---|
93 | var _style_text = 'cursor: pointer; padding:2px 4px 2px 20px; background: no-repeat '; |
---|
94 | |
---|
95 | var _xml = _xtools.xml('menu'); |
---|
96 | var _option; |
---|
97 | var _item; |
---|
98 | var _action; |
---|
99 | var _style; |
---|
100 | |
---|
101 | for ( var i in _options ) |
---|
102 | { |
---|
103 | _option = _xml.createElement('option'); |
---|
104 | |
---|
105 | _item = _xml.createElement('item'); |
---|
106 | _item.appendChild(_xml.createTextNode(imGetLang(_options[i]))); |
---|
107 | |
---|
108 | _style = _xml.createElement('style'); |
---|
109 | _style.appendChild(_xml.createTextNode(_style_text + 'url(' + eval('im_' + _options[i] + '.src') + ')')); |
---|
110 | |
---|
111 | _option.appendChild(_item); |
---|
112 | _option.appendChild(_style); |
---|
113 | |
---|
114 | _xml.documentElement.appendChild(_option); |
---|
115 | } |
---|
116 | |
---|
117 | _menu.innerHTML = _xtools.parse(_xml, 'options.xsl'); |
---|
118 | _showhidden.action('onmouseover', 'onmouseout', _menu); |
---|
119 | |
---|
120 | _item = _menu.firstChild; |
---|
121 | |
---|
122 | for ( var i in _options ) |
---|
123 | { |
---|
124 | _item.childNodes[i].setAttribute('presence', _options[i]); |
---|
125 | _config( |
---|
126 | _item.childNodes[i], |
---|
127 | 'onclick', |
---|
128 | function(e) |
---|
129 | { |
---|
130 | var el = ( e.target ) ? e.target : e.srcElement; |
---|
131 | |
---|
132 | _status.style.backgroundImage = el.style.backgroundImage; |
---|
133 | |
---|
134 | var _presence = el.getAttribute('presence'); |
---|
135 | _jabber.setPresence(_presence); |
---|
136 | |
---|
137 | var _win_contacts = _window.get('contact_list'); |
---|
138 | if ( _win_contacts ) |
---|
139 | _win_contacts.icon(eval('im_' + _presence + '.src')); |
---|
140 | } |
---|
141 | ); |
---|
142 | } |
---|
143 | } |
---|
144 | ); |
---|
145 | } |
---|
146 | |
---|
147 | function _stream(data) |
---|
148 | { |
---|
149 | data = data.replace(/^ | $/, ''); |
---|
150 | |
---|
151 | if ( !(data.length) ) |
---|
152 | return false; |
---|
153 | |
---|
154 | if ( data.indexOf('disconnected') != -1 || data.indexOf('</stream:stream>') != -1 ) |
---|
155 | { |
---|
156 | _disconnected = true; |
---|
157 | //alert('disconnected'); |
---|
158 | } |
---|
159 | else |
---|
160 | { |
---|
161 | if ( data.lastIndexOf('>') != data.length - 1) |
---|
162 | buffer += data + ' '; |
---|
163 | else |
---|
164 | { |
---|
165 | if ( buffer.length ) |
---|
166 | { |
---|
167 | data = buffer + data; |
---|
168 | buffer = ''; |
---|
169 | } |
---|
170 | |
---|
171 | // com xmlns ocorre problemas durante o parse entre |
---|
172 | // xml e xsl e por esse motivo está sendo retirado |
---|
173 | data = data.replace(/ xmlns='.*?'/g, ''); |
---|
174 | data = '<handler>' + data + '</handler>'; |
---|
175 | |
---|
176 | var _xml = _xtools.convert(data); |
---|
177 | |
---|
178 | if ( _xml ) |
---|
179 | { |
---|
180 | var _node = _xml.documentElement.firstChild; |
---|
181 | |
---|
182 | while ( _node ) |
---|
183 | { |
---|
184 | if ( _node.nodeType == 1 ) |
---|
185 | _jabber.action(_node); |
---|
186 | //else |
---|
187 | // alert('name : ' + _node.nodeName + "\n\nhas children:" + _node.hasChildNodes() + "\nvalue\n\n" + _node.nodeValue); |
---|
188 | |
---|
189 | _node = _node.nextSibling; |
---|
190 | } |
---|
191 | } |
---|
192 | } |
---|
193 | } |
---|
194 | } |
---|
195 | |
---|
196 | var _count_reconnect = 0; |
---|
197 | function _request() |
---|
198 | { |
---|
199 | if ( !_disconnected ) |
---|
200 | return _conn.go( |
---|
201 | '$this.Ujabber.listen', |
---|
202 | {'stream':_stream, 'request':_wait}, |
---|
203 | 'classCostructor=read' |
---|
204 | ); |
---|
205 | else |
---|
206 | setTimeout(_reconnect, (++_count_reconnect * 2000)); |
---|
207 | } |
---|
208 | |
---|
209 | function _wait() |
---|
210 | { |
---|
211 | setTimeout(_request, 3000); |
---|
212 | } |
---|
213 | |
---|
214 | function _reconnect() |
---|
215 | { |
---|
216 | _disconnected = false; |
---|
217 | _request(); |
---|
218 | } |
---|
219 | |
---|
220 | function _disconnect() |
---|
221 | { |
---|
222 | _disconnected = true; |
---|
223 | } |
---|
224 | |
---|
225 | function _contacts() |
---|
226 | { |
---|
227 | var _win = _window.get('contact_list'); |
---|
228 | if ( _win ) |
---|
229 | if ( _win.wm.style.display != 'none' ) |
---|
230 | _win.wm.style.display = 'none'; |
---|
231 | else |
---|
232 | _win.wm.style.display = 'block'; |
---|
233 | else |
---|
234 | if ( !_disconnected ) |
---|
235 | _conn.go('$this.Ujabber.getContacts'); |
---|
236 | } |
---|
237 | |
---|
238 | function Client() |
---|
239 | { |
---|
240 | _request(); |
---|
241 | setTimeout('InstantMessengerContacts()', 3000); |
---|
242 | } |
---|
243 | |
---|
244 | window.InstantMessengerClient = Client; |
---|
245 | window.InstantMessengerReconnect = _reconnect; |
---|
246 | window.InstantMessengerDisconnect = _disconnect; |
---|
247 | window.InstantMessengerContacts = _contacts; |
---|
248 | } |
---|
249 | )(); |
---|