]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/javascript/tiny_mce/classes/util/XHR.js
Release 6.5.0
[Github/sugarcrm.git] / include / javascript / tiny_mce / classes / util / XHR.js
1 /**
2  * XHR.js
3  *
4  * Copyright 2009, Moxiecode Systems AB
5  * Released under LGPL License.
6  *
7  * License: http://tinymce.moxiecode.com/license
8  * Contributing: http://tinymce.moxiecode.com/contributing
9  */
10
11 /**
12  * This class enables you to send XMLHTTPRequests cross browser.
13  * @class tinymce.util.XHR
14  * @static
15  * @example
16  * // Sends a low level Ajax request
17  * tinymce.util.XHR.send({
18  *    url : 'someurl',
19  *    success : function(text) {
20  *       console.debug(text);
21  *    }
22  * });
23  */
24 tinymce.create('static tinymce.util.XHR', {
25         /**
26          * Sends a XMLHTTPRequest.
27          * Consult the Wiki for details on what settings this method takes.
28          *
29          * @method send
30          * @param {Object} o Object will target URL, callbacks and other info needed to make the request.
31          */
32         send : function(o) {
33                 var x, t, w = window, c = 0;
34
35                 // Default settings
36                 o.scope = o.scope || this;
37                 o.success_scope = o.success_scope || o.scope;
38                 o.error_scope = o.error_scope || o.scope;
39                 o.async = o.async === false ? false : true;
40                 o.data = o.data || '';
41
42                 function get(s) {
43                         x = 0;
44
45                         try {
46                                 x = new ActiveXObject(s);
47                         } catch (ex) {
48                         }
49
50                         return x;
51                 };
52
53                 x = w.XMLHttpRequest ? new XMLHttpRequest() : get('Microsoft.XMLHTTP') || get('Msxml2.XMLHTTP');
54
55                 if (x) {
56                         if (x.overrideMimeType)
57                                 x.overrideMimeType(o.content_type);
58
59                         x.open(o.type || (o.data ? 'POST' : 'GET'), o.url, o.async);
60
61                         if (o.content_type)
62                                 x.setRequestHeader('Content-Type', o.content_type);
63
64                         x.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
65
66                         x.send(o.data);
67
68                         function ready() {
69                                 if (!o.async || x.readyState == 4 || c++ > 10000) {
70                                         if (o.success && c < 10000 && x.status == 200)
71                                                 o.success.call(o.success_scope, '' + x.responseText, x, o);
72                                         else if (o.error)
73                                                 o.error.call(o.error_scope, c > 10000 ? 'TIMED_OUT' : 'GENERAL', x, o);
74
75                                         x = null;
76                                 } else
77                                         w.setTimeout(ready, 10);
78                         };
79
80                         // Syncronous request
81                         if (!o.async)
82                                 return ready();
83
84                         // Wait for response, onReadyStateChange can not be used since it leaks memory in IE
85                         t = w.setTimeout(ready, 10);
86                 }
87         }
88 });