mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-03 14:59:18 +02:00
Added error handles, sections, updated url resolving, use book.request for requests
This commit is contained in:
parent
c6465177c5
commit
c29e5f84ee
19 changed files with 3049 additions and 683 deletions
|
@ -1,87 +1,42 @@
|
|||
EPUBJS.hooks = {};
|
||||
EPUBJS.Hooks = (function(){
|
||||
function hooks(){}
|
||||
|
||||
//-- Get pre-registered hooks
|
||||
hooks.prototype.getHooks = function(){
|
||||
var plugs, hooks;
|
||||
this.hooks = {};
|
||||
Array.prototype.slice.call(arguments).forEach(function(arg){
|
||||
this.hooks[arg] = [];
|
||||
}, this);
|
||||
EPUBJS.Hook = function(context){
|
||||
this.context = context || this;
|
||||
this.hooks = [];
|
||||
};
|
||||
|
||||
for (var plugType in this.hooks) {
|
||||
hooks = EPUBJS.hooks[plugType];
|
||||
if(hooks){
|
||||
plugs = EPUBJS.core.values();
|
||||
|
||||
plugs.forEach(function(hook){
|
||||
this.registerHook(plugType, hook);
|
||||
}, this);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//-- Hooks allow for injecting async functions that must all complete before continuing
|
||||
// Functions must have a callback as their first argument.
|
||||
hooks.prototype.registerHook = function(type, toAdd, toFront){
|
||||
|
||||
if(typeof(this.hooks[type]) != "undefined"){
|
||||
|
||||
if(typeof(toAdd) === "function"){
|
||||
if(toFront) {
|
||||
this.hooks[type].unshift(toAdd);
|
||||
}else{
|
||||
this.hooks[type].push(toAdd);
|
||||
}
|
||||
}else if(Array.isArray(toAdd)){
|
||||
toAdd.forEach(function(hook){
|
||||
if(toFront) {
|
||||
this.hooks[type].unshift(hook);
|
||||
}else{
|
||||
this.hooks[type].push(hook);
|
||||
}
|
||||
}, this);
|
||||
}
|
||||
}else{
|
||||
//-- Allows for undefined hooks, but maybe this should error?
|
||||
this.hooks[type] = [func];
|
||||
}
|
||||
};
|
||||
|
||||
hooks.prototype.triggerHooks = function(type, callback, passed){
|
||||
var hooks, count;
|
||||
|
||||
if(typeof(this.hooks[type]) == "undefined") return false;
|
||||
|
||||
hooks = this.hooks[type];
|
||||
|
||||
count = hooks.length;
|
||||
if(count === 0 && callback) {
|
||||
callback();
|
||||
}
|
||||
//-- Hooks allow for injecting async functions that must all complete in order before finishing
|
||||
// Functions must return a promise.
|
||||
|
||||
function countdown(){
|
||||
count--;
|
||||
if(count <= 0 && callback) callback();
|
||||
}
|
||||
|
||||
hooks.forEach(function(hook){
|
||||
hook(countdown, passed);
|
||||
});
|
||||
};
|
||||
|
||||
return {
|
||||
register: function(name) {
|
||||
if(EPUBJS.hooks[name] === undefined) { EPUBJS.hooks[name] = {}; }
|
||||
if(typeof EPUBJS.hooks[name] !== 'object') { throw "Already registered: "+name; }
|
||||
return EPUBJS.hooks[name];
|
||||
},
|
||||
mixin: function(object) {
|
||||
for (var prop in hooks.prototype) {
|
||||
object[prop] = hooks.prototype[prop];
|
||||
}
|
||||
}
|
||||
};
|
||||
})();
|
||||
// this.beforeDisplay = new EPUBJS.Hook();
|
||||
// this.beforeDisplay.register(function(){});
|
||||
// this.beforeDisplay.trigger(args).then(function(){});
|
||||
|
||||
// Adds a function to be run before a hook completes
|
||||
EPUBJS.Hook.prototype.register = function(func){
|
||||
this.hooks.push(func);
|
||||
};
|
||||
|
||||
// Triggers a hook to run all functions
|
||||
EPUBJS.Hook.prototype.trigger = function(){
|
||||
var length = this.hooks.length;
|
||||
var current = 0;
|
||||
var executing;
|
||||
var defer = new RSVP.defer();
|
||||
var args = arguments;
|
||||
|
||||
if(length) {
|
||||
|
||||
executing = this.hooks[current].apply(this.context, args);
|
||||
executing.then(function(){
|
||||
current += 1;
|
||||
if(current < length) {
|
||||
return this.hooks[current].apply(this.context, args);
|
||||
}
|
||||
}.bind(this));
|
||||
|
||||
} else {
|
||||
executing = defer.promise;
|
||||
defer.resolve();
|
||||
}
|
||||
|
||||
return executing;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue