source: trunk/contactcenter/js/connector.js @ 2473

Revision 2473, 7.7 KB checked in by rodsouza, 14 years ago (diff)

Ticket #1009 - Permitindo que o ExpressoContacts? não realize reload de página.

  • Property svn:eol-style set to native
  • Property svn:executable set to *
Line 
1  /***************************************************************************\
2  * eGroupWare - Contacts Center                                              *
3  * http://www.egroupware.org                                                 *
4  * Written by:                                                               *
5  *  - Raphael Derosso Pereira <raphaelpereira@users.sourceforge.net>         *
6  *  sponsored by Thyamad - http://www.thyamad.com                            *
7  * ------------------------------------------------------------------------- *
8  *  This program is free software; you can redistribute it and/or modify it  *
9  *  under the terms of the GNU General Public License as published by the    *
10  *  Free Software Foundation; either version 2 of the License, or (at your   *
11  *  option) any later version.                                               *
12  \***************************************************************************/
13
14        function cConnector()
15        {
16                /* Public Attributes */
17                this.requests = new Array();
18                this.progressContents = new Array();
19                this.visible = false;
20
21                var _this = this;
22
23                /* Private Attributes */
24                this._progressBox = null;
25                this._progressHolder = document.createElement('span');
26                this._progressBlank   = null;
27
28                this._progressHolder.style.visibility = 'hidden';
29//              this._progressHolder.style.backgroundColor = '#db7e22';
30        }
31
32        cConnector.prototype.newRequest = function (id, target, method, handler, data)
33        {
34                var _this = this;
35               
36                if (this.requests[id] && this.requests[id] != null)
37                {
38                       
39                        //this.requests[id].abort();
40                        //delete this.requests[id];
41                        //this.requests[id] = null;
42                       
43                        //setTimeout(function() { _this.newRequest(id, target, method, handler, data); }, 100);
44
45                        return;
46                }
47
48                var oxmlhttp = null;
49               
50                try
51                {
52                        oxmlhttp = new XMLHttpRequest();
53                        oxmlhttp.overrideMimeType('text/plain');
54                }
55                catch (e)
56                {
57                        try
58                        {
59                                oxmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
60                        }
61                        catch (e1)
62                        {
63                                try
64                                {
65                                        oxmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
66                                }
67                                catch (e2)
68                                {
69                                        oxmlhttp = null;
70                                }
71                        }
72                }
73               
74                if (!oxmlhttp)
75                {
76                        return false;
77                }
78               
79                this.requests[id] = oxmlhttp;
80
81                var _this = this;
82               
83                var sub_handler = function ()
84                {
85                        try
86                        {
87                                _this._setProgressState(oxmlhttp.readyState);
88                               
89                                if (oxmlhttp.readyState == 4 )//&& oxmlhttp.channel.status == 0)
90                                {
91                                        switch (oxmlhttp.status)
92                                        {
93                                                case 200:
94                                                        if (typeof(handler) == 'function')
95                                                        {
96                                                                handler(oxmlhttp.responseText);
97                                                        }
98                                                        delete _this.requests[id];
99                                                        _this.requests[id] = null;
100                                                        break;
101
102                                                case 404:
103                                                        alert('Page Not Found!');
104                                                        break;
105
106                                                default:
107                                                        //alert('Some problem while accessing the server. The status is '+oxmlhttp.status);
108                                        }
109                                }
110                        }
111                        catch (e)
112                        {
113                                //showMessage(e);
114                        }
115                }
116
117                try
118                {
119                        target += '&BackgroundRequest=' + Date.parse( new Date );
120                        if (method == '' || method == 'GET')
121                        {
122                                if (typeof(handler) == 'function')
123                                {
124                                        oxmlhttp.onreadystatechange = sub_handler;
125                                }
126                                oxmlhttp.open("GET",target,true);
127                                oxmlhttp.send(null);
128                        }
129                        else if (method == 'POST')
130                        {
131                                if (typeof(handler) == 'function')
132                                {
133                                        oxmlhttp.onreadystatechange = sub_handler;
134                                }
135                                oxmlhttp.open("POST",target, true);
136                                oxmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
137                                oxmlhttp.send(data);
138                                //oxmlhttp.setRequestHeader('Content-Type','multipart/form-data; boundary=-----------------------------1156053686807595044986274307');
139                                //oxmlhttp.setRequestHeader('Accept', 'text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5');
140                        }
141                }
142                catch(e)
143                {
144                        //showMessage(e);
145                }
146               
147                return true;
148        }
149
150        cConnector.prototype.cancelRequest = function (id)
151        {
152                if (!this.requests[id])
153                {
154                        return false;
155                }
156
157                this.requests[id].abort();
158        }
159
160        cConnector.prototype.setProgressContent = function (state, content)
161        {
162                switch (state)
163                {
164                        case 0:
165                        case 'UNINITIALIZED':
166                                this.progressContents[0] = content;
167                                break;
168
169                        case 1:
170                        case 'LOADING':
171                                this.progressContents[1] = content;
172                                break;
173
174                        case 2:
175                        case 'LOADED':
176                                this.progressContents[2] = content;
177                                break;
178
179                        case 3:
180                        case 'INTERACTIVE':
181                                this.progressContents[3] = content;
182                                break;
183
184                        case 4:
185                        case 'COMPLETED':
186                                this.progressContents[4] = content;
187                                break;
188
189                        default:
190                                throw('INVALID STATE!');
191                }
192        }
193
194        cConnector.prototype.setProgressHolder = function (holder)
195        {
196                var objHolder;
197               
198                if (typeof(holder) == 'string')
199                {
200                        objHolder = Element(holder);
201                }
202                else if (typeof(holder) == 'object')
203                {
204                        objHolder = holder;
205                }
206                else
207                {
208                        return false;
209                }
210
211                objHolder.appendChild(this._progressHolder);
212        }
213
214        cConnector.prototype.setProgressBox = function (box, auto)
215        {
216                var objBox;
217
218                if (typeof(box) == 'string')
219                {
220                        objBox = Element(box);
221                }
222                else if (typeof(box) == 'object')
223                {
224                        objBox = box;
225                }
226                else
227                {
228                        return false;
229                }
230
231                this._progressBox = objBox;
232                this._progressBoxAuto = auto ? true : false;
233        }
234
235        cConnector.prototype.setVisible = function (visible)
236        {
237                this.visible = visible;
238                if (!visible)
239                {
240                        this._progressHolder.style.visibility = 'hidden';
241                }
242        }
243
244        /****************************************************************************\
245         *                          Private Methods                                 *
246        \****************************************************************************/
247       
248        cConnector.prototype._setProgressState = function (state)
249        {
250                switch (state)
251                {
252                        case 0:
253                        case 4:
254                                if (this._progressBox != null)
255                                {
256                                        this._progressBox.style.visibility = 'hidden';
257                                        this._progressBox.style.zIndex = '-1';
258                               
259                                        if (is_ie && this._progressBlank)
260                                                this._progressBlank.style.visibility = 'hidden';
261                                }
262                               
263                                this._progressHolder.style.visibility = 'hidden';
264                                this._progressHolder.style.zIndex = '-1';
265                               
266                                if (is_ie && this._progressBlank)
267                                        this._progressBlank.style.visibility = 'hidden';
268                               
269                                break;
270
271                        default:
272                                if (this.visible && Element('cc_connector_visible').value == 'true')
273                                {
274                                        if (this._progressBox != null)
275                                        {
276                                                if (this._progressBoxAuto)
277                                                {
278                                                        if (is_ie)
279                                                        {
280                                                                this._progressBox.style.top = parseInt(document.body.offsetHeight)/2 + 'px';
281                                                                this._progressBox.style.left = parseInt(document.body.offsetWidth)/2 + 'px';
282                                                                this._progressBlank = document.getElementById('divBlank');                                                             
283                                                                if(! this._progressBlank ) {
284                                                                        document.body.insertAdjacentHTML("beforeEnd", '<iframe id="divBlank" src="about:blank" style="position:absolute" scrolling="no" frameborder="0"></iframe>');
285                                                                        this._progressBlank = document.getElementById('divBlank');
286                                                                        this._progressBlank.style.top = parseInt(document.body.offsetHeight)/2 + 'px';
287                                                                        this._progressBlank.style.left = parseInt(document.body.offsetWidth)/2 + 'px';
288                                                                        this._progressBlank.style.height = "36px";
289                                                                        this._progressBlank.style.width = "130px";
290                                                                }
291                                                        }
292                                                        else
293                                                        {
294                                                                this._progressBox.style.top = parseInt(window.innerHeight)/2 + parseInt(window.pageYOffset) - this._progressBox.style.height/2 + 'px';
295                                                                this._progressBox.style.left = parseInt(window.innerWidth)/2 + parseInt(window.pageXOffset) - this._progressBox.style.width/2 + 'px';
296                                                        }
297                                                }
298                                                this._progressBox.style.visibility = 'visible';
299                                                this._progressBox.style.zIndex = '1000';
300                                        }
301                                       
302                                        this._progressHolder.style.visibility = 'visible';
303                                        this._progressHolder.style.zIndex = '1000';
304                                       
305                                        this._progressHolder.innerHTML = this.progressContents[state] ? this.progressContents[state] : '';
306
307                                        if(is_ie) {
308                                                this._progressBlank.style.visibility = 'visible';
309                                                this._progressBlank .style.zIndex = '999';
310                                        }                                       
311                                       
312                                }
313                }
314        }
Note: See TracBrowser for help on using the repository browser.