3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
\r
4 <title>The source code</title>
\r
5 <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
\r
6 <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
\r
8 <body onload="prettyPrint();">
\r
9 <pre class="prettyprint lang-js">/*
\r
10 * Portions of this file are based on pieces of Yahoo User Interface Library
\r
11 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
\r
12 * YUI licensed under the BSD License:
\r
13 * http://developer.yahoo.net/yui/license.txt
\r
15 Ext.lib.Ajax = function() {
\r
16 var activeX = ['MSXML2.XMLHTTP.3.0',
\r
18 'Microsoft.XMLHTTP'],
\r
19 CONTENTTYPE = 'Content-Type';
\r
22 function setHeader(o) {
\r
26 function setTheHeaders(conn, headers){
\r
27 for (prop in headers) {
\r
28 if (headers.hasOwnProperty(prop)) {
\r
29 conn.setRequestHeader(prop, headers[prop]);
\r
34 if (pub.defaultHeaders) {
\r
35 setTheHeaders(conn, pub.defaultHeaders);
\r
39 setTheHeaders(conn, pub.headers);
\r
40 delete pub.headers;
\r
45 function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
\r
48 status : isAbort ? -1 : 0,
\r
49 statusText : isAbort ? 'transaction aborted' : 'communication failure',
\r
51 isTimeout: isTimeout,
\r
52 argument : callbackArg
\r
57 function initHeader(label, value) {
\r
58 (pub.headers = pub.headers || {})[label] = value;
\r
62 function createResponseObject(o, callbackArg) {
\r
70 headerStr = o.conn.getAllResponseHeaders();
\r
71 Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
\r
74 s = v.substr(0, t).toLowerCase();
\r
75 if(v.charAt(t + 1) == ' '){
\r
78 headerObj[s] = v.substr(t + 1);
\r
85 status : conn.status,
\r
86 statusText : conn.statusText,
\r
87 getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
\r
88 getAllResponseHeaders : function(){return headerStr},
\r
89 responseText : conn.responseText,
\r
90 responseXML : conn.responseXML,
\r
91 argument : callbackArg
\r
96 function releaseObject(o) {
\r
102 function handleTransactionResponse(o, callback, isAbort, isTimeout) {
\r
108 var httpStatus, responseObject;
\r
111 if (o.conn.status !== undefined && o.conn.status != 0) {
\r
112 httpStatus = o.conn.status;
\r
115 httpStatus = 13030;
\r
119 httpStatus = 13030;
\r
122 if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
\r
123 responseObject = createResponseObject(o, callback.argument);
\r
124 if (callback.success) {
\r
125 if (!callback.scope) {
\r
126 callback.success(responseObject);
\r
129 callback.success.apply(callback.scope, [responseObject]);
\r
134 switch (httpStatus) {
\r
141 responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
\r
142 if (callback.failure) {
\r
143 if (!callback.scope) {
\r
144 callback.failure(responseObject);
\r
147 callback.failure.apply(callback.scope, [responseObject]);
\r
152 responseObject = createResponseObject(o, callback.argument);
\r
153 if (callback.failure) {
\r
154 if (!callback.scope) {
\r
155 callback.failure(responseObject);
\r
158 callback.failure.apply(callback.scope, [responseObject]);
\r
165 responseObject = null;
\r
169 function handleReadyState(o, callback){
\r
170 callback = callback || {};
\r
174 cbTimeout = callback.timeout || null;
\r
177 pub.timeout[tId] = setTimeout(function() {
\r
178 pub.abort(o, callback, true);
\r
182 poll[tId] = setInterval(
\r
184 if (conn && conn.readyState == 4) {
\r
185 clearInterval(poll[tId]);
\r
189 clearTimeout(pub.timeout[tId]);
\r
190 pub.timeout[tId] = null;
\r
193 handleTransactionResponse(o, callback);
\r
200 function asyncRequest(method, uri, callback, postData) {
\r
201 var o = getConnectionObject() || null;
\r
204 o.conn.open(method, uri, true);
\r
206 if (pub.useDefaultXhrHeader) {
\r
207 initHeader('X-Requested-With', pub.defaultXhrHeader);
\r
210 if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
\r
211 initHeader(CONTENTTYPE, pub.defaultPostHeader);
\r
214 if (pub.defaultHeaders || pub.headers) {
\r
218 handleReadyState(o, callback);
\r
219 o.conn.send(postData || null);
\r
225 function getConnectionObject() {
\r
229 if (o = createXhrObject(pub.transactionId)) {
\r
230 pub.transactionId++;
\r
239 function createXhrObject(transactionId) {
\r
243 http = new XMLHttpRequest();
\r
245 for (var i = 0; i < activeX.length; ++i) {
\r
247 http = new ActiveXObject(activeX[i]);
\r
252 return {conn : http, tId : transactionId};
\r
257 request : function(method, uri, cb, data, options) {
\r
260 xmlData = options.xmlData,
\r
261 jsonData = options.jsonData,
\r
264 Ext.applyIf(me, options);
\r
266 if(xmlData || jsonData){
\r
268 if(!hs || !hs[CONTENTTYPE]){
\r
269 initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
\r
271 data = xmlData || (!Ext.isPrimitive(jsonData) ? Ext.encode(jsonData) : jsonData);
\r
274 return asyncRequest(method || options.method || "POST", uri, cb, data);
\r
277 serializeForm : function(form) {
\r
278 var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
\r
280 encoder = encodeURIComponent,
\r
288 Ext.each(fElements, function(element) {
\r
289 name = element.name;
\r
290 type = element.type;
\r
292 if (!element.disabled && name){
\r
293 if(/select-(one|multiple)/i.test(type)) {
\r
294 Ext.each(element.options, function(opt) {
\r
295 if (opt.selected) {
\r
296 data += String.format("{0}={1}&", encoder(name), encoder((opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttribute('value') !== null) ? opt.value : opt.text));
\r
299 } else if(!/file|undefined|reset|button/i.test(type)) {
\r
300 if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
\r
302 data += encoder(name) + '=' + encoder(element.value) + '&';
\r
303 hasSubmit = /submit/i.test(type);
\r
308 return data.substr(0, data.length - 1);
\r
311 useDefaultHeader : true,
\r
312 defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
\r
313 useDefaultXhrHeader : true,
\r
314 defaultXhrHeader : 'XMLHttpRequest',
\r
320 // This is never called - Is it worth exposing this?
\r
321 // setProgId : function(id) {
\r
322 // activeX.unshift(id);
\r
325 // This is never called - Is it worth exposing this?
\r
326 // setDefaultPostHeader : function(b) {
\r
327 // this.useDefaultHeader = b;
\r
330 // This is never called - Is it worth exposing this?
\r
331 // setDefaultXhrHeader : function(b) {
\r
332 // this.useDefaultXhrHeader = b;
\r
335 // This is never called - Is it worth exposing this?
\r
336 // setPollingInterval : function(i) {
\r
337 // if (typeof i == 'number' && isFinite(i)) {
\r
338 // this.pollInterval = i;
\r
342 // This is never called - Is it worth exposing this?
\r
343 // resetDefaultHeaders : function() {
\r
344 // this.defaultHeaders = null;
\r
347 abort : function(o, callback, isTimeout) {
\r
352 if (me.isCallInProgress(o)) {
\r
354 clearInterval(me.poll[tId]);
\r
355 me.poll[tId] = null;
\r
356 clearTimeout(pub.timeout[tId]);
\r
357 me.timeout[tId] = null;
\r
359 handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
\r
364 isCallInProgress : function(o) {
\r
365 // if there is a connection and readyState is not 0 or 4
\r
366 return o.conn && !{0:true,4:true}[o.conn.readyState];
\r