/hippo/src/main/webapp/ext/src/adapter/core/ext-base-ajax.js

http://hdbc.googlecode.com/ · JavaScript · 371 lines · 279 code · 47 blank · 45 comment · 72 complexity · 5ed6be75be9fa162e41c931ea423faf9 MD5 · raw file

  1. /*!
  2. * Ext JS Library 3.0.0
  3. * Copyright(c) 2006-2009 Ext JS, LLC
  4. * licensing@extjs.com
  5. * http://www.extjs.com/license
  6. */
  7. /*
  8. * Portions of this file are based on pieces of Yahoo User Interface Library
  9. * Copyright (c) 2007, Yahoo! Inc. All rights reserved.
  10. * YUI licensed under the BSD License:
  11. * http://developer.yahoo.net/yui/license.txt
  12. */
  13. Ext.lib.Ajax = function() {
  14. var activeX = ['MSXML2.XMLHTTP.3.0',
  15. 'MSXML2.XMLHTTP',
  16. 'Microsoft.XMLHTTP'],
  17. CONTENTTYPE = 'Content-Type';
  18. // private
  19. function setHeader(o) {
  20. var conn = o.conn,
  21. prop;
  22. function setTheHeaders(conn, headers){
  23. for (prop in headers) {
  24. if (headers.hasOwnProperty(prop)) {
  25. conn.setRequestHeader(prop, headers[prop]);
  26. }
  27. }
  28. }
  29. if (pub.defaultHeaders) {
  30. setTheHeaders(conn, pub.defaultHeaders);
  31. }
  32. if (pub.headers) {
  33. setTheHeaders(conn, pub.headers);
  34. pub.headers = null;
  35. }
  36. }
  37. // private
  38. function createExceptionObject(tId, callbackArg, isAbort, isTimeout) {
  39. return {
  40. tId : tId,
  41. status : isAbort ? -1 : 0,
  42. statusText : isAbort ? 'transaction aborted' : 'communication failure',
  43. isAbort: true,
  44. isTimeout: true,
  45. argument : callbackArg
  46. };
  47. }
  48. // private
  49. function initHeader(label, value) {
  50. (pub.headers = pub.headers || {})[label] = value;
  51. }
  52. // private
  53. function createResponseObject(o, callbackArg) {
  54. var headerObj = {},
  55. headerStr,
  56. conn = o.conn,
  57. t,
  58. s;
  59. try {
  60. headerStr = o.conn.getAllResponseHeaders();
  61. Ext.each(headerStr.replace(/\r\n/g, '\n').split('\n'), function(v){
  62. t = v.indexOf(':');
  63. if(t >= 0){
  64. s = v.substr(0, t).toLowerCase();
  65. if(v.charAt(t + 1) == ' '){
  66. ++t;
  67. }
  68. headerObj[s] = v.substr(t + 1);
  69. }
  70. });
  71. } catch(e) {}
  72. return {
  73. tId : o.tId,
  74. status : conn.status,
  75. statusText : conn.statusText,
  76. getResponseHeader : function(header){return headerObj[header.toLowerCase()];},
  77. getAllResponseHeaders : function(){return headerStr},
  78. responseText : conn.responseText,
  79. responseXML : conn.responseXML,
  80. argument : callbackArg
  81. };
  82. }
  83. // private
  84. function releaseObject(o) {
  85. o.conn = null;
  86. o = null;
  87. }
  88. // private
  89. function handleTransactionResponse(o, callback, isAbort, isTimeout) {
  90. if (!callback) {
  91. releaseObject(o);
  92. return;
  93. }
  94. var httpStatus, responseObject;
  95. try {
  96. if (o.conn.status !== undefined && o.conn.status != 0) {
  97. httpStatus = o.conn.status;
  98. }
  99. else {
  100. httpStatus = 13030;
  101. }
  102. }
  103. catch(e) {
  104. httpStatus = 13030;
  105. }
  106. if ((httpStatus >= 200 && httpStatus < 300) || (Ext.isIE && httpStatus == 1223)) {
  107. responseObject = createResponseObject(o, callback.argument);
  108. if (callback.success) {
  109. if (!callback.scope) {
  110. callback.success(responseObject);
  111. }
  112. else {
  113. callback.success.apply(callback.scope, [responseObject]);
  114. }
  115. }
  116. }
  117. else {
  118. switch (httpStatus) {
  119. case 12002:
  120. case 12029:
  121. case 12030:
  122. case 12031:
  123. case 12152:
  124. case 13030:
  125. responseObject = createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false), isTimeout);
  126. if (callback.failure) {
  127. if (!callback.scope) {
  128. callback.failure(responseObject);
  129. }
  130. else {
  131. callback.failure.apply(callback.scope, [responseObject]);
  132. }
  133. }
  134. break;
  135. default:
  136. responseObject = createResponseObject(o, callback.argument);
  137. if (callback.failure) {
  138. if (!callback.scope) {
  139. callback.failure(responseObject);
  140. }
  141. else {
  142. callback.failure.apply(callback.scope, [responseObject]);
  143. }
  144. }
  145. }
  146. }
  147. releaseObject(o);
  148. responseObject = null;
  149. }
  150. // private
  151. function handleReadyState(o, callback){
  152. callback = callback || {};
  153. var conn = o.conn,
  154. tId = o.tId,
  155. poll = pub.poll,
  156. cbTimeout = callback.timeout || null;
  157. if (cbTimeout) {
  158. pub.timeout[tId] = setTimeout(function() {
  159. pub.abort(o, callback, true);
  160. }, cbTimeout);
  161. }
  162. poll[tId] = setInterval(
  163. function() {
  164. if (conn && conn.readyState == 4) {
  165. clearInterval(poll[tId]);
  166. poll[tId] = null;
  167. if (cbTimeout) {
  168. clearTimeout(pub.timeout[tId]);
  169. pub.timeout[tId] = null;
  170. }
  171. handleTransactionResponse(o, callback);
  172. }
  173. },
  174. pub.pollInterval);
  175. }
  176. // private
  177. function asyncRequest(method, uri, callback, postData) {
  178. var o = getConnectionObject() || null;
  179. if (o) {
  180. o.conn.open(method, uri, true);
  181. if (pub.useDefaultXhrHeader) {
  182. initHeader('X-Requested-With', pub.defaultXhrHeader);
  183. }
  184. if(postData && pub.useDefaultHeader && (!pub.headers || !pub.headers[CONTENTTYPE])){
  185. initHeader(CONTENTTYPE, pub.defaultPostHeader);
  186. }
  187. if (pub.defaultHeaders || pub.headers) {
  188. setHeader(o);
  189. }
  190. handleReadyState(o, callback);
  191. o.conn.send(postData || null);
  192. }
  193. return o;
  194. }
  195. // private
  196. function getConnectionObject() {
  197. var o;
  198. try {
  199. if (o = createXhrObject(pub.transactionId)) {
  200. pub.transactionId++;
  201. }
  202. } catch(e) {
  203. } finally {
  204. return o;
  205. }
  206. }
  207. // private
  208. function createXhrObject(transactionId) {
  209. var http;
  210. try {
  211. http = new XMLHttpRequest();
  212. } catch(e) {
  213. for (var i = 0; i < activeX.length; ++i) {
  214. try {
  215. http = new ActiveXObject(activeX[i]);
  216. break;
  217. } catch(e) {}
  218. }
  219. } finally {
  220. return {conn : http, tId : transactionId};
  221. }
  222. }
  223. var pub = {
  224. request : function(method, uri, cb, data, options) {
  225. if(options){
  226. var me = this,
  227. xmlData = options.xmlData,
  228. jsonData = options.jsonData,
  229. hs;
  230. Ext.applyIf(me, options);
  231. if(xmlData || jsonData){
  232. hs = me.headers;
  233. if(!hs || !hs[CONTENTTYPE]){
  234. initHeader(CONTENTTYPE, xmlData ? 'text/xml' : 'application/json');
  235. }
  236. data = xmlData || (Ext.isObject(jsonData) ? Ext.encode(jsonData) : jsonData);
  237. }
  238. }
  239. return asyncRequest(method || options.method || "POST", uri, cb, data);
  240. },
  241. serializeForm : function(form) {
  242. var fElements = form.elements || (document.forms[form] || Ext.getDom(form)).elements,
  243. hasSubmit = false,
  244. encoder = encodeURIComponent,
  245. element,
  246. options,
  247. name,
  248. val,
  249. data = '',
  250. type;
  251. Ext.each(fElements, function(element) {
  252. name = element.name;
  253. type = element.type;
  254. if (!element.disabled && name){
  255. if(/select-(one|multiple)/i.test(type)){
  256. Ext.each(element.options, function(opt) {
  257. if (opt.selected) {
  258. data += String.format("{0}={1}&",
  259. encoder(name),
  260. (opt.hasAttribute ? opt.hasAttribute('value') : opt.getAttributeNode('value').specified) ? opt.value : opt.text);
  261. }
  262. });
  263. } else if(!/file|undefined|reset|button/i.test(type)) {
  264. if(!(/radio|checkbox/i.test(type) && !element.checked) && !(type == 'submit' && hasSubmit)){
  265. data += encoder(name) + '=' + encoder(element.value) + '&';
  266. hasSubmit = /submit/i.test(type);
  267. }
  268. }
  269. }
  270. });
  271. return data.substr(0, data.length - 1);
  272. },
  273. useDefaultHeader : true,
  274. defaultPostHeader : 'application/x-www-form-urlencoded; charset=UTF-8',
  275. useDefaultXhrHeader : true,
  276. defaultXhrHeader : 'XMLHttpRequest',
  277. poll : {},
  278. timeout : {},
  279. pollInterval : 50,
  280. transactionId : 0,
  281. // This is never called - Is it worth exposing this?
  282. // setProgId : function(id) {
  283. // activeX.unshift(id);
  284. // },
  285. // This is never called - Is it worth exposing this?
  286. // setDefaultPostHeader : function(b) {
  287. // this.useDefaultHeader = b;
  288. // },
  289. // This is never called - Is it worth exposing this?
  290. // setDefaultXhrHeader : function(b) {
  291. // this.useDefaultXhrHeader = b;
  292. // },
  293. // This is never called - Is it worth exposing this?
  294. // setPollingInterval : function(i) {
  295. // if (typeof i == 'number' && isFinite(i)) {
  296. // this.pollInterval = i;
  297. // }
  298. // },
  299. // This is never called - Is it worth exposing this?
  300. // resetDefaultHeaders : function() {
  301. // this.defaultHeaders = null;
  302. // },
  303. abort : function(o, callback, isTimeout) {
  304. var me = this,
  305. tId = o.tId,
  306. isAbort = false;
  307. if (me.isCallInProgress(o)) {
  308. o.conn.abort();
  309. clearInterval(me.poll[tId]);
  310. me.poll[tId] = null;
  311. if (isTimeout) {
  312. me.timeout[tId] = null;
  313. }
  314. handleTransactionResponse(o, callback, (isAbort = true), isTimeout);
  315. }
  316. return isAbort;
  317. },
  318. isCallInProgress : function(o) {
  319. // if there is a connection and readyState is not 0 or 4
  320. return o.conn && !{0:true,4:true}[o.conn.readyState];
  321. }
  322. };
  323. return pub;
  324. }();