Subversion Repositories eFlore/Applications.cel

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
27 jpm 1
/*
2
 * Ext JS Library 2.0.2
3
 * Copyright(c) 2006-2008, Ext JS, LLC.
4
 * licensing@extjs.com
5
 *
6
 * http://extjs.com/license
7
 */
8
 
9
/**
10
 * @class Ext.data.Connection
11
 * @extends Ext.util.Observable
12
 * The class encapsulates a connection to the page's originating domain, allowing requests to be made
13
 * either to a configured URL, or to a URL specified at request time.<br><br>
14
 * <p>
15
 * Requests made by this class are asynchronous, and will return immediately. No data from
16
 * the server will be available to the statement immediately following the {@link #request} call.
17
 * To process returned data, use a callback in the request options object, or an event listener.</p><br>
18
 * <p>
19
 * Note: If you are doing a file upload, you will not get a normal response object sent back to
20
 * your callback or event handler.  Since the upload is handled via in IFRAME, there is no XMLHttpRequest.
21
 * The response object is created using the innerHTML of the IFRAME's document as the responseText
22
 * property and, if present, the IFRAME's XML document as the responseXML property.</p><br>
23
 * This means that a valid XML or HTML document must be returned. If JSON data is required, it is suggested
24
 * that it be placed either inside a &lt;textarea> in an HTML document and retrieved from the responseText
25
 * using a regex, or inside a CDATA section in an XML document and retrieved from the responseXML using
26
 * standard DOM methods.
27
 * @constructor
28
 * @param {Object} config a configuration object.
29
 */
30
Ext.data.Connection = function(config){
31
    Ext.apply(this, config);
32
    this.addEvents(
33
        /**
34
         * @event beforerequest
35
         * Fires before a network request is made to retrieve a data object.
36
         * @param {Connection} conn This Connection object.
37
         * @param {Object} options The options config object passed to the {@link #request} method.
38
         */
39
        "beforerequest",
40
        /**
41
         * @event requestcomplete
42
         * Fires if the request was successfully completed.
43
         * @param {Connection} conn This Connection object.
44
         * @param {Object} response The XHR object containing the response data.
45
         * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
46
         * for details.
47
         * @param {Object} options The options config object passed to the {@link #request} method.
48
         */
49
        "requestcomplete",
50
        /**
51
         * @event requestexception
52
         * Fires if an error HTTP status was returned from the server.
53
         * See <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html">HTTP Status Code Definitions</a>
54
         * for details of HTTP status codes.
55
         * @param {Connection} conn This Connection object.
56
         * @param {Object} response The XHR object containing the response data.
57
         * See <a href="http://www.w3.org/TR/XMLHttpRequest/">The XMLHttpRequest Object</a>
58
         * for details.
59
         * @param {Object} options The options config object passed to the {@link #request} method.
60
         */
61
        "requestexception"
62
    );
63
    Ext.data.Connection.superclass.constructor.call(this);
64
};
65
 
