PageRenderTime 50ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/reqwest/0.1.4/reqwest.js

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