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) {
231 this.timeout[o.tId] = window.setTimeout(function() {
232 oConn.abort(o, callback, true);
233 }, callback.timeout);
236 this.poll[o.tId] = window.setInterval(
238 if (o.conn && o.conn.readyState == 4) {
239 window.clearInterval(oConn.poll[o.tId]);
240 delete oConn.poll[o.tId];
242 if(callback && callback.timeout) {
243 window.clearTimeout(oConn.timeout[o.tId]);
244 delete oConn.timeout[o.tId];
247 oConn.handleTransactionResponse(o, callback);
250 , this.pollInterval);
253 handleTransactionResponse:function(o, callback, isAbort)
257 this.releaseObject(o);
261 var httpStatus, responseObject;
265 if (o.conn.status !== undefined && o.conn.status != 0) {
266 httpStatus = o.conn.status;
278 if (httpStatus >= 200 && httpStatus < 300) {
279 responseObject = this.createResponseObject(o, callback.argument);
280 if (callback.success) {
281 if (!callback.scope) {
282 callback.success(responseObject);
287 callback.success.apply(callback.scope, [responseObject]);
292 switch (httpStatus) {
300 responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
301 if (callback.failure) {
302 if (!callback.scope) {
303 callback.failure(responseObject);
306 callback.failure.apply(callback.scope, [responseObject]);
311 responseObject = this.createResponseObject(o, callback.argument);
312 if (callback.failure) {
313 if (!callback.scope) {
314 callback.failure(responseObject);
317 callback.failure.apply(callback.scope, [responseObject]);
323 this.releaseObject(o);
324 responseObject = null;
327 createResponseObject:function(o, callbackArg)
334 var headerStr = o.conn.getAllResponseHeaders();
335 var header = headerStr.split('\n');
336 for (var i = 0; i < header.length; i++) {
337 var delimitPos = header[i].indexOf(':');
338 if (delimitPos != -1) {
339 headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
347 obj.status = o.conn.status;
348 obj.statusText = o.conn.statusText;
349 obj.getResponseHeader = headerObj;
350 obj.getAllResponseHeaders = headerStr;
351 obj.responseText = o.conn.responseText;
352 obj.responseXML = o.conn.responseXML;
354 if (typeof callbackArg !== undefined) {
355 obj.argument = callbackArg;
361 createExceptionObject:function(tId, callbackArg, isAbort)
364 var COMM_ERROR = 'communication failure';
366 var ABORT_ERROR = 'transaction aborted';
372 obj.status = ABORT_CODE;
373 obj.statusText = ABORT_ERROR;
376 obj.status = COMM_CODE;
377 obj.statusText = COMM_ERROR;
381 obj.argument = callbackArg;
387 initHeader:function(label, value, isDefault)
389 var headerObj = (isDefault) ? this.defaultHeaders : this.headers;
391 if (headerObj[label] === undefined) {
392 headerObj[label] = value;
397 headerObj[label] = value + "," + headerObj[label];
401 this.hasDefaultHeaders = true;
404 this.hasHeaders = true;
409 setHeader:function(o)
411 if (this.hasDefaultHeaders) {
412 for (var prop in this.defaultHeaders) {
413 if (this.defaultHeaders.hasOwnProperty(prop)) {
414 o.conn.setRequestHeader(prop, this.defaultHeaders[prop]);
419 if (this.hasHeaders) {
420 for (var prop in this.headers) {
421 if (this.headers.hasOwnProperty(prop)) {
422 o.conn.setRequestHeader(prop, this.headers[prop]);
426 this.hasHeaders = false;
430 resetDefaultHeaders:function() {
431 delete this.defaultHeaders;
432 this.defaultHeaders = {};
433 this.hasDefaultHeaders = false;
436 abort:function(o, callback, isTimeout)
438 if(this.isCallInProgress(o)) {
440 window.clearInterval(this.poll[o.tId]);
441 delete this.poll[o.tId];
443 delete this.timeout[o.tId];
446 this.handleTransactionResponse(o, callback, true);
456 isCallInProgress:function(o)
459 return o.conn.readyState != 4 && o.conn.readyState != 0;
468 releaseObject:function(o)
477 'MSXML2.XMLHTTP.3.0',