66
Ext.extend(Ext.data.Connection, Ext.util.Observable, {
67
    /**
68
     * @cfg {String} url (Optional) The default URL to be used for requests to the server. (defaults to undefined)
69
     */
70
    /**
71
     * @cfg {Object} extraParams (Optional) An object containing properties which are used as
72
     * extra parameters to each request made by this object. (defaults to undefined)
73
     */
74
    /**
75
     * @cfg {Object} defaultHeaders (Optional) An object containing request headers which are added
76
     *  to each request made by this object. (defaults to undefined)
77
     */
78
    /**
79
     * @cfg {String} method (Optional) The default HTTP method to be used for requests. (defaults to undefined; if not set but parms are present will use POST, otherwise GET)
80
     */
81
    /**
82
     * @cfg {Number} timeout (Optional) The timeout in milliseconds to be used for requests. (defaults to 30000)
83
     */
84
    timeout : 30000,
85
    /**
86
     * @cfg {Boolean} autoAbort (Optional) Whether this request should abort any pending requests. (defaults to false)
87
     * @type Boolean
88
     */
89
    autoAbort:false,
90
 
91
    /**
92
     * @cfg {Boolean} disableCaching (Optional) True to add a unique cache-buster param to GET requests. (defaults to true)
93
     * @type Boolean
94
     */
95
    disableCaching: true,
96
 
97
    /**
98
     * <p>Sends an HTTP request to a remote server.</p>
99
     * <p><b>Important:</b> Ajax server requests are asynchronous, and this call will
100
     * return before the response has been recieved. Process any returned data
101
     * in a callback function.
102
     * @param {Object} options An object which may contain the following properties:<ul>
103
     * <li><b>url</b> : String (Optional)<p style="margin-left:1em">The URL to
104
     * which to send the request. Defaults to configured URL</p></li>
105
     * <li><b>params</b> : Object/String/Function (Optional)<p style="margin-left:1em">
106
     * An object containing properties which are used as parameters to the
107
     * request, a url encoded string or a function to call to get either.</p></li>
108
     * <li><b>method</b> : String (Optional)<p style="margin-left:1em">The HTTP method to use
109
     * for the request. Defaults to the configured method, or if no method was configured,
110
     * "GET" if no parameters are being sent, and "POST" if parameters are being sent.  Note that
111
     * the method name is case-sensitive and should be all caps.</p></li>
112
     * <li><b>callback</b> : Function (Optional)<p style="margin-left:1em">The
113
     * function to be called upon receipt of the HTTP response. The callback is
114
     * called regardless of success or failure and is passed the following
115
     * parameters:<ul>
116
     * <li><b>options</b> : Object<p style="margin-left:1em">The parameter to the request call.</p></li>
117
     * <li><b>success</b> : Boolean<p style="margin-left:1em">True if the request succeeded.</p></li>
118
     * <li><b>response</b> : Object<p style="margin-left:1em">The XMLHttpRequest object containing the response data. See http://www.w3.org/TR/XMLHttpRequest/ for details about accessing elements of the response.</p></li>
119
     * </ul></p></li>
120
     * <li><b>success</b> : Function (Optional)<p style="margin-left:1em">The function
121
     * to be called upon success of the request. The callback is passed the following
122
     * parameters:<ul>
123
     * <li><b>response</b> : Object<p style="margin-left:1em">The XMLHttpRequest object containing the response data.</p></li>
124
     * <li><b>options</b> : Object<p style="margin-left:1em">The parameter to the request call.</p></li>
125
     * </ul></p></li>
126
     * <li><b>failure</b> : Function (Optional)<p style="margin-left:1em">The function
127
     * to be called upon failure of the request. The callback is passed the
128
     * following parameters:<ul>
129
     * <li><b>response</b> : Object<p style="margin-left:1em">The XMLHttpRequest object containing the response data.</p></li>
130
     * <li><b>options</b> : Object<p style="margin-left:1em">The parameter to the request call.</p></li>
131
     * </ul></p></li>
132
     * <li><b>scope</b> : Object (Optional)<p style="margin-left:1em">The scope in
133
     * which to execute the callbacks: The "this" object for the callback function.
134
     * Defaults to the browser window.</p></li>
135
     * <li><b>form</b> : Object/String (Optional)<p style="margin-left:1em">A form
136
     * object or id to pull parameters from.</p></li>
137
     * <li><b>isUpload</b> : Boolean (Optional)<p style="margin-left:1em">True if
138
     * the form object is a file upload (will usually be automatically detected).</p></li>
139
     * <li><b>headers</b> : Object (Optional)<p style="margin-left:1em">Request
140
     * headers to set for the request.</p></li>
141
     * <li><b>xmlData</b> : Object (Optional)<p style="margin-left:1em">XML document
142
     * to use for the post. Note: This will be used instead of params for the post
143
     * data. Any params will be appended to the URL.</p></li>
144
     * <li><b>jsonData</b> : Object/String (Optional)<p style="margin-left:1em">JSON
145
     * data to use as the post. Note: This will be used instead of params for the post
146
     * data. Any params will be appended to the URL.</p></li>
147
     * <li><b>disableCaching</b> : Boolean (Optional)<p style="margin-left:1em">True
148
     * to add a unique cache-buster param to GET requests.</p></li>
149
     * </ul></p>
150
     * <p>The options object may also contain any other property which might be needed to perform
151
     * postprocessing in a callback because it is passed to callback functions.</p>
152
     * @return {Number} transactionId The id of the server transaction. This may be used
153
     * to cancel the request.
154
     */
155
    request : function(o){
156
        if(this.fireEvent("beforerequest", this, o) !== false){
157
            var p = o.params;
158
 
159
            if(typeof p == "function"){
160
                p = p.call(o.scope||window, o);
161
            }
162
            if(typeof p == "object"){
163
                p = Ext.urlEncode(p);
164
            }
165
            if(this.extraParams){
166
                var extras = Ext.urlEncode(this.extraParams);
167
                p = p ? (p + '&' + extras) : extras;
168
            }
169
 
170
            var url = o.url || this.url;
171
            if(typeof url == 'function'){
172
                url = url.call(o.scope||window, o);
173
            }
174
 
175
            if(o.form){
176
                var form = Ext.getDom(o.form);
177
                url = url || form.action;
178
 
179
                var enctype = form.getAttribute("enctype");
180
                if(o.isUpload || (enctype && enctype.toLowerCase() == 'multipart/form-data')){
181
                    return this.doFormUpload(o, p, url);
182
                }
183
                var f = Ext.lib.Ajax.serializeForm(form);
184
                p = p ? (p + '&' + f) : f;
185
            }
186
 
187
            var hs = o.headers;
188
            if(this.defaultHeaders){
189
                hs = Ext.apply(hs || {}, this.defaultHeaders);
190
                if(!o.headers){
191
                    o.headers = hs;
192
                }
193
            }
194
 
195
            var cb = {
196
                success: this.handleResponse,
197
                failure: this.handleFailure,
198
                scope: this,
199
                argument: {options: o},
200
                timeout : o.timeout || this.timeout
201
            };
202
 
203
            var method = o.method||this.method||(p ? "POST" : "GET");
204
 
205
            if(method == 'GET' && (this.disableCaching && o.disableCaching !== false) || o.disableCaching === true){
206
                url += (url.indexOf('?') != -1 ? '&' : '?') + '_dc=' + (new Date().getTime());
207
            }
208
 
209
            if(typeof o.autoAbort == 'boolean'){ // options gets top priority
210
                if(o.autoAbort){
211
                    this.abort();
212
                }
213
            }else if(this.autoAbort !== false){
214
                this.abort();
215
            }
216
            if((method == 'GET' && p) || o.xmlData || o.jsonData){
217
                url += (url.indexOf('?') != -1 ? '&' : '?') + p;
218
                p = '';
219
            }
220
            this.transId = Ext.lib.Ajax.request(method, url, cb, p, o);
221
            return this.transId;
222
        }else{
223
            Ext.callback(o.callback, o.scope, [o, null, null]);
224
            return null;
225
        }
226
    },
227
 
228
    /**
229
     * Determine whether this object has a request outstanding.
230
     * @param {Number} transactionId (Optional) defaults to the last transaction
231
     * @return {Boolean} True if there is an outstanding request.
232
     */
233
    isLoading : function(transId){
234
        if(transId){
235
            return Ext.lib.Ajax.isCallInProgress(transId);
236
        }else{
237
            return this.transId ? true : false;
238
        }
239
    },
240
 
241
    /**
242
     * Aborts any outstanding request.
243
     * @param {Number} transactionId (Optional) defaults to the last transaction
244
     */
245
    abort : function(transId){
246
        if(transId || this.isLoading()){
247
            Ext.lib.Ajax.abort(transId || this.transId);
248
        }
249
    },
250
 
251
    // private
252
    handleResponse : function(response){
253
        this.transId = false;
254
        var options = response.argument.options;
255
        response.argument = options ? options.argument : null;
256
        this.fireEvent("requestcomplete", this, response, options);
257
        Ext.callback(options.success, options.scope, [response, options]);
258
        Ext.callback(options.callback, options.scope, [options, true, response]);
259
    },
260
 
261
    // private
262
    handleFailure : function(response, e){
263
        this.transId = false;
264
        var options = response.argument.options;
265
        response.argument = options ? options.argument : null;
266
        this.fireEvent("requestexception", this, response, options, e);
267
        Ext.callback(options.failure, options.scope, [response, options]);
268
        Ext.callback(options.callback, options.scope, [options, false, response]);
269
    },
270
 
271
    // private
272
    doFormUpload : function(o, ps, url){
273
        var id = Ext.id();
274
        var frame = document.createElement('iframe');
275
        frame.id = id;
276
        frame.name = id;
277
        frame.className = 'x-hidden';
278
        if(Ext.isIE){
279
            frame.src = Ext.SSL_SECURE_URL;
280
        }
281
        document.body.appendChild(frame);
282
 
283
        if(Ext.isIE){
284
           document.frames[id].name = id;
285
        }
286
 
287
        var form = Ext.getDom(o.form);
288
        form.target = id;
289
        form.method = 'POST';
290
        form.enctype = form.encoding = 'multipart/form-data';
291
        if(url){
292
            form.action = url;
293
        }
294
 
295
        var hiddens, hd;
296
        if(ps){ // add dynamic params
297
            hiddens = [];
298
            ps = Ext.urlDecode(ps, false);
299
            for(var k in ps){
300
                if(ps.hasOwnProperty(k)){
301
                    hd = document.createElement('input');
302
                    hd.type = 'hidden';
303
                    hd.name = k;
304
                    hd.value = ps[k];
305
                    form.appendChild(hd);
306
                    hiddens.push(hd);
307
                }
308
            }
309
        }
310
 
311
        function cb(){
312
            var r = {  // bogus response object
313
                responseText : '',
314
                responseXML : null
315
            };
316
 
317
            r.argument = o ? o.argument : null;
318
 
319
            try { //
320
                var doc;
321
                if(Ext.isIE){
322
                    doc = frame.contentWindow.document;
323
                }else {
324
                    doc = (frame.contentDocument || window.frames[id].document);
325
                }
326
                if(doc && doc.body){
327
                    r.responseText = doc.body.innerHTML;
328
                }
329
                if(doc && doc.XMLDocument){
330
                    r.responseXML = doc.XMLDocument;
331
                }else {
332
                    r.responseXML = doc;
333
                }
334
            }
335
            catch(e) {
336
                // ignore
337
            }
338
 
339
            Ext.EventManager.removeListener(frame, 'load', cb, this);
340
 
341
            this.fireEvent("requestcomplete", this, r, o);
342
 
343
            Ext.callback(o.success, o.scope, [r, o]);
344
            Ext.callback(o.callback, o.scope, [o, true, r]);
345
 
346
            setTimeout(function(){Ext.removeNode(frame);}, 100);
347
        }
348
 
349
        Ext.EventManager.on(frame, 'load', cb, this);
350
        form.submit();
351
 
352
        if(hiddens){ // remove dynamic params
353
            for(var i = 0, len = hiddens.length; i < len; i++){
354
                Ext.removeNode(hiddens[i]);
355
            }
356
        }
357
    }
358
});
359
 
