~cdparra/gelee/trunk

« back to all changes in this revision

Viewing changes to webui/extjs/source/adapter/core/ext-base-ajax.js

  • Committer: parra
  • Date: 2010-03-15 15:56:56 UTC
  • Revision ID: svn-v4:ac5bba68-f036-4e09-846e-8f32731cc928:trunk/gelee:1448
merged gelee at svn

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Ext JS Library 3.0 RC2
 
3
 * Copyright(c) 2006-2009, Ext JS, LLC.
 
4
 * licensing@extjs.com
 
5
 * 
 
6
 * http://extjs.com/license
 
7
 */
 
8
 
 
9
/*
 
10
 * Portions of this file are based on pieces of Yahoo User Interface Library
 
11
 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
 
12
 * YUI licensed under the BSD License:
 
13
 * http://developer.yahoo.net/yui/license.txt
 
14
 */
 
15
    Ext.lib.Ajax = function() {     
 
16
            var activeX = ['MSXML2.XMLHTTP.3.0',
 
17
                                   'MSXML2.XMLHTTP',
 
18
                                   'Microsoft.XMLHTTP'];
 
19
                                   
 
20
                // private
 
21
                function setHeader(o) {
 
22
                var conn = o.conn,
 
23
                        prop;
 
24
                
 
25
                function setTheHeaders(conn, headers){
 
26
                        for (prop in headers) {
 
27
                    if (headers.hasOwnProperty(prop)) {
 
28
                        conn.setRequestHeader(prop, headers[prop]);
 
29
                    }
 
30
                }   
 
31
                }               
 
32
                
 
33
            if (pub.defaultHeaders) {
 
34
                    setTheHeaders(conn, pub.defaultHeaders);
 
35
            }
 
36
 
 
37
            if (pub.headers) {
 
38
                                setTheHeaders(conn, pub.headers);
 
39
                pub.headers = null;                
 
40
            }
 
41
        }    
 
42
        
 
43
        // private
 
44
        function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {          
 
45
            return {
 
46
                    tId : tId,
 
47
                    status : isAbort ? -1 : 0,
 
48
                    statusText : isAbort ? 'transaction aborted' : 'communication failure',
 
49
                    isAbort: true,
 
50
                    isTimeout: true,
 
51
                    argument : callbackArg
 
52
            };
 
53
        }  
 
54
        
 
55
        // private 
 
56
        function initHeader(label, value) {         
 
57
                        (pub.headers = pub.headers || {})[label] = value;                                   
 
58
        }
 
59
            
 
60
        // private
 
61
        function createResponseObject(o, callbackArg) {
 
62
            var headerObj = {},
 
63
                headerStr,              
 
64
                conn = o.conn;                  
 
65
 
 
66
            try {
 
67
                headerStr = o.conn.getAllResponseHeaders();                
 
68
                Ext.each(headerStr.split('\n'), function(v){
 
69
                        var t = v.indexOf(':');
 
70
                    headerObj[v.substr(0, t)] = v.substr(t + 1);
 
71
                });
 
72
            } catch(e) {}
 
73
                        
 
74
            return {
 
75
                        tId : o.tId,
 
76
                    status : conn.status,
 
77
                    statusText : conn.statusText,
 
78
                    getResponseHeader : function(header){return headerObj[header];},
 
79
                getAllResponseHeaders : function(){return headerStr},
 
80
                    responseText : conn.responseText,
 
81
                    responseXML : conn.responseXML,
 
82
                    argument : callbackArg
 
83
                };
 
84
        }
 
85
        
 
86
        // private
 
87
        function releaseObject(o) {
 
88
            o.conn = null;
 
89
            o = null;
 
90
        }        
 
91
            
 
92
        // private
 
93
        function handleTransactionResponse(o, callback, isAbort, isTimeout) {
 
94
            if (!callback) {
 
95
                releaseObject(o);
 
96
                return;
 
97
            }
 
98
 
 
99
            var httpStatus, responseObject;
 
100
 
 
101
            try {
 
102
                if (o.conn.status !== undefined && o.conn.status != 0) {
 
103
                    httpStatus = o.conn.status;
 
104
                }
 
105
                else {
 
106
                    httpStatus = 13030;
 
107
                }
 
108
            }
 
109
            catch(e) {
 
110
                httpStatus = 13030;
 
111
            }
 
112
 
 
113
            if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
 
114
                responseObject = createResponseObject(o, callback.argument);
 
115
                if (callback.success) {
 
116
                    if (!callback.scope) {
 
117
                        callback.success(responseObject);
 
118
                    }
 
119
                    else {
 
120
                        callback.success.apply(callback.scope, [responseObject]);
 
121
                    }
 
122
                }
 
123
            }
 
124
            else {
 
125
                switch (httpStatus) {
 
126
                    case 12002:
 
127
                    case 12029:
 
128
                    case 12030:
 
129
                    case 12031:
 
130
                    case 12152:
 
131
                    case 13030:
 
132
                        responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
 
133
                        if (callback.failure) {
 
134
                            if (!callback.scope) {
 
135
                                callback.failure(responseObject);
 
136
                            }
 
137
                            else {
 
138
                                callback.failure.apply(callback.scope, [responseObject]);
 
139
                            }
 
140
                        }
 
141
                        break;
 
142
                    default:
 
143
                        responseObject = createResponseObject(o, callback.argument);
 
144
                        if (callback.failure) {
 
145
                            if (!callback.scope) {
 
146
                                callback.failure(responseObject);
 
147
                            }
 
148
                            else {
 
149
                                callback.failure.apply(callback.scope, [responseObject]);
 
150
                            }
 
151
                        }
 
152
                }
 
153
            }
 
154
 
 
155
            releaseObject(o);
 
156
            responseObject = null;
 
157
        }  
 
158
        
 
159
        // private
 
160
        function handleReadyState(o, callback){
 
161
            callback = callback || {};
 
162
            var conn = o.conn,
 
163
                tId = o.tId,
 
164
                poll = pub.poll,
 
165
                cbTimeout = callback.timeout || null;
 
166
 
 
167
            if (cbTimeout) {
 
168
                pub.timeout[tId] = setTimeout(function() {
 
169
                    pub.abort(o, callback, true);
 
170
                }, cbTimeout);
 
171
            }
 
172
 
 
173
            poll[tId] = setInterval(
 
174
                function() {
 
175
                    if (conn && conn.readyState == 4) {
 
176
                        clearInterval(poll[tId]);
 
177
                        poll[tId] = null;
 
178
 
 
179
                        if (cbTimeout) {
 
180
                            clearTimeout(pub.timeout[tId]);
 
181
                            pub.timeout[tId] = null;
 
182
                        }
 
183
 
 
184
                        handleTransactionResponse(o, callback);
 
185
                    }
 
186
                },
 
187
                pub.pollInterval);
 
188
        }
 
189
        
 
190
        // private
 
191
        function asyncRequest(method, uri, callback, postData) {
 
192
            var o = getConnectionObject() || null;
 
193
 
 
194
            if (o) {
 
195
                o.conn.open(method, uri, true);
 
196
 
 
197
                if (pub.useDefaultXhrHeader) {                    
 
198
                        initHeader('X-Requested-With', pub.defaultXhrHeader);
 
199
                }
 
200
 
 
201
                if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers['Content-Type'])){
 
202
                    initHeader('Content-Type', pub.defaultPostHeader);
 
203
                }
 
204
 
 
205
                if (pub.defaultHeaders || pub.headers) {
 
206
                    setHeader(o);
 
207
                }
 
208
 
 
209
                handleReadyState(o, callback);
 
210
                o.conn.send(postData || null);
 
211
            }
 
212
            return o;
 
213
        }
 
214
        
 
215
        // private
 
216
        function getConnectionObject() {
 
217
            var o;              
 
218
 
 
219
            try {
 
220
                if (o = createXhrObject(pub.transactionId)) {
 
221
                    pub.transactionId++;
 
222
                }
 
223
            } catch(e) {
 
224
            } finally {
 
225
                return o;
 
226
            }
 
227
        }
 
228
               
 
229
        // private
 
230
        function createXhrObject(transactionId) {
 
231
            var http;
 
232
                
 
233
            try {
 
234
                http = new XMLHttpRequest();                
 
235
            } catch(e) {
 
236
                for (var i = 0; i < activeX.length; ++i) {                  
 
237
                    try {
 
238
                        http = new ActiveXObject(activeX[i]);                        
 
239
                        break;
 
240
                    } catch(e) {}
 
241
                }
 
242
            } finally {
 
243
                return {conn : http, tId : transactionId};
 
244
            }
 
245
        }
 
