3 <title>The source code</title>
4 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
5 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
7 <body onload="prettyPrint();">
8 <pre class="prettyprint lang-js">/*!
10 * Copyright(c) 2006-2009 Ext JS, LLC
12 * http://www.extjs.com/license
15 * Portions of this file are based on pieces of Yahoo User Interface Library
\r
16 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
\r
17 * YUI licensed under the BSD License:
\r
18 * http://developer.yahoo.net/yui/license.txt
\r
20 Ext.lib.Ajax = function() {
\r
21 var activeX = ['MSXML2.XMLHTTP.3.0',
\r
23 'Microsoft.XMLHTTP'],
\r
24 CONTENTTYPE = 'Content-Type';
\r
27 function setHeader(o) {
\r
31 function setTheHeaders(conn, headers){
\r
32 for (prop in headers) {
\r
33 if (headers.hasOwnProperty(prop)) {
\r
34 conn.setRequestHeader(prop, headers[prop]);
\r
39 if (pub.defaultHeaders) {
\r
40 setTheHeaders(conn, pub.defaultHeaders);
\r
44 setTheHeaders(conn, pub.headers);
\r
45 delete pub.headers;
\r
50 function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
\r
53 status : isAbort ? -1 : 0,
\r
54 statusText : isAbort ? 'transaction aborted' : 'communication failure',
\r
56 isTimeout: isTimeout,
\r
57 argument : callbackArg
\r
62 function initHeader(label, value) {
\r
63 (pub.headers = pub.headers || {})[label] = value;
\r
67 function createResponseObject(o, callbackArg) {
\r
75 headerStr = o.conn.getAllResponseHeaders();
\r
76 Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
\r
79 s = v.substr(0, t).toLowerCase();
\r
80 if(v.charAt(t + 1) == ' '){
\r
83 headerObj[s] = v.substr(t + 1);
\r
90 status : conn.status,
\r
91 statusText : conn.statusText,
\r
92 getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
\r
93 getAllResponseHeaders : function(){return headerStr},
\r
94 responseText : conn.responseText,
\r
95 responseXML : conn.responseXML,
\r
96 argument : callbackArg
\r
101 function releaseObject(o) {
\r
107 function handleTransactionResponse(o, callback, isAbort, isTimeout) {
\r
113 var httpStatus, responseObject;
\r
116 if (o.conn.status !== undefined && o.conn.status != 0) {
\r
117 httpStatus = o.conn.status;
\r
120 httpStatus = 13030;
\r
124 httpStatus = 13030;
\r
127 if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
\r
128 responseObject = createResponseObject(o, callback.argument);
\r
129 if (callback.success) {
\r
130 if (!callback.scope) {
\r
131 callback.success(responseObject);
\r
134 callback.success.apply(callback.scope, [responseObject]);
\r
139 switch (httpStatus) {
\r
146 responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
\r
147 if (callback.failure) {
\r
148 if (!callback.scope) {
\r
149 callback.failure(responseObject);
\r
152 callback.failure.apply(callback.scope, [responseObject]);
\r
157 responseObject = createResponseObject(o, callback.argument);
\r
158 if (callback.failure) {
\r
159 if (!callback.scope) {
\r
160 callback.failure(responseObject);
\r
163 callback.failure.apply(callback.scope, [responseObject]);
\r
170 responseObject = null;
\r
174 function handleReadyState(o, callback){
\r
175 callback = callback || {};
\r
179 cbTimeout = callback.timeout || null;
\r
182 pub.timeout[tId] = setTimeout(function() {
\r
183 pub.abort(o, callback, true);
\r
187 poll[tId] = setInterval(
\r
189 if (conn && conn.readyState == 4) {
\r
190 clearInterval(poll[tId]);
\r
194 clearTimeout(pub.timeout[tId]);
\r
195 pub.timeout[tId] = null;
\r
198 handleTransactionResponse(o, callback);
\r
205 function asyncRequest(method, uri, callback, postData) {
\r
206 var o = getConnectionObject() || null;
\r
209 o.conn.open(method, uri, true);
\r
211 if (pub.useDefaultXhrHeader) {
\r
212 initHeader('X-Requested-With', pub.defaultXhrHeader);
\r
215 if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
\r
216 initHeader(CONTENTTYPE, pub.defaultPostHeader);
\r
219 if (pub.defaultHeaders || pub.headers) {
\r
223 handleReadyState(o, callback);
\r
224 o.conn.send(postData || null);
\r
230 function getConnectionObject() {
\r
234 if (o = createXhrObject(pub.transactionId)) {
\r
235 pub.transactionId++;
\r
244 function createXhrObject(transactionId) {
\r
248 http = new XMLHttpRequest();
\r
250 for (var i = 0; i < activeX.length; ++i) {
\r
252 http = new ActiveXObject(activeX[i]);
\r
257 return {conn : http, tId : transactionId};
\r
262 request : function(method, uri, cb, data, options) {
\r
265 xmlData = options.xmlData,
\r
266 jsonData = options.jsonData,
\r
269 Ext.applyIf(me, options);
\r
271 if(xmlData || jsonData){
\r
273 if(!hs || !hs[CONTENTTYPE]){
\r
274 initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
\r
276 data = xmlData || (Ext.isObject(jsonData) ? Ext.encode(jsonData) : jsonData);
\r
279 return asyncRequest(method || options.method || "POST", uri, cb, data);
\r
282 serializeForm : function(form) {
\r
283 var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
\r
285 encoder = encodeURIComponent,
\r
293 Ext.each(fElements, function(element) {
\r
294 name = element.name;
\r
295 type = element.type;
\r
297 if (!element.disabled && name){
\r
298 if(/select-(one|multiple)/i.test(type)){
\r
299 Ext.each(element.options, function(opt) {
\r
300 if (opt.selected) {
\r
301 data += String.format("{0}={1}&",
\r
303 encoder((opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text));
\r
306 } else if(!/file|undefined|reset|button/i.test(type)) {
\r
307 if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
\r
309 data += encoder(name) + '=' + encoder(element.value) + '&';
\r
310 hasSubmit = /submit/i.test(type);
\r
315 return data.substr(0, data.length - 1);
\r
318 useDefaultHeader : true,
\r
319 defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
\r
320 useDefaultXhrHeader : true,
\r
321 defaultXhrHeader : 'XMLHttpRequest',
\r
327 // This is never called - Is it worth exposing this?
\r
328 // setProgId : function(id) {
\r
329 // activeX.unshift(id);
\r
332 // This is never called - Is it worth exposing this?
\r
333 // setDefaultPostHeader : function(b) {
\r
334 // this.useDefaultHeader = b;
\r
337 // This is never called - Is it worth exposing this?
\r
338 // setDefaultXhrHeader : function(b) {
\r
339 // this.useDefaultXhrHeader = b;
\r
342 // This is never called - Is it worth exposing this?
\r
343 // setPollingInterval : function(i) {
\r
344 // if (typeof i == 'number' && isFinite(i)) {
\r
345 // this.pollInterval = i;
\r
349 // This is never called - Is it worth exposing this?
\r
350 // resetDefaultHeaders : function() {
\r
351 // this.defaultHeaders = null;
\r
354 abort : function(o, callback, isTimeout) {
\r
359 if (me.isCallInProgress(o)) {
\r
361 clearInterval(me.poll[tId]);
\r
362 me.poll[tId] = null;
\r
364 me.timeout[tId] = null;
\r
367 handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
\r
372 isCallInProgress : function(o) {
\r
373 // if there is a connection and readyState is not 0 or 4
\r
374 return o.conn && !{0:true,4:true}[o.conn.readyState];
\r