3
* Copyright(c) 2006-2010 Ext JS, Inc.
5
* http://www.extjs.com/license
8
* Portions of this file are based on pieces of Yahoo User Interface Library
9
* Copyright (c) 2007, Yahoo! Inc. All rights reserved.
10
* YUI licensed under the BSD License:
11
* http://developer.yahoo.net/yui/license.txt
13
Ext.lib.Ajax = function() {
14
var activeX = ['MSXML2.XMLHTTP.3.0',
17
CONTENTTYPE = 'Content-Type';
20
function setHeader(o) {
24
function setTheHeaders(conn, headers){
25
for (prop in headers) {
26
if (headers.hasOwnProperty(prop)) {
27
conn.setRequestHeader(prop, headers[prop]);
32
if (pub.defaultHeaders) {
33
setTheHeaders(conn, pub.defaultHeaders);
37
setTheHeaders(conn, pub.headers);
43
function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
46
status : isAbort ? -1 : 0,
47
statusText : isAbort ? 'transaction aborted' : 'communication failure',
50
argument : callbackArg
55
function initHeader(label, value) {
56
(pub.headers = pub.headers || {})[label] = value;
60
function createResponseObject(o, callbackArg) {
66
// see: https://prototype.lighthouseapp.com/projects/8886/tickets/129-ie-mangles-http-response-status-code-204-to-1223
67
isBrokenStatus = conn.status == 1223;
70
headerStr = o.conn.getAllResponseHeaders();
71
Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
74
s = v.substr(0, t).toLowerCase();
75
if(v.charAt(t + 1) == ' '){
78
headerObj[s] = v.substr(t + 1);
85
// Normalize the status and statusText when IE returns 1223, see the above link.
86
status : isBrokenStatus ? 204 : conn.status,
87
statusText : isBrokenStatus ? 'No Content' : conn.statusText,
88
getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
89
getAllResponseHeaders : function(){return headerStr},
90
responseText : conn.responseText,
91
responseXML : conn.responseXML,
92
argument : callbackArg
97
function releaseObject(o) {
99
pub.conn[o.tId] = null;
106
function handleTransactionResponse(o, callback, isAbort, isTimeout) {
112
var httpStatus, responseObject;
115
if (o.conn.status !== undefined && o.conn.status != 0) {
116
httpStatus = o.conn.status;
126
if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
127
responseObject = createResponseObject(o, callback.argument);
128
if (callback.success) {
129
if (!callback.scope) {
130
callback.success(responseObject);
133
callback.success.apply(callback.scope, [responseObject]);
138
switch (httpStatus) {
145
responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
146
if (callback.failure) {
147
if (!callback.scope) {
148
callback.failure(responseObject);
151
callback.failure.apply(callback.scope, [responseObject]);
156
responseObject = createResponseObject(o, callback.argument);
157
if (callback.failure) {
158
if (!callback.scope) {
159
callback.failure(responseObject);
162
callback.failure.apply(callback.scope, [responseObject]);
169
responseObject = null;
173
function handleReadyState(o, callback){
174
callback = callback || {};
178
cbTimeout = callback.timeout || null;
181
pub.conn[tId] = conn;
182
pub.timeout[tId] = setTimeout(function() {
183
pub.abort(o, callback, true);
187
poll[tId] = setInterval(
189
if (conn && conn.readyState == 4) {
190
clearInterval(poll[tId]);
194
clearTimeout(pub.timeout[tId]);
195
pub.timeout[tId] = null;
198
handleTransactionResponse(o, callback);
205
function asyncRequest(method, uri, callback, postData) {
206
var o = getConnectionObject() || null;
209
o.conn.open(method, uri, true);
211
if (pub.useDefaultXhrHeader) {
212
initHeader('X-Requested-With', pub.defaultXhrHeader);
215
if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
216
initHeader(CONTENTTYPE, pub.defaultPostHeader);
219
if (pub.defaultHeaders || pub.headers) {
223
handleReadyState(o, callback);
224
o.conn.send(postData || null);
230
function getConnectionObject() {
234
if (o = createXhrObject(pub.transactionId)) {
244
function createXhrObject(transactionId) {
248
http = new XMLHttpRequest();
250
for (var i = 0; i < activeX.length; ++i) {
252
http = new ActiveXObject(activeX[i]);
257
return {conn : http, tId : transactionId};
262
request : function(method, uri, cb, data, options) {
265
xmlData = options.xmlData,
266
jsonData = options.jsonData,
269
Ext.applyIf(me, options);
271
if(xmlData || jsonData){
273
if(!hs || !hs[CONTENTTYPE]){
274
initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
276
data = xmlData || (!Ext.isPrimitive(jsonData) ? Ext.encode(jsonData) : jsonData);
279
return asyncRequest(method || options.method || "POST", uri, cb, data);
282
serializeForm : function(form) {
283
var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
285
encoder = encodeURIComponent,
293
Ext.each(fElements, function(element) {
297
if (!element.disabled && name){
298
if(/select-(one|multiple)/i.test(type)) {
299
Ext.each(element.options, function(opt) {
301
data += String.format("{0}={1}&", encoder(name), encoder((opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text));
304
} else if(!/file|undefined|reset|button/i.test(type)) {
305
if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
307
data += encoder(name) + '=' + encoder(element.value) + '&';
308
hasSubmit = /submit/i.test(type);
313
return data.substr(0, data.length - 1);
316
useDefaultHeader : true,
317
defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
318
useDefaultXhrHeader : true,
319
defaultXhrHeader : 'XMLHttpRequest',
326
// This is never called - Is it worth exposing this?
327
// setProgId : function(id) {
328
// activeX.unshift(id);
331
// This is never called - Is it worth exposing this?
332
// setDefaultPostHeader : function(b) {
333
// this.useDefaultHeader = b;
336
// This is never called - Is it worth exposing this?
337
// setDefaultXhrHeader : function(b) {
338
// this.useDefaultXhrHeader = b;
341
// This is never called - Is it worth exposing this?
342
// setPollingInterval : function(i) {
343
// if (typeof i == 'number' && isFinite(i)) {
344
// this.pollInterval = i;
348
// This is never called - Is it worth exposing this?
349
// resetDefaultHeaders : function() {
350
// this.defaultHeaders = null;
353
abort : function(o, callback, isTimeout) {
358
if (me.isCallInProgress(o)) {
360
clearInterval(me.poll[tId]);
362
clearTimeout(pub.timeout[tId]);
363
me.timeout[tId] = null;
365
handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
370
isCallInProgress : function(o) {
371
// if there is a connection and readyState is not 0 or 4
372
return o.conn && !{0:true,4:true}[o.conn.readyState];
b'\\ No newline at end of file'