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