PageRenderTime 60ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/ajax/libs/reqwest/0.2.8/reqwest.js

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