2
* Ext JS Library 3.0 RC2
3
* Copyright(c) 2006-2009, Ext JS, LLC.
6
* http://extjs.com/license
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
15
Ext.lib.Ajax = function() {
16
var activeX = ['MSXML2.XMLHTTP.3.0',
21
function setHeader(o) {
25
function setTheHeaders(conn, headers){
26
for (prop in headers) {
27
if (headers.hasOwnProperty(prop)) {
28
conn.setRequestHeader(prop, headers[prop]);
33
if (pub.defaultHeaders) {
34
setTheHeaders(conn, pub.defaultHeaders);
38
setTheHeaders(conn, pub.headers);
44
function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
47
status : isAbort ? -1 : 0,
48
statusText : isAbort ? 'transaction aborted' : 'communication failure',
51
argument : callbackArg
56
function initHeader(label, value) {
57
(pub.headers = pub.headers || {})[label] = value;
61
function createResponseObject(o, callbackArg) {
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);
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
87
function releaseObject(o) {
93
function handleTransactionResponse(o, callback, isAbort, isTimeout) {
99
var httpStatus, responseObject;
102
if (o.conn.status !== undefined && o.conn.status != 0) {
103
httpStatus = o.conn.status;
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);
120
callback.success.apply(callback.scope, [responseObject]);
125
switch (httpStatus) {
132
responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
133
if (callback.failure) {
134
if (!callback.scope) {
135
callback.failure(responseObject);
138
callback.failure.apply(callback.scope, [responseObject]);
143
responseObject = createResponseObject(o, callback.argument);
144
if (callback.failure) {
145
if (!callback.scope) {
146
callback.failure(responseObject);
149
callback.failure.apply(callback.scope, [responseObject]);
156
responseObject = null;
160
function handleReadyState(o, callback){
161
callback = callback || {};
165
cbTimeout = callback.timeout || null;
168
pub.timeout[tId] = setTimeout(function() {
169
pub.abort(o, callback, true);
173
poll[tId] = setInterval(
175
if (conn && conn.readyState == 4) {
176
clearInterval(poll[tId]);
180
clearTimeout(pub.timeout[tId]);
181
pub.timeout[tId] = null;
184
handleTransactionResponse(o, callback);
191
function asyncRequest(method, uri, callback, postData) {
192
var o = getConnectionObject() || null;
195
o.conn.open(method, uri, true);
197
if (pub.useDefaultXhrHeader) {
198
initHeader('X-Requested-With', pub.defaultXhrHeader);
201
if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers['Content-Type'])){
202
initHeader('Content-Type', pub.defaultPostHeader);
205
if (pub.defaultHeaders || pub.headers) {
209
handleReadyState(o, callback);
210
o.conn.send(postData || null);
216
function getConnectionObject() {
220
if (o = createXhrObject(pub.transactionId)) {
230
function createXhrObject(transactionId) {
234
http = new XMLHttpRequest();
236
for (var i = 0; i < activeX.length; ++i) {
238
http = new ActiveXObject(activeX[i]);
243
return {conn : http, tId : transactionId};
248
request : function(method, uri, cb, data, options) {
251
xmlData = options.xmlData,
252
jsonData = options.jsonData;
254
Ext.applyIf(me, options);
256
if(xmlData || jsonData){
257
initHeader('Content-Type', xmlData ? 'text/xml' : 'application/json');
258
data = xmlData || Ext.encode(jsonData);
261
return asyncRequest(method || options.method || "POST", uri, cb, data);
264
serializeForm : function(form) {
265
var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
267
encoder = encodeURIComponent,
275
Ext.each(fElements, function(element) {
279
if (!element.disabled && name){
280
if(/select-(one|multiple)/i.test(type)){
281
Ext.each(element.options, function(opt) {
283
data += String.format("{0}={1}&",
285
(opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text);
288
} else if(!/file|undefined|reset|button/i.test(type)) {
289
if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
291
data += encoder(name) + '=' + encoder(element.value) + '&';
292
hasSubmit = /submit/i.test(type);
297
return data.substr(0, data.length - 1);
300
useDefaultHeader : true,
301
defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
302
useDefaultXhrHeader : true,
303
defaultXhrHeader : 'XMLHttpRequest',
309
// This is never called - Is it worth exposing this?
310
// setProgId : function(id) {
311
// activeX.unshift(id);
314
// This is never called - Is it worth exposing this?
315
// setDefaultPostHeader : function(b) {
316
// this.useDefaultHeader = b;
319
// This is never called - Is it worth exposing this?
320
// setDefaultXhrHeader : function(b) {
321
// this.useDefaultXhrHeader = b;
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;
331
// This is never called - Is it worth exposing this?
332
// resetDefaultHeaders : function() {
333
// this.defaultHeaders = null;
336
abort : function(o, callback, isTimeout) {
341
if (me.isCallInProgress(o)) {
343
clearInterval(me.poll[tId]);
346
me.timeout[tId] = null;
349
handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
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];
b'\\ No newline at end of file'