1/* 2 ---------------------------------------------------------- 3 util/Request : 0.1.1 : 2015-03-26 4 ---------------------------------------------------------- 5 util.request({ 6 url: './dir/something.extension', 7 data: 'test!', 8 format: 'text', // text | xml | json | binary 9 responseType: 'text', // arraybuffer | blob | document | json | text 10 headers: {}, 11 withCredentials: true, // true | false 12 /// 13 onerror: function(evt, percent) { 14 console.log(evt); 15 }, 16 onsuccess: function(evt, responseText) { 17 console.log(responseText); 18 }, 19 onprogress: function(evt, percent) { 20 percent = Math.round(percent * 100); 21 loader.create('thread', 'loading... ', percent); 22 } 23 }); 24*/ 25 26if (typeof MIDI === 'undefined') MIDI = {}; 27 28(function(root) { 29 30 var util = root.util || (root.util = {}); 31 32 util.request = function(opts, onsuccess, onerror, onprogress) { 'use strict'; 33 if (typeof opts === 'string') opts = {url: opts}; 34 /// 35 var data = opts.data; 36 var url = opts.url; 37 var method = opts.method || (opts.data ? 'POST' : 'GET'); 38 var format = opts.format; 39 var headers = opts.headers; 40 var responseType = opts.responseType; 41 var withCredentials = opts.withCredentials || false; 42 /// 43 var onsuccess = onsuccess || opts.onsuccess; 44 var onerror = onerror || opts.onerror; 45 var onprogress = onprogress || opts.onprogress; 46 /// 47 if (typeof NodeFS !== 'undefined' && root.loc.isLocalUrl(url)) { 48 NodeFS.readFile(url, 'utf8', function(err, res) { 49 if (err) { 50 onerror && onerror(err); 51 } else { 52 onsuccess && onsuccess({responseText: res}); 53 } 54 }); 55 return; 56 } 57 /// 58 var xhr = new XMLHttpRequest(); 59 xhr.open(method, url, true); 60 /// 61 if (headers) { 62 for (var type in headers) { 63 xhr.setRequestHeader(type, headers[type]); 64 } 65 } else if (data) { // set the default headers for POST 66 xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded'); 67 } 68 if (format === 'binary') { //- default to responseType="blob" when supported 69 if (xhr.overrideMimeType) { 70 xhr.overrideMimeType('text/plain; charset=x-user-defined'); 71 } 72 } 73 if (responseType) { 74 xhr.responseType = responseType; 75 } 76 if (withCredentials) { 77 xhr.withCredentials = 'true'; 78 } 79 if (onerror && 'onerror' in xhr) { 80 xhr.onerror = onerror; 81 } 82 if (onprogress && xhr.upload && 'onprogress' in xhr.upload) { 83 if (data) { 84 xhr.upload.onprogress = function(evt) { 85 onprogress.call(xhr, evt, event.loaded / event.total); 86 }; 87 } else { 88 xhr.addEventListener('progress', function(evt) { 89 var totalBytes = 0; 90 if (evt.lengthComputable) { 91 totalBytes = evt.total; 92 } else if (xhr.totalBytes) { 93 totalBytes = xhr.totalBytes; 94 } else { 95 var rawBytes = parseInt(xhr.getResponseHeader('Content-Length-Raw')); 96 if (isFinite(rawBytes)) { 97 xhr.totalBytes = totalBytes = rawBytes; 98 } else { 99 return; 100 } 101 } 102 onprogress.call(xhr, evt, evt.loaded / totalBytes); 103 }); 104 } 105 } 106 /// 107 xhr.onreadystatechange = function(evt) { 108 if (xhr.readyState === 4) { // The request is complete 109 if (xhr.status === 200 || // Response OK 110 xhr.status === 304 || // Not Modified 111 xhr.status === 308 || // Permanent Redirect 112 xhr.status === 0 && root.client.cordova // Cordova quirk 113 ) { 114 if (onsuccess) { 115 var res; 116 if (format === 'xml') { 117 res = evt.target.responseXML; 118 } else if (format === 'text') { 119 res = evt.target.responseText; 120 } else if (format === 'json') { 121 try { 122 res = JSON.parse(evt.target.response); 123 } catch(err) { 124 onerror && onerror.call(xhr, evt); 125 } 126 } 127 /// 128 onsuccess.call(xhr, evt, res); 129 } 130 } else { 131 onerror && onerror.call(xhr, evt); 132 } 133 } 134 }; 135 xhr.send(data); 136 return xhr; 137 }; 138 139 /// NodeJS 140 if (typeof module !== 'undefined' && module.exports) { 141 var NodeFS = require('fs'); 142 XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; 143 module.exports = root.util.request; 144 } 145 146})(MIDI);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.