3 changed files with 2089 additions and 0 deletions
@ -0,0 +1,130 @@
@@ -0,0 +1,130 @@
|
||||
/* |
||||
|
||||
DOMLoader.XMLHttp |
||||
-------------------------- |
||||
DOMLoader.sendRequest({ |
||||
url: "./dir/something.extension", |
||||
data: "test!", |
||||
onerror: function(event) { |
||||
console.log(event); |
||||
}, |
||||
onload: function(response) { |
||||
console.log(response.responseText); |
||||
},
|
||||
onprogress: function (event) { |
||||
var percent = event.loaded / event.total * 100 >> 0; |
||||
loader.message("loading: " + percent + "%"); |
||||
} |
||||
}); |
||||
|
||||
*/ |
||||
|
||||
if (typeof(DOMLoader) === "undefined") var DOMLoader = {}; |
||||
|
||||
// Add XMLHttpRequest when not available
|
||||
|
||||
if (typeof (XMLHttpRequest) === "undefined") { |
||||
var XMLHttpRequest; |
||||
(function () { // find equivalent for IE
|
||||
var factories = [ |
||||
function () { |
||||
return new ActiveXObject("Msxml2.XMLHTTP") |
||||
}, function () { |
||||
return new ActiveXObject("Msxml3.XMLHTTP") |
||||
}, function () { |
||||
return new ActiveXObject("Microsoft.XMLHTTP") |
||||
}]; |
||||
for (var i = 0; i < factories.length; i++) { |
||||
try { |
||||
factories[i](); |
||||
} catch (e) { |
||||
continue; |
||||
} |
||||
break; |
||||
} |
||||
XMLHttpRequest = factories[i]; |
||||
})(); |
||||
} |
||||
|
||||
if (typeof ((new XMLHttpRequest()).responseText) === "undefined") { |
||||
// http://stackoverflow.com/questions/1919972/how-do-i-access-xhr-responsebody-for-binary-data-from-javascript-in-ie
|
||||
var IEBinaryToArray_ByteStr_Script = |
||||
"<!-- IEBinaryToArray_ByteStr -->\r\n"+ |
||||
"<script type='text/vbscript'>\r\n"+ |
||||
"Function IEBinaryToArray_ByteStr(Binary)\r\n"+ |
||||
" IEBinaryToArray_ByteStr = CStr(Binary)\r\n"+ |
||||
"End Function\r\n"+ |
||||
"Function IEBinaryToArray_ByteStr_Last(Binary)\r\n"+ |
||||
" Dim lastIndex\r\n"+ |
||||
" lastIndex = LenB(Binary)\r\n"+ |
||||
" if lastIndex mod 2 Then\r\n"+ |
||||
" IEBinaryToArray_ByteStr_Last = Chr( AscB( MidB( Binary, lastIndex, 1 ) ) )\r\n"+ |
||||
" Else\r\n"+ |
||||
" IEBinaryToArray_ByteStr_Last = "+'""'+"\r\n"+ |
||||
" End If\r\n"+ |
||||
"End Function\r\n"+ |
||||
"</script>\r\n"; |
||||
|
||||
// inject VBScript
|
||||
document.write(IEBinaryToArray_ByteStr_Script); |
||||
|
||||
DOMLoader.sendRequest = function(conf) { |
||||
// helper to convert from responseBody to a "responseText" like thing
|
||||
function getResponseText(binary) { |
||||
var byteMapping = {}; |
||||
for (var i = 0; i < 256; i++) { |
||||
for (var j = 0; j < 256; j++) { |
||||
byteMapping[String.fromCharCode(i + j * 256)] = String.fromCharCode(i) + String.fromCharCode(j); |
||||
} |
||||
} |
||||
// call into VBScript utility fns
|
||||
var rawBytes = IEBinaryToArray_ByteStr(binary); |
||||
var lastChr = IEBinaryToArray_ByteStr_Last(binary); |
||||
return rawBytes.replace(/[\s\S]/g, function (match) { |
||||
return byteMapping[match]; |
||||
}) + lastChr; |
||||
}; |
||||
//
|
||||
var req = XMLHttpRequest(); |
||||
req.open("GET", conf.url, true); |
||||
if (conf.responseType) req.responseType = conf.responseType; |
||||
if (conf.onerror) req.onerror = conf.onerror; |
||||
if (conf.onprogress) req.onprogress = conf.onprogress; |
||||
req.onreadystatechange = function (event) { |
||||
if (req.readyState === 4) { |
||||
if (req.status === 200) { |
||||
req.responseText = getResponseText(req.responseBody); |
||||
} else { |
||||
req = false; |
||||
} |
||||
if (conf.onload) conf.onload(req); |
||||
} |
||||
}; |
||||
req.setRequestHeader("Accept-Charset", "x-user-defined"); |
||||
req.send(null); |
||||
return req; |
||||
} |
||||
} else { |
||||
DOMLoader.sendRequest = function(conf) { |
||||
var req = new XMLHttpRequest(); |
||||
req.open(conf.data ? "POST" : "GET", conf.url, true); |
||||
if (req.overrideMimeType) req.overrideMimeType("text/plain; charset=x-user-defined"); |
||||
if (conf.data) req.setRequestHeader('Content-type','application/x-www-form-urlencoded'); |
||||
if (conf.responseType) req.responseType = conf.responseType; |
||||
if (conf.onerror) req.onerror = conf.onerror; |
||||
if (conf.onprogress) req.onprogress = conf.onprogress; |
||||
req.onreadystatechange = function (event) { |
||||
if (req.readyState === 4) { |
||||
if (req.status !== 200 && req.status != 304) { |
||||
if (conf.onerror) conf.onerror(event, false); |
||||
return; |
||||
} |
||||
if (conf.onload) { |
||||
conf.onload(req); |
||||
} |
||||
} |
||||
}; |
||||
req.send(conf.data); |
||||
return req; |
||||
}; |
||||
} |
||||
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
/* |
||||
var queue = new Queue({ |
||||
items: list, |
||||
oncomplete: function() { |
||||
queue.reset(); // infinite loop!
|
||||
queue.next(); |
||||
}, |
||||
next: function(item) { |
||||
if (item[0] !== "." && item.indexOf(".") === -1) { |
||||
readDir(dir + item + "/", queue.next); |
||||
} else { |
||||
setTimeout(queue.next, 1) |
||||
} |
||||
} |
||||
}); |
||||
*/ |
||||
|
||||
if (typeof(window) === "undefined") window = {}; |
||||
|
||||
window.Queue = function(conf) { |
||||
var that = this; |
||||
/// Request the next item in stack.
|
||||
this.next = function() { |
||||
var arr = that.queue; |
||||
/// Emit the progress of the queue.
|
||||
if (conf.onprogress) { |
||||
conf.onprogress(that.length ? 1 - that.remaining / that.length : 1); |
||||
} |
||||
/// Check whether the queue is complete.
|
||||
if (!arr.length) { |
||||
if (conf.oncomplete) { |
||||
conf.oncomplete(); |
||||
} |
||||
return; |
||||
} |
||||
/// Indicate previous element as processed.
|
||||
that.remaining --; |
||||
/// Cleanup previous completed dimension.
|
||||
|
||||
if (String(arr[0]) === "[object Object]" && !arr[0].length) { |
||||
arr.shift(); |
||||
} |
||||
/// Process next item in multi-dimensional stack.
|
||||
if (String(arr[0]) === "[object Object]" && arr[0].length) { |
||||
conf.next(arr[0].shift()); |
||||
} else { // ditto for single-dimensional stack.
|
||||
conf.next(arr.shift()); |
||||
} |
||||
}; |
||||
///
|
||||
this.reset = function(items) { |
||||
items = items || conf.items; |
||||
this.length = 0; |
||||
this.remaining = -1; |
||||
this.queue = []; |
||||
/// Flatten multi-dimensional objects.
|
||||
for (var key in items) { |
||||
if (String(items[key]) === "[object Object]") { |
||||
var sub = []; |
||||
this.queue.push(sub); |
||||
for (var id in items[key]) { |
||||
sub.push(items[key][id]); |
||||
this.length ++; |
||||
this.remaining ++; |
||||
} |
||||
} else { |
||||
this.queue.push(items[key]); |
||||
this.length ++; |
||||
this.remaining ++; |
||||
} |
||||
} |
||||
}; |
||||
///
|
||||
this.reset(); |
||||
/// Escape event loop.
|
||||
setTimeout(this.next, 1); |
||||
///
|
||||
return this; |
||||
}; |
||||
|
||||
/// For NodeJS
|
||||
if (typeof (module) !== "undefined" && module.exports) { |
||||
module.exports = window.Queue; |
||||
} |
||||
Loading…
Reference in new issue