diff --git a/.DS_Store b/.DS_Store index e1f729b..c09dc4f 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/fpjs/reader/app.js b/fpjs/reader/app.js index 1024d19..1caf007 100644 --- a/fpjs/reader/app.js +++ b/fpjs/reader/app.js @@ -38,6 +38,8 @@ FPR.app.init = (function($){ Book.listen("book:online", goOnline); Book.listen("book:offline", goOffline); + //Book.registerHook("beforeChapterDisplay", FP.Hooks.transculsions.insert); + //-- Start loading / parsing of the book. // This must be done AFTER adding listeners or hooks Book.start(bookURL); diff --git a/fpjs/render/base.js b/fpjs/render/base.js new file mode 100644 index 0000000..eeeadc7 --- /dev/null +++ b/fpjs/render/base.js @@ -0,0 +1,4 @@ +var FP = FP || {}; +FP.VERSION = "0.1.4"; + +FP.plugins = FP.plugins || {}; \ No newline at end of file diff --git a/fpjs/render/book.js b/fpjs/render/book.js index 97c3767..f779050 100644 --- a/fpjs/render/book.js +++ b/fpjs/render/book.js @@ -16,6 +16,7 @@ FP.Book = function(elem, bookPath){ this.createEvent("book:bookReady"); this.createEvent("book:chapterReady"); this.createEvent("book:chapterDisplayed"); + this.createEvent("book:chapterDestroy"); this.createEvent("book:resized"); this.createEvent("book:stored"); this.createEvent("book:online"); @@ -26,6 +27,9 @@ FP.Book = function(elem, bookPath){ "beforeChapterDisplay" : [] }; + //-- Get pre-registered hooks + this.getHooks(); + this.useHash = true; this.initialize(this.el); @@ -79,8 +83,10 @@ FP.Book.prototype.listeners = function(){ //-- Check bookUrl and start parsing book Assets or load them from storage FP.Book.prototype.start = function(bookPath){ - var pathname = window.location.pathname, - folder = (pathname[pathname.length - 1] == "/") ? pathname : "/"; + var location = window.location, + pathname = location.pathname, + folder = (pathname[pathname.length - 1] == "/") ? pathname : "/", + origin; this.bookPath = bookPath; @@ -94,10 +100,11 @@ FP.Book.prototype.start = function(bookPath){ return; }else{ this.bookUrl = (bookPath[bookPath.length - 1] == "/") ? bookPath : bookPath + "/"; - + if(this.bookUrl.search("://") == -1){ //-- get full path - this.bookUrl = window.location.origin + folder + this.bookUrl; + origin = location.origin || location.protocol + "//" + location.host; + this.bookUrl = origin + folder + this.bookUrl; } } @@ -541,7 +548,12 @@ FP.Book.prototype.displayChapter = function(pos, callback){ localStorage.setItem("spinePos", pos); this.spinePos = pos; - + + //-- Destroy previous + if(this.currentChapter) { + this.tell("book:chapterDestroy", this.currentChapter.getID()); + } + //-- Create a new chapter this.currentChapter = new FP.Chapter(this); @@ -668,6 +680,21 @@ FP.Book.prototype.hideHashChanges = function(){ this.useHash = false; } +//-- Get pre-registered hooks +FP.Book.prototype.getHooks = function(){ + var that = this; + + plugTypes = FP.core.toArray(this.hooks); + + plugTypes.forEach(function(plug){ + var type = plug.ident; + plugs = FP.core.toArray(FP.Hooks[type]); + plugs.forEach(function(hook){ + that.registerHook(type, hook); + }); + }); +} + //-- Hooks allow for injecting async functions that must all complete before continuing // Functions must have a callback as their first argument. FP.Book.prototype.registerHook = function(type, toAdd){ @@ -690,12 +717,13 @@ FP.Book.prototype.registerHook = function(type, toAdd){ } } -FP.Book.prototype.triggerHooks = function(type, callback){ +FP.Book.prototype.triggerHooks = function(type, callback, passed){ var hooks, count; if(typeof(this.hooks[type]) == "undefined") return false; hooks = this.hooks[type]; + count = hooks.length; function countdown(){ @@ -704,6 +732,6 @@ FP.Book.prototype.triggerHooks = function(type, callback){ } hooks.forEach(function(hook){ - hook(countdown); + hook(countdown, passed); }); } diff --git a/fpjs/render/chapter.js b/fpjs/render/chapter.js index 223d220..bd9a1d8 100644 --- a/fpjs/render/chapter.js +++ b/fpjs/render/chapter.js @@ -291,6 +291,6 @@ FP.Chapter.prototype.section = function(fragment){ } FP.Chapter.prototype.beforeDisplay = function(callback){ - this.book.triggerHooks("beforeChapterDisplay", callback.bind(this)); + this.book.triggerHooks("beforeChapterDisplay", callback.bind(this), this); } diff --git a/fpjs/render/events.js b/fpjs/render/events.js index 07e3ecc..f3d75f9 100644 --- a/fpjs/render/events.js +++ b/fpjs/render/events.js @@ -12,6 +12,7 @@ FP.Events = function(obj, el){ obj.tell = this.tell; obj.listen = this.listen; obj.deafen = this.deafen; + obj.listenUntil = this.listenUntil; return this; } @@ -55,4 +56,15 @@ FP.Events.prototype.listen = function(evt, func, bindto){ FP.Events.prototype.deafen = function(evt, func){ this.el.removeEventListener(evt, func, false); +} + +FP.Events.prototype.listenUntil = function(OnEvt, OffEvt, func, bindto){ + this.listen(OnEvt, func, bindto); + + function unlisten(){ + this.deafen(OnEvt, func); + this.deafen(OffEvt, unlisten); + } + + this.listen(OffEvt, unlisten); } \ No newline at end of file diff --git a/fpjs/render/hooks.js b/fpjs/render/hooks.js new file mode 100644 index 0000000..e757c24 --- /dev/null +++ b/fpjs/render/hooks.js @@ -0,0 +1,11 @@ +FP.Hooks = (function(){ + + "use strict"; + return { + register: function(name) { + if(this[name] === undefined) { this[name] = {}; } + if(typeof this[name] !== 'object') { throw "Already registered: "+name; } + return this[name]; + } + }; +})(); \ No newline at end of file diff --git a/index.html b/index.html index 1e22fc3..a5c4462 100755 --- a/index.html +++ b/index.html @@ -17,9 +17,6 @@ - - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - - - - - +