diff options
author | Brian Picciano <mediocregopher@gmail.com> | 2020-04-26 18:15:15 -0600 |
---|---|---|
committer | Brian Picciano <mediocregopher@gmail.com> | 2020-04-26 18:15:15 -0600 |
commit | 6c4da7fac6ea909adac1be00e238d213e84c0ec6 (patch) | |
tree | 5d1de0a1ef2aae046093fed2fe2c9c83a1f9448d /assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js | |
parent | 40ce9c4b7e6b2c04e84db88c17a5bada40084974 (diff) |
add trading in the rain post
Diffstat (limited to 'assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js')
-rw-r--r-- | assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js b/assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js new file mode 100644 index 0000000..84c208e --- /dev/null +++ b/assets/trading-in-the-rain/MIDI.js/js/util/dom_request_xhr.js @@ -0,0 +1,146 @@ +/* + ---------------------------------------------------------- + util/Request : 0.1.1 : 2015-03-26 + ---------------------------------------------------------- + util.request({ + url: './dir/something.extension', + data: 'test!', + format: 'text', // text | xml | json | binary + responseType: 'text', // arraybuffer | blob | document | json | text + headers: {}, + withCredentials: true, // true | false + /// + onerror: function(evt, percent) { + console.log(evt); + }, + onsuccess: function(evt, responseText) { + console.log(responseText); + }, + onprogress: function(evt, percent) { + percent = Math.round(percent * 100); + loader.create('thread', 'loading... ', percent); + } + }); +*/ + +if (typeof MIDI === 'undefined') MIDI = {}; + +(function(root) { + + var util = root.util || (root.util = {}); + + util.request = function(opts, onsuccess, onerror, onprogress) { 'use strict'; + if (typeof opts === 'string') opts = {url: opts}; + /// + var data = opts.data; + var url = opts.url; + var method = opts.method || (opts.data ? 'POST' : 'GET'); + var format = opts.format; + var headers = opts.headers; + var responseType = opts.responseType; + var withCredentials = opts.withCredentials || false; + /// + var onsuccess = onsuccess || opts.onsuccess; + var onerror = onerror || opts.onerror; + var onprogress = onprogress || opts.onprogress; + /// + if (typeof NodeFS !== 'undefined' && root.loc.isLocalUrl(url)) { + NodeFS.readFile(url, 'utf8', function(err, res) { + if (err) { + onerror && onerror(err); + } else { + onsuccess && onsuccess({responseText: res}); + } + }); + return; + } + /// + var xhr = new XMLHttpRequest(); + xhr.open(method, url, true); + /// + if (headers) { + for (var type in headers) { + xhr.setRequestHeader(type, headers[type]); + } + } else if (data) { // set the default headers for POST + xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); + } + if (format === 'binary') { //- default to responseType="blob" when supported + if (xhr.overrideMimeType) { + xhr.overrideMimeType('text/plain; charset=x-user-defined'); + } + } + if (responseType) { + xhr.responseType = responseType; + } + if (withCredentials) { + xhr.withCredentials = 'true'; + } + if (onerror && 'onerror' in xhr) { + xhr.onerror = onerror; + } + if (onprogress && xhr.upload && 'onprogress' in xhr.upload) { + if (data) { + xhr.upload.onprogress = function(evt) { + onprogress.call(xhr, evt, event.loaded / event.total); + }; + } else { + xhr.addEventListener('progress', function(evt) { + var totalBytes = 0; + if (evt.lengthComputable) { + totalBytes = evt.total; + } else if (xhr.totalBytes) { + totalBytes = xhr.totalBytes; + } else { + var rawBytes = parseInt(xhr.getResponseHeader('Content-Length-Raw')); + if (isFinite(rawBytes)) { + xhr.totalBytes = totalBytes = rawBytes; + } else { + return; + } + } + onprogress.call(xhr, evt, evt.loaded / totalBytes); + }); + } + } + /// + xhr.onreadystatechange = function(evt) { + if (xhr.readyState === 4) { // The request is complete + if (xhr.status === 200 || // Response OK + xhr.status === 304 || // Not Modified + xhr.status === 308 || // Permanent Redirect + xhr.status === 0 && root.client.cordova // Cordova quirk + ) { + if (onsuccess) { + var res; + if (format === 'xml') { + res = evt.target.responseXML; + } else if (format === 'text') { + res = evt.target.responseText; + } else if (format === 'json') { + try { + res = JSON.parse(evt.target.response); + } catch(err) { + onerror && onerror.call(xhr, evt); + } + } + /// + onsuccess.call(xhr, evt, res); + } + } else { + onerror && onerror.call(xhr, evt); + } + } + }; + xhr.send(data); + return xhr; + }; + + /// NodeJS + if (typeof module !== 'undefined' && module.exports) { + var NodeFS = require('fs'); + XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; + module.exports = root.util.request; + } + +})(MIDI);
\ No newline at end of file |