diff --git a/dist/epub.js b/dist/epub.js index 4b23f1a..84e22bf 100644 --- a/dist/epub.js +++ b/dist/epub.js @@ -3371,19 +3371,18 @@ EPUBJS.Infinite.prototype.check = function(){ if(this.scrolled && !this.displaying) { - // var scrollTop = window.pageYOffset || document.documentElement.scrollTop - // var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft var scrollTop = this.container.scrollTop; + var scrollLeft = this.container.scrollLeft; - // var scrollTop = document.body.scrollTop;//TODO: make document.body a variable - // var scrollHeight = document.documentElement.scrollHeight; var scrollHeight = this.container.scrollHeight; + var scrollWidth = this.container.scrollWidth; + var direction = scrollTop - this.prevScrollTop; var height = this.container.getBoundingClientRect().height; - var up = height - (scrollHeight - scrollTop) > -this.offset; + var up = scrollTop + this.offset > scrollHeight-height; var down = scrollTop < this.offset; - + // console.debug("scroll", scrollTop) if(up && direction > 0) { this.forwards(); } @@ -3391,15 +3390,34 @@ EPUBJS.Infinite.prototype.check = function(){ this.backwards(); } - // console.log(document.body.scrollTop) + // console.log(scrollTop) this.prevScrollTop = scrollTop; + this.scrolled = false; + } else { + this.displaying = false; this.scrolled = false; } this.tick.call(window, this.check.bind(this)); } +EPUBJS.Infinite.prototype.scrollBy = function(x, y, silent){ + if(silent) { + this.displaying = true; + } + this.container.scrollLeft += x; + this.container.scrollTop += y; +}; + +EPUBJS.Infinite.prototype.scroll = function(x, y, silent){ + if(silent) { + this.displaying = true; + } + this.container.scrollLeft = x; + this.container.scrollTop = y; +}; + RSVP.EventTarget.mixin(EPUBJS.Infinite.prototype); EPUBJS.Layout = function(){}; @@ -4030,7 +4048,7 @@ EPUBJS.Renderer = function(book, _options) { var options = _options || {}; this.settings = { hidden: options.hidden || false, - viewLimit: 3, + viewsLimit: 4, width: options.width || false, height: options.height || false, }; @@ -4153,8 +4171,13 @@ EPUBJS.Renderer.prototype.attachTo = function(_element){ this.infinite.start(); - this.infinite.on("forwards", this.forwards.bind(this)); - this.infinite.on("backwards", this.backwards.bind(this)); + this.infinite.on("forwards", function(){ + if(!this.rendering) this.forwards(); + }.bind(this)); + + this.infinite.on("backwards", function(){ + if(!this.rendering) this.backwards(); + }.bind(this)); window.addEventListener("resize", this.onResized.bind(this), false); @@ -4179,10 +4202,11 @@ EPUBJS.Renderer.prototype.display = function(what){ var section = this.book.spine.get(what); var rendered = this.render(section); - rendered.then(function(){ - this.fill(); - displaying.resolve(this); - }.bind(this)); + rendered. + then(this.fill.bind(this)). + then(function(){ + displaying.resolve(this); + }.bind(this)); }.bind(this)); @@ -4201,12 +4225,19 @@ EPUBJS.Renderer.prototype.render = function(section){ rendered = section.render(); view.index = section.index; - // Place view in correct position - this.insert(view, section.index); + - return rendered.then(function(contents){ - return view.load(contents); - }); + return rendered. + then(function(contents){ + // Place view in correct position + this.insert(view, section.index); + + return view.load(contents); + }.bind(this)) + .then(function(){ + this.rendering = false; + return view; + }.bind(this)); }; @@ -4216,23 +4247,36 @@ EPUBJS.Renderer.prototype.forwards = function(){ var rendered; var section; - if(this.rendering) return; - console.log("going forwards") - next = this.last().index + 1; - if(next === this.book.spine.length){ - return; + if(this.rendering || next === this.book.spine.length){ + rendered = new RSVP.defer(); + rendered.reject({message: "reject forwards"}); + return rendered.promise; } + console.log("going forwards") + + this.rendering = true; section = this.book.spine.get(next); rendered = this.render(section); - this.rendering = true; rendered.then(function(){ - console.log("last:", this.last().height) + // this.rendering = false; + var first = this.first(); + var bounds = first.bounds(); + var container = this.container.getBoundingClientRect(); + var offset; + var prev = this.container.scrollTop; + if(this.views.length > this.settings.viewsLimit) { + + // Remove the item + this.remove(first); - this.rendering = false; + // Reset Position + this.infinite.scroll(0, prev - bounds.height, true) + + } }.bind(this)); return rendered; @@ -4243,23 +4287,28 @@ EPUBJS.Renderer.prototype.backwards = function(view){ var rendered; var section; - if(this.rendering) return; - console.log("going backwards") prev = this.first().index - 1; - if(prev < 0){ - return; //TODO: should reject + if(this.rendering || prev < 0){ + rendered = new RSVP.defer(); + rendered.reject({message: "reject backwards"}); + return rendered.promise; } + console.log("going backwards") + + this.rendering = true; section = this.book.spine.get(prev); rendered = this.render(section); - this.rendering = true; rendered.then(function(){ - this.rendering = false; - // -- this might want to be in infinite - this.container.scrollTop += this.first().height; + // this.container.scrollTop += this.first().height; + this.infinite.scrollBy(0, this.first().height, true); + if(this.views.length > this.settings.viewsLimit) { + last = this.last(); + this.remove(last); + } }.bind(this)); return rendered; @@ -4270,31 +4319,45 @@ EPUBJS.Renderer.prototype.backwards = function(view){ // -- this might want to be in infinite EPUBJS.Renderer.prototype.fill = function() { - var filling = this.backwards(); var height = this.container.getBoundingClientRect().height; - - if(!filling) return; - - filling.then(function(){ + var next = function(){ var bottom = this.last().bounds().bottom; - while (height && bottom && bottom < height) { - this.forwards(); - }; - }.bind(this)); -}; + var defer = new RSVP.defer(); + var promise = defer.promise; + + if (height && bottom && (bottom < height) && (this.last().index + 1 < this.book.spine.length)) { + return this.forwards().then(next); + } else { + this.rendering = false; + defer.resolve(); + return promise; + } + }.bind(this); + + + return next(). + then(this.backwards.bind(this)). + then(function(){ + this.rendering = false; + }.bind(this)); + -EPUBJS.Renderer.prototype.jump = function(view){ - this.views.push(view); }; EPUBJS.Renderer.prototype.append = function(view){ + var first, prevTop, prevHeight, offset; + this.views.push(view); view.appendTo(this.container); + }; EPUBJS.Renderer.prototype.prepend = function(view){ + var last; + this.views.unshift(view); view.prependTo(this.container); + }; // Simple Insert @@ -4307,13 +4370,17 @@ EPUBJS.Renderer.prototype.insert = function(view, index){ } else if(index - this.last().index <= 0) { this.prepend(view); } - + console.log("insert") // return position; }; // Remove the render element and clean up listeners -EPUBJS.Renderer.prototype.destroy = function() { - +EPUBJS.Renderer.prototype.remove = function(view) { + var index = this.views.indexOf(view); + view.destroy(); + if(index > -1) { + this.views.splice(index, 1); + } }; EPUBJS.Renderer.prototype.first = function() { @@ -4436,7 +4503,7 @@ EPUBJS.SpineItem.prototype.render = function(){ EPUBJS.SpineItem.prototype.find = function(_query){ }; -EPUBJS.View = function(options) { +EPUBJS.View = function(width, height) { this.id = "epubjs-view:" + EPUBJS.core.uuid(); this.loading = new RSVP.defer(); this.loaded = this.loading.promise; @@ -4451,9 +4518,10 @@ EPUBJS.View.prototype.load = function(contents) { this.document = this.iframe.contentDocument; - this.iframe.onload = function(e) { + this.iframe.addEventListener("load", function(event) { + var layout; + this.window = this.iframe.contentWindow; - this.window.addEventListener("resize", this.resized.bind(this), false); this.document = this.iframe.contentDocument; this.iframe.style.display = "block"; @@ -4463,15 +4531,25 @@ EPUBJS.View.prototype.load = function(contents) { this.document.body.style.display = "inline-block"; this.layout(); - - // This needs to run twice to get to the correct size sometimes? - this.layout(); - + this.iframe.style.visibility = "visible"; + setTimeout(function(){ + this.window.addEventListener("resize", this.resized.bind(this), false); + }.bind(this), 10); // Wait to listen for resize events + + this.document.fonts.onloading = function(){ + console.log("loaded fonts"); + // this.layout(); + }.bind(this); + + // this.observer = this.observe(this.document); + loading.resolve(this); this.loading.resolve(this); - }.bind(this); + + }.bind(this)); + // this.iframe.srcdoc = contents; this.document.open(); @@ -4481,11 +4559,6 @@ EPUBJS.View.prototype.load = function(contents) { return loaded; }; - -EPUBJS.View.prototype.unload = function() { - -}; - EPUBJS.View.prototype.create = function() { this.iframe = document.createElement('iframe'); this.iframe.id = this.id; @@ -4493,16 +4566,19 @@ EPUBJS.View.prototype.create = function() { this.iframe.seamless = "seamless"; // Back up if seamless isn't supported this.iframe.style.border = "none"; + + this.resizing = true; this.iframe.width = "100%"; - this.iframe.style.height = "0"; + this.iframe.style.height = "100%"; this.iframe.style.display = "none"; this.iframe.style.visibility = "hidden"; + return this.iframe; }; -EPUBJS.View.prototype.resized = function() { +EPUBJS.View.prototype.resized = function(e) { if (!this.resizing) { this.layout(); @@ -4513,32 +4589,48 @@ EPUBJS.View.prototype.resized = function() { }; EPUBJS.View.prototype.layout = function() { - var bounds = {}, content, width = 0, height = 0; - - this.resizing = true; + var bounds; + console.log("layout") // Check bounds bounds = this.document.body.getBoundingClientRect(); + if(!bounds || (bounds.height == 0 && bounds.width == 0)) { + console.error("View not shown"); + } + // Apply Changes + this.resizing = true; this.iframe.style.height = bounds.height + "px"; - this.iframe.style.width = bounds.width + "px"; + // this.iframe.style.width = bounds.width + "px"; // Check again - content = this.document.body.getBoundingClientRect(); + bounds = this.document.body.getBoundingClientRect(); + this.resizing = true; + this.iframe.style.height = bounds.height + "px"; + // this.iframe.style.width = bounds.width + "px"; - height = content.height; - width = content.width; + this.width = bounds.width; + this.height = bounds.height; - this.width = width; - this.height = height; - - - // if(bounds.height != content.height || bounds.width != content.width) { - // // this.layout(); - // console.log(bounds, content) - // } - +}; +EPUBJS.View.prototype.observe = function(target) { + var renderer = this; + + // create an observer instance + var observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + renderer.layout(); + }); + }); + + // configuration of the observer: + var config = { attributes: true, childList: true, characterData: true, subtree: true }; + + // pass in the target node, as well as the observer options + observer.observe(target, config); + + return observer; }; EPUBJS.View.prototype.appendTo = function(element) { @@ -4556,6 +4648,9 @@ EPUBJS.View.prototype.bounds = function() { }; EPUBJS.View.prototype.destroy = function() { + // Stop observing + // this.observer.disconnect(); + this.element.removeChild(this.iframe); }; diff --git a/dist/epub.min.js b/dist/epub.min.js index 9b131fb..eea15e9 100644 --- a/dist/epub.min.js +++ b/dist/epub.min.js @@ -1,2 +1,2 @@ -"undefined"==typeof EPUBJS&&(("undefined"!=typeof window?window:this).EPUBJS={}),EPUBJS.VERSION="0.3.0",EPUBJS.Render={},function(e){"use strict";var t=function(e){return new EPUBJS.Book(e)};t.Render={register:function(e,r){t.Render[e]=r}},"object"==typeof exports?(e.RSVP=require("rsvp"),module.exports=t):"function"==typeof define&&define.amd?define(t):e.ePub=t}(this),function(e){var t,r;!function(){var e={},n={};t=function(t,r,n){e[t]={deps:r,callback:n}},r=function(t){function i(e){if("."!==e.charAt(0))return e;for(var r=e.split("/"),n=t.split("/").slice(0,-1),i=0,o=r.length;o>i;i++){var s=r[i];if(".."===s)n.pop();else{if("."===s)continue;n.push(s)}}return n.join("/")}if(n[t])return n[t];if(n[t]={},!e[t])throw new Error("Could not find module "+t);for(var o,s=e[t],a=s.deps,u=s.callback,c=[],p=0,h=a.length;h>p;p++)c.push("exports"===a[p]?o={}:r(i(a[p])));var l=u.apply(this,c);return n[t]=o||l},r.entries=e}(),t("rsvp/-internal",["./utils","./instrument","./config","exports"],function(e,t,r,n){"use strict";function i(){}function o(e){try{return e.then}catch(t){return J.error=t,J}}function s(e,t,r,n){try{e.call(t,r,n)}catch(i){return i}}function a(e,t,r){w.async(function(e){var n=!1,i=s(r,t,function(r){n||(n=!0,t!==r?p(e,r):l(e,r))},function(t){n||(n=!0,f(e,t))},"Settle: "+(e._label||" unknown promise"));!n&&i&&(n=!0,f(e,i))},e)}function u(e,t){e._onerror=null,t._state===x?l(e,t._result):e._state===U?f(e,t._result):d(t,void 0,function(r){t!==r?p(e,r):l(e,r)},function(t){f(e,t)})}function c(e,t){if(t instanceof e.constructor)u(e,t);else{var r=o(t);r===J?f(e,J.error):void 0===r?l(e,t):P(r)?a(e,t,r):l(e,t)}}function p(e,t){e===t?l(e,t):S(t)?c(e,t):l(e,t)}function h(e){e._onerror&&e._onerror(e._result),v(e)}function l(e,t){e._state===B&&(e._result=t,e._state=x,0===e._subscribers.length?w.instrument&&b("fulfilled",e):w.async(v,e))}function f(e,t){e._state===B&&(e._state=U,e._result=t,w.async(h,e))}function d(e,t,r,n){var i=e._subscribers,o=i.length;e._onerror=null,i[o]=t,i[o+x]=r,i[o+U]=n,0===o&&e._state&&w.async(v,e)}function v(e){var t=e._subscribers,r=e._state;if(w.instrument&&b(r===x?"fulfilled":"rejected",e),0!==t.length){for(var n,i,o=e._result,s=0;se;e+=2){var t=h[e],r=h[e+1];t(r),h[e]=void 0,h[e+1]=void 0}s=0}var s=0;e["default"]=function(e,t){h[s]=e,h[s+1]=t,s+=2,2===s&&a()};var a,u="undefined"!=typeof window?window:{},c=u.MutationObserver||u.WebKitMutationObserver,p="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,h=new Array(1e3);a="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?t():c?r():p?n():i()}),t("rsvp/config",["./events","exports"],function(e,t){"use strict";function r(e,t){return"onerror"===e?void i.on("error",t):2!==arguments.length?i[e]:void(i[e]=t)}var n=e["default"],i={instrument:!1};n.mixin(i),t.config=i,t.configure=r}),t("rsvp/defer",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e){var t={};return t.promise=new r(function(e,r){t.resolve=e,t.reject=r},e),t}}),t("rsvp/enumerator",["./utils","./-internal","exports"],function(e,t,r){"use strict";function n(e,t,r){return e===h?{state:"fulfilled",value:r}:{state:"rejected",reason:r}}function i(e,t,r,n){this._instanceConstructor=e,this.promise=new e(a,n),this._abortOnReject=r,this._validateInput(t)?(this._input=t,this.length=t.length,this._remaining=t.length,this._init(),0===this.length?c(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&c(this.promise,this._result))):u(this.promise,this._validationError())}var o=e.isArray,s=e.isMaybeThenable,a=t.noop,u=t.reject,c=t.fulfill,p=t.subscribe,h=t.FULFILLED,l=t.REJECTED,f=t.PENDING,d=!0;r.ABORT_ON_REJECTION=d,r.makeSettledResult=n,i.prototype._validateInput=function(e){return o(e)},i.prototype._validationError=function(){return new Error("Array Methods must be provided an Array")},i.prototype._init=function(){this._result=new Array(this.length)},r["default"]=i,i.prototype._enumerate=function(){for(var e=this.length,t=this.promise,r=this._input,n=0;t._state===f&&e>n;n++)this._eachEntry(r[n],n)},i.prototype._eachEntry=function(e,t){var r=this._instanceConstructor;s(e)?e.constructor===r&&e._state!==f?(e._onerror=null,this._settledAt(e._state,t,e._result)):this._willSettleAt(r.resolve(e),t):(this._remaining--,this._result[t]=this._makeResult(h,t,e))},i.prototype._settledAt=function(e,t,r){var n=this.promise;n._state===f&&(this._remaining--,this._abortOnReject&&e===l?u(n,r):this._result[t]=this._makeResult(e,t,r)),0===this._remaining&&c(n,this._result)},i.prototype._makeResult=function(e,t,r){return r},i.prototype._willSettleAt=function(e,t){var r=this;p(e,void 0,function(e){r._settledAt(h,t,e)},function(e){r._settledAt(l,t,e)})}}),t("rsvp/events",["exports"],function(e){"use strict";function t(e,t){for(var r=0,n=e.length;n>r;r++)if(e[r]===t)return r;return-1}function r(e){var t=e._promiseCallbacks;return t||(t=e._promiseCallbacks={}),t}e["default"]={mixin:function(e){return e.on=this.on,e.off=this.off,e.trigger=this.trigger,e._promiseCallbacks=void 0,e},on:function(e,n){var i,o=r(this);i=o[e],i||(i=o[e]=[]),-1===t(i,n)&&i.push(n)},off:function(e,n){var i,o,s=r(this);return n?(i=s[e],o=t(i,n),void(-1!==o&&i.splice(o,1))):void(s[e]=[])},trigger:function(e,t){var n,i,o=r(this);if(n=o[e])for(var s=0;sa;a++)s[a]=t(e[a]);return n.all(s,r).then(function(t){for(var r=new Array(o),n=0,i=0;o>i;i++)t[i]&&(r[n]=e[i],n++);return r.length=n,r})})}}),t("rsvp/hash-settled",["./promise","./enumerator","./promise-hash","./utils","exports"],function(e,t,r,n,i){"use strict";function o(e,t,r){this._superConstructor(e,t,!1,r)}var s=e["default"],a=t.makeSettledResult,u=r["default"],c=t["default"],p=n.o_create;o.prototype=p(u.prototype),o.prototype._superConstructor=c,o.prototype._makeResult=a,o.prototype._validationError=function(){return new Error("hashSettled must be called with an object")},i["default"]=function(e,t){return new o(s,e,t).promise}}),t("rsvp/hash",["./promise","./promise-hash","./enumerator","exports"],function(e,t,r,n){"use strict";{var i=e["default"],o=t["default"];r.ABORT_ON_REJECTION}n["default"]=function(e,t){return new o(i,e,t).promise}}),t("rsvp/instrument",["./config","./utils","exports"],function(e,t,r){"use strict";var n=e.config,i=t.now,o=[];r["default"]=function(e,t,r){1===o.push({name:e,payload:{guid:t._guidKey+t._id,eventName:e,detail:t._result,childGuid:r&&t._guidKey+r._id,label:t._label,timeStamp:i(),stack:new Error(t._label).stack}})&&setTimeout(function(){for(var e,t=0;ta;a++)s[a]=t(e[a]);return n.all(s,r)})}}),t("rsvp/node",["./promise","./utils","exports"],function(e,t,r){"use strict";var n=e["default"],i=t.isArray;r["default"]=function(e,t){function r(){for(var r=arguments.length,i=new Array(r),a=0;r>a;a++)i[a]=arguments[a];var u;return o||s||!t?u=this:("object"==typeof console&&console.warn('Deprecation: RSVP.denodeify() doesn\'t allow setting the "this" binding anymore. Use yourFunction.bind(yourThis) instead.'),u=t),n.all(i).then(function(r){function i(n,i){function a(){for(var e=arguments.length,r=new Array(e),a=0;e>a;a++)r[a]=arguments[a];var u=r[0],c=r[1];if(u)i(u);else if(o)n(r.slice(1));else if(s){var p,h,l={},f=r.slice(1);for(h=0;ha;a++)o=r[a],this._eachEntry(o.entry,o.position)}}),t("rsvp/promise",["./config","./events","./instrument","./utils","./-internal","./promise/cast","./promise/all","./promise/race","./promise/resolve","./promise/reject","exports"],function(e,t,r,n,i,o,s,a,u,c,p){"use strict";function h(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function l(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function f(e,t){this._id=C++,this._label=t,this._subscribers=[],d.instrument&&v("created",this),y!==e&&(m(e)||h(),this instanceof f||l(),S(this,e))}var d=e.config,v=(t["default"],r["default"]),m=(n.objectOrFunction,n.isFunction),g=n.now,y=i.noop,E=(i.resolve,i.reject,i.fulfill,i.subscribe),S=i.initializePromise,P=i.invokeCallback,b=i.FULFILLED,w=o["default"],B=s["default"],x=a["default"],U=u["default"],J=c["default"],_="rsvp_"+g()+"-",C=0;p["default"]=f,f.cast=w,f.all=B,f.race=x,f.resolve=U,f.reject=J,f.prototype={constructor:f,_id:void 0,_guidKey:_,_label:void 0,_state:void 0,_result:void 0,_subscribers:void 0,_onerror:function(e){d.trigger("error",e)},then:function(e,t,r){var n=this;n._onerror=null;var i=new this.constructor(y,r),o=n._state,s=n._result;return d.instrument&&v("chained",n,i),o===b&&e?d.async(function(){P(o,i,e,s)}):E(n,i,e,t),i},"catch":function(e,t){return this.then(null,e,t)},"finally":function(e,t){var r=this.constructor;return this.then(function(t){return r.resolve(e()).then(function(){return t})},function(t){return r.resolve(e()).then(function(){throw t})},t)}}}),t("rsvp/promise/all",["../enumerator","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return new r(this,e,!0,t).promise}}),t("rsvp/promise/cast",["./resolve","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=r}),t("rsvp/promise/race",["../utils","../-internal","exports"],function(e,t,r){"use strict";var n=e.isArray,i=(e.isFunction,e.isMaybeThenable,t.noop),o=t.resolve,s=t.reject,a=t.subscribe,u=t.PENDING;r["default"]=function(e,t){function r(e){o(h,e)}function c(e){s(h,e)}var p=this,h=new p(i,t);if(!n(e))return s(h,new TypeError("You must pass an array to race.")),h;for(var l=e.length,f=0;h._state===u&&l>f;f++)a(p.resolve(e[f]),void 0,r,c);return h}}),t("rsvp/promise/reject",["../-internal","exports"],function(e,t){"use strict";var r=e.noop,n=e.reject;t["default"]=function(e,t){var i=this,o=new i(r,t);return n(o,e),o}}),t("rsvp/promise/resolve",["../-internal","exports"],function(e,t){"use strict";var r=e.noop,n=e.resolve;t["default"]=function(e,t){var i=this;if(e&&"object"==typeof e&&e.constructor===i)return e;var o=new i(r,t);return n(o,e),o}}),t("rsvp/race",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.race(e,t)}}),t("rsvp/reject",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.reject(e,t)}}),t("rsvp/resolve",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.resolve(e,t)}}),t("rsvp/rethrow",["exports"],function(e){"use strict";e["default"]=function(e){throw setTimeout(function(){throw e}),e}}),t("rsvp/utils",["exports"],function(e){"use strict";function t(e){return"function"==typeof e||"object"==typeof e&&null!==e}function r(e){return"function"==typeof e}function n(e){return"object"==typeof e&&null!==e}e.objectOrFunction=t,e.isFunction=r,e.isMaybeThenable=n;var i;i=Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)};var o=i;e.isArray=o;var s=Date.now||function(){return(new Date).getTime()};e.now=s;var a=Object.create||function(e){var t=function(){};return t.prototype=e,t};e.o_create=a}),t("rsvp",["./rsvp/promise","./rsvp/events","./rsvp/node","./rsvp/all","./rsvp/all-settled","./rsvp/race","./rsvp/hash","./rsvp/hash-settled","./rsvp/rethrow","./rsvp/defer","./rsvp/config","./rsvp/map","./rsvp/resolve","./rsvp/reject","./rsvp/filter","./rsvp/asap","exports"],function(e,t,r,n,i,o,s,a,u,c,p,h,l,f,d,v,m){"use strict";function g(e,t){k.async(e,t)}function y(){k.on.apply(k,arguments)}function E(){k.off.apply(k,arguments)}var S=e["default"],P=t["default"],b=r["default"],w=n["default"],B=i["default"],x=o["default"],U=s["default"],J=a["default"],_=u["default"],C=c["default"],k=p.config,R=p.configure,T=h["default"],N=l["default"],I=f["default"],F=d["default"],A=v["default"];if(k.async=A,"undefined"!=typeof window&&"object"==typeof window.__PROMISE_INSTRUMENTATION__){var O=window.__PROMISE_INSTRUMENTATION__;R("instrument",!0);for(var j in O)O.hasOwnProperty(j)&&y(j,O[j])}m.Promise=S,m.EventTarget=P,m.all=w,m.allSettled=B,m.race=x,m.hash=U,m.hashSettled=J,m.rethrow=_,m.defer=C,m.denodeify=b,m.configure=R,m.on=y,m.off=E,m.resolve=N,m.reject=I,m.async=g,m.map=T,m.filter=F}),e.RSVP=r("rsvp")}(self),EPUBJS.Book=function(e){this.opening=new RSVP.defer,this.opened=this.opening.promise,this.isOpen=!1,this.url=void 0,this.spine=void 0,this.loading={manifest:new RSVP.defer,spine:new RSVP.defer,metadata:new RSVP.defer,cover:new RSVP.defer,navigation:new RSVP.defer,pageList:new RSVP.defer},this.loaded={manifest:this.loading.manifest.promise,spine:this.loading.spine.promise,metadata:this.loading.metadata.promise,cover:this.loading.cover.promise,navigation:this.loading.navigation.promise,pageList:this.loading.pageList.promise},this.ready=RSVP.hash(this.loaded),this.isRendered=!1,this._q=EPUBJS.core.queue(this),e&&this.open(e)},EPUBJS.Book.prototype.open=function(e){var t,r,n,i=new EPUBJS.Parser,o=this,s="META-INF/container.xml";return t="object"==typeof e?e:EPUBJS.core.uri(e),"opf"===t.extension?(this.packageUrl=t.href,this.containerUrl="",t.origin?this.url=t.origin+"/"+t.directory:window?(n=EPUBJS.core.uri(window.location.href),this.url=EPUBJS.core.resolveUrl(n.base,t.directory)):this.url=t.directory,r=this.request(this.packageUrl)):"epub"===t.extension||"zip"===t.extension?(this.archived=!0,this.url=""):t.extension||(this.containerUrl=e+s,r=this.request(this.containerUrl).then(function(e){return i.container(e)}).then(function(t){var r=EPUBJS.core.uri(t.packagePath);return o.packageUrl=e+t.packagePath,o.url=e+r.directory,o.encoding=t.encoding,o.request(o.packageUrl)}).catch(function(e){console.error("Could not load book at: "+(this.packageUrl||this.containerPath)),o.trigger("book:loadFailed",this.packageUrl||this.containerPath),o.opening.reject(e)})),r.then(function(e){o.unpack(e),o.loading.manifest.resolve(o.package.manifest),o.loading.metadata.resolve(o.package.metadata),o.loading.spine.resolve(o.spine),o.loading.cover.resolve(o.cover),this.isOpen=!0,o.opening.resolve(o)}).catch(function(e){console.error(e.message,e.stack),o.opening.reject(e)}),this.opened},EPUBJS.Book.prototype.unpack=function(e){var t=this,r=new EPUBJS.Parser;t.package=r.packageContents(e),t.package.baseUrl=t.url,t.spine=new EPUBJS.Spine(t.package,this.request),t.navigation=new EPUBJS.Navigation(t.package,this.request),t.navigation.load().then(function(e){t.toc=e,t.loading.navigation.resolve(t.toc)}),t.cover=t.url+t.package.coverPath},EPUBJS.Book.prototype.section=function(e){return this.spine.get(e)},EPUBJS.Book.prototype.renderTo=function(e,t){var r=new EPUBJS.Renderer(this,t);return r.attachTo(e),r},EPUBJS.Book.prototype.request=function(e){return this.archived?void 0:EPUBJS.core.request(e,"xml",this.credentials)},EPUBJS.Book.prototype.setCredentials=function(e){this.credentials=e},RSVP.EventTarget.mixin(EPUBJS.Book.prototype),RSVP.on("error",function(){}),RSVP.configure("instrument",!0),RSVP.on("rejected",function(e){console.error(e.detail.message,e.detail.stack)}),EPUBJS.core={},EPUBJS.core.request=function(e,t,r){function n(){if(this.readyState===this.DONE)if(200===this.status||this.responseXML){var e;e="xml"==t?this.responseXML:"json"==t?JSON.parse(this.response):"blob"==t?i?this.response:new Blob([this.response]):this.response,s.resolve(e)}else s.reject({message:this.response,stack:(new Error).stack})}var i=window.URL,o=i?"blob":"arraybuffer",s=new RSVP.defer,a=new XMLHttpRequest,u=XMLHttpRequest.prototype;return"overrideMimeType"in u||Object.defineProperty(u,"overrideMimeType",{value:function(){}}),r&&(a.withCredentials=!0),a.open("GET",e,!0),a.onreadystatechange=n,"blob"==t&&(a.responseType=o),"json"==t&&a.setRequestHeader("Accept","application/json"),"xml"==t&&a.overrideMimeType("text/xml"),a.send(),s.promise},EPUBJS.core.uri=function(e){var t,r,n,i={protocol:"",host:"",path:"",origin:"",directory:"",base:"",filename:"",extension:"",fragment:"",href:e},o=e.indexOf("://"),s=e.indexOf("?"),a=e.indexOf("#");return-1!=a&&(i.fragment=e.slice(a+1),e=e.slice(0,a)),-1!=s&&(i.search=e.slice(s+1),e=e.slice(0,s),href=e),-1!=o?(i.protocol=e.slice(0,o),t=e.slice(o+3),n=t.indexOf("/"),-1===n?(i.host=i.path,i.path=""):(i.host=t.slice(0,n),i.path=t.slice(n)),i.origin=i.protocol+"://"+i.host,i.directory=EPUBJS.core.folder(i.path),i.base=i.origin+i.directory):(i.path=e,i.directory=EPUBJS.core.folder(e),i.base=i.directory),i.filename=e.replace(i.base,""),r=i.filename.lastIndexOf("."),-1!=r&&(i.extension=i.filename.slice(r+1)),i},EPUBJS.core.folder=function(e){var t=e.lastIndexOf("/");if(-1==t)var r="";return r=e.slice(0,t+1)},EPUBJS.core.queue=function(e){var t=[],r=e,n=function(e,r,n){return t.push({funcName:e,args:r,context:n}),t},i=function(){var e;t.length&&(e=t.shift(),r[e.funcName].apply(e.context||r,e.args))},o=function(){for(;t.length;)i()},s=function(){t=[]},a=function(){return t.length};return{enqueue:n,dequeue:i,flush:o,clear:s,length:a}},EPUBJS.core.isElement=function(e){return!(!e||1!=e.nodeType)},EPUBJS.core.uuid=function(){var e=(new Date).getTime(),t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var r=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"==t?r:7&r|8).toString(16)});return t},EPUBJS.core.values=function(e){for(var t=-1,r=Object.keys(e),n=r.length,i=Array(n);++t0;){if(r=n.shift(),"text"===r.type?(i=s.childNodes[r.index],s=i.parentNode||s):s=r.id?o.getElementById(r.id):a[r.index],"undefined"==typeof s)return console.error("No Element For",r,e.str),!1;a=Array.prototype.slice.call(s.children)}return s},EPUBJS.EpubCFI.prototype.compare=function(e,t){if("string"==typeof e&&(e=new EPUBJS.EpubCFI(e)),"string"==typeof t&&(t=new EPUBJS.EpubCFI(t)),e.spinePos>t.spinePos)return 1;if(e.spinePost.steps[r].index)return 1;if(e.steps[r].indext.characterOffset?1:e.characterOffset=0?(o=i.length,e.characterOffset=o&&t&&t()}var i,o;return"undefined"==typeof this.hooks[e]?!1:(i=this.hooks[e],o=i.length,0===o&&t&&t(),void i.forEach(function(e){e(n,r)}))},{register:function(e){if(void 0===EPUBJS.hooks[e]&&(EPUBJS.hooks[e]={}),"object"!=typeof EPUBJS.hooks[e])throw"Already registered: "+e;return EPUBJS.hooks[e]},mixin:function(t){for(var r in e.prototype)t[r]=e.prototype[r]}}}(),EPUBJS.Infinite=function(e,t){this.container=e,this.windowHeight=window.innerHeight,this.tick=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,this.scrolled=!1,this.ignore=!1,this.displaying=!1,this.offset=250,this.views=[],this.renderer=t,this.prevScrollTop=0},EPUBJS.Infinite.prototype.start=function(){this.container.addEventListener("scroll",function(){this.ignore?this.ignore=!1:this.scrolled=!0}.bind(this)),window.addEventListener("unload",function(){this.ignore=!0}),this.tick.call(window,this.check.bind(this)),this.scrolled=!1},EPUBJS.Infinite.prototype.forwards=function(){this.trigger("forwards")},EPUBJS.Infinite.prototype.backwards=function(){this.trigger("backwards")},EPUBJS.Infinite.prototype.check=function(){if(this.scrolled&&!this.displaying){var e=this.container.scrollTop,t=this.container.scrollHeight,r=e-this.prevScrollTop,n=this.container.getBoundingClientRect().height,i=n-(t-e)>-this.offset,o=e0?this.forwards():o&&0>r&&this.backwards(),this.prevScrollTop=e,this.scrolled=!1}this.tick.call(window,this.check.bind(this))},RSVP.EventTarget.mixin(EPUBJS.Infinite.prototype),EPUBJS.Layout=function(){},EPUBJS.Layout.prototype.reconcileLayoutSettings=function(e,t){var r={};for(var n in e)e.hasOwnProperty(n)&&(r[n]=e[n]);return t.forEach(function(e){var t,n,i=e.replace("rendition:",""),o=i.indexOf("-");-1!=o&&(t=i.slice(0,o),n=i.slice(o+1),r[t]=n)}),r},EPUBJS.Layout.prototype.determineLayout=function(e){var t=this.determineSpreads(this.minSpreadWidth),r=t?"ReflowableSpreads":"Reflowable",n=!1;return"pre-paginated"===e.layout&&(r="Fixed",n=!0,t=!1),"reflowable"===e.layout&&"none"===e.spread&&(r="Reflowable",n=!1,t=!1),"reflowable"===e.layout&&"both"===e.spread&&(r="ReflowableSpreads",n=!1,t=!0),this.spreads=t,this.render.scroll(n),this.trigger("renderer:spreads",t),r},EPUBJS.Layout.prototype.applyStyles=function(e){for(var t in e)for(var r in this.views)r.setStyle(t,e[t])},EPUBJS.Layout.prototype.setStyle=function(e,t,r){for(var n in this.views)n.setStyle(e,t,r)},EPUBJS.Layout.prototype.removeStyle=function(e){for(var t in this.views)t.removeStyle(e)},EPUBJS.Layout.prototype.applyHeadTags=function(e){for(var t in e)this.render.addHeadTag(t,e[t])},EPUBJS.Navigation=function(e,t){var r=this,n=new EPUBJS.Parser,i=t||EPUBJS.core.request;this.package=e,this.toc=[],this.tocByHref={},this.tocById={},e.navPath&&(this.navUrl=e.baseUrl+e.navPath,this.nav={},this.nav.load=function(){var e=new RSVP.defer,t=e.promise;return i(r.navUrl,"xml").then(function(t){r.toc=n.nav(t),r.loaded(r.toc),e.resolve(r.toc)}),t}),e.ncxPath&&(this.ncxUrl=e.baseUrl+e.ncxPath,this.ncx={},this.ncx.load=function(){var e=new RSVP.defer,t=e.promise;return i(r.ncxUrl,"xml").then(function(t){r.toc=n.ncx(t),r.loaded(r.toc),e.resolve(r.toc)}),t})},EPUBJS.Navigation.prototype.load=function(e){{var t;e||EPUBJS.core.request}return this.nav?t=this.nav.load():this.ncx&&(t=this.ncx.load()),t},EPUBJS.Navigation.prototype.loaded=function(e){for(var t,r=0;r=0;s--){var a=i.snapshotItem(s),u=a.getAttribute("id")||!1,c=a.querySelector("content"),p=c.getAttribute("src"),h=a.querySelector("navLabel"),l=h.textContent?h.textContent:"",f=p.split("#"),d=(f[0],t(a));n.unshift({id:u,href:p,label:l,subitems:d,parent:r?r.getAttribute("id"):null})}return n}var r=e.querySelector("navMap");return r?t(r):[]},EPUBJS.Renderer=function(e,t){var r=t||{};this.settings={hidden:r.hidden||!1,viewLimit:3,width:r.width||!1,height:r.height||!1},this.book=e,this.epubcfi=new EPUBJS.EpubCFI,this.layoutSettings={},EPUBJS.Hooks.mixin(this),this.getHooks("beforeChapterDisplay"),this._q=EPUBJS.core.queue(this),this.position=1,this.initialize({width:this.settings.width,height:this.settings.height,hidden:!0}),this.rendering=!1,this.views=[],this.positions=[]},EPUBJS.Renderer.prototype.initialize=function(e){{var t=e||{},r=t.height?t.height+"px":"100%",n=t.width?t.width+"px":"100%";t.hidden||!1}return this.container=document.createElement("div"),this.infinite=new EPUBJS.Infinite(this.container,this),this.container.style.width=n,this.container.style.height=r,this.container.style.overflow="scroll",t.hidden?(this.wrapper=document.createElement("div"),this.wrapper.style.visibility="hidden",this.wrapper.style.overflow="hidden",this.wrapper.style.width="0",this.wrapper.style.height="0",this.wrapper.appendChild(this.container),this.wrapper):this.container},EPUBJS.Renderer.prototype.resize=function(e,t){var r=e,n=t;e||(r=window.innerWidth),t||(n=window.innerHeight),this.container.style.width=r+"px",this.container.style.height=n+"px",this.trigger("resized",{width:this.width,height:this.height})},EPUBJS.Renderer.prototype.onResized=function(){var e=this.element.getBoundingClientRect();this.resize(e.width,e.height)},EPUBJS.Renderer.prototype.attachTo=function(e){var t;return EPUBJS.core.isElement(e)?this.element=e:"string"==typeof e&&(this.element=document.getElementById(e)),this.element?(this.element.appendChild(this.container),this.settings.height||this.settings.width||(t=this.element.getBoundingClientRect(),this.resize(t.width,t.height)),this.infinite.start(),this.infinite.on("forwards",this.forwards.bind(this)),this.infinite.on("backwards",this.backwards.bind(this)),void window.addEventListener("resize",this.onResized.bind(this),!1)):void console.error("Not an Element")},EPUBJS.Renderer.prototype.clear=function(){this.views.forEach(function(e){e.destroy()}),this.views=[]},EPUBJS.Renderer.prototype.display=function(e){var t=new RSVP.defer,r=t.promise;return this.clear(),this.book.opened.then(function(){var r=this.book.spine.get(e),n=this.render(r);n.then(function(){this.fill(),t.resolve(this)}.bind(this))}.bind(this)),r},EPUBJS.Renderer.prototype.render=function(e){var t,r=new EPUBJS.View;return e?(t=e.render(),r.index=e.index,this.insert(r,e.index),t.then(function(e){return r.load(e)})):void t.reject()},EPUBJS.Renderer.prototype.forwards=function(){var e,t,r;return this.rendering||(console.log("going forwards"),e=this.last().index+1,e===this.book.spine.length)?void 0:(r=this.book.spine.get(e),t=this.render(r),this.rendering=!0,t.then(function(){console.log("last:",this.last().height),this.rendering=!1}.bind(this)),t)},EPUBJS.Renderer.prototype.backwards=function(){var e,t,r;return this.rendering||(console.log("going backwards"),e=this.first().index-1,0>e)?void 0:(r=this.book.spine.get(e),t=this.render(r),this.rendering=!0,t.then(function(){this.rendering=!1,this.container.scrollTop+=this.first().height}.bind(this)),t)},EPUBJS.Renderer.prototype.fill=function(){var e=this.backwards(),t=this.container.getBoundingClientRect().height;e&&e.then(function(){for(var e=this.last().bounds().bottom;t&&e&&t>e;)this.forwards()}.bind(this))},EPUBJS.Renderer.prototype.jump=function(e){this.views.push(e)},EPUBJS.Renderer.prototype.append=function(e){this.views.push(e),e.appendTo(this.container)},EPUBJS.Renderer.prototype.prepend=function(e){this.views.unshift(e),e.prependTo(this.container)},EPUBJS.Renderer.prototype.insert=function(e,t){this.first()?t-this.first().index>=0?this.append(e):t-this.last().index<=0&&this.prepend(e):this.append(e)},EPUBJS.Renderer.prototype.destroy=function(){},EPUBJS.Renderer.prototype.first=function(){return this.views[0]},EPUBJS.Renderer.prototype.last=function(){return this.views[this.views.length-1]},RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype),EPUBJS.Spine=function(e,t){this.items=e.spine,this.manifest=e.manifest,this.spineNodeIndex=e.spineNodeIndex,this.baseUrl=e.baseUrl||"",this.request=t,this.length=this.items.length,this.epubcfi=new EPUBJS.EpubCFI,this.spineItems=[],this.spineByHref={},this.spineById={},this.items.forEach(function(e,t){var r,n,i,o=this.epubcfi.generateChapterComponent(this.spineNodeIndex,e.index,e.idref),s=this.manifest[e.idref];s&&(r=s.href,n=this.baseUrl+r),i=new EPUBJS.SpineItem(e,r,n,o),this.spineItems.push(i),this.spineByHref[i.href]=t,this.spineById[i.idref]=t}.bind(this))},EPUBJS.Spine.prototype.get=function(e){var t=0;return!e||"number"!=typeof e&&isNaN(e)!==!1?e&&0===e.indexOf("#")?t=this.spineById[e.substring(1)]:e&&(t=this.spineByHref[e]):t=e,this.spineItems[t]},EPUBJS.SpineItem=function(e,t,r,n){this.idref=e.idref,this.linear=e.linear,this.properties=e.properties,this.index=e.index,this.href=t,this.url=r,this.cfiBase=n},EPUBJS.SpineItem.prototype.load=function(e){var t=e||this.request||EPUBJS.core.request,r=new RSVP.defer,n=r.promise;return this.contents?r.resolve(this.contents):t(this.url,"xml").then(function(e){EPUBJS.core.folder(this.url);this.document=e,this.contents=e.documentElement,this.replacements(this.document),r.resolve(this.contents)}.bind(this)),n},EPUBJS.SpineItem.prototype.replacements=function(e){var t=e.createElement("base");t.setAttribute("href",this.url),e.head.insertBefore(t,e.head.firstChild)},EPUBJS.SpineItem.prototype.render=function(){var e=new RSVP.defer,t=e.promise;return this.load().then(function(t){var r=new XMLSerializer,n=r.serializeToString(t);e.resolve(n)}),t},EPUBJS.SpineItem.prototype.find=function(){},EPUBJS.View=function(){this.id="epubjs-view:"+EPUBJS.core.uuid(),this.loading=new RSVP.defer,this.loaded=this.loading.promise,this.iframe=this.create(),this.height,this.width},EPUBJS.View.prototype.load=function(e){var t=new RSVP.defer,r=t.promise;return this.document=this.iframe.contentDocument,this.iframe.onload=function(){this.window=this.iframe.contentWindow,this.window.addEventListener("resize",this.resized.bind(this),!1),this.document=this.iframe.contentDocument,this.iframe.style.display="block",this.document.body.style.margin="0",this.document.body.style.display="inline-block",this.layout(),this.layout(),this.iframe.style.visibility="visible",t.resolve(this),this.loading.resolve(this)}.bind(this),this.document.open(),this.document.write(e),this.document.close(),r},EPUBJS.View.prototype.unload=function(){},EPUBJS.View.prototype.create=function(){return this.iframe=document.createElement("iframe"),this.iframe.id=this.id,this.iframe.scrolling="no",this.iframe.seamless="seamless",this.iframe.style.border="none",this.iframe.width="100%",this.iframe.style.height="0",this.iframe.style.display="none",this.iframe.style.visibility="hidden",this.iframe},EPUBJS.View.prototype.resized=function(){this.resizing?this.resizing=!1:this.layout()},EPUBJS.View.prototype.layout=function(){var e,t={},r=0,n=0;this.resizing=!0,t=this.document.body.getBoundingClientRect(),this.iframe.style.height=t.height+"px",this.iframe.style.width=t.width+"px",e=this.document.body.getBoundingClientRect(),n=e.height,r=e.width,this.width=r,this.height=n},EPUBJS.View.prototype.appendTo=function(e){this.element=e,this.element.appendChild(this.iframe)},EPUBJS.View.prototype.prependTo=function(e){this.element=e,e.insertBefore(this.iframe,e.firstChild)},EPUBJS.View.prototype.bounds=function(){return this.iframe.getBoundingClientRect()},EPUBJS.View.prototype.destroy=function(){this.element.removeChild(this.iframe)}; \ No newline at end of file +"undefined"==typeof EPUBJS&&(("undefined"!=typeof window?window:this).EPUBJS={}),EPUBJS.VERSION="0.3.0",EPUBJS.Render={},function(e){"use strict";var t=function(e){return new EPUBJS.Book(e)};t.Render={register:function(e,r){t.Render[e]=r}},"object"==typeof exports?(e.RSVP=require("rsvp"),module.exports=t):"function"==typeof define&&define.amd?define(t):e.ePub=t}(this),function(e){var t,r;!function(){var e={},n={};t=function(t,r,n){e[t]={deps:r,callback:n}},r=function(t){function i(e){if("."!==e.charAt(0))return e;for(var r=e.split("/"),n=t.split("/").slice(0,-1),i=0,o=r.length;o>i;i++){var s=r[i];if(".."===s)n.pop();else{if("."===s)continue;n.push(s)}}return n.join("/")}if(n[t])return n[t];if(n[t]={},!e[t])throw new Error("Could not find module "+t);for(var o,s=e[t],a=s.deps,u=s.callback,c=[],h=0,p=a.length;p>h;h++)c.push("exports"===a[h]?o={}:r(i(a[h])));var l=u.apply(this,c);return n[t]=o||l},r.entries=e}(),t("rsvp/-internal",["./utils","./instrument","./config","exports"],function(e,t,r,n){"use strict";function i(){}function o(e){try{return e.then}catch(t){return J.error=t,J}}function s(e,t,r,n){try{e.call(t,r,n)}catch(i){return i}}function a(e,t,r){w.async(function(e){var n=!1,i=s(r,t,function(r){n||(n=!0,t!==r?h(e,r):l(e,r))},function(t){n||(n=!0,f(e,t))},"Settle: "+(e._label||" unknown promise"));!n&&i&&(n=!0,f(e,i))},e)}function u(e,t){e._onerror=null,t._state===x?l(e,t._result):e._state===U?f(e,t._result):d(t,void 0,function(r){t!==r?h(e,r):l(e,r)},function(t){f(e,t)})}function c(e,t){if(t instanceof e.constructor)u(e,t);else{var r=o(t);r===J?f(e,J.error):void 0===r?l(e,t):P(r)?a(e,t,r):l(e,t)}}function h(e,t){e===t?l(e,t):S(t)?c(e,t):l(e,t)}function p(e){e._onerror&&e._onerror(e._result),v(e)}function l(e,t){e._state===B&&(e._result=t,e._state=x,0===e._subscribers.length?w.instrument&&b("fulfilled",e):w.async(v,e))}function f(e,t){e._state===B&&(e._state=U,e._result=t,w.async(p,e))}function d(e,t,r,n){var i=e._subscribers,o=i.length;e._onerror=null,i[o]=t,i[o+x]=r,i[o+U]=n,0===o&&e._state&&w.async(v,e)}function v(e){var t=e._subscribers,r=e._state;if(w.instrument&&b(r===x?"fulfilled":"rejected",e),0!==t.length){for(var n,i,o=e._result,s=0;se;e+=2){var t=p[e],r=p[e+1];t(r),p[e]=void 0,p[e+1]=void 0}s=0}var s=0;e["default"]=function(e,t){p[s]=e,p[s+1]=t,s+=2,2===s&&a()};var a,u="undefined"!=typeof window?window:{},c=u.MutationObserver||u.WebKitMutationObserver,h="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,p=new Array(1e3);a="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?t():c?r():h?n():i()}),t("rsvp/config",["./events","exports"],function(e,t){"use strict";function r(e,t){return"onerror"===e?void i.on("error",t):2!==arguments.length?i[e]:void(i[e]=t)}var n=e["default"],i={instrument:!1};n.mixin(i),t.config=i,t.configure=r}),t("rsvp/defer",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e){var t={};return t.promise=new r(function(e,r){t.resolve=e,t.reject=r},e),t}}),t("rsvp/enumerator",["./utils","./-internal","exports"],function(e,t,r){"use strict";function n(e,t,r){return e===p?{state:"fulfilled",value:r}:{state:"rejected",reason:r}}function i(e,t,r,n){this._instanceConstructor=e,this.promise=new e(a,n),this._abortOnReject=r,this._validateInput(t)?(this._input=t,this.length=t.length,this._remaining=t.length,this._init(),0===this.length?c(this.promise,this._result):(this.length=this.length||0,this._enumerate(),0===this._remaining&&c(this.promise,this._result))):u(this.promise,this._validationError())}var o=e.isArray,s=e.isMaybeThenable,a=t.noop,u=t.reject,c=t.fulfill,h=t.subscribe,p=t.FULFILLED,l=t.REJECTED,f=t.PENDING,d=!0;r.ABORT_ON_REJECTION=d,r.makeSettledResult=n,i.prototype._validateInput=function(e){return o(e)},i.prototype._validationError=function(){return new Error("Array Methods must be provided an Array")},i.prototype._init=function(){this._result=new Array(this.length)},r["default"]=i,i.prototype._enumerate=function(){for(var e=this.length,t=this.promise,r=this._input,n=0;t._state===f&&e>n;n++)this._eachEntry(r[n],n)},i.prototype._eachEntry=function(e,t){var r=this._instanceConstructor;s(e)?e.constructor===r&&e._state!==f?(e._onerror=null,this._settledAt(e._state,t,e._result)):this._willSettleAt(r.resolve(e),t):(this._remaining--,this._result[t]=this._makeResult(p,t,e))},i.prototype._settledAt=function(e,t,r){var n=this.promise;n._state===f&&(this._remaining--,this._abortOnReject&&e===l?u(n,r):this._result[t]=this._makeResult(e,t,r)),0===this._remaining&&c(n,this._result)},i.prototype._makeResult=function(e,t,r){return r},i.prototype._willSettleAt=function(e,t){var r=this;h(e,void 0,function(e){r._settledAt(p,t,e)},function(e){r._settledAt(l,t,e)})}}),t("rsvp/events",["exports"],function(e){"use strict";function t(e,t){for(var r=0,n=e.length;n>r;r++)if(e[r]===t)return r;return-1}function r(e){var t=e._promiseCallbacks;return t||(t=e._promiseCallbacks={}),t}e["default"]={mixin:function(e){return e.on=this.on,e.off=this.off,e.trigger=this.trigger,e._promiseCallbacks=void 0,e},on:function(e,n){var i,o=r(this);i=o[e],i||(i=o[e]=[]),-1===t(i,n)&&i.push(n)},off:function(e,n){var i,o,s=r(this);return n?(i=s[e],o=t(i,n),void(-1!==o&&i.splice(o,1))):void(s[e]=[])},trigger:function(e,t){var n,i,o=r(this);if(n=o[e])for(var s=0;sa;a++)s[a]=t(e[a]);return n.all(s,r).then(function(t){for(var r=new Array(o),n=0,i=0;o>i;i++)t[i]&&(r[n]=e[i],n++);return r.length=n,r})})}}),t("rsvp/hash-settled",["./promise","./enumerator","./promise-hash","./utils","exports"],function(e,t,r,n,i){"use strict";function o(e,t,r){this._superConstructor(e,t,!1,r)}var s=e["default"],a=t.makeSettledResult,u=r["default"],c=t["default"],h=n.o_create;o.prototype=h(u.prototype),o.prototype._superConstructor=c,o.prototype._makeResult=a,o.prototype._validationError=function(){return new Error("hashSettled must be called with an object")},i["default"]=function(e,t){return new o(s,e,t).promise}}),t("rsvp/hash",["./promise","./promise-hash","./enumerator","exports"],function(e,t,r,n){"use strict";{var i=e["default"],o=t["default"];r.ABORT_ON_REJECTION}n["default"]=function(e,t){return new o(i,e,t).promise}}),t("rsvp/instrument",["./config","./utils","exports"],function(e,t,r){"use strict";var n=e.config,i=t.now,o=[];r["default"]=function(e,t,r){1===o.push({name:e,payload:{guid:t._guidKey+t._id,eventName:e,detail:t._result,childGuid:r&&t._guidKey+r._id,label:t._label,timeStamp:i(),stack:new Error(t._label).stack}})&&setTimeout(function(){for(var e,t=0;ta;a++)s[a]=t(e[a]);return n.all(s,r)})}}),t("rsvp/node",["./promise","./utils","exports"],function(e,t,r){"use strict";var n=e["default"],i=t.isArray;r["default"]=function(e,t){function r(){for(var r=arguments.length,i=new Array(r),a=0;r>a;a++)i[a]=arguments[a];var u;return o||s||!t?u=this:("object"==typeof console&&console.warn('Deprecation: RSVP.denodeify() doesn\'t allow setting the "this" binding anymore. Use yourFunction.bind(yourThis) instead.'),u=t),n.all(i).then(function(r){function i(n,i){function a(){for(var e=arguments.length,r=new Array(e),a=0;e>a;a++)r[a]=arguments[a];var u=r[0],c=r[1];if(u)i(u);else if(o)n(r.slice(1));else if(s){var h,p,l={},f=r.slice(1);for(p=0;pa;a++)o=r[a],this._eachEntry(o.entry,o.position)}}),t("rsvp/promise",["./config","./events","./instrument","./utils","./-internal","./promise/cast","./promise/all","./promise/race","./promise/resolve","./promise/reject","exports"],function(e,t,r,n,i,o,s,a,u,c,h){"use strict";function p(){throw new TypeError("You must pass a resolver function as the first argument to the promise constructor")}function l(){throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.")}function f(e,t){this._id=R++,this._label=t,this._subscribers=[],d.instrument&&v("created",this),y!==e&&(g(e)||p(),this instanceof f||l(),S(this,e))}var d=e.config,v=(t["default"],r["default"]),g=(n.objectOrFunction,n.isFunction),m=n.now,y=i.noop,E=(i.resolve,i.reject,i.fulfill,i.subscribe),S=i.initializePromise,P=i.invokeCallback,b=i.FULFILLED,w=o["default"],B=s["default"],x=a["default"],U=u["default"],J=c["default"],_="rsvp_"+m()+"-",R=0;h["default"]=f,f.cast=w,f.all=B,f.race=x,f.resolve=U,f.reject=J,f.prototype={constructor:f,_id:void 0,_guidKey:_,_label:void 0,_state:void 0,_result:void 0,_subscribers:void 0,_onerror:function(e){d.trigger("error",e)},then:function(e,t,r){var n=this;n._onerror=null;var i=new this.constructor(y,r),o=n._state,s=n._result;return d.instrument&&v("chained",n,i),o===b&&e?d.async(function(){P(o,i,e,s)}):E(n,i,e,t),i},"catch":function(e,t){return this.then(null,e,t)},"finally":function(e,t){var r=this.constructor;return this.then(function(t){return r.resolve(e()).then(function(){return t})},function(t){return r.resolve(e()).then(function(){throw t})},t)}}}),t("rsvp/promise/all",["../enumerator","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return new r(this,e,!0,t).promise}}),t("rsvp/promise/cast",["./resolve","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=r}),t("rsvp/promise/race",["../utils","../-internal","exports"],function(e,t,r){"use strict";var n=e.isArray,i=(e.isFunction,e.isMaybeThenable,t.noop),o=t.resolve,s=t.reject,a=t.subscribe,u=t.PENDING;r["default"]=function(e,t){function r(e){o(p,e)}function c(e){s(p,e)}var h=this,p=new h(i,t);if(!n(e))return s(p,new TypeError("You must pass an array to race.")),p;for(var l=e.length,f=0;p._state===u&&l>f;f++)a(h.resolve(e[f]),void 0,r,c);return p}}),t("rsvp/promise/reject",["../-internal","exports"],function(e,t){"use strict";var r=e.noop,n=e.reject;t["default"]=function(e,t){var i=this,o=new i(r,t);return n(o,e),o}}),t("rsvp/promise/resolve",["../-internal","exports"],function(e,t){"use strict";var r=e.noop,n=e.resolve;t["default"]=function(e,t){var i=this;if(e&&"object"==typeof e&&e.constructor===i)return e;var o=new i(r,t);return n(o,e),o}}),t("rsvp/race",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.race(e,t)}}),t("rsvp/reject",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.reject(e,t)}}),t("rsvp/resolve",["./promise","exports"],function(e,t){"use strict";var r=e["default"];t["default"]=function(e,t){return r.resolve(e,t)}}),t("rsvp/rethrow",["exports"],function(e){"use strict";e["default"]=function(e){throw setTimeout(function(){throw e}),e}}),t("rsvp/utils",["exports"],function(e){"use strict";function t(e){return"function"==typeof e||"object"==typeof e&&null!==e}function r(e){return"function"==typeof e}function n(e){return"object"==typeof e&&null!==e}e.objectOrFunction=t,e.isFunction=r,e.isMaybeThenable=n;var i;i=Array.isArray?Array.isArray:function(e){return"[object Array]"===Object.prototype.toString.call(e)};var o=i;e.isArray=o;var s=Date.now||function(){return(new Date).getTime()};e.now=s;var a=Object.create||function(e){var t=function(){};return t.prototype=e,t};e.o_create=a}),t("rsvp",["./rsvp/promise","./rsvp/events","./rsvp/node","./rsvp/all","./rsvp/all-settled","./rsvp/race","./rsvp/hash","./rsvp/hash-settled","./rsvp/rethrow","./rsvp/defer","./rsvp/config","./rsvp/map","./rsvp/resolve","./rsvp/reject","./rsvp/filter","./rsvp/asap","exports"],function(e,t,r,n,i,o,s,a,u,c,h,p,l,f,d,v,g){"use strict";function m(e,t){k.async(e,t)}function y(){k.on.apply(k,arguments)}function E(){k.off.apply(k,arguments)}var S=e["default"],P=t["default"],b=r["default"],w=n["default"],B=i["default"],x=o["default"],U=s["default"],J=a["default"],_=u["default"],R=c["default"],k=h.config,C=h.configure,T=p["default"],I=l["default"],N=f["default"],F=d["default"],A=v["default"];if(k.async=A,"undefined"!=typeof window&&"object"==typeof window.__PROMISE_INSTRUMENTATION__){var O=window.__PROMISE_INSTRUMENTATION__;C("instrument",!0);for(var j in O)O.hasOwnProperty(j)&&y(j,O[j])}g.Promise=S,g.EventTarget=P,g.all=w,g.allSettled=B,g.race=x,g.hash=U,g.hashSettled=J,g.rethrow=_,g.defer=R,g.denodeify=b,g.configure=C,g.on=y,g.off=E,g.resolve=I,g.reject=N,g.async=m,g.map=T,g.filter=F}),e.RSVP=r("rsvp")}(self),EPUBJS.Book=function(e){this.opening=new RSVP.defer,this.opened=this.opening.promise,this.isOpen=!1,this.url=void 0,this.spine=void 0,this.loading={manifest:new RSVP.defer,spine:new RSVP.defer,metadata:new RSVP.defer,cover:new RSVP.defer,navigation:new RSVP.defer,pageList:new RSVP.defer},this.loaded={manifest:this.loading.manifest.promise,spine:this.loading.spine.promise,metadata:this.loading.metadata.promise,cover:this.loading.cover.promise,navigation:this.loading.navigation.promise,pageList:this.loading.pageList.promise},this.ready=RSVP.hash(this.loaded),this.isRendered=!1,this._q=EPUBJS.core.queue(this),e&&this.open(e)},EPUBJS.Book.prototype.open=function(e){var t,r,n,i=new EPUBJS.Parser,o=this,s="META-INF/container.xml";return t="object"==typeof e?e:EPUBJS.core.uri(e),"opf"===t.extension?(this.packageUrl=t.href,this.containerUrl="",t.origin?this.url=t.origin+"/"+t.directory:window?(n=EPUBJS.core.uri(window.location.href),this.url=EPUBJS.core.resolveUrl(n.base,t.directory)):this.url=t.directory,r=this.request(this.packageUrl)):"epub"===t.extension||"zip"===t.extension?(this.archived=!0,this.url=""):t.extension||(this.containerUrl=e+s,r=this.request(this.containerUrl).then(function(e){return i.container(e)}).then(function(t){var r=EPUBJS.core.uri(t.packagePath);return o.packageUrl=e+t.packagePath,o.url=e+r.directory,o.encoding=t.encoding,o.request(o.packageUrl)}).catch(function(e){console.error("Could not load book at: "+(this.packageUrl||this.containerPath)),o.trigger("book:loadFailed",this.packageUrl||this.containerPath),o.opening.reject(e)})),r.then(function(e){o.unpack(e),o.loading.manifest.resolve(o.package.manifest),o.loading.metadata.resolve(o.package.metadata),o.loading.spine.resolve(o.spine),o.loading.cover.resolve(o.cover),this.isOpen=!0,o.opening.resolve(o)}).catch(function(e){console.error(e.message,e.stack),o.opening.reject(e)}),this.opened},EPUBJS.Book.prototype.unpack=function(e){var t=this,r=new EPUBJS.Parser;t.package=r.packageContents(e),t.package.baseUrl=t.url,t.spine=new EPUBJS.Spine(t.package,this.request),t.navigation=new EPUBJS.Navigation(t.package,this.request),t.navigation.load().then(function(e){t.toc=e,t.loading.navigation.resolve(t.toc)}),t.cover=t.url+t.package.coverPath},EPUBJS.Book.prototype.section=function(e){return this.spine.get(e)},EPUBJS.Book.prototype.renderTo=function(e,t){var r=new EPUBJS.Renderer(this,t);return r.attachTo(e),r},EPUBJS.Book.prototype.request=function(e){return this.archived?void 0:EPUBJS.core.request(e,"xml",this.credentials)},EPUBJS.Book.prototype.setCredentials=function(e){this.credentials=e},RSVP.EventTarget.mixin(EPUBJS.Book.prototype),RSVP.on("error",function(){}),RSVP.configure("instrument",!0),RSVP.on("rejected",function(e){console.error(e.detail.message,e.detail.stack)}),EPUBJS.core={},EPUBJS.core.request=function(e,t,r){function n(){if(this.readyState===this.DONE)if(200===this.status||this.responseXML){var e;e="xml"==t?this.responseXML:"json"==t?JSON.parse(this.response):"blob"==t?i?this.response:new Blob([this.response]):this.response,s.resolve(e)}else s.reject({message:this.response,stack:(new Error).stack})}var i=window.URL,o=i?"blob":"arraybuffer",s=new RSVP.defer,a=new XMLHttpRequest,u=XMLHttpRequest.prototype;return"overrideMimeType"in u||Object.defineProperty(u,"overrideMimeType",{value:function(){}}),r&&(a.withCredentials=!0),a.open("GET",e,!0),a.onreadystatechange=n,"blob"==t&&(a.responseType=o),"json"==t&&a.setRequestHeader("Accept","application/json"),"xml"==t&&a.overrideMimeType("text/xml"),a.send(),s.promise},EPUBJS.core.uri=function(e){var t,r,n,i={protocol:"",host:"",path:"",origin:"",directory:"",base:"",filename:"",extension:"",fragment:"",href:e},o=e.indexOf("://"),s=e.indexOf("?"),a=e.indexOf("#");return-1!=a&&(i.fragment=e.slice(a+1),e=e.slice(0,a)),-1!=s&&(i.search=e.slice(s+1),e=e.slice(0,s),href=e),-1!=o?(i.protocol=e.slice(0,o),t=e.slice(o+3),n=t.indexOf("/"),-1===n?(i.host=i.path,i.path=""):(i.host=t.slice(0,n),i.path=t.slice(n)),i.origin=i.protocol+"://"+i.host,i.directory=EPUBJS.core.folder(i.path),i.base=i.origin+i.directory):(i.path=e,i.directory=EPUBJS.core.folder(e),i.base=i.directory),i.filename=e.replace(i.base,""),r=i.filename.lastIndexOf("."),-1!=r&&(i.extension=i.filename.slice(r+1)),i},EPUBJS.core.folder=function(e){var t=e.lastIndexOf("/");if(-1==t)var r="";return r=e.slice(0,t+1)},EPUBJS.core.queue=function(e){var t=[],r=e,n=function(e,r,n){return t.push({funcName:e,args:r,context:n}),t},i=function(){var e;t.length&&(e=t.shift(),r[e.funcName].apply(e.context||r,e.args))},o=function(){for(;t.length;)i()},s=function(){t=[]},a=function(){return t.length};return{enqueue:n,dequeue:i,flush:o,clear:s,length:a}},EPUBJS.core.isElement=function(e){return!(!e||1!=e.nodeType)},EPUBJS.core.uuid=function(){var e=(new Date).getTime(),t="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(t){var r=(e+16*Math.random())%16|0;return e=Math.floor(e/16),("x"==t?r:7&r|8).toString(16)});return t},EPUBJS.core.values=function(e){for(var t=-1,r=Object.keys(e),n=r.length,i=Array(n);++t0;){if(r=n.shift(),"text"===r.type?(i=s.childNodes[r.index],s=i.parentNode||s):s=r.id?o.getElementById(r.id):a[r.index],"undefined"==typeof s)return console.error("No Element For",r,e.str),!1;a=Array.prototype.slice.call(s.children)}return s},EPUBJS.EpubCFI.prototype.compare=function(e,t){if("string"==typeof e&&(e=new EPUBJS.EpubCFI(e)),"string"==typeof t&&(t=new EPUBJS.EpubCFI(t)),e.spinePos>t.spinePos)return 1;if(e.spinePost.steps[r].index)return 1;if(e.steps[r].indext.characterOffset?1:e.characterOffset=0?(o=i.length,e.characterOffset=o&&t&&t()}var i,o;return"undefined"==typeof this.hooks[e]?!1:(i=this.hooks[e],o=i.length,0===o&&t&&t(),void i.forEach(function(e){e(n,r)}))},{register:function(e){if(void 0===EPUBJS.hooks[e]&&(EPUBJS.hooks[e]={}),"object"!=typeof EPUBJS.hooks[e])throw"Already registered: "+e;return EPUBJS.hooks[e]},mixin:function(t){for(var r in e.prototype)t[r]=e.prototype[r]}}}(),EPUBJS.Infinite=function(e,t){this.container=e,this.windowHeight=window.innerHeight,this.tick=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,this.scrolled=!1,this.ignore=!1,this.displaying=!1,this.offset=250,this.views=[],this.renderer=t,this.prevScrollTop=0},EPUBJS.Infinite.prototype.start=function(){this.container.addEventListener("scroll",function(){this.ignore?this.ignore=!1:this.scrolled=!0}.bind(this)),window.addEventListener("unload",function(){this.ignore=!0}),this.tick.call(window,this.check.bind(this)),this.scrolled=!1},EPUBJS.Infinite.prototype.forwards=function(){this.trigger("forwards")},EPUBJS.Infinite.prototype.backwards=function(){this.trigger("backwards")},EPUBJS.Infinite.prototype.check=function(){if(this.scrolled&&!this.displaying){var e=this.container.scrollTop,t=(this.container.scrollLeft,this.container.scrollHeight),r=(this.container.scrollWidth,e-this.prevScrollTop),n=this.container.getBoundingClientRect().height,i=e+this.offset>t-n,o=e0?this.forwards():o&&0>r&&this.backwards(),this.prevScrollTop=e,this.scrolled=!1}else this.displaying=!1,this.scrolled=!1;this.tick.call(window,this.check.bind(this))},EPUBJS.Infinite.prototype.scrollBy=function(e,t,r){r&&(this.displaying=!0),this.container.scrollLeft+=e,this.container.scrollTop+=t},EPUBJS.Infinite.prototype.scroll=function(e,t,r){r&&(this.displaying=!0),this.container.scrollLeft=e,this.container.scrollTop=t},RSVP.EventTarget.mixin(EPUBJS.Infinite.prototype),EPUBJS.Layout=function(){},EPUBJS.Layout.prototype.reconcileLayoutSettings=function(e,t){var r={};for(var n in e)e.hasOwnProperty(n)&&(r[n]=e[n]);return t.forEach(function(e){var t,n,i=e.replace("rendition:",""),o=i.indexOf("-");-1!=o&&(t=i.slice(0,o),n=i.slice(o+1),r[t]=n)}),r},EPUBJS.Layout.prototype.determineLayout=function(e){var t=this.determineSpreads(this.minSpreadWidth),r=t?"ReflowableSpreads":"Reflowable",n=!1;return"pre-paginated"===e.layout&&(r="Fixed",n=!0,t=!1),"reflowable"===e.layout&&"none"===e.spread&&(r="Reflowable",n=!1,t=!1),"reflowable"===e.layout&&"both"===e.spread&&(r="ReflowableSpreads",n=!1,t=!0),this.spreads=t,this.render.scroll(n),this.trigger("renderer:spreads",t),r},EPUBJS.Layout.prototype.applyStyles=function(e){for(var t in e)for(var r in this.views)r.setStyle(t,e[t])},EPUBJS.Layout.prototype.setStyle=function(e,t,r){for(var n in this.views)n.setStyle(e,t,r)},EPUBJS.Layout.prototype.removeStyle=function(e){for(var t in this.views)t.removeStyle(e)},EPUBJS.Layout.prototype.applyHeadTags=function(e){for(var t in e)this.render.addHeadTag(t,e[t])},EPUBJS.Navigation=function(e,t){var r=this,n=new EPUBJS.Parser,i=t||EPUBJS.core.request;this.package=e,this.toc=[],this.tocByHref={},this.tocById={},e.navPath&&(this.navUrl=e.baseUrl+e.navPath,this.nav={},this.nav.load=function(){var e=new RSVP.defer,t=e.promise;return i(r.navUrl,"xml").then(function(t){r.toc=n.nav(t),r.loaded(r.toc),e.resolve(r.toc)}),t}),e.ncxPath&&(this.ncxUrl=e.baseUrl+e.ncxPath,this.ncx={},this.ncx.load=function(){var e=new RSVP.defer,t=e.promise;return i(r.ncxUrl,"xml").then(function(t){r.toc=n.ncx(t),r.loaded(r.toc),e.resolve(r.toc)}),t})},EPUBJS.Navigation.prototype.load=function(e){{var t;e||EPUBJS.core.request}return this.nav?t=this.nav.load():this.ncx&&(t=this.ncx.load()),t},EPUBJS.Navigation.prototype.loaded=function(e){for(var t,r=0;r=0;s--){var a=i.snapshotItem(s),u=a.getAttribute("id")||!1,c=a.querySelector("content"),h=c.getAttribute("src"),p=a.querySelector("navLabel"),l=p.textContent?p.textContent:"",f=h.split("#"),d=(f[0],t(a));n.unshift({id:u,href:h,label:l,subitems:d,parent:r?r.getAttribute("id"):null})}return n}var r=e.querySelector("navMap");return r?t(r):[]},EPUBJS.Renderer=function(e,t){var r=t||{};this.settings={hidden:r.hidden||!1,viewsLimit:4,width:r.width||!1,height:r.height||!1},this.book=e,this.epubcfi=new EPUBJS.EpubCFI,this.layoutSettings={},EPUBJS.Hooks.mixin(this),this.getHooks("beforeChapterDisplay"),this._q=EPUBJS.core.queue(this),this.position=1,this.initialize({width:this.settings.width,height:this.settings.height,hidden:!0}),this.rendering=!1,this.views=[],this.positions=[]},EPUBJS.Renderer.prototype.initialize=function(e){{var t=e||{},r=t.height?t.height+"px":"100%",n=t.width?t.width+"px":"100%";t.hidden||!1}return this.container=document.createElement("div"),this.infinite=new EPUBJS.Infinite(this.container,this),this.container.style.width=n,this.container.style.height=r,this.container.style.overflow="scroll",t.hidden?(this.wrapper=document.createElement("div"),this.wrapper.style.visibility="hidden",this.wrapper.style.overflow="hidden",this.wrapper.style.width="0",this.wrapper.style.height="0",this.wrapper.appendChild(this.container),this.wrapper):this.container},EPUBJS.Renderer.prototype.resize=function(e,t){var r=e,n=t;e||(r=window.innerWidth),t||(n=window.innerHeight),this.container.style.width=r+"px",this.container.style.height=n+"px",this.trigger("resized",{width:this.width,height:this.height})},EPUBJS.Renderer.prototype.onResized=function(){var e=this.element.getBoundingClientRect();this.resize(e.width,e.height)},EPUBJS.Renderer.prototype.attachTo=function(e){var t;return EPUBJS.core.isElement(e)?this.element=e:"string"==typeof e&&(this.element=document.getElementById(e)),this.element?(this.element.appendChild(this.container),this.settings.height||this.settings.width||(t=this.element.getBoundingClientRect(),this.resize(t.width,t.height)),this.infinite.start(),this.infinite.on("forwards",function(){this.rendering||this.forwards()}.bind(this)),this.infinite.on("backwards",function(){this.rendering||this.backwards()}.bind(this)),void window.addEventListener("resize",this.onResized.bind(this),!1)):void console.error("Not an Element")},EPUBJS.Renderer.prototype.clear=function(){this.views.forEach(function(e){e.destroy()}),this.views=[]},EPUBJS.Renderer.prototype.display=function(e){var t=new RSVP.defer,r=t.promise;return this.clear(),this.book.opened.then(function(){var r=this.book.spine.get(e),n=this.render(r);n.then(this.fill.bind(this)).then(function(){t.resolve(this)}.bind(this))}.bind(this)),r},EPUBJS.Renderer.prototype.render=function(e){var t,r=new EPUBJS.View;return e?(t=e.render(),r.index=e.index,t.then(function(t){return this.insert(r,e.index),r.load(t)}.bind(this)).then(function(){return this.rendering=!1,r}.bind(this))):void t.reject()},EPUBJS.Renderer.prototype.forwards=function(){var e,t,r;return e=this.last().index+1,this.rendering||e===this.book.spine.length?(t=new RSVP.defer,t.reject({message:"reject forwards"}),t.promise):(console.log("going forwards"),this.rendering=!0,r=this.book.spine.get(e),t=this.render(r),t.then(function(){var e=this.first(),t=e.bounds(),r=(this.container.getBoundingClientRect(),this.container.scrollTop);this.views.length>this.settings.viewsLimit&&(this.remove(e),this.infinite.scroll(0,r-t.height,!0))}.bind(this)),t)},EPUBJS.Renderer.prototype.backwards=function(){var e,t,r;return e=this.first().index-1,this.rendering||0>e?(t=new RSVP.defer,t.reject({message:"reject backwards"}),t.promise):(console.log("going backwards"),this.rendering=!0,r=this.book.spine.get(e),t=this.render(r),t.then(function(){this.infinite.scrollBy(0,this.first().height,!0),this.views.length>this.settings.viewsLimit&&(last=this.last(),this.remove(last))}.bind(this)),t)},EPUBJS.Renderer.prototype.fill=function(){var e=this.container.getBoundingClientRect().height,t=function(){var r=this.last().bounds().bottom,n=new RSVP.defer,i=n.promise;return e&&r&&e>r&&this.last().index+1=0?this.append(e):t-this.last().index<=0&&this.prepend(e):this.append(e),console.log("insert")},EPUBJS.Renderer.prototype.remove=function(e){var t=this.views.indexOf(e);e.destroy(),t>-1&&this.views.splice(t,1)},EPUBJS.Renderer.prototype.first=function(){return this.views[0]},EPUBJS.Renderer.prototype.last=function(){return this.views[this.views.length-1]},RSVP.EventTarget.mixin(EPUBJS.Renderer.prototype),EPUBJS.Spine=function(e,t){this.items=e.spine,this.manifest=e.manifest,this.spineNodeIndex=e.spineNodeIndex,this.baseUrl=e.baseUrl||"",this.request=t,this.length=this.items.length,this.epubcfi=new EPUBJS.EpubCFI,this.spineItems=[],this.spineByHref={},this.spineById={},this.items.forEach(function(e,t){var r,n,i,o=this.epubcfi.generateChapterComponent(this.spineNodeIndex,e.index,e.idref),s=this.manifest[e.idref];s&&(r=s.href,n=this.baseUrl+r),i=new EPUBJS.SpineItem(e,r,n,o),this.spineItems.push(i),this.spineByHref[i.href]=t,this.spineById[i.idref]=t}.bind(this))},EPUBJS.Spine.prototype.get=function(e){var t=0;return!e||"number"!=typeof e&&isNaN(e)!==!1?e&&0===e.indexOf("#")?t=this.spineById[e.substring(1)]:e&&(t=this.spineByHref[e]):t=e,this.spineItems[t]},EPUBJS.SpineItem=function(e,t,r,n){this.idref=e.idref,this.linear=e.linear,this.properties=e.properties,this.index=e.index,this.href=t,this.url=r,this.cfiBase=n},EPUBJS.SpineItem.prototype.load=function(e){var t=e||this.request||EPUBJS.core.request,r=new RSVP.defer,n=r.promise;return this.contents?r.resolve(this.contents):t(this.url,"xml").then(function(e){EPUBJS.core.folder(this.url);this.document=e,this.contents=e.documentElement,this.replacements(this.document),r.resolve(this.contents)}.bind(this)),n},EPUBJS.SpineItem.prototype.replacements=function(e){var t=e.createElement("base");t.setAttribute("href",this.url),e.head.insertBefore(t,e.head.firstChild)},EPUBJS.SpineItem.prototype.render=function(){var e=new RSVP.defer,t=e.promise;return this.load().then(function(t){var r=new XMLSerializer,n=r.serializeToString(t);e.resolve(n)}),t},EPUBJS.SpineItem.prototype.find=function(){},EPUBJS.View=function(){this.id="epubjs-view:"+EPUBJS.core.uuid(),this.loading=new RSVP.defer,this.loaded=this.loading.promise,this.iframe=this.create(),this.height,this.width},EPUBJS.View.prototype.load=function(e){var t=new RSVP.defer,r=t.promise;return this.document=this.iframe.contentDocument,this.iframe.addEventListener("load",function(){this.window=this.iframe.contentWindow,this.document=this.iframe.contentDocument,this.iframe.style.display="block",this.document.body.style.margin="0",this.document.body.style.display="inline-block",this.layout(),this.iframe.style.visibility="visible",setTimeout(function(){this.window.addEventListener("resize",this.resized.bind(this),!1)}.bind(this),10),this.document.fonts.onloading=function(){console.log("loaded fonts")}.bind(this),t.resolve(this),this.loading.resolve(this)}.bind(this)),this.document.open(),this.document.write(e),this.document.close(),r},EPUBJS.View.prototype.create=function(){return this.iframe=document.createElement("iframe"),this.iframe.id=this.id,this.iframe.scrolling="no",this.iframe.seamless="seamless",this.iframe.style.border="none",this.resizing=!0,this.iframe.width="100%",this.iframe.style.height="100%",this.iframe.style.display="none",this.iframe.style.visibility="hidden",this.iframe},EPUBJS.View.prototype.resized=function(){this.resizing?this.resizing=!1:this.layout()},EPUBJS.View.prototype.layout=function(){var e;console.log("layout"),e=this.document.body.getBoundingClientRect(),(!e||0==e.height&&0==e.width)&&console.error("View not shown"),this.resizing=!0,this.iframe.style.height=e.height+"px",e=this.document.body.getBoundingClientRect(),this.resizing=!0,this.iframe.style.height=e.height+"px",this.width=e.width,this.height=e.height},EPUBJS.View.prototype.observe=function(e){var t=this,r=new MutationObserver(function(e){e.forEach(function(){t.layout()})}),n={attributes:!0,childList:!0,characterData:!0,subtree:!0};return r.observe(e,n),r},EPUBJS.View.prototype.appendTo=function(e){this.element=e,this.element.appendChild(this.iframe)},EPUBJS.View.prototype.prependTo=function(e){this.element=e,e.insertBefore(this.iframe,e.firstChild)},EPUBJS.View.prototype.bounds=function(){return this.iframe.getBoundingClientRect()},EPUBJS.View.prototype.destroy=function(){this.element.removeChild(this.iframe)}; \ No newline at end of file diff --git a/examples/basic-render.html b/examples/basic-render.html index 0bc65ce..a4e1f33 100644 --- a/examples/basic-render.html +++ b/examples/basic-render.html @@ -12,14 +12,14 @@ } #viewer { - /* position: absolute; + position: absolute; left: 10%; width: 80%; height: 100%; - overflow: hidden; */ - display: block; + overflow: hidden; +/* display: block; margin: 50px auto; - width: 600px; + width: 600px;*/ } #prev { @@ -66,7 +66,7 @@ var currentSectionIndex = 10; // Load the opf var book = ePub("../books/moby-dick/OPS/package.opf"); - var rendition = book.renderTo("viewer", {width: 600, height: 400}); + var rendition = book.renderTo("viewer"); var displayed = rendition.display(currentSectionIndex); diff --git a/examples/toc.html b/examples/toc.html index 253ffa7..e9bd9da 100644 --- a/examples/toc.html +++ b/examples/toc.html @@ -64,9 +64,9 @@ var $viewer = document.getElementById("viewer"); var $next = document.getElementById("next"); var $prev = document.getElementById("prev"); - var currentSectionIndex = 10; + var currentSectionIndex = 9; // Load the opf - var book = ePub("https://s3.amazonaws.com/moby-dick/OPS/package.opf"); + var book = ePub("../books/moby-dick/OPS/package.opf"); // https://s3.amazonaws.com/moby-dick/OPS/package.opf var rendition = book.renderTo("viewer", {width: 600, height: 400}); var displayed = rendition.display(currentSectionIndex); diff --git a/gulpfile.js b/gulpfile.js index 14fdc64..cb54aba 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -3,7 +3,11 @@ var jshint = require('gulp-jshint'); var concat = require('gulp-concat'); var rename = require('gulp-rename'); var uglify = require('gulp-uglify'); - +var gutil = require('gulp-util'); +var plumber = require('gulp-plumber'); +var onError = function (err) { + gutil.log(gutil.colors.green(err)); +}; // Lint JS gulp.task('lint', function() { @@ -15,6 +19,7 @@ gulp.task('lint', function() { // Concat & Minify JS gulp.task('minify', function(){ return gulp.src(['lib/*.js', 'bower_components/rsvp/rsvp.js', 'lib/epubjs/*.js']) + .pipe(plumber({ errorHandler: onError })) .pipe(concat('epub.js')) .pipe(gulp.dest('dist')) .pipe(rename('epub.min.js')) diff --git a/lib/epubjs/infinite.js b/lib/epubjs/infinite.js index 4d3c39f..d6ee196 100644 --- a/lib/epubjs/infinite.js +++ b/lib/epubjs/infinite.js @@ -88,19 +88,18 @@ EPUBJS.Infinite.prototype.check = function(){ if(this.scrolled && !this.displaying) { - // var scrollTop = window.pageYOffset || document.documentElement.scrollTop - // var scrollLeft = window.pageXOffset || document.documentElement.scrollLeft var scrollTop = this.container.scrollTop; + var scrollLeft = this.container.scrollLeft; - // var scrollTop = document.body.scrollTop;//TODO: make document.body a variable - // var scrollHeight = document.documentElement.scrollHeight; var scrollHeight = this.container.scrollHeight; + var scrollWidth = this.container.scrollWidth; + var direction = scrollTop - this.prevScrollTop; var height = this.container.getBoundingClientRect().height; - var up = height - (scrollHeight - scrollTop) > -this.offset; + var up = scrollTop + this.offset > scrollHeight-height; var down = scrollTop < this.offset; - + // console.debug("scroll", scrollTop) if(up && direction > 0) { this.forwards(); } @@ -108,13 +107,32 @@ EPUBJS.Infinite.prototype.check = function(){ this.backwards(); } - // console.log(document.body.scrollTop) + // console.log(scrollTop) this.prevScrollTop = scrollTop; + this.scrolled = false; + } else { + this.displaying = false; this.scrolled = false; } this.tick.call(window, this.check.bind(this)); } +EPUBJS.Infinite.prototype.scrollBy = function(x, y, silent){ + if(silent) { + this.displaying = true; + } + this.container.scrollLeft += x; + this.container.scrollTop += y; +}; + +EPUBJS.Infinite.prototype.scroll = function(x, y, silent){ + if(silent) { + this.displaying = true; + } + this.container.scrollLeft = x; + this.container.scrollTop = y; +}; + RSVP.EventTarget.mixin(EPUBJS.Infinite.prototype); \ No newline at end of file diff --git a/lib/epubjs/renderer.js b/lib/epubjs/renderer.js index de87914..0466bf4 100644 --- a/lib/epubjs/renderer.js +++ b/lib/epubjs/renderer.js @@ -2,7 +2,7 @@ EPUBJS.Renderer = function(book, _options) { var options = _options || {}; this.settings = { hidden: options.hidden || false, - viewLimit: 3, + viewsLimit: 4, width: options.width || false, height: options.height || false, }; @@ -125,8 +125,13 @@ EPUBJS.Renderer.prototype.attachTo = function(_element){ this.infinite.start(); - this.infinite.on("forwards", this.forwards.bind(this)); - this.infinite.on("backwards", this.backwards.bind(this)); + this.infinite.on("forwards", function(){ + if(!this.rendering) this.forwards(); + }.bind(this)); + + this.infinite.on("backwards", function(){ + if(!this.rendering) this.backwards(); + }.bind(this)); window.addEventListener("resize", this.onResized.bind(this), false); @@ -151,10 +156,11 @@ EPUBJS.Renderer.prototype.display = function(what){ var section = this.book.spine.get(what); var rendered = this.render(section); - rendered.then(function(){ - this.fill(); - displaying.resolve(this); - }.bind(this)); + rendered. + then(this.fill.bind(this)). + then(function(){ + displaying.resolve(this); + }.bind(this)); }.bind(this)); @@ -173,12 +179,19 @@ EPUBJS.Renderer.prototype.render = function(section){ rendered = section.render(); view.index = section.index; - // Place view in correct position - this.insert(view, section.index); + - return rendered.then(function(contents){ - return view.load(contents); - }); + return rendered. + then(function(contents){ + // Place view in correct position + this.insert(view, section.index); + + return view.load(contents); + }.bind(this)) + .then(function(){ + this.rendering = false; + return view; + }.bind(this)); }; @@ -188,23 +201,36 @@ EPUBJS.Renderer.prototype.forwards = function(){ var rendered; var section; - if(this.rendering) return; - console.log("going forwards") - next = this.last().index + 1; - if(next === this.book.spine.length){ - return; + if(this.rendering || next === this.book.spine.length){ + rendered = new RSVP.defer(); + rendered.reject({message: "reject forwards"}); + return rendered.promise; } + console.log("going forwards") + + this.rendering = true; section = this.book.spine.get(next); rendered = this.render(section); - this.rendering = true; rendered.then(function(){ - console.log("last:", this.last().height) + // this.rendering = false; + var first = this.first(); + var bounds = first.bounds(); + var container = this.container.getBoundingClientRect(); + var offset; + var prev = this.container.scrollTop; + if(this.views.length > this.settings.viewsLimit) { + + // Remove the item + this.remove(first); - this.rendering = false; + // Reset Position + this.infinite.scroll(0, prev - bounds.height, true) + + } }.bind(this)); return rendered; @@ -215,23 +241,28 @@ EPUBJS.Renderer.prototype.backwards = function(view){ var rendered; var section; - if(this.rendering) return; - console.log("going backwards") prev = this.first().index - 1; - if(prev < 0){ - return; //TODO: should reject + if(this.rendering || prev < 0){ + rendered = new RSVP.defer(); + rendered.reject({message: "reject backwards"}); + return rendered.promise; } + console.log("going backwards") + + this.rendering = true; section = this.book.spine.get(prev); rendered = this.render(section); - this.rendering = true; rendered.then(function(){ - this.rendering = false; - // -- this might want to be in infinite - this.container.scrollTop += this.first().height; + // this.container.scrollTop += this.first().height; + this.infinite.scrollBy(0, this.first().height, true); + if(this.views.length > this.settings.viewsLimit) { + last = this.last(); + this.remove(last); + } }.bind(this)); return rendered; @@ -242,31 +273,45 @@ EPUBJS.Renderer.prototype.backwards = function(view){ // -- this might want to be in infinite EPUBJS.Renderer.prototype.fill = function() { - var filling = this.backwards(); var height = this.container.getBoundingClientRect().height; - - if(!filling) return; - - filling.then(function(){ + var next = function(){ var bottom = this.last().bounds().bottom; - while (height && bottom && bottom < height) { - this.forwards(); - }; - }.bind(this)); -}; + var defer = new RSVP.defer(); + var promise = defer.promise; + + if (height && bottom && (bottom < height) && (this.last().index + 1 < this.book.spine.length)) { + return this.forwards().then(next); + } else { + this.rendering = false; + defer.resolve(); + return promise; + } + }.bind(this); + + + return next(). + then(this.backwards.bind(this)). + then(function(){ + this.rendering = false; + }.bind(this)); + -EPUBJS.Renderer.prototype.jump = function(view){ - this.views.push(view); }; EPUBJS.Renderer.prototype.append = function(view){ + var first, prevTop, prevHeight, offset; + this.views.push(view); view.appendTo(this.container); + }; EPUBJS.Renderer.prototype.prepend = function(view){ + var last; + this.views.unshift(view); view.prependTo(this.container); + }; // Simple Insert @@ -279,13 +324,17 @@ EPUBJS.Renderer.prototype.insert = function(view, index){ } else if(index - this.last().index <= 0) { this.prepend(view); } - + console.log("insert") // return position; }; // Remove the render element and clean up listeners -EPUBJS.Renderer.prototype.destroy = function() { - +EPUBJS.Renderer.prototype.remove = function(view) { + var index = this.views.indexOf(view); + view.destroy(); + if(index > -1) { + this.views.splice(index, 1); + } }; EPUBJS.Renderer.prototype.first = function() { diff --git a/lib/epubjs/view.js b/lib/epubjs/view.js index 749fd10..5ea638e 100644 --- a/lib/epubjs/view.js +++ b/lib/epubjs/view.js @@ -1,4 +1,4 @@ -EPUBJS.View = function(options) { +EPUBJS.View = function(width, height) { this.id = "epubjs-view:" + EPUBJS.core.uuid(); this.loading = new RSVP.defer(); this.loaded = this.loading.promise; @@ -13,9 +13,10 @@ EPUBJS.View.prototype.load = function(contents) { this.document = this.iframe.contentDocument; - this.iframe.onload = function(e) { + this.iframe.addEventListener("load", function(event) { + var layout; + this.window = this.iframe.contentWindow; - this.window.addEventListener("resize", this.resized.bind(this), false); this.document = this.iframe.contentDocument; this.iframe.style.display = "block"; @@ -25,15 +26,25 @@ EPUBJS.View.prototype.load = function(contents) { this.document.body.style.display = "inline-block"; this.layout(); - - // This needs to run twice to get to the correct size sometimes? - this.layout(); - + this.iframe.style.visibility = "visible"; + setTimeout(function(){ + this.window.addEventListener("resize", this.resized.bind(this), false); + }.bind(this), 10); // Wait to listen for resize events + + this.document.fonts.onloading = function(){ + console.log("loaded fonts"); + // this.layout(); + }.bind(this); + + // this.observer = this.observe(this.document); + loading.resolve(this); this.loading.resolve(this); - }.bind(this); + + }.bind(this)); + // this.iframe.srcdoc = contents; this.document.open(); @@ -43,11 +54,6 @@ EPUBJS.View.prototype.load = function(contents) { return loaded; }; - -EPUBJS.View.prototype.unload = function() { - -}; - EPUBJS.View.prototype.create = function() { this.iframe = document.createElement('iframe'); this.iframe.id = this.id; @@ -55,16 +61,19 @@ EPUBJS.View.prototype.create = function() { this.iframe.seamless = "seamless"; // Back up if seamless isn't supported this.iframe.style.border = "none"; + + this.resizing = true; this.iframe.width = "100%"; - this.iframe.style.height = "0"; + this.iframe.style.height = "100%"; this.iframe.style.display = "none"; this.iframe.style.visibility = "hidden"; + return this.iframe; }; -EPUBJS.View.prototype.resized = function() { +EPUBJS.View.prototype.resized = function(e) { if (!this.resizing) { this.layout(); @@ -75,32 +84,48 @@ EPUBJS.View.prototype.resized = function() { }; EPUBJS.View.prototype.layout = function() { - var bounds = {}, content, width = 0, height = 0; - - this.resizing = true; + var bounds; + console.log("layout") // Check bounds bounds = this.document.body.getBoundingClientRect(); + if(!bounds || (bounds.height == 0 && bounds.width == 0)) { + console.error("View not shown"); + } + // Apply Changes + this.resizing = true; this.iframe.style.height = bounds.height + "px"; - this.iframe.style.width = bounds.width + "px"; + // this.iframe.style.width = bounds.width + "px"; // Check again - content = this.document.body.getBoundingClientRect(); + bounds = this.document.body.getBoundingClientRect(); + this.resizing = true; + this.iframe.style.height = bounds.height + "px"; + // this.iframe.style.width = bounds.width + "px"; - height = content.height; - width = content.width; + this.width = bounds.width; + this.height = bounds.height; - this.width = width; - this.height = height; - - - // if(bounds.height != content.height || bounds.width != content.width) { - // // this.layout(); - // console.log(bounds, content) - // } - +}; +EPUBJS.View.prototype.observe = function(target) { + var renderer = this; + + // create an observer instance + var observer = new MutationObserver(function(mutations) { + mutations.forEach(function(mutation) { + renderer.layout(); + }); + }); + + // configuration of the observer: + var config = { attributes: true, childList: true, characterData: true, subtree: true }; + + // pass in the target node, as well as the observer options + observer.observe(target, config); + + return observer; }; EPUBJS.View.prototype.appendTo = function(element) { @@ -118,6 +143,9 @@ EPUBJS.View.prototype.bounds = function() { }; EPUBJS.View.prototype.destroy = function() { + // Stop observing + // this.observer.disconnect(); + this.element.removeChild(this.iframe); }; diff --git a/package.json b/package.json index 8bd3b2a..5c52faf 100644 --- a/package.json +++ b/package.json @@ -17,15 +17,16 @@ "express": "^4.5.1", "gulp": "^3.8.7", "gulp-concat": "^2.3.4", + "gulp-connect": "~2.0.6", "gulp-jshint": "^1.8.4", + "gulp-plumber": "^0.6.4", "gulp-rename": "^1.2.0", "gulp-uglify": "^0.3.1", + "gulp-util": "^3.0.0", "morgan": "^1.1.1", "optimist": "^0.6.1", "portfinder": "^0.2.1", "qunitjs": "^1.14.0", - "serve-static": "^1.3.1", - "gulp-util": "~3.0.0", - "gulp-connect": "~2.0.6" + "serve-static": "^1.3.1" } }