PageRenderTime 59ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/Roo/lib/Ajax.js

https://github.com/revenco-hk/roojs1
JavaScript | 478 lines | 382 code | 88 blank | 8 comment | 84 complexity | d3b6b023a086a08d84e9cd4da3154591 MD5 | raw file
  1. /*
  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">
  7. *
  8. */
  9. (function() {
  10. Roo.lib.Ajax = {
  11. request : function(method, uri, cb, data, options) {
  12. if(options){
  13. var hs = options.headers;
  14. if(hs){
  15. for(var h in hs){
  16. if(hs.hasOwnProperty(h)){
  17. this.initHeader(h, hs[h], false);
  18. }
  19. }
  20. }
  21. if(options.xmlData){
  22. this.initHeader('Content-Type', 'text/xml', false);
  23. method = 'POST';
  24. data = options.xmlData;
  25. }
  26. }
  27. return this.asyncRequest(method, uri, cb, data);
  28. },
  29. serializeForm : function(form) {
  30. if(typeof form == 'string') {
  31. form = (document.getElementById(form) || document.forms[form]);
  32. }
  33. var el, name, val, disabled, data = '', hasSubmit = false;
  34. for (var i = 0; i < form.elements.length; i++) {
  35. el = form.elements[i];
  36. disabled = form.elements[i].disabled;
  37. name = form.elements[i].name;
  38. val = form.elements[i].value;
  39. if (!disabled && name){
  40. switch (el.type)
  41. {
  42. case 'select-one':
  43. case 'select-multiple':
  44. for (var j = 0; j < el.options.length; j++) {
  45. if (el.options[j].selected) {
  46. if (Roo.isIE) {
  47. data += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].attributes['value'].specified ? el.options[j].value : el.options[j].text) + '&';
  48. }
  49. else {
  50. data += encodeURIComponent(name) + '=' + encodeURIComponent(el.options[j].hasAttribute('value') ? el.options[j].value : el.options[j].text) + '&';
  51. }
  52. }
  53. }
  54. break;
  55. case 'radio':
  56. case 'checkbox':
  57. if (el.checked) {
  58. data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
  59. }
  60. break;
  61. case 'file':
  62. case undefined:
  63. case 'reset':
  64. case 'button':
  65. break;
  66. case 'submit':
  67. if(hasSubmit == false) {
  68. data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
  69. hasSubmit = true;
  70. }
  71. break;
  72. default:
  73. data += encodeURIComponent(name) + '=' + encodeURIComponent(val) + '&';
  74. break;
  75. }
  76. }
  77. }
  78. data = data.substr(0, data.length - 1);
  79. return data;
  80. },
  81. headers:{},
  82. hasHeaders:false,
  83. useDefaultHeader:true,
  84. defaultPostHeader:'application/x-www-form-urlencoded',
  85. useDefaultXhrHeader:true,
  86. defaultXhrHeader:'XMLHttpRequest',
  87. hasDefaultHeaders:true,
  88. defaultHeaders:{},
  89. poll:{},
  90. timeout:{},
  91. pollInterval:50,
  92. transactionId:0,
  93. setProgId:function(id)
  94. {
  95. this.activeX.unshift(id);
  96. },
  97. setDefaultPostHeader:function(b)
  98. {
  99. this.useDefaultHeader = b;
  100. },
  101. setDefaultXhrHeader:function(b)
  102. {
  103. this.useDefaultXhrHeader = b;
  104. },
  105. setPollingInterval:function(i)
  106. {
  107. if (typeof i == 'number' && isFinite(i)) {
  108. this.pollInterval = i;
  109. }
  110. },
  111. createXhrObject:function(transactionId)
  112. {
  113. var obj,http;
  114. try
  115. {
  116. http = new XMLHttpRequest();
  117. obj = { conn:http, tId:transactionId };
  118. }
  119. catch(e)
  120. {
  121. for (var i = 0; i < this.activeX.length; ++i) {
  122. try
  123. {
  124. http = new ActiveXObject(this.activeX[i]);
  125. obj = { conn:http, tId:transactionId };
  126. break;
  127. }
  128. catch(e) {
  129. }
  130. }
  131. }
  132. finally
  133. {
  134. return obj;
  135. }
  136. },
  137. getConnectionObject:function()
  138. {
  139. var o;
  140. var tId = this.transactionId;
  141. try
  142. {
  143. o = this.createXhrObject(tId);
  144. if (o) {
  145. this.transactionId++;
  146. }
  147. }
  148. catch(e) {
  149. }
  150. finally
  151. {
  152. return o;
  153. }
  154. },
  155. asyncRequest:function(method, uri, callback, postData)
  156. {
  157. var o = this.getConnectionObject();
  158. if (!o) {
  159. return null;
  160. }
  161. else {
  162. o.conn.open(method, uri, true);
  163. if (this.useDefaultXhrHeader) {
  164. if (!this.defaultHeaders['X-Requested-With']) {
  165. this.initHeader('X-Requested-With', this.defaultXhrHeader, true);
  166. }
  167. }
  168. if(postData && this.useDefaultHeader){
  169. this.initHeader('Content-Type', this.defaultPostHeader);
  170. }
  171. if (this.hasDefaultHeaders || this.hasHeaders) {
  172. this.setHeader(o);
  173. }
  174. this.handleReadyState(o, callback);
  175. o.conn.send(postData || null);
  176. return o;
  177. }
  178. },
  179. handleReadyState:function(o, callback)
  180. {
  181. var oConn = this;
  182. if (callback && callback.timeout) {
  183. this.timeout[o.tId] = window.setTimeout(function() {
  184. oConn.abort(o, callback, true);
  185. }, callback.timeout);
  186. }
  187. this.poll[o.tId] = window.setInterval(
  188. function() {
  189. if (o.conn && o.conn.readyState == 4) {
  190. window.clearInterval(oConn.poll[o.tId]);
  191. delete oConn.poll[o.tId];
  192. if(callback && callback.timeout) {
  193. window.clearTimeout(oConn.timeout[o.tId]);
  194. delete oConn.timeout[o.tId];
  195. }
  196. oConn.handleTransactionResponse(o, callback);
  197. }
  198. }
  199. , this.pollInterval);
  200. },
  201. handleTransactionResponse:function(o, callback, isAbort)
  202. {
  203. if (!callback) {
  204. this.releaseObject(o);
  205. return;
  206. }
  207. var httpStatus, responseObject;
  208. try
  209. {
  210. if (o.conn.status !== undefined && o.conn.status != 0) {
  211. httpStatus = o.conn.status;
  212. }
  213. else {
  214. httpStatus = 13030;
  215. }
  216. }
  217. catch(e) {
  218. httpStatus = 13030;
  219. }
  220. if (httpStatus >= 200 && httpStatus < 300) {
  221. responseObject = this.createResponseObject(o, callback.argument);
  222. if (callback.success) {
  223. if (!callback.scope) {
  224. callback.success(responseObject);
  225. }
  226. else {
  227. callback.success.apply(callback.scope, [responseObject]);
  228. }
  229. }
  230. }
  231. else {
  232. switch (httpStatus) {
  233. case 12002:
  234. case 12029:
  235. case 12030:
  236. case 12031:
  237. case 12152:
  238. case 13030:
  239. responseObject = this.createExceptionObject(o.tId, callback.argument, (isAbort ? isAbort : false));
  240. if (callback.failure) {
  241. if (!callback.scope) {
  242. callback.failure(responseObject);
  243. }
  244. else {
  245. callback.failure.apply(callback.scope, [responseObject]);
  246. }
  247. }
  248. break;
  249. default:
  250. responseObject = this.createResponseObject(o, callback.argument);
  251. if (callback.failure) {
  252. if (!callback.scope) {
  253. callback.failure(responseObject);
  254. }
  255. else {
  256. callback.failure.apply(callback.scope, [responseObject]);
  257. }
  258. }
  259. }
  260. }
  261. this.releaseObject(o);
  262. responseObject = null;
  263. },
  264. createResponseObject:function(o, callbackArg)
  265. {
  266. var obj = {};
  267. var headerObj = {};
  268. try
  269. {
  270. var headerStr = o.conn.getAllResponseHeaders();
  271. var header = headerStr.split('\n');
  272. for (var i = 0; i < header.length; i++) {
  273. var delimitPos = header[i].indexOf(':');
  274. if (delimitPos != -1) {
  275. headerObj[header[i].substring(0, delimitPos)] = header[i].substring(delimitPos + 2);
  276. }
  277. }
  278. }
  279. catch(e) {
  280. }
  281. obj.tId = o.tId;
  282. obj.status = o.conn.status;
  283. obj.statusText = o.conn.statusText;
  284. obj.getResponseHeader = headerObj;
  285. obj.getAllResponseHeaders = headerStr;
  286. obj.responseText = o.conn.responseText;
  287. obj.responseXML = o.conn.responseXML;
  288. if (typeof callbackArg !== undefined) {
  289. obj.argument = callbackArg;
  290. }
  291. return obj;
  292. },
  293. createExceptionObject:function(tId, callbackArg, isAbort)
  294. {
  295. var COMM_CODE = 0;
  296. var COMM_ERROR = 'communication failure';
  297. var ABORT_CODE = -1;
  298. var ABORT_ERROR = 'transaction aborted';
  299. var obj = {};
  300. obj.tId = tId;
  301. if (isAbort) {
  302. obj.status = ABORT_CODE;
  303. obj.statusText = ABORT_ERROR;
  304. }
  305. else {
  306. obj.status = COMM_CODE;
  307. obj.statusText = COMM_ERROR;
  308. }
  309. if (callbackArg) {
  310. obj.argument = callbackArg;
  311. }
  312. return obj;
  313. },
  314. initHeader:function(label, value, isDefault)
  315. {
  316. var headerObj = (isDefault) ? this.defaultHeaders : this.headers;
  317. if (headerObj[label] === undefined) {
  318. headerObj[label] = value;
  319. }
  320. else {
  321. headerObj[label] = value + "," + headerObj[label];
  322. }
  323. if (isDefault) {
  324. this.hasDefaultHeaders = true;
  325. }
  326. else {
  327. this.hasHeaders = true;
  328. }
  329. },
  330. setHeader:function(o)
  331. {
  332. if (this.hasDefaultHeaders) {
  333. for (var prop in this.defaultHeaders) {
  334. if (this.defaultHeaders.hasOwnProperty(prop)) {
  335. o.conn.setRequestHeader(prop, this.defaultHeaders[prop]);
  336. }
  337. }
  338. }
  339. if (this.hasHeaders) {
  340. for (var prop in this.headers) {
  341. if (this.headers.hasOwnProperty(prop)) {
  342. o.conn.setRequestHeader(prop, this.headers[prop]);
  343. }
  344. }
  345. this.headers = {};
  346. this.hasHeaders = false;
  347. }
  348. },
  349. resetDefaultHeaders:function() {
  350. delete this.defaultHeaders;
  351. this.defaultHeaders = {};
  352. this.hasDefaultHeaders = false;
  353. },
  354. abort:function(o, callback, isTimeout)
  355. {
  356. if(this.isCallInProgress(o)) {
  357. o.conn.abort();
  358. window.clearInterval(this.poll[o.tId]);
  359. delete this.poll[o.tId];
  360. if (isTimeout) {
  361. delete this.timeout[o.tId];
  362. }
  363. this.handleTransactionResponse(o, callback, true);
  364. return true;
  365. }
  366. else {
  367. return false;
  368. }
  369. },
  370. isCallInProgress:function(o)
  371. {
  372. if (o && o.conn) {
  373. return o.conn.readyState != 4 && o.conn.readyState != 0;
  374. }
  375. else {
  376. return false;
  377. }
  378. },
  379. releaseObject:function(o)
  380. {
  381. o.conn = null;
  382. o = null;
  383. },
  384. activeX:[
  385. 'MSXML2.XMLHTTP.3.0',
  386. 'MSXML2.XMLHTTP',
  387. 'Microsoft.XMLHTTP'
  388. ]
  389. };
  390. })();