360
/**
361
 * @class Ext.Ajax
362
 * @extends Ext.data.Connection
363
 * Global Ajax request class.  Provides a simple way to make Ajax requests with maximum flexibility.  Example usage:
364
 * <pre><code>
365
// Basic request
366
Ext.Ajax.request({
367
   url: 'foo.php',
368
   success: someFn,
369
   failure: otherFn,
370
   headers: {
371
       'my-header': 'foo'
372
   },
373
   params: { foo: 'bar' }
374
});
375
 
376
// Simple ajax form submission
377
Ext.Ajax.request({
378
    form: 'some-form',
379
    params: 'foo=bar'
380
});
381
 
382
// Default headers to pass in every request
383
Ext.Ajax.defaultHeaders = {
384
    'Powered-By': 'Ext'
385
};
386
 
387
// Global Ajax events can be handled on every request!
388
Ext.Ajax.on('beforerequest', this.showSpinner, this);
389
</code></pre>
390
 * @singleton
391
 */
392
Ext.Ajax = new Ext.data.Connection({
393
    /**
394
     * @cfg {String} url @hide
395
     */
396
    /**
397
     * @cfg {Object} extraParams @hide
398
     */
399
    /**
400
     * @cfg {Object} defaultHeaders @hide
401
     */
402
    /**
403
     * @cfg {String} method (Optional) @hide
404
     */
405
    /**
406
     * @cfg {Number} timeout (Optional) @hide
407
     */
408
    /**
409
     * @cfg {Boolean} autoAbort (Optional) @hide
410
     */
411
 
412
    /**
413
     * @cfg {Boolean} disableCaching (Optional) @hide
414
     */
415
 
416
    /**
417
     * @property  disableCaching
418
     * True to add a unique cache-buster param to GET requests. (defaults to true)
419
     * @type Boolean
420
     */
421
    /**
422
     * @property  url
423
     * The default URL to be used for requests to the server. (defaults to undefined)
424
     * @type String
425
     */
426
    /**
427
     * @property  extraParams
428
     * An object containing properties which are used as
429
     * extra parameters to each request made by this object. (defaults to undefined)
430
     * @type Object
431
     */
432
    /**
433
     * @property  defaultHeaders
434
     * An object containing request headers which are added to each request made by this object. (defaults to undefined)
435
     * @type Object
436
     */
437
    /**
438
     * @property  method
439
     * The default HTTP method to be used for requests. Note that this is case-sensitive and should be all caps (defaults
440
     * to undefined; if not set but parms are present will use "POST," otherwise "GET.")
441
     * @type String
442
     */
443
    /**
444
     * @property  timeout
445
     * The timeout in milliseconds to be used for requests. (defaults to 30000)
446
     * @type Number
447
     */
448
 
449
    /**
450
     * @property  autoAbort
451
     * Whether a new request should abort any pending requests. (defaults to false)
452
     * @type Boolean
453
     */
454
    autoAbort : false,
455
 
456
    /**
457
     * Serialize the passed form into a url encoded string
458
     * @param {String/HTMLElement} form
459
     * @return {String}
460
     */
461
    serializeForm : function(form){
462
        return Ext.lib.Ajax.serializeForm(form);
463
    }
464
});