mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
260 lines
No EOL
5.7 KiB
JavaScript
260 lines
No EOL
5.7 KiB
JavaScript
EPUBJS.core = {};
|
|
|
|
EPUBJS.core.request = function(url, type, withCredentials) {
|
|
var supportsURL = window.URL;
|
|
var BLOB_RESPONSE = supportsURL ? "blob" : "arraybuffer";
|
|
|
|
var deferred = new RSVP.defer();
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
//-- Check from PDF.js:
|
|
// https://github.com/mozilla/pdf.js/blob/master/web/compatibility.js
|
|
var xhrPrototype = XMLHttpRequest.prototype;
|
|
|
|
if (!('overrideMimeType' in xhrPrototype)) {
|
|
// IE10 might have response, but not overrideMimeType
|
|
Object.defineProperty(xhrPrototype, 'overrideMimeType', {
|
|
value: function xmlHttpRequestOverrideMimeType(mimeType) {}
|
|
});
|
|
}
|
|
if(withCredentials) {
|
|
xhr.withCredentials = true;
|
|
}
|
|
xhr.open("GET", url, true);
|
|
xhr.onreadystatechange = handler;
|
|
|
|
if(type == 'blob'){
|
|
xhr.responseType = BLOB_RESPONSE;
|
|
}
|
|
|
|
if(type == "json") {
|
|
xhr.setRequestHeader("Accept", "application/json");
|
|
}
|
|
|
|
if(type == 'xml') {
|
|
xhr.overrideMimeType('text/xml');
|
|
}
|
|
|
|
xhr.send();
|
|
|
|
function handler() {
|
|
if (this.readyState === this.DONE) {
|
|
if (this.status === 200 || this.responseXML ) { //-- Firefox is reporting 0 for blob urls
|
|
var r;
|
|
|
|
if(type == 'xml'){
|
|
r = this.responseXML;
|
|
}else
|
|
if(type == 'json'){
|
|
r = JSON.parse(this.response);
|
|
}else
|
|
if(type == 'blob'){
|
|
|
|
if(supportsURL) {
|
|
r = this.response;
|
|
} else {
|
|
//-- Safari doesn't support responseType blob, so create a blob from arraybuffer
|
|
r = new Blob([this.response]);
|
|
}
|
|
|
|
}else{
|
|
r = this.response;
|
|
}
|
|
|
|
deferred.resolve(r);
|
|
} else {
|
|
deferred.reject({
|
|
message : this.response,
|
|
stack : new Error().stack
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
return deferred.promise;
|
|
};
|
|
|
|
//-- Parse the different parts of a url, returning a object
|
|
EPUBJS.core.uri = function(url){
|
|
var uri = {
|
|
protocol : '',
|
|
host : '',
|
|
path : '',
|
|
origin : '',
|
|
directory : '',
|
|
base : '',
|
|
filename : '',
|
|
extension : '',
|
|
fragment : '',
|
|
href : url
|
|
},
|
|
doubleSlash = url.indexOf('://'),
|
|
search = url.indexOf('?'),
|
|
fragment = url.indexOf("#"),
|
|
withoutProtocol,
|
|
dot,
|
|
firstSlash;
|
|
|
|
if(fragment != -1) {
|
|
uri.fragment = url.slice(fragment + 1);
|
|
url = url.slice(0, fragment);
|
|
}
|
|
|
|
if(search != -1) {
|
|
uri.search = url.slice(search + 1);
|
|
url = url.slice(0, search);
|
|
href = url;
|
|
}
|
|
|
|
if(doubleSlash != -1) {
|
|
uri.protocol = url.slice(0, doubleSlash);
|
|
withoutProtocol = url.slice(doubleSlash+3);
|
|
firstSlash = withoutProtocol.indexOf('/');
|
|
|
|
if(firstSlash === -1) {
|
|
uri.host = uri.path;
|
|
uri.path = "";
|
|
} else {
|
|
uri.host = withoutProtocol.slice(0, firstSlash);
|
|
uri.path = withoutProtocol.slice(firstSlash);
|
|
}
|
|
|
|
|
|
uri.origin = uri.protocol + "://" + uri.host;
|
|
|
|
uri.directory = EPUBJS.core.folder(uri.path);
|
|
|
|
uri.base = uri.origin + uri.directory;
|
|
// return origin;
|
|
} else {
|
|
uri.path = url;
|
|
uri.directory = EPUBJS.core.folder(url);
|
|
uri.base = uri.directory;
|
|
}
|
|
|
|
//-- Filename
|
|
uri.filename = url.replace(uri.base, '');
|
|
dot = uri.filename.lastIndexOf('.');
|
|
if(dot != -1) {
|
|
uri.extension = uri.filename.slice(dot+1);
|
|
}
|
|
return uri;
|
|
};
|
|
|
|
//-- Parse out the folder, will return everything before the last slash
|
|
EPUBJS.core.folder = function(url){
|
|
|
|
var lastSlash = url.lastIndexOf('/');
|
|
|
|
if(lastSlash == -1) var folder = '';
|
|
|
|
folder = url.slice(0, lastSlash + 1);
|
|
|
|
return folder;
|
|
|
|
};
|
|
|
|
|
|
EPUBJS.core.queue = function(_scope){
|
|
var _q = [];
|
|
var scope = _scope;
|
|
// Add an item to the queue
|
|
var enqueue = function(funcName, args, context) {
|
|
_q.push({
|
|
"funcName" : funcName,
|
|
"args" : args,
|
|
"context" : context
|
|
});
|
|
return _q;
|
|
};
|
|
// Run one item
|
|
var dequeue = function(){
|
|
var inwait;
|
|
if(_q.length) {
|
|
inwait = _q.shift();
|
|
// Defer to any current tasks
|
|
// setTimeout(function(){
|
|
scope[inwait.funcName].apply(inwait.context || scope, inwait.args);
|
|
// }, 0);
|
|
}
|
|
};
|
|
|
|
// Run All
|
|
var flush = function(){
|
|
while(_q.length) {
|
|
dequeue();
|
|
}
|
|
};
|
|
// Clear all items in wait
|
|
var clear = function(){
|
|
_q = [];
|
|
};
|
|
|
|
var length = function(){
|
|
return _q.length;
|
|
};
|
|
|
|
return {
|
|
"enqueue" : enqueue,
|
|
"dequeue" : dequeue,
|
|
"flush" : flush,
|
|
"clear" : clear,
|
|
"length" : length
|
|
};
|
|
};
|
|
|
|
EPUBJS.core.isElement = function(obj) {
|
|
return !!(obj && obj.nodeType == 1);
|
|
};
|
|
|
|
// http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
|
|
EPUBJS.core.uuid = function() {
|
|
var d = new Date().getTime();
|
|
var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
var r = (d + Math.random()*16)%16 | 0;
|
|
d = Math.floor(d/16);
|
|
return (c=='x' ? r : (r&0x7|0x8)).toString(16);
|
|
});
|
|
return uuid;
|
|
};
|
|
|
|
// From Lodash
|
|
EPUBJS.core.values = function(object) {
|
|
var index = -1,
|
|
props = Object.keys(object),
|
|
length = props.length,
|
|
result = Array(length);
|
|
|
|
while (++index < length) {
|
|
result[index] = object[props[index]];
|
|
}
|
|
return result;
|
|
};
|
|
|
|
EPUBJS.core.resolveUrl = function(base, path) {
|
|
var url,
|
|
segments = [],
|
|
// uri = EPUBJS.core.uri(path),
|
|
folders = base.split("/"),
|
|
paths;
|
|
|
|
// if(uri.host) {
|
|
// return path;
|
|
// }
|
|
|
|
folders.pop();
|
|
|
|
paths = path.split("/");
|
|
paths.forEach(function(p){
|
|
if(p === ".."){
|
|
folders.pop();
|
|
}else{
|
|
segments.push(p);
|
|
}
|
|
});
|
|
|
|
url = folders.concat(segments);
|
|
|
|
return url.join("/");
|
|
}; |