2 * Portions of this file are based on pieces of Yahoo User Interface Library
3 * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
4 * YUI licensed under the BSD License:
5 * http://developer.yahoo.net/yui/license.txt
6 * <script type="text/javascript">
19 request : function(method, uri, cb, data, options) {
21 var hs = options.headers;
24 if(hs.hasOwnProperty(h)){
25 this.initHeader(h, hs[h], false);
30 this.initHeader('Content-Type', 'text/xml', false);
32 data = options.xmlData;
36 return this.asyncRequest(method, uri, cb, data);
39 serializeForm : function(form) {
40 if(typeof form == 'string') {
41 form = (document.getElementById(form) || document.forms[form]);
44 var el, name, val, disabled, data = '', hasSubmit = false;
45 for (var i = 0; i < form.elements.length; i++) {
46 el = form.elements[i];
47 disabled = form.elements[i].disabled;
48 name = form.elements[i].name;
49 val = form.elements[i].value;
51 if (!disabled && name){
55 case 'select-multiple':
56 for (var j = 0; j < el.options.length; j++) {
57 if (el.options[j].selected) {
59 data += Roo.encodeURIComponent(name) + '=' + Roo.encodeURIComponent(el.options[j].attributes['value'].specified ? el.options[j].value : el.options[j].text) + '&';
62 data += Roo.encodeURIComponent(name) + '=' + Roo.encodeURIComponent(el.options[j].hasAttribute('value') ? el.options[j].value : el.options[j].text) + '&';
70 data += Roo.encodeURIComponent(name) + '=' + Roo.encodeURIComponent(val) + '&';
83 if(hasSubmit == false) {
84 data += Roo.encodeURIComponent(name) + '=' + Roo.encodeURIComponent(val) + '&';
89 data += Roo.encodeURIComponent(name) + '=' + Roo.encodeURIComponent(val) + '&';
94 data = data.substr(0, data.length - 1);
102 useDefaultHeader:true,
104 defaultPostHeader:'application/x-www-form-urlencoded',
106 useDefaultXhrHeader:true,
108 defaultXhrHeader:'XMLHttpRequest',
110 hasDefaultHeaders:true,
122 setProgId:function(id)
124 this.activeX.unshift(id);
127 setDefaultPostHeader:function(b)
129 this.useDefaultHeader = b;
132 setDefaultXhrHeader:function(b)
134 this.useDefaultXhrHeader = b;
137 setPollingInterval:function(i)
139 if (typeof i == 'number' && isFinite(i)) {
140 this.pollInterval = i;
144 createXhrObject:function(transactionId)
150 http = new XMLHttpRequest();
152 obj = { conn:http, tId:transactionId };
156 for (var i = 0; i < this.activeX.length; ++i) {
160 http = new ActiveXObject(this.activeX[i]);
162 obj = { conn:http, tId:transactionId };
175 getConnectionObject:function()
178 var tId = this.transactionId;
182 o = this.createXhrObject(tId);
184 this.transactionId++;
195 asyncRequest:function(method, uri, callback, postData)
197 var o = this.getConnectionObject();
203 o.conn.open(method, uri, true);
205 if (this.useDefaultXhrHeader) {
206 if (!this.defaultHeaders['X-Requested-With']) {
207 this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
211 if(postData && this.useDefaultHeader){
212 this.initHeader('Content-Type', this.defaultPostHeader);
215 if (this.hasDefaultHeaders || this.hasHeaders) {
219 this.handleReadyState(o, callback);
220 o.conn.send(postData || null);
226 handleReadyState:function(o, callback)
230 if (callback && callback.timeout) {
232 this.timeout[o.tId] = window.setTimeout(function() {
233 oConn.abort(o, callback, true);
234 }, callback.timeout);
237 this.poll[o.tId] = window.setInterval(
239 if (o.conn && o.conn.readyState == 4) {
240 window.clearInterval(oConn.poll[o.tId]);
241 delete oConn.poll[o.tId];
243 if(callback && callback.timeout) {
244 window.clearTimeout(oConn.timeout[o.tId]);
245 delete oConn.timeout[o.tId];
248 oConn.handleTransactionResponse(o, callback);
251 , this.pollInterval);
254 handleTransactionResponse:function(o, callback, isAbort)
258 this.releaseObject(o);
262 var httpStatus, responseObject;
266 if (o.conn.status !== undefined && o.conn.status != 0) {
267 httpStatus = o.conn.status;
279 if (httpStatus >= 200 && httpStatus < 300) {
280 responseObject = this.createResponseObject(o, callback.argument);
281 if (callback.success) {
282 if (!callback.scope) {
283 callback.success(responseObject);
288 callback.success.apply(callback.scope, [responseObject]);
293 switch (httpStatus) {
301 responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
302 if (callback.failure) {
303 if (!callback.scope) {
304 callback.failure(responseObject);
307 callback.failure.apply(callback.scope, [responseObject]);
312 responseObject = this.createResponseObject(o, callback.argument);
313 if (callback.failure) {
314 if (!callback.scope) {
315 callback.failure(responseObject);
318 callback.failure.apply(callback.scope, [responseObject]);
324 this.releaseObject(o);
325 responseObject = null;
328 createResponseObject:function(o, callbackArg)
335 var headerStr = o.conn.getAllResponseHeaders();
336 var header = headerStr.split('\n');
337 for (var i = 0; i < header.length; i++) {
338 var delimitPos = header[i].indexOf(':');
339 if (delimitPos != -1) {
340 headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
348 obj.status = o.conn.status;
349 obj.statusText = o.conn.statusText;
350 obj.getResponseHeader = headerObj;
351 obj.getAllResponseHeaders = headerStr;
352 obj.responseText = o.conn.responseText;
353 obj.responseXML = o.conn.responseXML;
355 if (typeof callbackArg !== undefined) {
356 obj.argument = callbackArg;
362 createExceptionObject:function(tId, callbackArg, isAbort)
365 var COMM_ERROR = 'communication failure';
367 var ABORT_ERROR = 'transaction aborted';
373 obj.status = ABORT_CODE;
374 obj.statusText = ABORT_ERROR;
377 obj.status = COMM_CODE;
378 obj.statusText = COMM_ERROR;
382 obj.argument = callbackArg;
388 initHeader:function(label, value, isDefault)
390 var headerObj = (isDefault) ? this.defaultHeaders : this.headers;
392 if (headerObj[label] === undefined) {
393 headerObj[label] = value;
398 headerObj[label] = value + "," + headerObj[label];
402 this.hasDefaultHeaders = true;
405 this.hasHeaders = true;
410 setHeader:function(o)
412 if (this.hasDefaultHeaders) {
413 for (var prop in this.defaultHeaders) {
414 if (this.defaultHeaders.hasOwnProperty(prop)) {
415 o.conn.setRequestHeader(prop, this.defaultHeaders[prop]);
420 if (this.hasHeaders) {
421 for (var prop in this.headers) {
422 if (this.headers.hasOwnProperty(prop)) {
423 o.conn.setRequestHeader(prop, this.headers[prop]);
427 this.hasHeaders = false;
431 resetDefaultHeaders:function() {
432 delete this.defaultHeaders;
433 this.defaultHeaders = {};
434 this.hasDefaultHeaders = false;
437 abort:function(o, callback, isTimeout)
439 if(this.isCallInProgress(o)) {
441 window.clearInterval(this.poll[o.tId]);
442 delete this.poll[o.tId];
444 delete this.timeout[o.tId];
447 this.handleTransactionResponse(o, callback, true);
457 isCallInProgress:function(o)
460 return o.conn.readyState != 4 && o.conn.readyState != 0;
469 releaseObject:function(o)
478 'MSXML2.XMLHTTP.3.0',