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