PageRenderTime 75ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/ajax/libs/reqwest/0.1.0/reqwest.js

https://gitlab.com/Mirros/cdnjs
JavaScript | 251 lines | 28 code | 3 blank | 220 comment | 6 complexity | 9385987ff33a69b0d87cf3a79e0a48ea MD5 | raw file
  1. /*!
  2. * Reqwest! A x-browser general purpose XHR connection manager
  3. * copyright Dustin Diaz 2011
  4. * https://github.com/ded/reqwest
  5. * license MIT
  6. */
  7. /*!
  8. * Reqwest! A x-browser general purpose XHR connection manager
  9. * copyright Dustin Diaz 2011
  10. * https://github.com/ded/reqwest
  11. * license MIT
  12. */
  13. !function (window) {
  14. var twoHundo = /^20\d$/,
  15. doc = document,
  16. byTag = 'getElementsByTagName',
  17. topScript = doc[byTag]('script')[0],
  18. head = topScript.parentNode,
  19. xhr = ('XMLHttpRequest' in window) ?
  20. function () {
  21. return new XMLHttpRequest();
  22. } :
  23. function () {
  24. return new ActiveXObject('Microsoft.XMLHTTP');
  25. };
  26. var uniqid = 0;
  27. function readyState(o, success, error) {
  28. return function () {
  29. if (o && o.readyState == 4) {
  30. if (twoHundo.test(o.status)) {
  31. success(o);
  32. } else {
  33. error(o);
  34. }
  35. }
  36. };
  37. }
  38. function setHeaders(http, options) {
  39. var headers = options.headers || {};
  40. headers.Accept = 'text/javascript, text/html, application/xml, text/xml, */*';
  41. if (options.data) {
  42. headers['Content-type'] = 'application/x-www-form-urlencoded';
  43. for (var h in headers) {
  44. headers.hasOwnProperty(h) && http.setRequestHeader(h, headers[h], false);
  45. }
  46. }
  47. }
  48. function getCallbackName(o) {
  49. var callbackVar = o.jsonpCallback || "callback";
  50. if (o.url.substr(-(callbackVar.length + 2)) == (callbackVar + "=?")) {
  51. // Generate a guaranteed unique callback name
  52. var callbackName = "reqwest_" + uniqid++;
  53. // Replace the ? in the URL with the generated name
  54. o.url = o.url.substr(0, o.url.length - 1) + callbackName;
  55. return callbackName;
  56. } else {
  57. // Find the supplied callback name
  58. var regex = new RegExp(callbackVar + "=([\\w]+)");
  59. return o.url.match(regex)[1];
  60. }
  61. }
  62. function getRequest(o, fn, err) {
  63. if (o.type == 'jsonp') {
  64. var script = doc.createElement('script');
  65. // Add the global callback
  66. var callbackName = getCallbackName(o);
  67. window[callbackName] = function (data) {
  68. // Call the success callback
  69. o.success && o.success(data);
  70. };
  71. // Setup our script element
  72. script.type = "text/javascript";
  73. script.src = o.url;
  74. script.async = true;
  75. script.onload = function () {
  76. // Script has been loaded, and thus the user callback has
  77. // been called, so lets clean up now.
  78. head.removeChild(script);
  79. delete window[callbackName];
  80. };
  81. // Add the script to the DOM head
  82. head.insertBefore(script, topScript);
  83. } else {
  84. var http = xhr();
  85. http.open(o.method || 'GET', typeof o == 'string' ? o : o.url, true);
  86. setHeaders(http, o);
  87. http.onreadystatechange = readyState(http, fn, err);
  88. o.before && o.before(http);
  89. http.send(o.data || null);
  90. return http;
  91. }
  92. }
  93. function Reqwest(o, fn) {
  94. this.o = o;
  95. this.fn = fn;
  96. init.apply(this, arguments);
  97. }
  98. function setType(url) {
  99. if (/\.json$/.test(url)) {
  100. return 'json';
  101. }
  102. if (/\.jsonp$/.test(url)) {
  103. return 'jsonp';
  104. }
  105. if (/\.js$/.test(url)) {
  106. return 'js';
  107. }
  108. if (/\.html?$/.test(url)) {
  109. return 'html';
  110. }
  111. if (/\.xml$/.test(url)) {
  112. return 'xml';
  113. }
  114. return 'js';
  115. }
  116. function init(o, fn) {
  117. this.url = typeof o == 'string' ? o : o.url;
  118. this.timeout = null;
  119. var type = o.type || setType(this.url), self = this;
  120. fn = fn || function () {};
  121. if (o.timeout) {
  122. this.timeout = setTimeout(function () {
  123. self.abort();
  124. error();
  125. }, o.timeout);
  126. }
  127. function complete(resp) {
  128. o.complete && o.complete(resp);
  129. }
  130. function success(resp) {
  131. o.timeout && clearTimeout(self.timeout) && (self.timeout = null);
  132. var r = resp.responseText;
  133. switch (type) {
  134. case 'json':
  135. resp = eval('(' + r + ')');
  136. break;
  137. case 'js':
  138. resp = eval(r);
  139. break;
  140. case 'html':
  141. resp = r;
  142. break;
  143. // default is the response from server
  144. }
  145. fn(resp);
  146. o.success && o.success(resp);
  147. complete(resp);
  148. }
  149. function error(resp) {
  150. o.error && o.error(resp);
  151. complete(resp);
  152. }
  153. this.request = getRequest(o, success, error);
  154. }
  155. Reqwest.prototype = {
  156. abort: function () {
  157. this.request.abort();
  158. },
  159. retry: function () {
  160. init.call(this, this.o, this.fn);
  161. }
  162. };
  163. function reqwest(o, fn) {
  164. return new Reqwest(o, fn);
  165. }
  166. function enc(v) {
  167. return encodeURIComponent(v);
  168. }
  169. function serial(el) {
  170. var n = el.name;
  171. // don't serialize elements that are disabled or without a name
  172. if (el.disabled || !n) {
  173. return '';
  174. }
  175. n = enc(n);
  176. switch (el.tagName.toLowerCase()) {
  177. case 'input':
  178. switch (el.type) {
  179. // silly wabbit
  180. case 'reset':
  181. case 'button':
  182. case 'image':
  183. case 'file':
  184. return '';
  185. case 'checkbox':
  186. case 'radio':
  187. return el.checked ? n + '=' + (el.value ? enc(el.value) : true) + '&' : '';
  188. default: // text hidden password submit
  189. return n + '=' + (el.value ? enc(el.value) : true) + '&';
  190. }
  191. break;
  192. case 'textarea':
  193. return n + '=' + enc(el.value) + '&';
  194. case 'select':
  195. // @todo refactor beyond basic single selected value case
  196. return n + '=' + enc(el.options[el.selectedIndex].value) + '&';
  197. }
  198. return '';
  199. }
  200. reqwest.serialize = function (form) {
  201. var inputs = form[byTag]('input'),
  202. selects = form[byTag]('select'),
  203. texts = form[byTag]('textarea');
  204. return (v(inputs).chain().toArray().map(serial).value().join('') +
  205. v(selects).chain().toArray().map(serial).value().join('') +
  206. v(texts).chain().toArray().map(serial).value().join('')).replace(/&$/, '');
  207. };
  208. reqwest.serializeArray = function (f) {
  209. for (var pairs = this.serialize(f).split('&'), i = 0, l = pairs.length, r = [], o; i < l; i++) {
  210. pairs[i] && (o = pairs[i].split('=')) && r.push({name: o[0], value: o[1]});
  211. }
  212. return r;
  213. };
  214. var old = window.reqwest;
  215. reqwest.noConflict = function () {
  216. window.reqwest = old;
  217. return this;
  218. };
  219. // defined as extern for Closure Compilation
  220. // do not change to (dot) '.' syntax
  221. window['reqwest'] = reqwest;
  222. }(this);