246
                 
 
247
            var pub = {
 
248
                request : function(method, uri, cb, data, options) {
 
249
                            if(options){
 
250
                                var me = this,                  
 
251
                                        xmlData = options.xmlData,
 
252
                                        jsonData = options.jsonData;
 
253
                                        
 
254
                                Ext.applyIf(me, options);               
 
255
                            
 
256
                            if(xmlData || jsonData){
 
257
                                    initHeader('Content-Type', xmlData ? 'text/xml' : 'application/json');
 
258
                                    data = xmlData || Ext.encode(jsonData);
 
259
                                }
 
260
                            }                               
 
261
                            return asyncRequest(method || options.method || "POST", uri, cb, data);
 
262
                },
 
263
        
 
264
                serializeForm : function(form) {
 
265
                        var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
 
266
                        hasSubmit = false,
 
267
                        encoder = encodeURIComponent,
 
268
                                element,
 
269
                        options, 
 
270
                        name, 
 
271
                        val,                    
 
272
                        data = '',
 
273
                        type;
 
274
                        
 
275
                        Ext.each(fElements, function(element) {                     
 
276
                        name = element.name;                 
 
277
                                        type = element.type;
 
278
                                        
 
279
                        if (!element.disabled && name){
 
280
                                if(/select-(one|multiple)/i.test(type)){                                        
 
281
                                            Ext.each(element.options, function(opt) {
 
282
                                                    if (opt.selected) {
 
283
                                                            data += String.format("{0}={1}&",                                                                                             
 
284
                                                                                                 encoder(name),                                                                                          
 
285
                                                                                                  (opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text);
 
286
                                }                                                               
 
287
                            });
 
288
                                } else if(!/file|undefined|reset|button/i.test(type)) {
 
289
                                        if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
 
290
                                    
 
291
                                data += encoder(name) + '=' + encoder(element.value) + '&';                     
 
292
                                hasSubmit = /submit/i.test(type);    
 
293
                            }                           
 
294
                                } 
 
295
                        }
 
296
                    });            
 
297
                    return data.substr(0, data.length - 1);
 
298
                },
 
299
                
 
300
                useDefaultHeader : true,
 
301
                defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
 
302
                useDefaultXhrHeader : true,
 
303
                defaultXhrHeader : 'XMLHttpRequest',        
 
304
                poll : {},
 
305
                timeout : {},
 
306
                pollInterval : 50,
 
307
                transactionId : 0,
 
308
                
 
309
//      This is never called - Is it worth exposing this?                       
 
310
//              setProgId : function(id) {
 
311
//                  activeX.unshift(id);
 
312
//              },
 
313
 
 
314
//      This is never called - Is it worth exposing this?       
 
315
//              setDefaultPostHeader : function(b) {
 
316
//                  this.useDefaultHeader = b;
 
317
//              },
 
318
                
 
319
//      This is never called - Is it worth exposing this?       
 
320
//              setDefaultXhrHeader : function(b) {
 
321
//                  this.useDefaultXhrHeader = b;
 
322
//              },
 
323
 
 
324
//      This is never called - Is it worth exposing this?               
 
325
//              setPollingInterval : function(i) {
 
326
//                  if (typeof i == 'number' && isFinite(i)) {
 
327
//                      this.pollInterval = i;
 
328
//                  }
 
329
//              },
 
330
                
 
331
//      This is never called - Is it worth exposing this?
 
332
//              resetDefaultHeaders : function() {
 
333
//                  this.defaultHeaders = null;
 
334
//              },
 
335
        
 
336
                abort : function(o, callback, isTimeout) {
 
337
                        var me = this,
 
338
                                tId = o.tId,
 
339
                                isAbort = false;
 
340
                        
 
341
                    if (me.isCallInProgress(o)) {
 
342
                        o.conn.abort();
 
343
                        clearInterval(me.poll[tId]);
 
344
                        me.poll[tId] = null;
 
345
                        if (isTimeout) {
 
346
                            me.timeout[tId] = null;
 
347
                        }
 
348
                                        
 
349
                        handleTransactionResponse(o, callback, (isAbort = true), isTimeout);                
 
350
                    }
 
351
                    return isAbort;
 
352
                },
 
353
        
 
354
                isCallInProgress : function(o) {
 
355
                    // if there is a connection and readyState is not 0 or 4
 
356
                    return o.conn && !{0:true,4:true}[o.conn.readyState];               
 
357
                }
 
358
            };
 
359
            return pub;
 
360
    }();
 
 
b'\\ No newline at end of file'