From ae726a1dab2766b641bf1a9fd166bbd131a5d48d Mon Sep 17 00:00:00 2001 From: fchasen Date: Fri, 26 Jun 2015 16:32:24 -0400 Subject: [PATCH] Updated offset location on display --- dist/epub.js | 427 ++++++++++++++++++++++++------------------ dist/epub.min.js | 6 +- examples/single.html | 14 +- examples/spreads.html | 34 ++-- src/continuous.js | 190 ++++++++++--------- src/rendition.js | 136 +++++++++----- src/view.js | 103 +++++----- 7 files changed, 517 insertions(+), 393 deletions(-) diff --git a/dist/epub.js b/dist/epub.js index 0abec5e..b5a3d65 100644 --- a/dist/epub.js +++ b/dist/epub.js @@ -3839,7 +3839,7 @@ EPUBJS.View = function(section, options) { this.element = document.createElement('div'); this.element.classList.add("epub-view"); - + // this.element.style.minHeight = "100px"; this.element.style.height = "0px"; @@ -3849,7 +3849,7 @@ EPUBJS.View = function(section, options) { this.added = false; this.displayed = false; this.rendered = false; - + //this.width = 0; //this.height = 0; @@ -3861,7 +3861,7 @@ EPUBJS.View = function(section, options) { } else { this.element.style.display = "block"; } - + }; EPUBJS.View.prototype.create = function() { @@ -3877,13 +3877,13 @@ 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.style.display = "none"; this.element.style.visibility = "hidden"; this.iframe.style.visibility = "hidden"; - + this.iframe.style.width = "0"; this.iframe.style.height = "0"; this._width = 0; @@ -3891,7 +3891,7 @@ EPUBJS.View.prototype.create = function() { this.element.appendChild(this.iframe); this.added = true; - + this.elementBounds = EPUBJS.core.bounds(this.element); // if(width || height){ @@ -3901,7 +3901,7 @@ EPUBJS.View.prototype.create = function() { // } else { // this.iframeBounds = EPUBJS.core.bounds(this.iframe); // } - + // Firefox has trouble with baseURI and srcdoc // Disabled for now /* @@ -3918,7 +3918,7 @@ EPUBJS.View.prototype.create = function() { EPUBJS.View.prototype.lock = function(what, width, height) { - + var elBorders = EPUBJS.core.borders(this.element); var iframeBorders; @@ -3938,10 +3938,10 @@ EPUBJS.View.prototype.lock = function(what, width, height) { this.resize(width, this.lockedHeight); } - if(what === "both" && + if(what === "both" && EPUBJS.core.isNumber(width) && EPUBJS.core.isNumber(height)){ - + this.lockedWidth = width - elBorders.width - iframeBorders.width; this.lockedHeight = height - elBorders.height - iframeBorders.height; @@ -3955,14 +3955,14 @@ EPUBJS.View.prototype.lock = function(what, width, height) { } - + }; EPUBJS.View.prototype.expand = function(force) { var width = this.lockedWidth; var height = this.lockedHeight; - + var textWidth, textHeight; // console.log("expanding a") if(!this.iframe || this._expanding) return; @@ -3987,7 +3987,7 @@ EPUBJS.View.prototype.expand = function(force) { width = this._contentWidth; } } - + // Expand Vertically if(width && !height) { textHeight = this.textHeight(); @@ -3999,7 +3999,7 @@ EPUBJS.View.prototype.expand = function(force) { height = this._contentHeight; } } - + // Only Resize if dimensions have changed or // if Frame is still hidden, so needs reframing if(this._needsReframe || width != this._width || height != this._height){ @@ -4012,7 +4012,7 @@ EPUBJS.View.prototype.expand = function(force) { EPUBJS.View.prototype.contentWidth = function(min) { var prev; var width; - + // Save previous width prev = this.iframe.style.width; // Set the iframe size to min, width will only ever be greater @@ -4039,10 +4039,10 @@ EPUBJS.View.prototype.contentHeight = function(min) { EPUBJS.View.prototype.textWidth = function() { var width; var range = this.document.createRange(); - + // Select the contents of frame range.selectNodeContents(this.document.body); - + // get the width of the text content width = range.getBoundingClientRect().width; return width; @@ -4052,15 +4052,15 @@ EPUBJS.View.prototype.textWidth = function() { EPUBJS.View.prototype.textHeight = function() { var height; var range = this.document.createRange(); - + range.selectNodeContents(this.document.body); - - height = range.getBoundingClientRect().height; + + height = range.getBoundingClientRect().height; return height; }; EPUBJS.View.prototype.resize = function(width, height) { - + if(!this.iframe) return; if(EPUBJS.core.isNumber(width)){ @@ -4086,7 +4086,7 @@ EPUBJS.View.prototype.reframe = function(width, height) { this._needsReframe = true; return; } - + if(EPUBJS.core.isNumber(width)){ this.element.style.width = width + "px"; } @@ -4146,29 +4146,29 @@ EPUBJS.View.prototype.load = function(contents) { } this.iframe.onload = function(event) { - + this.window = this.iframe.contentWindow; this.document = this.iframe.contentDocument; this.rendering = false; loading.resolve(this); - + }.bind(this); - + if(this.supportsSrcdoc){ this.iframe.srcdoc = contents; } else { - + this.document = this.iframe.contentDocument; - + if(!this.document) { loading.reject(new Error("No Document Available")); return loaded; } - + this.document.open(); this.document.write(contents); this.document.close(); - + } return loaded; @@ -4183,7 +4183,7 @@ EPUBJS.View.prototype.layout = function(layoutFunc) { this.document.body.style.margin = "0"; //this.document.body.style.display = "inline-block"; //this.document.documentElement.style.width = "auto"; - + if(layoutFunc){ layoutFunc(this); } @@ -4206,7 +4206,7 @@ EPUBJS.View.prototype.listeners = function() { // Wait for fonts to load to finish // http://dev.w3.org/csswg/css-font-loading/ // Not implemented fully except in chrome - + if(this.document.fonts && this.document.fonts.status === "loading") { // console.log("fonts unloaded"); this.document.fonts.onloadingdone = function(){ @@ -4222,15 +4222,15 @@ EPUBJS.View.prototype.listeners = function() { this.imageLoadListeners(); this.mediaQueryListeners(); - + this.resizeListenters(); - + }; EPUBJS.View.prototype.resizeListenters = function() { // Test size again clearTimeout(this.expanding); - // this.expanding = setTimeout(this.expand.bind(this), 350); + // this.expanding = setTimeout(this.expand.bind(this), 350); }; //https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js @@ -4283,7 +4283,7 @@ EPUBJS.View.prototype.observe = function(target) { // this.element = element; // this.element.appendChild(this.iframe); // }; -// +// // EPUBJS.View.prototype.prependTo = function(element) { // this.element = element; // element.insertBefore(this.iframe, element.firstChild); @@ -4349,7 +4349,7 @@ EPUBJS.View.prototype.onDisplayed = function(view) { // Stub, override with a custom functions }; -EPUBJS.View.prototype.bounds = function() { +EPUBJS.View.prototype.bounds = function() { if(!this.elementBounds) { this.elementBounds = EPUBJS.core.bounds(this.element); } @@ -4367,7 +4367,7 @@ EPUBJS.View.prototype.destroy = function() { this.element.removeChild(this.iframe); this.displayed = false; this.iframe = null; - + this._textWidth = null; this._textHeight = null; this._width = null; @@ -4383,7 +4383,9 @@ EPUBJS.View.prototype.root = function() { }; EPUBJS.View.prototype.locationOf = function(target) { - + var parentPos = this.iframe.getBoundingClientRect(); + var targetPos = {"left": 0, "top": 0}; + if(!this.document) return; if(this.epubcfi.isCfiString(target)) { @@ -4395,38 +4397,41 @@ EPUBJS.View.prototype.locationOf = function(target) { // Must Clean up Marker before going to page this.epubcfi.removeMarker(marker, this.document); - return marker.getBoundingClientRect(); + targetPos = marker.getBoundingClientRect(); } } else { range = this.epubcfi.generateRangeFromCfi(cfi, this.document); if(range) { - return range.getBoundingClientRect(); + targetPos = range.getBoundingClientRect(); } } } else if(typeof target === "string" && target.indexOf("#") > -1) { - id = target.substring(target.indexOf("#")); + id = target.substring(target.indexOf("#")+1); el = this.document.getElementById(id); if(el) { - return el.getBoundingClientRect(); + targetPos = el.getBoundingClientRect(); } } - return {"left": 0, "top": 0}; + return { + "left": window.scrollX + parentPos.left + targetPos.left, + "top": window.scrollY + parentPos.top + targetPos.top + }; }; EPUBJS.View.prototype.addCss = function(src) { var $stylesheet = document.createElement('link'); var ready = false; - + return new RSVP.Promise(function(resolve, reject){ if(!this.document) { resolve(false); return; } - + $stylesheet.type = 'text/css'; $stylesheet.rel = "stylesheet"; $stylesheet.href = src; @@ -4439,7 +4444,7 @@ EPUBJS.View.prototype.addCss = function(src) { }, 1); } }; - + this.document.head.appendChild($stylesheet); }.bind(this)); @@ -4449,9 +4454,9 @@ EPUBJS.View.prototype.addCss = function(src) { EPUBJS.View.prototype.addStylesheetRules = function(rules) { var styleEl = document.createElement('style'), styleSheet; - + if(!this.document) return; - + // Append style element to head this.document.head.appendChild(styleEl); @@ -4477,6 +4482,7 @@ EPUBJS.View.prototype.addStylesheetRules = function(rules) { }; RSVP.EventTarget.mixin(EPUBJS.View.prototype); + EPUBJS.Layout = EPUBJS.Layout || {}; EPUBJS.Layout.Reflowable = function(){ @@ -4634,7 +4640,7 @@ EPUBJS.Layout.Scroll.prototype.count = function(){ }; EPUBJS.Rendition = function(book, options) { - + this.settings = EPUBJS.core.extend(this.settings || {}, { infinite: true, hidden: false, @@ -4645,13 +4651,13 @@ EPUBJS.Rendition = function(book, options) { }); EPUBJS.core.extend(this.settings, options); - + this.viewSettings = {}; this.book = book; - + this.views = []; - + //-- Adds Hook methods to the Rendition prototype this.hooks = {}; this.hooks.display = new EPUBJS.Hook(this); @@ -4685,7 +4691,7 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ var hidden = options.hidden || false; var container; var wrapper; - + if(options.height && EPUBJS.core.isNumber(options.height)) { height = options.height + "px"; } @@ -4693,10 +4699,10 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ if(options.width && EPUBJS.core.isNumber(options.width)) { width = options.width + "px"; } - + // Create new container element container = document.createElement("div"); - + container.id = "epubjs-container:" + EPUBJS.core.uuid(); container.classList.add("epub-container"); @@ -4713,7 +4719,7 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ if(width){ container.style.width = width; } - + if(height){ container.style.height = height; } @@ -4725,21 +4731,21 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ EPUBJS.Rendition.wrap = function(container) { var wrapper = document.createElement("div"); - + wrapper.style.visibility = "hidden"; wrapper.style.overflow = "hidden"; wrapper.style.width = "0"; wrapper.style.height = "0"; - + wrapper.appendChild(container); return wrapper; }; - + // Call to attach the container to an element in the dom // Container must be attached before rendering can begin EPUBJS.Rendition.prototype.attachTo = function(_element){ var bounds; - + this.container = this.initialize({ "width" : this.settings.width, "height" : this.settings.height @@ -4749,7 +4755,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ this.element = _element; } else if (typeof _element === "string") { this.element = document.getElementById(_element); - } + } if(!this.element){ console.error("Not an Element"); @@ -4762,7 +4768,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ } else { this.element.appendChild(this.container); } - + // Attach Listeners this.attachListeners(); @@ -4771,19 +4777,19 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ // Add Layout method this.applyLayoutMethod(); - + // Trigger Attached this.trigger("attached"); // Start processing queue // this.q.run(); - + }; EPUBJS.Rendition.prototype.attachListeners = function(){ // Listen to window for resize event if width or height is set to 100% - if(!EPUBJS.core.isNumber(this.settings.width) || + if(!EPUBJS.core.isNumber(this.settings.width) || !EPUBJS.core.isNumber(this.settings.height) ) { window.addEventListener("resize", this.onResized.bind(this), false); } @@ -4795,13 +4801,13 @@ EPUBJS.Rendition.prototype.bounds = function() { }; EPUBJS.Rendition.prototype.display = function(target){ - + return this.q.enqueue(this._display, target); }; EPUBJS.Rendition.prototype._display = function(target){ - + var displaying = new RSVP.defer(); var displayed = displaying.promise; @@ -4818,7 +4824,7 @@ EPUBJS.Rendition.prototype._display = function(target){ } this.displaying = true; - + // Hide current views this.hide(); @@ -4829,11 +4835,19 @@ EPUBJS.Rendition.prototype._display = function(target){ this.q.enqueue(this.append, view); // Move to correct place within the section, if needed - // this.moveTo(what) + this.q.enqueue(function(){ + + var offset = view.locationOf(target); + + if(offset.top > 250 || offset.left > 250){ // Terrible temp fix to prevent unneeded jumps + return this.moveTo(offset); + } + + }); // Show views this.show(); - + // This hook doesn't prevent showing, but waits to resolve until // all the hooks have finished. Might want to block showing. this.hooks.display.trigger(view) @@ -4850,14 +4864,14 @@ EPUBJS.Rendition.prototype._display = function(target){ }; // Takes a cfi, fragment or page? -EPUBJS.Rendition.prototype.moveTo = function(what){ - +EPUBJS.Rendition.prototype.moveTo = function(offset){ + this.scrollBy(offset.left, offset.top); }; EPUBJS.Rendition.prototype.render = function(view, show) { view.create(); - + view.onLayout = this.layout.format.bind(this.layout); // Fit to size of the container, apply padding @@ -4878,13 +4892,13 @@ EPUBJS.Rendition.prototype.render = function(view, show) { return this.hooks.render.trigger(view, this); }.bind(this)) .then(function(){ - + if(show !== false && this.hidden === false) { this.q.enqueue(function(view){ view.show(); }, view); } - + // this.map = new EPUBJS.Map(view, this.layout); this.trigger("rendered", view.section); @@ -4931,7 +4945,7 @@ EPUBJS.Rendition.prototype.remove = function(view) { this.views.splice(index, 1); } - + this._remove(view); }; @@ -4948,7 +4962,7 @@ EPUBJS.Rendition.prototype._remove = function(view) { }; EPUBJS.Rendition.prototype.resizeView = function(view) { - + if(this.globalLayoutProperties.layout === "pre-paginated") { view.lock("both", this.stage.width, this.stage.height); } else { @@ -4987,7 +5001,7 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ width = bounds.width; //height = bounds.height; } - + if(height && !EPUBJS.core.isNumber(height)) { bounds = bounds || this.container.getBoundingClientRect(); //width = bounds.width; @@ -5004,11 +5018,11 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ }; this.stage = { - width: width - - this.containerPadding.left - + width: width - + this.containerPadding.left - this.containerPadding.right, - height: height - - this.containerPadding.top - + height: height - + this.containerPadding.top - this.containerPadding.bottom }; @@ -5017,7 +5031,7 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ }; EPUBJS.Rendition.prototype.applyLayoutMethod = function() { - + this.layout = new EPUBJS.Layout.Scroll(); this.updateLayout(); @@ -5025,7 +5039,7 @@ EPUBJS.Rendition.prototype.applyLayoutMethod = function() { }; EPUBJS.Rendition.prototype.updateLayout = function() { - + this.layout.calculate(this.stage.width, this.stage.height); }; @@ -5120,12 +5134,12 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c var position = view.position(); var container = _container || this.container.getBoundingClientRect(); - if(this.settings.axis === "horizontal" && + if(this.settings.axis === "horizontal" && (position.right > container.left - offsetPrev) && !(position.left >= container.right + offsetNext)) { return true; - + } else if(this.settings.axis === "vertical" && (position.bottom > container.top - offsetPrev) && !(position.top >= container.bottom + offsetNext)) { @@ -5134,7 +5148,7 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c } return false; - + }; EPUBJS.Rendition.prototype.visible = function(){ @@ -5154,12 +5168,12 @@ EPUBJS.Rendition.prototype.visible = function(){ } return visible; - + }; EPUBJS.Rendition.prototype.bounds = function(func) { var bounds; - + if(!this.settings.height) { bounds = EPUBJS.core.windowBounds(); } else { @@ -5208,8 +5222,8 @@ EPUBJS.Rendition.prototype.destroy = function(){ this.q.clear(); this.clear(); - - clearTimeout(this.trimTimeout); + + clearTimeout(this.trimTimeout); if(this.settings.hidden) { this.element.removeChild(this.wrapper); } else { @@ -5228,7 +5242,7 @@ EPUBJS.Rendition.prototype.reportLocation = function(){ EPUBJS.Rendition.prototype.currentLocation = function(){ var view; var start, end; - + if(this.views.length) { view = this.views[0]; // start = container.left - view.position().left; @@ -5236,14 +5250,52 @@ EPUBJS.Rendition.prototype.currentLocation = function(){ return this.map.page(view); } - + }; +EPUBJS.Rendition.prototype.scrollBy = function(x, y, silent){ + if(silent) { + this.ignore = true; + } + + if(this.settings.height) { + + if(x) this.container.scrollLeft += x; + if(y) this.container.scrollTop += y; + + } else { + window.scrollBy(x,y); + } + // console.log("scrollBy", x, y); + this.scrolled = true; +}; + +EPUBJS.Rendition.prototype.scrollTo = function(x, y, silent){ + if(silent) { + this.ignore = true; + } + + if(this.settings.height) { + this.container.scrollLeft = x; + this.container.scrollTop = y; + } else { + window.scrollTo(x,y); + } + // console.log("scrollTo", x, y); + this.scrolled = true; + // if(this.container.scrollLeft != x){ + // setTimeout(function() { + // this.scrollTo(x, y, silent); + // }.bind(this), 10); + // return; + // }; + }; + //-- Enable binding events to Renderer RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); EPUBJS.Continuous = function(book, options) { - + EPUBJS.Rendition.apply(this, arguments); // call super constructor. this.settings = EPUBJS.core.extend(this.settings || {}, { @@ -5253,14 +5305,14 @@ EPUBJS.Continuous = function(book, options) { offset: 500, offsetDelta: 250 }); - + EPUBJS.core.extend(this.settings, options); - + if(this.settings.hidden) { this.wrapper = this.wrap(this.container); } - - + + }; // subclass extends superclass @@ -5270,7 +5322,7 @@ EPUBJS.Continuous.prototype.constructor = EPUBJS.Continuous; EPUBJS.Continuous.prototype.attachListeners = function(){ // Listen to window for resize event if width or height is set to a percent - if(!EPUBJS.core.isNumber(this.settings.width) || + if(!EPUBJS.core.isNumber(this.settings.width) || !EPUBJS.core.isNumber(this.settings.height) ) { window.addEventListener("resize", this.onResized.bind(this), false); } @@ -5284,14 +5336,16 @@ EPUBJS.Continuous.prototype.attachListeners = function(){ }; EPUBJS.Continuous.prototype._display = function(target){ - + var displaying = new RSVP.defer(); var displayed = displaying.promise; - + var section; var view; var cfi, spinePos; + var visible; + if(this.epubcfi.isCfiString(target)) { cfi = this.epubcfi.parse(target); spinePos = cfi.spinePos; @@ -5300,12 +5354,48 @@ EPUBJS.Continuous.prototype._display = function(target){ section = this.book.spine.get(target); } - this.displaying = true; - this.hide(); + + + if(section){ + + this.displaying = true; + + // Check to make sure the section we want isn't already shown + visible = this.visible(); + for (var i = 0; i < visible.length; i++) { + if(visible.length && + section.index === visible[i].section.index){ + // Section already has a visible view + view = visible[i]; + // Move to target location + this.q.enqueue(function(){ + + var offset = view.locationOf(target); + + return this.moveTo(offset); + + }); + + this.q.enqueue(this.check); + // Trigger display hooks + this.hooks.display.trigger(view) + .then(function(){ + this.trigger("displayed", section); + displaying.resolve(this); + }.bind(this)); + + // Finished, no need to fill + return displayed; + + } + } + + this.hide(); + view = new EPUBJS.View(section, this.viewSettings); - + // This will clear all previous views this.q.enqueue(this.fill, view).then(function(){ @@ -5317,18 +5407,18 @@ EPUBJS.Continuous.prototype._display = function(target){ return this.moveTo(offset); }); - + this.q.enqueue(this.check); this.q.enqueue(this.show); - // This hook doesn't prevent showing, but waits to resolve until - // all the hooks have finished. Might want to block showing. - this.hooks.display.trigger(view) - .then(function(){ - this.trigger("displayed", section); - displaying.resolve(this); - }.bind(this)); + // // This hook doesn't prevent showing, but waits to resolve until + // // all the hooks have finished. Might want to block showing. + // this.hooks.display.trigger(view) + // .then(function(){ + // this.trigger("displayed", section); + // // displaying.resolve(this); + // }.bind(this)); }.bind(this)); @@ -5337,7 +5427,14 @@ EPUBJS.Continuous.prototype._display = function(target){ // this.displaying = false; // displaying.resolve(this); //}.bind(this)); - displaying.resolve(this); + + // This hook doesn't prevent showing, but waits to resolve until + // all the hooks have finished. Might want to block showing. + this.hooks.display.trigger(view) + .then(function(){ + this.trigger("displayed", section); + displaying.resolve(this); + }.bind(this)); } else { displaying.reject(new Error("No Section Found")); @@ -5348,18 +5445,20 @@ EPUBJS.Continuous.prototype._display = function(target){ EPUBJS.Continuous.prototype.moveTo = function(offset){ - var bounds = this.bounds(); - var dist = Math.floor(offset.top / bounds.height) * bounds.height; + // var bounds = this.bounds(); + // var dist = Math.floor(offset.top / bounds.height) * bounds.height; + return this.check( + offset.left+this.settings.offset, + offset.top+this.settings.offset) + .then(function(){ - return this.check(0, dist+this.settings.offset).then(function(){ - - if(this.settings.axis === "vertical") { - this.scrollBy(0, dist); - } else { - this.scrollBy(dist, 0); - } + if(this.settings.axis === "vertical") { + this.scrollBy(0, offset.top); + } else { + this.scrollBy(offset.left, 0); + } - }.bind(this)); + }.bind(this)); }; EPUBJS.Continuous.prototype.afterDisplayed = function(currView){ @@ -5410,7 +5509,7 @@ EPUBJS.Continuous.prototype.append = function(view){ EPUBJS.Continuous.prototype.prepend = function(view){ this.views.unshift(view); this.container.insertBefore(view.element, this.container.firstChild); - + // view.on("shown", this.afterDisplayedAbove.bind(this)); view.onDisplayed = this.afterDisplayed.bind(this); @@ -5470,15 +5569,15 @@ EPUBJS.Continuous.prototype.insert = function(view, index) { // } // this.container.removeChild(view.element); - + // view.off("resized"); // if(view.displayed){ // view.destroy(); // } - + // view = null; - + // }; EPUBJS.Continuous.prototype.first = function() { @@ -5511,7 +5610,7 @@ EPUBJS.Continuous.prototype.check = function(_offset){ } } else { - + if(view.displayed) { // console.log("destroy", view.section.index) this.q.enqueue(view.destroy.bind(view)); @@ -5527,7 +5626,7 @@ EPUBJS.Continuous.prototype.check = function(_offset){ }.bind(this)); - + if(promises.length){ return RSVP.all(promises) @@ -5575,12 +5674,12 @@ EPUBJS.Continuous.prototype.trim = function(){ var lastIndex = this.views.indexOf(last); var above = this.views.slice(0, firstIndex); var below = this.views.slice(lastIndex+1); - + // Erase all but last above for (var i = 0; i < above.length-1; i++) { this.erase(above[i], above); } - + // Erase all except first below for (var j = 1; j < below.length; j++) { this.erase(below[j]); @@ -5591,7 +5690,7 @@ EPUBJS.Continuous.prototype.trim = function(){ }; EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim - + var prevTop; var prevLeft; @@ -5606,7 +5705,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim var bounds = view.bounds(); this.remove(view); - + if(above) { if(this.settings.axis === "vertical") { @@ -5615,7 +5714,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim this.scrollTo(prevLeft - bounds.width, 0, true); } } - + }; @@ -5631,12 +5730,12 @@ EPUBJS.Continuous.prototype.checkCurrent = function(position) { if(this.settings.axis === "horizontal") { // TODO: Check for current horizontal } else { - + for (var i = length; i >= 0; i--) { view = this.views[i]; top = view.bounds().top; if(top < container.bottom) { - + if(this.current == view.section) { break; } @@ -5680,7 +5779,7 @@ EPUBJS.Continuous.prototype.start = function() { this.ignore = false; } }.bind(this)); - + window.addEventListener('unload', function(e){ this.ignore = true; this.destroy(); @@ -5711,13 +5810,13 @@ EPUBJS.Continuous.prototype.onScroll = function(){ // left: scrollLeft // }); - if((this.scrollDeltaVert === 0 && + if((this.scrollDeltaVert === 0 && this.scrollDeltaHorz === 0) || this.scrollDeltaVert > this.settings.offsetDelta || this.scrollDeltaHorz > this.settings.offsetDelta) { this.q.enqueue(this.check); - + this.scrollDeltaVert = 0; this.scrollDeltaHorz = 0; @@ -5753,43 +5852,6 @@ EPUBJS.Continuous.prototype.onScroll = function(){ }; -EPUBJS.Continuous.prototype.scrollBy = function(x, y, silent){ - if(silent) { - this.ignore = true; - } - - if(this.settings.height) { - - if(x) this.container.scrollLeft += x; - if(y) this.container.scrollTop += y; - - } else { - window.scrollBy(x,y); - } - // console.log("scrollBy", x, y); - this.scrolled = true; -}; - -EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){ - if(silent) { - this.ignore = true; - } - - if(this.settings.height) { - this.container.scrollLeft = x; - this.container.scrollTop = y; - } else { - window.scrollTo(x,y); - } - // console.log("scrollTo", x, y); - this.scrolled = true; - // if(this.container.scrollLeft != x){ - // setTimeout(function() { - // this.scrollTo(x, y, silent); - // }.bind(this), 10); - // return; - // }; - }; EPUBJS.Continuous.prototype.resizeView = function(view) { @@ -5806,7 +5868,7 @@ EPUBJS.Continuous.prototype.currentLocation = function(){ var startPage, endPage; var container = this.container.getBoundingClientRect(); - + if(visible.length === 1) { return this.map.page(visible[0]); } @@ -5821,7 +5883,7 @@ EPUBJS.Continuous.prototype.currentLocation = function(){ end: endPage.end }; } - + }; /* @@ -5831,13 +5893,13 @@ EPUBJS.Continuous.prototype.current = function(what){ var length = this.views.length - 1; if(this.settings.axis === "horizontal") { - + for (var i = length; i >= 0; i--) { view = this.views[i]; left = view.position().left; if(left < container.right) { - + if(this._current == view) { break; } @@ -5848,12 +5910,12 @@ EPUBJS.Continuous.prototype.current = function(what){ } } else { - + for (var i = length; i >= 0; i--) { view = this.views[i]; top = view.bounds().top; if(top < container.bottom) { - + if(this._current == view) { break; } @@ -5869,6 +5931,7 @@ EPUBJS.Continuous.prototype.current = function(what){ return this._current; }; */ + EPUBJS.Paginate = function(book, options) { EPUBJS.Continuous.apply(this, arguments); diff --git a/dist/epub.min.js b/dist/epub.min.js index 1e6114c..6d66db5 100644 --- a/dist/epub.min.js +++ b/dist/epub.min.js @@ -1,3 +1,3 @@ -(function(){"use strict";function t(t,e){for(var i=0,n=t.length;n>i;i++)if(t[i]===e)return i;return-1}function e(t){var e=t._promiseCallbacks;return e||(e=t._promiseCallbacks={}),e}function i(t,e){return"onerror"===t?(Y.on("error",e),void 0):2!==arguments.length?Y[t]:(Y[t]=e,void 0)}function n(t){return"function"==typeof t||"object"==typeof t&&null!==t}function r(t){return"function"==typeof t}function o(t){return"object"==typeof t&&null!==t}function s(){}function h(){}function a(t){try{return t.then}catch(e){return oe.error=e,oe}}function u(t,e,i,n){try{t.call(e,i,n)}catch(r){return r}}function c(t,e,i){Y.async(function(t){var n=!1,r=u(i,e,function(i){n||(n=!0,e!==i?d(t,i):g(t,i))},function(e){n||(n=!0,y(t,e))},"Settle: "+(t._label||" unknown promise"));!n&&r&&(n=!0,y(t,r))},t)}function l(t,e){e._state===ne?g(t,e._result):t._state===re?y(t,e._result):m(e,void 0,function(i){e!==i?d(t,i):g(t,i)},function(e){y(t,e)})}function p(t,e){if(e.constructor===t.constructor)l(t,e);else{var i=a(e);i===oe?y(t,oe.error):void 0===i?g(t,e):r(i)?c(t,e,i):g(t,e)}}function d(t,e){t===e?g(t,e):n(e)?p(t,e):g(t,e)}function f(t){t._onerror&&t._onerror(t._result),S(t)}function g(t,e){t._state===ie&&(t._result=e,t._state=ne,0===t._subscribers.length?Y.instrument&&ee("fulfilled",t):Y.async(S,t))}function y(t,e){t._state===ie&&(t._state=re,t._result=e,Y.async(f,t))}function m(t,e,i,n){var r=t._subscribers,o=r.length;t._onerror=null,r[o]=e,r[o+ne]=i,r[o+re]=n,0===o&&t._state&&Y.async(S,t)}function S(t){var e=t._subscribers,i=t._state;if(Y.instrument&&ee(i===ne?"fulfilled":"rejected",t),0!==e.length){for(var n,r,o=t._result,s=0;sh;h++)s[h]=t[h];for(n=0;nn;n++)i[n-1]=t[n];return i}function N(t,e){return{then:function(i,n){return t.call(e,i,n)}}}function q(t,e,i,n){var r=_(i,n,e);return r===ge&&y(t,r.value),t}function L(t,e,i,n){return fe.all(e).then(function(e){var r=_(i,n,e);return r===ge&&y(t,r.value),t})}function F(t){return t&&"object"==typeof t?t.constructor===fe?!0:R(t):!1}function O(t,e,i){this._superConstructor(t,e,!1,i)}function I(t,e,i){this._superConstructor(t,e,!0,i)}function V(t,e,i){this._superConstructor(t,e,!1,i)}function A(){return function(){process.nextTick(z)}}function M(){var t=0,e=new qe(z),i=document.createTextNode("");return e.observe(i,{characterData:!0}),function(){i.data=t=++t%2}}function H(){var t=new MessageChannel;return t.port1.onmessage=z,function(){t.port2.postMessage(0)}}function j(){return function(){setTimeout(z,1)}}function z(){for(var t=0;Te>t;t+=2){var e=Fe[t],i=Fe[t+1];e(i),Fe[t]=void 0,Fe[t+1]=void 0}Te=0}function D(t,e){Y.async(t,e)}function W(){Y.on.apply(Y,arguments)}function X(){Y.off.apply(Y,arguments)}var Q={mixin:function(t){return t.on=this.on,t.off=this.off,t.trigger=this.trigger,t._promiseCallbacks=void 0,t},on:function(i,n){var r,o=e(this);r=o[i],r||(r=o[i]=[]),-1===t(r,n)&&r.push(n)},off:function(i,n){var r,o,s=e(this);return n?(r=s[i],o=t(r,n),-1!==o&&r.splice(o,1),void 0):(s[i]=[],void 0)},trigger:function(t,i){var n,r,o=e(this);if(n=o[t])for(var s=0;s1)throw new Error("Second argument not supported");if("object"!=typeof t)throw new TypeError("Argument must be an object");return s.prototype=t,new s},te=[],ee=function(t,e,i){1===te.push({name:t,payload:{guid:e._guidKey+e._id,eventName:t,detail:e._result,childGuid:i&&e._guidKey+i._id,label:e._label,timeStamp:Z(),stack:new Error(e._label).stack}})&&setTimeout(function(){for(var t,e=0;en;n++)this._eachEntry(i[n],n)},U.prototype._eachEntry=function(t,e){var i=this._instanceConstructor;o(t)?t.constructor===i&&t._state!==ie?(t._onerror=null,this._settledAt(t._state,e,t._result)):this._willSettleAt(i.resolve(t),e):(this._remaining--,this._result[e]=this._makeResult(ne,e,t))},U.prototype._settledAt=function(t,e,i){var n=this.promise;n._state===ie&&(this._remaining--,this._abortOnReject&&t===re?y(n,i):this._result[e]=this._makeResult(t,e,i)),0===this._remaining&&g(n,this._result)},U.prototype._makeResult=function(t,e,i){return i},U.prototype._willSettleAt=function(t,e){var i=this;m(t,void 0,function(t){i._settledAt(ne,e,t)},function(t){i._settledAt(re,e,t)})};var ae=function(t,e){return new he(this,t,!0,e).promise},ue=function(t,e){function i(t){d(o,t)}function n(t){y(o,t)}var r=this,o=new r(h,e);if(!G(t))return y(o,new TypeError("You must pass an array to race.")),o;for(var s=t.length,a=0;o._state===ie&&s>a;a++)m(r.resolve(t[a]),void 0,i,n);return o},ce=function(t,e){var i=this;if(t&&"object"==typeof t&&t.constructor===i)return t;var n=new i(h,e);return d(n,t),n},le=function(t,e){var i=this,n=new i(h,e);return y(n,t),n},pe="rsvp_"+Z()+"-",de=0,fe=x;x.cast=ce,x.all=ae,x.race=ue,x.resolve=ce,x.reject=le,x.prototype={constructor:x,_guidKey:pe,_onerror:function(t){Y.trigger("error",t)},then:function(t,e,i){var n=this,r=n._state;if(r===ne&&!t||r===re&&!e)return Y.instrument&&ee("chained",this,this),this;n._onerror=null;var o=new this.constructor(h,i),s=n._result;if(Y.instrument&&ee("chained",n,o),r){var a=arguments[r-1];Y.async(function(){P(r,o,a,s)})}else m(n,o,t,e);return o},"catch":function(t,e){return this.then(null,t,e)},"finally":function(t,e){var i=this.constructor;return this.then(function(e){return i.resolve(t()).then(function(){return e})},function(e){return i.resolve(t()).then(function(){throw e})},e)}};var ge=new C,ye=new C,me=function(t,e){var i=function(){for(var i,n=this,r=arguments.length,o=new Array(r+1),s=!1,a=0;r>a;++a){if(i=arguments[a],!s){if(s=F(i),s===ye){var u=new fe(h);return y(u,ye.value),u}s&&s!==!0&&(i=N(s,i))}o[a]=i}var c=new fe(h);return o[r]=function(t,i){t?y(c,t):void 0===e?d(c,i):e===!0?d(c,k(arguments)):G(e)?d(c,T(arguments,e)):d(c,i)},s?L(c,o,t,n):q(c,o,t,n)};return i.__proto__=t,i},Se=function(t,e){return fe.all(t,e)};O.prototype=$(he.prototype),O.prototype._superConstructor=he,O.prototype._makeResult=w,O.prototype._validationError=function(){return new Error("allSettled must be called with an array")};var ve=function(t,e){return new O(fe,t,e).promise},Ee=function(t,e){return fe.race(t,e)},Pe=I;I.prototype=$(he.prototype),I.prototype._superConstructor=he,I.prototype._init=function(){this._result={}},I.prototype._validateInput=function(t){return t&&"object"==typeof t},I.prototype._validationError=function(){return new Error("Promise.hash must be called with an object")},I.prototype._enumerate=function(){var t=this.promise,e=this._input,i=[];for(var n in e)t._state===ie&&e.hasOwnProperty(n)&&i.push({position:n,entry:e[n]});var r=i.length;this._remaining=r;for(var o,s=0;t._state===ie&&r>s;s++)o=i[s],this._eachEntry(o.entry,o.position)};var Be=function(t,e){return new Pe(fe,t,e).promise};V.prototype=$(Pe.prototype),V.prototype._superConstructor=he,V.prototype._makeResult=w,V.prototype._validationError=function(){return new Error("hashSettled must be called with an object")};var we,Ue=function(t,e){return new V(fe,t,e).promise},be=function(t){throw setTimeout(function(){throw t}),t},Je=function(t){var e={};return e.promise=new fe(function(t,i){e.resolve=t,e.reject=i},t),e},xe=function(t,e,i){return fe.all(t,i).then(function(t){if(!r(e))throw new TypeError("You must pass a function as map's second argument.");for(var n=t.length,o=new Array(n),s=0;n>s;s++)o[s]=e(t[s]);return fe.all(o,i)})},Ce=function(t,e){return fe.resolve(t,e)},Re=function(t,e){return fe.reject(t,e)},_e=function(t,e,i){return fe.all(t,i).then(function(t){if(!r(e))throw new TypeError("You must pass a function as filter's second argument.");for(var n=t.length,o=new Array(n),s=0;n>s;s++)o[s]=e(t[s]);return fe.all(o,i).then(function(e){for(var i=new Array(n),r=0,o=0;n>o;o++)e[o]&&(i[r]=t[o],r++);return i.length=r,i})})},Te=0,ke=function(t,e){Fe[Te]=t,Fe[Te+1]=e,Te+=2,2===Te&&we()},Ne="undefined"!=typeof window?window:{},qe=Ne.MutationObserver||Ne.WebKitMutationObserver,Le="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,Fe=new Array(1e3);we="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?A():qe?M():Le?H():j(),Y.async=ke;if("undefined"!=typeof window&&"object"==typeof window.__PROMISE_INSTRUMENTATION__){var Oe=window.__PROMISE_INSTRUMENTATION__;i("instrument",!0);for(var Ie in Oe)Oe.hasOwnProperty(Ie)&&W(Ie,Oe[Ie])}var Ve={race:Ee,Promise:fe,allSettled:ve,hash:Be,hashSettled:Ue,denodeify:me,on:W,off:X,map:xe,filter:_e,resolve:Ce,reject:Re,all:Se,rethrow:be,defer:Je,EventTarget:Q,configure:i,async:D};"function"==typeof define&&define.amd?define(function(){return Ve}):"undefined"!=typeof module&&module.exports?module.exports=Ve:"undefined"!=typeof this&&(this.RSVP=Ve)}).call(this),"undefined"==typeof EPUBJS&&(("undefined"!=typeof window?window:this).EPUBJS={}),EPUBJS.VERSION="0.3.0",function(t){"use strict";var e=function(t){return new EPUBJS.Book(t)};"object"==typeof exports?(t.RSVP=require("rsvp"),module.exports=e):"function"==typeof define&&define.amd?define(e):t.ePub=e}(this),EPUBJS.core={},EPUBJS.core.request=function(t,e,i,n){function r(){if(this.readyState===this.DONE)if(200===this.status||this.responseXML){var t;t="xml"==e?this.responseXML?this.responseXML:(new DOMParser).parseFromString(this.response,"text/xml"):"json"==e?JSON.parse(this.response):"blob"==e?s?this.response:new Blob([this.response]):this.response,a.resolve(t)}else a.reject({status:this.status,message:this.response,stack:(new Error).stack})}var o,s=window.URL,h=s?"blob":"arraybuffer",a=new RSVP.defer,u=new XMLHttpRequest,c=XMLHttpRequest.prototype;"overrideMimeType"in c||Object.defineProperty(c,"overrideMimeType",{value:function(){}}),i&&(u.withCredentials=!0),u.open("GET",t,!0);for(o in n)u.setRequestHeader(o,n[o]);return u.onreadystatechange=r,"blob"==e&&(u.responseType=h),"json"==e&&u.setRequestHeader("Accept","application/json"),"xml"==e&&u.overrideMimeType("text/xml"),u.send(),a.promise},EPUBJS.core.uri=function(t){var e,i,n,r={protocol:"",host:"",path:"",origin:"",directory:"",base:"",filename:"",extension:"",fragment:"",href:t},o=t.indexOf("://"),s=t.indexOf("?"),h=t.indexOf("#");return-1!=h&&(r.fragment=t.slice(h+1),t=t.slice(0,h)),-1!=s&&(r.search=t.slice(s+1),t=t.slice(0,s),href=t),-1!=o?(r.protocol=t.slice(0,o),e=t.slice(o+3),n=e.indexOf("/"),-1===n?(r.host=r.path,r.path=""):(r.host=e.slice(0,n),r.path=e.slice(n)),r.origin=r.protocol+"://"+r.host,r.directory=EPUBJS.core.folder(r.path),r.base=r.origin+r.directory):(r.path=t,r.directory=EPUBJS.core.folder(t),r.base=r.directory),r.filename=t.replace(r.base,""),i=r.filename.lastIndexOf("."),-1!=i&&(r.extension=r.filename.slice(i+1)),r},EPUBJS.core.folder=function(t){var e=t.lastIndexOf("/");if(-1==e)var i="";return i=t.slice(0,e+1)},EPUBJS.core.queue=function(t){var e=[],i=t,n=function(t,i,n){return e.push({funcName:t,args:i,context:n}),e},r=function(){var t;e.length&&(t=e.shift(),i[t.funcName].apply(t.context||i,t.args))},o=function(){for(;e.length;)r()},s=function(){e=[]},h=function(){return e.length};return{enqueue:n,dequeue:r,flush:o,clear:s,length:h}},EPUBJS.core.isElement=function(t){return!(!t||1!=t.nodeType)},EPUBJS.core.uuid=function(){var t=(new Date).getTime(),e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var i=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"==e?i:7&i|8).toString(16)});return e},EPUBJS.core.values=function(t){for(var e=-1,i=Object.keys(t),n=i.length,r=Array(n);++er;r++)if("undefined"!=typeof document.body.style[e[r]+i])return e[r]+i;return t},EPUBJS.core.defaults=function(t){for(var e=1,i=arguments.length;i>e;e++){var n=arguments[e];for(var r in n)void 0===t[r]&&(t[r]=n[r])}return t},EPUBJS.core.extend=function(t){var e=[].slice.call(arguments,1);return e.forEach(function(e){e&&Object.getOwnPropertyNames(e).forEach(function(i){Object.defineProperty(t,i,Object.getOwnPropertyDescriptor(e,i))})}),t},EPUBJS.core.insert=function(t,e,i){var n=EPUBJS.core.locationOf(t,e,i);return e.splice(n,0,t),n},EPUBJS.core.locationOf=function(t,e,i,n,r){var o,s=n||0,h=r||e.length,a=parseInt(s+(h-s)/2);return i||(i=function(t,e){return t>e?1:e>t?-1:(t=e)?0:void 0}),0>=h-s?a:(o=i(e[a],t),h-s===1?o>0?a:a+1:0===o?a:-1===o?EPUBJS.core.locationOf(t,e,i,a,h):EPUBJS.core.locationOf(t,e,i,s,a))},EPUBJS.core.indexOfSorted=function(t,e,i,n,r){var o,s=n||0,h=r||e.length,a=parseInt(s+(h-s)/2);return i||(i=function(t,e){return t>e?1:e>t?-1:(t=e)?0:void 0}),0>=h-s?-1:(o=i(e[a],t),h-s===1?0===o?a:-1:0===o?a:-1===o?EPUBJS.core.indexOfSorted(t,e,i,a,h):EPUBJS.core.indexOfSorted(t,e,i,s,a))},EPUBJS.core.requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,EPUBJS.core.bounds=function(t){var e=window.getComputedStyle(t),i=["width","paddingRight","paddingLeft","marginRight","marginLeft","borderRightWidth","borderLeftWidth"],n=["height","paddingTop","paddingBottom","marginTop","marginBottom","borderTopWidth","borderBottomWidth"],r=0,o=0;return i.forEach(function(t){r+=parseFloat(e[t])||0}),n.forEach(function(t){o+=parseFloat(e[t])||0}),{height:o,width:r}},EPUBJS.core.borders=function(t){var e=window.getComputedStyle(t),i=["paddingRight","paddingLeft","marginRight","marginLeft","borderRightWidth","borderLeftWidth"],n=["paddingTop","paddingBottom","marginTop","marginBottom","borderTopWidth","borderBottomWidth"],r=0,o=0;return i.forEach(function(t){r+=parseFloat(e[t])||0}),n.forEach(function(t){o+=parseFloat(e[t])||0}),{height:o,width:r}},EPUBJS.core.windowBounds=function(){var t=window.innerWidth,e=window.innerHeight;return{top:0,left:0,right:t,bottom:e,width:t,height:e}},EPUBJS.core.cleanStringForXpath=function(t){var e=t.match(/[^'"]+|['"]/g);return e=e.map(function(t){return"'"===t?'"\'"':'"'===t?"'\"'":"'"+t+"'"}),"concat('',"+e.join(",")+")"},EPUBJS.core.indexOfTextNode=function(t){for(var e,i=t.parentNode,n=i.childNodes,r=-1,o=0;o=0;s--){var h=r.snapshotItem(s),a=h.getAttribute("id")||!1,u=h.querySelector("content"),c=u.getAttribute("src"),l=h.querySelector("navLabel"),p=l.textContent?l.textContent:"",d=c.split("#"),f=(d[0],e(h));n.unshift({id:a,href:c,label:p,subitems:f,parent:i?i.getAttribute("id"):null})}return n}var i=t.querySelector("navMap");return i?e(i):[]},EPUBJS.EpubCFI=function(t){return t?this.parse(t):void 0},EPUBJS.EpubCFI.prototype.generateChapterComponent=function(t,e,i){var n=parseInt(e),r=t+1,o="/"+r+"/";return o+=2*(n+1),i&&(o+="["+i+"]"),o},EPUBJS.EpubCFI.prototype.generatePathComponent=function(t){var e=[];return t.forEach(function(t){var i="";i+=2*(t.index+1),t.id&&(i+="["+t.id+"]"),e.push(i)}),e.join("/")},EPUBJS.EpubCFI.prototype.generateCfiFromElement=function(t,e){var i=this.pathTo(t),n=this.generatePathComponent(i);return n.length?"epubcfi("+e+"!"+n+"/1:0)":"epubcfi("+e+"!/4/)"},EPUBJS.EpubCFI.prototype.pathTo=function(t){for(var e,i=[];t&&null!==t.parentNode&&9!=t.parentNode.nodeType;)e=t.parentNode.children,i.unshift({id:t.id,tagName:t.tagName,index:e?Array.prototype.indexOf.call(e,t):0}),t=t.parentNode;return i},EPUBJS.EpubCFI.prototype.getChapterComponent=function(t){var e=t.split("!");return e[0]},EPUBJS.EpubCFI.prototype.getPathComponent=function(t){var e=t.split("!"),i=e[1]?e[1].split(":"):"";return i[0]},EPUBJS.EpubCFI.prototype.getCharecterOffsetComponent=function(t){var e=t.split(":");return e[1]||""},EPUBJS.EpubCFI.prototype.parse=function(t){var e,i,n,r,o,s,h,a,u,c={},l=function(t){var e,i,n,r;return e="element",i=parseInt(t)/2-1,n=t.match(/\[(.*)\]/),n&&n[1]&&(r=n[1]),{type:e,index:i,id:r||!1}};return"string"!=typeof t?{spinePos:-1}:(c.str=t,0===t.indexOf("epubcfi(")&&")"===t[t.length-1]&&(t=t.slice(8,t.length-1)),i=this.getChapterComponent(t),n=this.getPathComponent(t)||"",r=this.getCharecterOffsetComponent(t),i?(e=i.split("/")[2]||"")?(c.spinePos=parseInt(e)/2-1||0,s=e.match(/\[(.*)\]/),c.spineId=s?s[1]:!1,-1!=n.indexOf(",")&&console.warn("CFI Ranges are not supported"),h=n.split("/"),a=h.pop(),c.steps=[],h.forEach(function(t){var e;t&&(e=l(t),c.steps.push(e))}),u=parseInt(a),isNaN(u)||(u%2===0?c.steps.push(l(a)):c.steps.push({type:"text",index:(u-1)/2})),o=r.match(/\[(.*)\]/),o&&o[1]?(c.characterOffset=parseInt(r.split("[")[0]),c.textLocationAssertion=o[1]):c.characterOffset=parseInt(r),c):{spinePos:-1}:{spinePos:-1})},EPUBJS.EpubCFI.prototype.addMarker=function(t,e,i){var n,r,o,s,h=e||document,a=i||this.createMarker(h);return"string"==typeof t&&(t=this.parse(t)),r=t.steps[t.steps.length-1],-1===t.spinePos?!1:(n=this.findParent(t,h))?(r&&"text"===r.type?(o=n.childNodes[r.index],t.characterOffset?(s=o.splitText(t.characterOffset),a.classList.add("EPUBJS-CFI-SPLIT"),n.insertBefore(a,s)):n.insertBefore(a,o)):n.insertBefore(a,n.firstChild),a):!1},EPUBJS.EpubCFI.prototype.createMarker=function(t){var e=t||document,i=e.createElement("span");return i.id="EPUBJS-CFI-MARKER:"+EPUBJS.core.uuid(),i.classList.add("EPUBJS-CFI-MARKER"),i},EPUBJS.EpubCFI.prototype.removeMarker=function(t,e){t.classList.contains("EPUBJS-CFI-SPLIT")?(nextSib=t.nextSibling,prevSib=t.previousSibling,nextSib&&prevSib&&3===nextSib.nodeType&&3===prevSib.nodeType&&(prevSib.textContent+=nextSib.textContent,t.parentNode.removeChild(nextSib)),t.parentNode.removeChild(t)):t.classList.contains("EPUBJS-CFI-MARKER")&&t.parentNode.removeChild(t)},EPUBJS.EpubCFI.prototype.findParent=function(t,e){var i,n,r,o=e||document,s=o.getElementsByTagName("html")[0],h=Array.prototype.slice.call(s.children);if("string"==typeof t&&(t=this.parse(t)),n=t.steps.slice(0),!n.length)return o.getElementsByTagName("body")[0];for(;n&&n.length>0;){if(i=n.shift(),"text"===i.type?(r=s.childNodes[i.index],s=r.parentNode||s):s=i.id?o.getElementById(i.id):h[i.index],"undefined"==typeof s)return console.error("No Element For",i,t.str),!1;h=Array.prototype.slice.call(s.children)}return s},EPUBJS.EpubCFI.prototype.compare=function(t,e){if("string"==typeof t&&(t=new EPUBJS.EpubCFI(t)),"string"==typeof e&&(e=new EPUBJS.EpubCFI(e)),t.spinePos>e.spinePos)return 1;if(t.spinePose.steps[i].index)return 1;if(t.steps[i].indexe.characterOffset?1:t.characterOffset=0?(o=r.length,t.characterOffset-1?(delete this.spineByHref[t.href],delete this.spineById[t.idref],this.spineItems.splice(e,1)):void 0},EPUBJS.replace={},EPUBJS.replace.links=function(t,e){for(var i=t.document.querySelectorAll("a[href]"),n=function(t){var i=t.getAttribute("href"),n=new EPUBJS.core.uri(i);n.protocol?t.setAttribute("target","_blank"):0===i.indexOf("#")||(t.onclick=function(){return e.display(i),!1})},r=0;r-1&&(this.observer=this.observe(this.document.body)),this.imageLoadListeners(),this.mediaQueryListeners(),this.resizeListenters()},EPUBJS.View.prototype.resizeListenters=function(){clearTimeout(this.expanding)},EPUBJS.View.prototype.mediaQueryListeners=function(){for(var t=this.document.styleSheets,e=function(t){t.matches&&!this._expanding&&setTimeout(this.expand.bind(this),1)}.bind(this),i=0;i-1&&(id=t.substring(t.indexOf("#")),el=this.document.getElementById(id)))return el.getBoundingClientRect();return{left:0,top:0}}},EPUBJS.View.prototype.addCss=function(t){var e=document.createElement("link"),i=!1;return new RSVP.Promise(function(n){return this.document?(e.type="text/css",e.rel="stylesheet",e.href=t,e.onload=e.onreadystatechange=function(){i||this.readyState&&"complete"!=this.readyState||(i=!0,setTimeout(function(){n(!0)},1))},this.document.head.appendChild(e),void 0):(n(!1),void 0)}.bind(this))},EPUBJS.View.prototype.addStylesheetRules=function(t){var e,i=document.createElement("style");if(this.document){this.document.head.appendChild(i),e=i.sheet;for(var n=0,r=t.length;r>n;n++){var o=1,s=t[n],h=t[n][0],a="";"[object Array]"===Object.prototype.toString.call(s[1][0])&&(s=s[1],o=0);for(var u=s.length;u>o;o++){var c=s[o];a+=c[0]+":"+c[1]+(c[2]?" !important":"")+";\n"}e.insertRule(h+"{"+a+"}",e.cssRules.length)}}},RSVP.EventTarget.mixin(EPUBJS.View.prototype),EPUBJS.Layout=EPUBJS.Layout||{},EPUBJS.Layout.Reflowable=function(){},EPUBJS.Layout.Reflowable.prototype.calculate=function(t,e,i,n){var r,o,s,h=n||1,a=Math.floor(t),u=a%2===0?a:a-1,c=Math.floor(u/8),l=i>=0?i:c%2===0?c:c-1;r=h>1?Math.floor((u-l)/h):u,o=r*h,s=(r+l)*h,this.columnAxis=EPUBJS.core.prefixed("columnAxis"),this.columnGap=EPUBJS.core.prefixed("columnGap"),this.columnWidth=EPUBJS.core.prefixed("columnWidth"),this.columnFill=EPUBJS.core.prefixed("columnFill"),this.width=u,this.height=e,this.spread=o,this.delta=s,this.column=r,this.gap=l,this.divisor=h},EPUBJS.Layout.Reflowable.prototype.format=function(t){var e=t.document.documentElement,i=t.document.body;e.style.overflow="hidden",e.style.width=this.width+"px",i.style.height=this.height+"px",i.style[this.columnAxis]="horizontal",i.style[this.columnFill]="auto",i.style[this.columnGap]=this.gap+"px",i.style[this.columnWidth]=this.column+"px",t.iframe.style.marginRight=this.gap+"px"},EPUBJS.Layout.Reflowable.prototype.count=function(t){var e=t.root().scrollWidth,i=Math.ceil(e/this.spread);return{spreads:i,pages:i*this.divisor}},EPUBJS.Layout.Fixed=function(){},EPUBJS.Layout.Fixed.prototype.calculate=function(){},EPUBJS.Layout.Fixed.prototype.format=function(t){var e,i,n=t.document.documentElement,r=documentElement.querySelector("[name=viewport");r&&r.hasAttribute("content")&&(content=r.getAttribute("content"),contents=content.split(","),contents[0]&&(e=contents[0].replace("width=","")),contents[1]&&(i=contents[1].replace("height=",""))),t.resize(e,i),n.style.overflow="auto"},EPUBJS.Layout.Fixed.prototype.count=function(){return{spreads:1,pages:1}},EPUBJS.Layout.Scroll=function(){},EPUBJS.Layout.Scroll.prototype.calculate=function(t){this.spread=t,this.column=t,this.gap=0},EPUBJS.Layout.Scroll.prototype.format=function(t){var e=t.document.documentElement;e.style.width="auto",e.style.height="auto"},EPUBJS.Layout.Scroll.prototype.count=function(){return{spreads:1,pages:1}},EPUBJS.Rendition=function(t,e){this.settings=EPUBJS.core.extend(this.settings||{},{infinite:!0,hidden:!1,width:!1,height:null,layoutOveride:null,axis:"vertical"}),EPUBJS.core.extend(this.settings,e),this.viewSettings={},this.book=t,this.views=[],this.hooks={},this.hooks.display=new EPUBJS.Hook(this),this.hooks.content=new EPUBJS.Hook(this),this.hooks.layout=new EPUBJS.Hook(this),this.hooks.render=new EPUBJS.Hook(this),this.hooks.show=new EPUBJS.Hook(this),this.hooks.content.register(EPUBJS.replace.links.bind(this)),this.epubcfi=new EPUBJS.EpubCFI,this.q=new EPUBJS.Queue(this),this.q.enqueue(this.book.opened),this.q.enqueue(this.parseLayoutProperties)},EPUBJS.Rendition.prototype.initialize=function(t){{var e,i=t||{},n=i.height,r=i.width;i.hidden||!1}return i.height&&EPUBJS.core.isNumber(i.height)&&(n=i.height+"px"),i.width&&EPUBJS.core.isNumber(i.width)&&(r=i.width+"px"),e=document.createElement("div"),e.id="epubjs-container:"+EPUBJS.core.uuid(),e.classList.add("epub-container"),e.style.fontSize="0",e.style.wordSpacing="0",e.style.lineHeight="0",e.style.verticalAlign="top","horizontal"===this.settings.axis&&(e.style.whiteSpace="nowrap"),r&&(e.style.width=r),n&&(e.style.height=n),e.style.overflow=this.settings.overflow,e},EPUBJS.Rendition.wrap=function(t){var e=document.createElement("div");return e.style.visibility="hidden",e.style.overflow="hidden",e.style.width="0",e.style.height="0",e.appendChild(t),e},EPUBJS.Rendition.prototype.attachTo=function(t){return this.container=this.initialize({width:this.settings.width,height:this.settings.height}),EPUBJS.core.isElement(t)?this.element=t:"string"==typeof t&&(this.element=document.getElementById(t)),this.element?(this.settings.hidden?(this.wrapper=this.wrap(this.container),this.element.appendChild(this.wrapper)):this.element.appendChild(this.container),this.attachListeners(),this.stageSize(),this.applyLayoutMethod(),this.trigger("attached"),void 0):(console.error("Not an Element"),void 0)},EPUBJS.Rendition.prototype.attachListeners=function(){EPUBJS.core.isNumber(this.settings.width)&&EPUBJS.core.isNumber(this.settings.height)||window.addEventListener("resize",this.onResized.bind(this),!1)},EPUBJS.Rendition.prototype.bounds=function(){return this.container.getBoundingClientRect()},EPUBJS.Rendition.prototype.display=function(t){return this.q.enqueue(this._display,t)},EPUBJS.Rendition.prototype._display=function(t){var e,i,n,r,o=new RSVP.defer,s=o.promise;return this.epubcfi.isCfiString(t)?(n=this.epubcfi.parse(t),r=n.spinePos,e=this.book.spine.get(r)):e=this.book.spine.get(t),this.displaying=!0,this.hide(),e?(i=this.createView(e),this.q.enqueue(this.append,i),this.show(),this.hooks.display.trigger(i).then(function(){this.trigger("displayed",e),o.resolve(this)}.bind(this))):o.reject(new Error("No Section Found")),s},EPUBJS.Rendition.prototype.moveTo=function(){},EPUBJS.Rendition.prototype.render=function(t,e){return t.create(),t.onLayout=this.layout.format.bind(this.layout),this.resizeView(t),t.render(this.book.request).then(function(){return this.hooks.content.trigger(t,this)}.bind(this)).then(function(){return this.hooks.layout.trigger(t,this)}.bind(this)).then(function(){return t.display()}.bind(this)).then(function(){return this.hooks.render.trigger(t,this)}.bind(this)).then(function(){e!==!1&&this.hidden===!1&&this.q.enqueue(function(t){t.show()},t),this.trigger("rendered",t.section)}.bind(this)).catch(function(t){this.trigger("loaderror",t)}.bind(this))},EPUBJS.Rendition.prototype.afterDisplayed=function(t){this.trigger("added",t.section)},EPUBJS.Rendition.prototype.append=function(t){return this.clear(),this.views.push(t),this.container.appendChild(t.element),t.onDisplayed=this.afterDisplayed.bind(this),this.render(t)},EPUBJS.Rendition.prototype.clear=function(){this.views.forEach(function(t){this._remove(t)}.bind(this)),this.views=[]},EPUBJS.Rendition.prototype.remove=function(t){var e=this.views.indexOf(t);e>-1&&this.views.splice(e,1),this._remove(t)},EPUBJS.Rendition.prototype._remove=function(t){t.off("resized"),t.displayed&&t.destroy(),this.container.removeChild(t.element),t=null},EPUBJS.Rendition.prototype.resizeView=function(t){"pre-paginated"===this.globalLayoutProperties.layout?t.lock("both",this.stage.width,this.stage.height):t.lock("width",this.stage.width,this.stage.height)},EPUBJS.Rendition.prototype.stageSize=function(t,e){var i,n=t||this.settings.width,r=e||this.settings.height;return n===!1&&(i=this.element.getBoundingClientRect(),i.width&&(n=i.width,this.container.style.width=i.width+"px")),r===!1&&(i=i||this.element.getBoundingClientRect(),i.height&&(r=i.height,this.container.style.height=i.height+"px")),n&&!EPUBJS.core.isNumber(n)&&(i=this.container.getBoundingClientRect(),n=i.width),r&&!EPUBJS.core.isNumber(r)&&(i=i||this.container.getBoundingClientRect(),r=i.height),this.containerStyles=window.getComputedStyle(this.container),this.containerPadding={left:parseFloat(this.containerStyles["padding-left"])||0,right:parseFloat(this.containerStyles["padding-right"])||0,top:parseFloat(this.containerStyles["padding-top"])||0,bottom:parseFloat(this.containerStyles["padding-bottom"])||0},this.stage={width:n-this.containerPadding.left-this.containerPadding.right,height:r-this.containerPadding.top-this.containerPadding.bottom},this.stage},EPUBJS.Rendition.prototype.applyLayoutMethod=function(){this.layout=new EPUBJS.Layout.Scroll,this.updateLayout(),this.map=new EPUBJS.Map(this.layout)},EPUBJS.Rendition.prototype.updateLayout=function(){this.layout.calculate(this.stage.width,this.stage.height)},EPUBJS.Rendition.prototype.resize=function(t,e){this.stageSize(t,e),this.updateLayout(),this.views.forEach(this.resizeView.bind(this)),this.trigger("resized",{width:this.stage.width,height:this.stage.height})},EPUBJS.Rendition.prototype.onResized=function(){this.resize()},EPUBJS.Rendition.prototype.createView=function(t){return new EPUBJS.View(t,this.viewSettings)},EPUBJS.Rendition.prototype.next=function(){return this.q.enqueue(function(){var t,e;return this.views.length?(t=this.views[0].section.next(),t?(e=this.createView(t),this.append(e)):void 0):void 0})},EPUBJS.Rendition.prototype.prev=function(){return this.q.enqueue(function(){var t,e;return this.views.length?(t=this.views[0].section.prev(),t?(e=this.createView(t),this.append(e)):void 0):void 0})},EPUBJS.Rendition.prototype.parseLayoutProperties=function(t){var e=t||this.book.package.metadata,i=this.layoutOveride&&this.layoutOveride.layout||e.layout||"reflowable",n=this.layoutOveride&&this.layoutOveride.spread||e.spread||"auto",r=this.layoutOveride&&this.layoutOveride.orientation||e.orientation||"auto";return this.globalLayoutProperties={layout:i,spread:n,orientation:r},this.globalLayoutProperties},EPUBJS.Rendition.prototype.current=function(){var t=this.visible();return t.length?t[t.length-1]:null},EPUBJS.Rendition.prototype.isVisible=function(t,e,i,n){var r=t.position(),o=n||this.container.getBoundingClientRect();return"horizontal"===this.settings.axis&&r.right>o.left-e&&!(r.left>=o.right+i)?!0:"vertical"===this.settings.axis&&r.bottom>o.top-e&&!(r.top>=o.bottom+i)?!0:!1},EPUBJS.Rendition.prototype.visible=function(){for(var t,e,i=this.bounds(),n=[],r=0;r=0;r--)if(t=this.views[r],e=t.bounds().top,ethis.settings.offsetDelta||this.scrollDeltaHorz>this.settings.offsetDelta)&&(this.q.enqueue(this.check),this.scrollDeltaVert=0,this.scrollDeltaHorz=0),this.scrollDeltaVert+=Math.abs(scrollTop-this.prevScrollTop),this.scrollDeltaHorz+=Math.abs(scrollLeft-this.prevScrollLeft),this.settings.height?(this.prevScrollTop=this.container.scrollTop,this.prevScrollLeft=this.container.scrollLeft):(this.prevScrollTop=window.scrollY,this.prevScrollLeft=window.scrollX),clearTimeout(this.scrollTimeout),this.scrollTimeout=setTimeout(function(){this.scrollDeltaVert=0,this.scrollDeltaHorz=0}.bind(this),150),this.scrolled=!1),this.tick.call(window,this.onScroll.bind(this))},EPUBJS.Continuous.prototype.scrollBy=function(t,e,i){i&&(this.ignore=!0),this.settings.height?(t&&(this.container.scrollLeft+=t),e&&(this.container.scrollTop+=e)):window.scrollBy(t,e),this.scrolled=!0},EPUBJS.Continuous.prototype.scrollTo=function(t,e,i){i&&(this.ignore=!0),this.settings.height?(this.container.scrollLeft=t,this.container.scrollTop=e):window.scrollTo(t,e),this.scrolled=!0},EPUBJS.Continuous.prototype.resizeView=function(t){"horizontal"===this.settings.axis?t.lock("height",this.stage.width,this.stage.height):t.lock("width",this.stage.width,this.stage.height)},EPUBJS.Continuous.prototype.currentLocation=function(){{var t,e,i=this.visible();this.container.getBoundingClientRect()}return 1===i.length?this.map.page(i[0]):i.length>1?(t=this.map.page(i[0]),e=this.map.page(i[i.length-1]),{start:t.start,end:e.end}):void 0 -},EPUBJS.Paginate=function(t,e){EPUBJS.Continuous.apply(this,arguments),this.settings=EPUBJS.core.extend(this.settings||{},{width:600,height:400,axis:"horizontal",forceSingle:!1,minSpreadWidth:800,gap:"auto",overflow:"hidden",infinite:!1}),EPUBJS.core.extend(this.settings,e),this.isForcedSingle=this.settings.forceSingle,this.viewSettings={axis:this.settings.axis},this.start()},EPUBJS.Paginate.prototype=Object.create(EPUBJS.Continuous.prototype),EPUBJS.Paginate.prototype.constructor=EPUBJS.Paginate,EPUBJS.Paginate.prototype.determineSpreads=function(t){return this.isForcedSingle||!t||this.bounds().width1?(t=h.left-s[0].position().left,i=t+this.layout.column,e=h.left+this.layout.spread-s[s.length-1].position().left,n=e+this.layout.column,r=this.map.page(s[0],t,i),o=this.map.page(s[s.length-1],e,n),{start:r.start,end:o.end}):void 0},EPUBJS.Paginate.prototype.resize=function(t,e){this.q.clear(),this.stageSize(t,e),this.updateLayout(),this.display(this.location.start),this.trigger("resized",{width:this.stage.width,height:this.stage.height})},EPUBJS.Paginate.prototype.onResized=function(){this.clear(),clearTimeout(this.resizeTimeout),this.resizeTimeout=setTimeout(function(){this.resize()}.bind(this),150)},EPUBJS.Paginate.prototype.adjustImages=function(t){return t.addStylesheetRules([["img",["max-width",this.layout.spread+"px"],["max-height",this.layout.height+"px"]]]),new RSVP.Promise(function(t){setTimeout(function(){t()},1)})},EPUBJS.Map=function(t){this.layout=t},EPUBJS.Map.prototype.section=function(t){var e=this.findRanges(t),i=this.rangeListToCfiList(t,e);return i},EPUBJS.Map.prototype.page=function(t,e,i){var n=t.document.body;return this.rangePairToCfiPair(t.section,{start:this.findStart(n,e,i),end:this.findEnd(n,e,i)})},EPUBJS.Map.prototype.walk=function(t,e){for(var i,n,r=document.createTreeWalker(t,NodeFilter.SHOW_TEXT,{acceptNode:function(t){return t.data.trim().length>0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT}},!1);(i=r.nextNode())&&!(n=e(i)););return n},EPUBJS.Map.prototype.findRanges=function(t){for(var e,i,n=[],r=this.layout.count(t),o=this.layout.column,s=this.layout.gap,h=0;h=e&&i>=n?t:r>e?t:(s=t,o.push(t),void 0)}))return this.findTextStartRange(r,e,i);return this.findTextStartRange(s,e,i)},EPUBJS.Map.prototype.findEnd=function(t,e,i){for(var n,r,o=[t],s=t;o.length;)if(n=o.shift(),r=this.walk(n,function(t){var e,n,r,h;return t.nodeType==Node.TEXT_NODE?(h=document.createRange(),h.selectNodeContents(t),r=h.getBoundingClientRect()):r=t.getBoundingClientRect(),e=r.left,n=r.right,e>i&&s?s:n>i?t:(s=t,o.push(t),void 0)}))return this.findTextEndRange(r,e,i);return this.findTextEndRange(s,e,i)},EPUBJS.Map.prototype.findTextStartRange=function(t,e){for(var i,n,r,o=this.splitTextNodeIntoRanges(t),s=0;s=e)return n;i=n}return o[0]},EPUBJS.Map.prototype.findTextEndRange=function(t,e,i){for(var n,r,o,s=this.splitTextNodeIntoRanges(t),h=0;hi&&n)return n;if(o.right>i)return r;n=r}return s[s.length-1]},EPUBJS.Map.prototype.splitTextNodeIntoRanges=function(t,e){var i,n=[],r=t.textContent||"",o=r.trim(),s=t.ownerDocument,h=e||" ";if(pos=o.indexOf(h),-1===pos||t.nodeType!=Node.TEXT_NODE)return i=s.createRange(),i.selectNodeContents(t),[i];for(i=s.createRange(),i.setStart(t,0),i.setEnd(t,pos),n.push(i),i=!1;-1!=pos;)pos=o.indexOf(h,pos+1),pos>0&&(i&&(i.setEnd(t,pos),n.push(i)),i=s.createRange(),i.setStart(t,pos+1));return i&&(i.setEnd(t,o.length),n.push(i)),n},EPUBJS.Map.prototype.rangePairToCfiPair=function(t,e){var i=e.start,n=e.end;return i.collapse(!0),n.collapse(!0),startCfi=t.cfiFromRange(i),endCfi=t.cfiFromRange(n),{start:startCfi,end:endCfi}},EPUBJS.Map.prototype.rangeListToCfiList=function(t,e){for(var i,n=[],r=0;ri;i++)if(t[i]===e)return i;return-1}function e(t){var e=t._promiseCallbacks;return e||(e=t._promiseCallbacks={}),e}function i(t,e){return"onerror"===t?(Y.on("error",e),void 0):2!==arguments.length?Y[t]:(Y[t]=e,void 0)}function n(t){return"function"==typeof t||"object"==typeof t&&null!==t}function o(t){return"function"==typeof t}function r(t){return"object"==typeof t&&null!==t}function s(){}function h(){}function a(t){try{return t.then}catch(e){return re.error=e,re}}function u(t,e,i,n){try{t.call(e,i,n)}catch(o){return o}}function c(t,e,i){Y.async(function(t){var n=!1,o=u(i,e,function(i){n||(n=!0,e!==i?d(t,i):g(t,i))},function(e){n||(n=!0,y(t,e))},"Settle: "+(t._label||" unknown promise"));!n&&o&&(n=!0,y(t,o))},t)}function l(t,e){e._state===ne?g(t,e._result):t._state===oe?y(t,e._result):m(e,void 0,function(i){e!==i?d(t,i):g(t,i)},function(e){y(t,e)})}function p(t,e){if(e.constructor===t.constructor)l(t,e);else{var i=a(e);i===re?y(t,re.error):void 0===i?g(t,e):o(i)?c(t,e,i):g(t,e)}}function d(t,e){t===e?g(t,e):n(e)?p(t,e):g(t,e)}function f(t){t._onerror&&t._onerror(t._result),S(t)}function g(t,e){t._state===ie&&(t._result=e,t._state=ne,0===t._subscribers.length?Y.instrument&&ee("fulfilled",t):Y.async(S,t))}function y(t,e){t._state===ie&&(t._state=oe,t._result=e,Y.async(f,t))}function m(t,e,i,n){var o=t._subscribers,r=o.length;t._onerror=null,o[r]=e,o[r+ne]=i,o[r+oe]=n,0===r&&t._state&&Y.async(S,t)}function S(t){var e=t._subscribers,i=t._state;if(Y.instrument&&ee(i===ne?"fulfilled":"rejected",t),0!==e.length){for(var n,o,r=t._result,s=0;sh;h++)s[h]=t[h];for(n=0;nn;n++)i[n-1]=t[n];return i}function N(t,e){return{then:function(i,n){return t.call(e,i,n)}}}function q(t,e,i,n){var o=_(i,n,e);return o===ge&&y(t,o.value),t}function L(t,e,i,n){return fe.all(e).then(function(e){var o=_(i,n,e);return o===ge&&y(t,o.value),t})}function F(t){return t&&"object"==typeof t?t.constructor===fe?!0:R(t):!1}function O(t,e,i){this._superConstructor(t,e,!1,i)}function I(t,e,i){this._superConstructor(t,e,!0,i)}function V(t,e,i){this._superConstructor(t,e,!1,i)}function A(){return function(){process.nextTick(z)}}function M(){var t=0,e=new qe(z),i=document.createTextNode("");return e.observe(i,{characterData:!0}),function(){i.data=t=++t%2}}function H(){var t=new MessageChannel;return t.port1.onmessage=z,function(){t.port2.postMessage(0)}}function j(){return function(){setTimeout(z,1)}}function z(){for(var t=0;Te>t;t+=2){var e=Fe[t],i=Fe[t+1];e(i),Fe[t]=void 0,Fe[t+1]=void 0}Te=0}function D(t,e){Y.async(t,e)}function W(){Y.on.apply(Y,arguments)}function X(){Y.off.apply(Y,arguments)}var Q={mixin:function(t){return t.on=this.on,t.off=this.off,t.trigger=this.trigger,t._promiseCallbacks=void 0,t},on:function(i,n){var o,r=e(this);o=r[i],o||(o=r[i]=[]),-1===t(o,n)&&o.push(n)},off:function(i,n){var o,r,s=e(this);return n?(o=s[i],r=t(o,n),-1!==r&&o.splice(r,1),void 0):(s[i]=[],void 0)},trigger:function(t,i){var n,o,r=e(this);if(n=r[t])for(var s=0;s1)throw new Error("Second argument not supported");if("object"!=typeof t)throw new TypeError("Argument must be an object");return s.prototype=t,new s},te=[],ee=function(t,e,i){1===te.push({name:t,payload:{guid:e._guidKey+e._id,eventName:t,detail:e._result,childGuid:i&&e._guidKey+i._id,label:e._label,timeStamp:Z(),stack:new Error(e._label).stack}})&&setTimeout(function(){for(var t,e=0;en;n++)this._eachEntry(i[n],n)},U.prototype._eachEntry=function(t,e){var i=this._instanceConstructor;r(t)?t.constructor===i&&t._state!==ie?(t._onerror=null,this._settledAt(t._state,e,t._result)):this._willSettleAt(i.resolve(t),e):(this._remaining--,this._result[e]=this._makeResult(ne,e,t))},U.prototype._settledAt=function(t,e,i){var n=this.promise;n._state===ie&&(this._remaining--,this._abortOnReject&&t===oe?y(n,i):this._result[e]=this._makeResult(t,e,i)),0===this._remaining&&g(n,this._result)},U.prototype._makeResult=function(t,e,i){return i},U.prototype._willSettleAt=function(t,e){var i=this;m(t,void 0,function(t){i._settledAt(ne,e,t)},function(t){i._settledAt(oe,e,t)})};var ae=function(t,e){return new he(this,t,!0,e).promise},ue=function(t,e){function i(t){d(r,t)}function n(t){y(r,t)}var o=this,r=new o(h,e);if(!G(t))return y(r,new TypeError("You must pass an array to race.")),r;for(var s=t.length,a=0;r._state===ie&&s>a;a++)m(o.resolve(t[a]),void 0,i,n);return r},ce=function(t,e){var i=this;if(t&&"object"==typeof t&&t.constructor===i)return t;var n=new i(h,e);return d(n,t),n},le=function(t,e){var i=this,n=new i(h,e);return y(n,t),n},pe="rsvp_"+Z()+"-",de=0,fe=x;x.cast=ce,x.all=ae,x.race=ue,x.resolve=ce,x.reject=le,x.prototype={constructor:x,_guidKey:pe,_onerror:function(t){Y.trigger("error",t)},then:function(t,e,i){var n=this,o=n._state;if(o===ne&&!t||o===oe&&!e)return Y.instrument&&ee("chained",this,this),this;n._onerror=null;var r=new this.constructor(h,i),s=n._result;if(Y.instrument&&ee("chained",n,r),o){var a=arguments[o-1];Y.async(function(){P(o,r,a,s)})}else m(n,r,t,e);return r},"catch":function(t,e){return this.then(null,t,e)},"finally":function(t,e){var i=this.constructor;return this.then(function(e){return i.resolve(t()).then(function(){return e})},function(e){return i.resolve(t()).then(function(){throw e})},e)}};var ge=new C,ye=new C,me=function(t,e){var i=function(){for(var i,n=this,o=arguments.length,r=new Array(o+1),s=!1,a=0;o>a;++a){if(i=arguments[a],!s){if(s=F(i),s===ye){var u=new fe(h);return y(u,ye.value),u}s&&s!==!0&&(i=N(s,i))}r[a]=i}var c=new fe(h);return r[o]=function(t,i){t?y(c,t):void 0===e?d(c,i):e===!0?d(c,k(arguments)):G(e)?d(c,T(arguments,e)):d(c,i)},s?L(c,r,t,n):q(c,r,t,n)};return i.__proto__=t,i},Se=function(t,e){return fe.all(t,e)};O.prototype=$(he.prototype),O.prototype._superConstructor=he,O.prototype._makeResult=w,O.prototype._validationError=function(){return new Error("allSettled must be called with an array")};var ve=function(t,e){return new O(fe,t,e).promise},Ee=function(t,e){return fe.race(t,e)},Pe=I;I.prototype=$(he.prototype),I.prototype._superConstructor=he,I.prototype._init=function(){this._result={}},I.prototype._validateInput=function(t){return t&&"object"==typeof t},I.prototype._validationError=function(){return new Error("Promise.hash must be called with an object")},I.prototype._enumerate=function(){var t=this.promise,e=this._input,i=[];for(var n in e)t._state===ie&&e.hasOwnProperty(n)&&i.push({position:n,entry:e[n]});var o=i.length;this._remaining=o;for(var r,s=0;t._state===ie&&o>s;s++)r=i[s],this._eachEntry(r.entry,r.position)};var Be=function(t,e){return new Pe(fe,t,e).promise};V.prototype=$(Pe.prototype),V.prototype._superConstructor=he,V.prototype._makeResult=w,V.prototype._validationError=function(){return new Error("hashSettled must be called with an object")};var we,Ue=function(t,e){return new V(fe,t,e).promise},be=function(t){throw setTimeout(function(){throw t}),t},Je=function(t){var e={};return e.promise=new fe(function(t,i){e.resolve=t,e.reject=i},t),e},xe=function(t,e,i){return fe.all(t,i).then(function(t){if(!o(e))throw new TypeError("You must pass a function as map's second argument.");for(var n=t.length,r=new Array(n),s=0;n>s;s++)r[s]=e(t[s]);return fe.all(r,i)})},Ce=function(t,e){return fe.resolve(t,e)},Re=function(t,e){return fe.reject(t,e)},_e=function(t,e,i){return fe.all(t,i).then(function(t){if(!o(e))throw new TypeError("You must pass a function as filter's second argument.");for(var n=t.length,r=new Array(n),s=0;n>s;s++)r[s]=e(t[s]);return fe.all(r,i).then(function(e){for(var i=new Array(n),o=0,r=0;n>r;r++)e[r]&&(i[o]=t[r],o++);return i.length=o,i})})},Te=0,ke=function(t,e){Fe[Te]=t,Fe[Te+1]=e,Te+=2,2===Te&&we()},Ne="undefined"!=typeof window?window:{},qe=Ne.MutationObserver||Ne.WebKitMutationObserver,Le="undefined"!=typeof Uint8ClampedArray&&"undefined"!=typeof importScripts&&"undefined"!=typeof MessageChannel,Fe=new Array(1e3);we="undefined"!=typeof process&&"[object process]"==={}.toString.call(process)?A():qe?M():Le?H():j(),Y.async=ke;if("undefined"!=typeof window&&"object"==typeof window.__PROMISE_INSTRUMENTATION__){var Oe=window.__PROMISE_INSTRUMENTATION__;i("instrument",!0);for(var Ie in Oe)Oe.hasOwnProperty(Ie)&&W(Ie,Oe[Ie])}var Ve={race:Ee,Promise:fe,allSettled:ve,hash:Be,hashSettled:Ue,denodeify:me,on:W,off:X,map:xe,filter:_e,resolve:Ce,reject:Re,all:Se,rethrow:be,defer:Je,EventTarget:Q,configure:i,async:D};"function"==typeof define&&define.amd?define(function(){return Ve}):"undefined"!=typeof module&&module.exports?module.exports=Ve:"undefined"!=typeof this&&(this.RSVP=Ve)}).call(this),"undefined"==typeof EPUBJS&&(("undefined"!=typeof window?window:this).EPUBJS={}),EPUBJS.VERSION="0.3.0",function(t){"use strict";var e=function(t){return new EPUBJS.Book(t)};"object"==typeof exports?(t.RSVP=require("rsvp"),module.exports=e):"function"==typeof define&&define.amd?define(e):t.ePub=e}(this),EPUBJS.core={},EPUBJS.core.request=function(t,e,i,n){function o(){if(this.readyState===this.DONE)if(200===this.status||this.responseXML){var t;t="xml"==e?this.responseXML?this.responseXML:(new DOMParser).parseFromString(this.response,"text/xml"):"json"==e?JSON.parse(this.response):"blob"==e?s?this.response:new Blob([this.response]):this.response,a.resolve(t)}else a.reject({status:this.status,message:this.response,stack:(new Error).stack})}var r,s=window.URL,h=s?"blob":"arraybuffer",a=new RSVP.defer,u=new XMLHttpRequest,c=XMLHttpRequest.prototype;"overrideMimeType"in c||Object.defineProperty(c,"overrideMimeType",{value:function(){}}),i&&(u.withCredentials=!0),u.open("GET",t,!0);for(r in n)u.setRequestHeader(r,n[r]);return u.onreadystatechange=o,"blob"==e&&(u.responseType=h),"json"==e&&u.setRequestHeader("Accept","application/json"),"xml"==e&&u.overrideMimeType("text/xml"),u.send(),a.promise},EPUBJS.core.uri=function(t){var e,i,n,o={protocol:"",host:"",path:"",origin:"",directory:"",base:"",filename:"",extension:"",fragment:"",href:t},r=t.indexOf("://"),s=t.indexOf("?"),h=t.indexOf("#");return-1!=h&&(o.fragment=t.slice(h+1),t=t.slice(0,h)),-1!=s&&(o.search=t.slice(s+1),t=t.slice(0,s),href=t),-1!=r?(o.protocol=t.slice(0,r),e=t.slice(r+3),n=e.indexOf("/"),-1===n?(o.host=o.path,o.path=""):(o.host=e.slice(0,n),o.path=e.slice(n)),o.origin=o.protocol+"://"+o.host,o.directory=EPUBJS.core.folder(o.path),o.base=o.origin+o.directory):(o.path=t,o.directory=EPUBJS.core.folder(t),o.base=o.directory),o.filename=t.replace(o.base,""),i=o.filename.lastIndexOf("."),-1!=i&&(o.extension=o.filename.slice(i+1)),o},EPUBJS.core.folder=function(t){var e=t.lastIndexOf("/");if(-1==e)var i="";return i=t.slice(0,e+1)},EPUBJS.core.queue=function(t){var e=[],i=t,n=function(t,i,n){return e.push({funcName:t,args:i,context:n}),e},o=function(){var t;e.length&&(t=e.shift(),i[t.funcName].apply(t.context||i,t.args))},r=function(){for(;e.length;)o()},s=function(){e=[]},h=function(){return e.length};return{enqueue:n,dequeue:o,flush:r,clear:s,length:h}},EPUBJS.core.isElement=function(t){return!(!t||1!=t.nodeType)},EPUBJS.core.uuid=function(){var t=(new Date).getTime(),e="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,function(e){var i=(t+16*Math.random())%16|0;return t=Math.floor(t/16),("x"==e?i:7&i|8).toString(16)});return e},EPUBJS.core.values=function(t){for(var e=-1,i=Object.keys(t),n=i.length,o=Array(n);++eo;o++)if("undefined"!=typeof document.body.style[e[o]+i])return e[o]+i;return t},EPUBJS.core.defaults=function(t){for(var e=1,i=arguments.length;i>e;e++){var n=arguments[e];for(var o in n)void 0===t[o]&&(t[o]=n[o])}return t},EPUBJS.core.extend=function(t){var e=[].slice.call(arguments,1);return e.forEach(function(e){e&&Object.getOwnPropertyNames(e).forEach(function(i){Object.defineProperty(t,i,Object.getOwnPropertyDescriptor(e,i))})}),t},EPUBJS.core.insert=function(t,e,i){var n=EPUBJS.core.locationOf(t,e,i);return e.splice(n,0,t),n},EPUBJS.core.locationOf=function(t,e,i,n,o){var r,s=n||0,h=o||e.length,a=parseInt(s+(h-s)/2);return i||(i=function(t,e){return t>e?1:e>t?-1:(t=e)?0:void 0}),0>=h-s?a:(r=i(e[a],t),h-s===1?r>0?a:a+1:0===r?a:-1===r?EPUBJS.core.locationOf(t,e,i,a,h):EPUBJS.core.locationOf(t,e,i,s,a))},EPUBJS.core.indexOfSorted=function(t,e,i,n,o){var r,s=n||0,h=o||e.length,a=parseInt(s+(h-s)/2);return i||(i=function(t,e){return t>e?1:e>t?-1:(t=e)?0:void 0}),0>=h-s?-1:(r=i(e[a],t),h-s===1?0===r?a:-1:0===r?a:-1===r?EPUBJS.core.indexOfSorted(t,e,i,a,h):EPUBJS.core.indexOfSorted(t,e,i,s,a))},EPUBJS.core.requestAnimationFrame=window.requestAnimationFrame||window.mozRequestAnimationFrame||window.webkitRequestAnimationFrame||window.msRequestAnimationFrame,EPUBJS.core.bounds=function(t){var e=window.getComputedStyle(t),i=["width","paddingRight","paddingLeft","marginRight","marginLeft","borderRightWidth","borderLeftWidth"],n=["height","paddingTop","paddingBottom","marginTop","marginBottom","borderTopWidth","borderBottomWidth"],o=0,r=0;return i.forEach(function(t){o+=parseFloat(e[t])||0}),n.forEach(function(t){r+=parseFloat(e[t])||0}),{height:r,width:o}},EPUBJS.core.borders=function(t){var e=window.getComputedStyle(t),i=["paddingRight","paddingLeft","marginRight","marginLeft","borderRightWidth","borderLeftWidth"],n=["paddingTop","paddingBottom","marginTop","marginBottom","borderTopWidth","borderBottomWidth"],o=0,r=0;return i.forEach(function(t){o+=parseFloat(e[t])||0}),n.forEach(function(t){r+=parseFloat(e[t])||0}),{height:r,width:o}},EPUBJS.core.windowBounds=function(){var t=window.innerWidth,e=window.innerHeight;return{top:0,left:0,right:t,bottom:e,width:t,height:e}},EPUBJS.core.cleanStringForXpath=function(t){var e=t.match(/[^'"]+|['"]/g);return e=e.map(function(t){return"'"===t?'"\'"':'"'===t?"'\"'":"'"+t+"'"}),"concat('',"+e.join(",")+")"},EPUBJS.core.indexOfTextNode=function(t){for(var e,i=t.parentNode,n=i.childNodes,o=-1,r=0;r=0;s--){var h=o.snapshotItem(s),a=h.getAttribute("id")||!1,u=h.querySelector("content"),c=u.getAttribute("src"),l=h.querySelector("navLabel"),p=l.textContent?l.textContent:"",d=c.split("#"),f=(d[0],e(h));n.unshift({id:a,href:c,label:p,subitems:f,parent:i?i.getAttribute("id"):null})}return n}var i=t.querySelector("navMap");return i?e(i):[]},EPUBJS.EpubCFI=function(t){return t?this.parse(t):void 0},EPUBJS.EpubCFI.prototype.generateChapterComponent=function(t,e,i){var n=parseInt(e),o=t+1,r="/"+o+"/";return r+=2*(n+1),i&&(r+="["+i+"]"),r},EPUBJS.EpubCFI.prototype.generatePathComponent=function(t){var e=[];return t.forEach(function(t){var i="";i+=2*(t.index+1),t.id&&(i+="["+t.id+"]"),e.push(i)}),e.join("/")},EPUBJS.EpubCFI.prototype.generateCfiFromElement=function(t,e){var i=this.pathTo(t),n=this.generatePathComponent(i);return n.length?"epubcfi("+e+"!"+n+"/1:0)":"epubcfi("+e+"!/4/)"},EPUBJS.EpubCFI.prototype.pathTo=function(t){for(var e,i=[];t&&null!==t.parentNode&&9!=t.parentNode.nodeType;)e=t.parentNode.children,i.unshift({id:t.id,tagName:t.tagName,index:e?Array.prototype.indexOf.call(e,t):0}),t=t.parentNode;return i},EPUBJS.EpubCFI.prototype.getChapterComponent=function(t){var e=t.split("!");return e[0]},EPUBJS.EpubCFI.prototype.getPathComponent=function(t){var e=t.split("!"),i=e[1]?e[1].split(":"):"";return i[0]},EPUBJS.EpubCFI.prototype.getCharecterOffsetComponent=function(t){var e=t.split(":");return e[1]||""},EPUBJS.EpubCFI.prototype.parse=function(t){var e,i,n,o,r,s,h,a,u,c={},l=function(t){var e,i,n,o;return e="element",i=parseInt(t)/2-1,n=t.match(/\[(.*)\]/),n&&n[1]&&(o=n[1]),{type:e,index:i,id:o||!1}};return"string"!=typeof t?{spinePos:-1}:(c.str=t,0===t.indexOf("epubcfi(")&&")"===t[t.length-1]&&(t=t.slice(8,t.length-1)),i=this.getChapterComponent(t),n=this.getPathComponent(t)||"",o=this.getCharecterOffsetComponent(t),i?(e=i.split("/")[2]||"")?(c.spinePos=parseInt(e)/2-1||0,s=e.match(/\[(.*)\]/),c.spineId=s?s[1]:!1,-1!=n.indexOf(",")&&console.warn("CFI Ranges are not supported"),h=n.split("/"),a=h.pop(),c.steps=[],h.forEach(function(t){var e;t&&(e=l(t),c.steps.push(e))}),u=parseInt(a),isNaN(u)||(u%2===0?c.steps.push(l(a)):c.steps.push({type:"text",index:(u-1)/2})),r=o.match(/\[(.*)\]/),r&&r[1]?(c.characterOffset=parseInt(o.split("[")[0]),c.textLocationAssertion=r[1]):c.characterOffset=parseInt(o),c):{spinePos:-1}:{spinePos:-1})},EPUBJS.EpubCFI.prototype.addMarker=function(t,e,i){var n,o,r,s,h=e||document,a=i||this.createMarker(h);return"string"==typeof t&&(t=this.parse(t)),o=t.steps[t.steps.length-1],-1===t.spinePos?!1:(n=this.findParent(t,h))?(o&&"text"===o.type?(r=n.childNodes[o.index],t.characterOffset?(s=r.splitText(t.characterOffset),a.classList.add("EPUBJS-CFI-SPLIT"),n.insertBefore(a,s)):n.insertBefore(a,r)):n.insertBefore(a,n.firstChild),a):!1},EPUBJS.EpubCFI.prototype.createMarker=function(t){var e=t||document,i=e.createElement("span");return i.id="EPUBJS-CFI-MARKER:"+EPUBJS.core.uuid(),i.classList.add("EPUBJS-CFI-MARKER"),i},EPUBJS.EpubCFI.prototype.removeMarker=function(t,e){t.classList.contains("EPUBJS-CFI-SPLIT")?(nextSib=t.nextSibling,prevSib=t.previousSibling,nextSib&&prevSib&&3===nextSib.nodeType&&3===prevSib.nodeType&&(prevSib.textContent+=nextSib.textContent,t.parentNode.removeChild(nextSib)),t.parentNode.removeChild(t)):t.classList.contains("EPUBJS-CFI-MARKER")&&t.parentNode.removeChild(t)},EPUBJS.EpubCFI.prototype.findParent=function(t,e){var i,n,o,r=e||document,s=r.getElementsByTagName("html")[0],h=Array.prototype.slice.call(s.children);if("string"==typeof t&&(t=this.parse(t)),n=t.steps.slice(0),!n.length)return r.getElementsByTagName("body")[0];for(;n&&n.length>0;){if(i=n.shift(),"text"===i.type?(o=s.childNodes[i.index],s=o.parentNode||s):s=i.id?r.getElementById(i.id):h[i.index],"undefined"==typeof s)return console.error("No Element For",i,t.str),!1;h=Array.prototype.slice.call(s.children)}return s},EPUBJS.EpubCFI.prototype.compare=function(t,e){if("string"==typeof t&&(t=new EPUBJS.EpubCFI(t)),"string"==typeof e&&(e=new EPUBJS.EpubCFI(e)),t.spinePos>e.spinePos)return 1;if(t.spinePose.steps[i].index)return 1;if(t.steps[i].indexe.characterOffset?1:t.characterOffset=0?(r=o.length,t.characterOffset-1?(delete this.spineByHref[t.href],delete this.spineById[t.idref],this.spineItems.splice(e,1)):void 0},EPUBJS.replace={},EPUBJS.replace.links=function(t,e){for(var i=t.document.querySelectorAll("a[href]"),n=function(t){var i=t.getAttribute("href"),n=new EPUBJS.core.uri(i);n.protocol?t.setAttribute("target","_blank"):0===i.indexOf("#")||(t.onclick=function(){return e.display(i),!1})},o=0;o-1&&(this.observer=this.observe(this.document.body)),this.imageLoadListeners(),this.mediaQueryListeners(),this.resizeListenters()},EPUBJS.View.prototype.resizeListenters=function(){clearTimeout(this.expanding)},EPUBJS.View.prototype.mediaQueryListeners=function(){for(var t=this.document.styleSheets,e=function(t){t.matches&&!this._expanding&&setTimeout(this.expand.bind(this),1)}.bind(this),i=0;i-1&&(id=t.substring(t.indexOf("#")+1),el=this.document.getElementById(id),el&&(i=el.getBoundingClientRect())),{left:window.scrollX+e.left+i.left,top:window.scrollY+e.top+i.top}):void 0},EPUBJS.View.prototype.addCss=function(t){var e=document.createElement("link"),i=!1;return new RSVP.Promise(function(n){return this.document?(e.type="text/css",e.rel="stylesheet",e.href=t,e.onload=e.onreadystatechange=function(){i||this.readyState&&"complete"!=this.readyState||(i=!0,setTimeout(function(){n(!0)},1))},this.document.head.appendChild(e),void 0):(n(!1),void 0)}.bind(this))},EPUBJS.View.prototype.addStylesheetRules=function(t){var e,i=document.createElement("style");if(this.document){this.document.head.appendChild(i),e=i.sheet;for(var n=0,o=t.length;o>n;n++){var r=1,s=t[n],h=t[n][0],a="";"[object Array]"===Object.prototype.toString.call(s[1][0])&&(s=s[1],r=0);for(var u=s.length;u>r;r++){var c=s[r];a+=c[0]+":"+c[1]+(c[2]?" !important":"")+";\n"}e.insertRule(h+"{"+a+"}",e.cssRules.length)}}},RSVP.EventTarget.mixin(EPUBJS.View.prototype),EPUBJS.Layout=EPUBJS.Layout||{},EPUBJS.Layout.Reflowable=function(){},EPUBJS.Layout.Reflowable.prototype.calculate=function(t,e,i,n){var o,r,s,h=n||1,a=Math.floor(t),u=a%2===0?a:a-1,c=Math.floor(u/8),l=i>=0?i:c%2===0?c:c-1;o=h>1?Math.floor((u-l)/h):u,r=o*h,s=(o+l)*h,this.columnAxis=EPUBJS.core.prefixed("columnAxis"),this.columnGap=EPUBJS.core.prefixed("columnGap"),this.columnWidth=EPUBJS.core.prefixed("columnWidth"),this.columnFill=EPUBJS.core.prefixed("columnFill"),this.width=u,this.height=e,this.spread=r,this.delta=s,this.column=o,this.gap=l,this.divisor=h},EPUBJS.Layout.Reflowable.prototype.format=function(t){var e=t.document.documentElement,i=t.document.body;e.style.overflow="hidden",e.style.width=this.width+"px",i.style.height=this.height+"px",i.style[this.columnAxis]="horizontal",i.style[this.columnFill]="auto",i.style[this.columnGap]=this.gap+"px",i.style[this.columnWidth]=this.column+"px",t.iframe.style.marginRight=this.gap+"px"},EPUBJS.Layout.Reflowable.prototype.count=function(t){var e=t.root().scrollWidth,i=Math.ceil(e/this.spread);return{spreads:i,pages:i*this.divisor}},EPUBJS.Layout.Fixed=function(){},EPUBJS.Layout.Fixed.prototype.calculate=function(){},EPUBJS.Layout.Fixed.prototype.format=function(t){var e,i,n=t.document.documentElement,o=documentElement.querySelector("[name=viewport");o&&o.hasAttribute("content")&&(content=o.getAttribute("content"),contents=content.split(","),contents[0]&&(e=contents[0].replace("width=","")),contents[1]&&(i=contents[1].replace("height=",""))),t.resize(e,i),n.style.overflow="auto"},EPUBJS.Layout.Fixed.prototype.count=function(){return{spreads:1,pages:1}},EPUBJS.Layout.Scroll=function(){},EPUBJS.Layout.Scroll.prototype.calculate=function(t){this.spread=t,this.column=t,this.gap=0},EPUBJS.Layout.Scroll.prototype.format=function(t){var e=t.document.documentElement;e.style.width="auto",e.style.height="auto"},EPUBJS.Layout.Scroll.prototype.count=function(){return{spreads:1,pages:1}},EPUBJS.Rendition=function(t,e){this.settings=EPUBJS.core.extend(this.settings||{},{infinite:!0,hidden:!1,width:!1,height:null,layoutOveride:null,axis:"vertical"}),EPUBJS.core.extend(this.settings,e),this.viewSettings={},this.book=t,this.views=[],this.hooks={},this.hooks.display=new EPUBJS.Hook(this),this.hooks.content=new EPUBJS.Hook(this),this.hooks.layout=new EPUBJS.Hook(this),this.hooks.render=new EPUBJS.Hook(this),this.hooks.show=new EPUBJS.Hook(this),this.hooks.content.register(EPUBJS.replace.links.bind(this)),this.epubcfi=new EPUBJS.EpubCFI,this.q=new EPUBJS.Queue(this),this.q.enqueue(this.book.opened),this.q.enqueue(this.parseLayoutProperties)},EPUBJS.Rendition.prototype.initialize=function(t){{var e,i=t||{},n=i.height,o=i.width;i.hidden||!1}return i.height&&EPUBJS.core.isNumber(i.height)&&(n=i.height+"px"),i.width&&EPUBJS.core.isNumber(i.width)&&(o=i.width+"px"),e=document.createElement("div"),e.id="epubjs-container:"+EPUBJS.core.uuid(),e.classList.add("epub-container"),e.style.fontSize="0",e.style.wordSpacing="0",e.style.lineHeight="0",e.style.verticalAlign="top","horizontal"===this.settings.axis&&(e.style.whiteSpace="nowrap"),o&&(e.style.width=o),n&&(e.style.height=n),e.style.overflow=this.settings.overflow,e},EPUBJS.Rendition.wrap=function(t){var e=document.createElement("div");return e.style.visibility="hidden",e.style.overflow="hidden",e.style.width="0",e.style.height="0",e.appendChild(t),e},EPUBJS.Rendition.prototype.attachTo=function(t){return this.container=this.initialize({width:this.settings.width,height:this.settings.height}),EPUBJS.core.isElement(t)?this.element=t:"string"==typeof t&&(this.element=document.getElementById(t)),this.element?(this.settings.hidden?(this.wrapper=this.wrap(this.container),this.element.appendChild(this.wrapper)):this.element.appendChild(this.container),this.attachListeners(),this.stageSize(),this.applyLayoutMethod(),this.trigger("attached"),void 0):(console.error("Not an Element"),void 0)},EPUBJS.Rendition.prototype.attachListeners=function(){EPUBJS.core.isNumber(this.settings.width)&&EPUBJS.core.isNumber(this.settings.height)||window.addEventListener("resize",this.onResized.bind(this),!1)},EPUBJS.Rendition.prototype.bounds=function(){return this.container.getBoundingClientRect()},EPUBJS.Rendition.prototype.display=function(t){return this.q.enqueue(this._display,t)},EPUBJS.Rendition.prototype._display=function(t){var e,i,n,o,r=new RSVP.defer,s=r.promise;return this.epubcfi.isCfiString(t)?(n=this.epubcfi.parse(t),o=n.spinePos,e=this.book.spine.get(o)):e=this.book.spine.get(t),this.displaying=!0,this.hide(),e?(i=this.createView(e),this.q.enqueue(this.append,i),this.q.enqueue(function(){var e=i.locationOf(t);return e.top>250||e.left>250?this.moveTo(e):void 0}),this.show(),this.hooks.display.trigger(i).then(function(){this.trigger("displayed",e),r.resolve(this)}.bind(this))):r.reject(new Error("No Section Found")),s},EPUBJS.Rendition.prototype.moveTo=function(t){this.scrollBy(t.left,t.top)},EPUBJS.Rendition.prototype.render=function(t,e){return t.create(),t.onLayout=this.layout.format.bind(this.layout),this.resizeView(t),t.render(this.book.request).then(function(){return this.hooks.content.trigger(t,this)}.bind(this)).then(function(){return this.hooks.layout.trigger(t,this)}.bind(this)).then(function(){return t.display()}.bind(this)).then(function(){return this.hooks.render.trigger(t,this)}.bind(this)).then(function(){e!==!1&&this.hidden===!1&&this.q.enqueue(function(t){t.show()},t),this.trigger("rendered",t.section)}.bind(this)).catch(function(t){this.trigger("loaderror",t)}.bind(this))},EPUBJS.Rendition.prototype.afterDisplayed=function(t){this.trigger("added",t.section)},EPUBJS.Rendition.prototype.append=function(t){return this.clear(),this.views.push(t),this.container.appendChild(t.element),t.onDisplayed=this.afterDisplayed.bind(this),this.render(t)},EPUBJS.Rendition.prototype.clear=function(){this.views.forEach(function(t){this._remove(t)}.bind(this)),this.views=[]},EPUBJS.Rendition.prototype.remove=function(t){var e=this.views.indexOf(t);e>-1&&this.views.splice(e,1),this._remove(t)},EPUBJS.Rendition.prototype._remove=function(t){t.off("resized"),t.displayed&&t.destroy(),this.container.removeChild(t.element),t=null},EPUBJS.Rendition.prototype.resizeView=function(t){"pre-paginated"===this.globalLayoutProperties.layout?t.lock("both",this.stage.width,this.stage.height):t.lock("width",this.stage.width,this.stage.height)},EPUBJS.Rendition.prototype.stageSize=function(t,e){var i,n=t||this.settings.width,o=e||this.settings.height;return n===!1&&(i=this.element.getBoundingClientRect(),i.width&&(n=i.width,this.container.style.width=i.width+"px")),o===!1&&(i=i||this.element.getBoundingClientRect(),i.height&&(o=i.height,this.container.style.height=i.height+"px")),n&&!EPUBJS.core.isNumber(n)&&(i=this.container.getBoundingClientRect(),n=i.width),o&&!EPUBJS.core.isNumber(o)&&(i=i||this.container.getBoundingClientRect(),o=i.height),this.containerStyles=window.getComputedStyle(this.container),this.containerPadding={left:parseFloat(this.containerStyles["padding-left"])||0,right:parseFloat(this.containerStyles["padding-right"])||0,top:parseFloat(this.containerStyles["padding-top"])||0,bottom:parseFloat(this.containerStyles["padding-bottom"])||0},this.stage={width:n-this.containerPadding.left-this.containerPadding.right,height:o-this.containerPadding.top-this.containerPadding.bottom},this.stage},EPUBJS.Rendition.prototype.applyLayoutMethod=function(){this.layout=new EPUBJS.Layout.Scroll,this.updateLayout(),this.map=new EPUBJS.Map(this.layout)},EPUBJS.Rendition.prototype.updateLayout=function(){this.layout.calculate(this.stage.width,this.stage.height)},EPUBJS.Rendition.prototype.resize=function(t,e){this.stageSize(t,e),this.updateLayout(),this.views.forEach(this.resizeView.bind(this)),this.trigger("resized",{width:this.stage.width,height:this.stage.height})},EPUBJS.Rendition.prototype.onResized=function(){this.resize()},EPUBJS.Rendition.prototype.createView=function(t){return new EPUBJS.View(t,this.viewSettings)},EPUBJS.Rendition.prototype.next=function(){return this.q.enqueue(function(){var t,e;return this.views.length?(t=this.views[0].section.next(),t?(e=this.createView(t),this.append(e)):void 0):void 0})},EPUBJS.Rendition.prototype.prev=function(){return this.q.enqueue(function(){var t,e;return this.views.length?(t=this.views[0].section.prev(),t?(e=this.createView(t),this.append(e)):void 0):void 0})},EPUBJS.Rendition.prototype.parseLayoutProperties=function(t){var e=t||this.book.package.metadata,i=this.layoutOveride&&this.layoutOveride.layout||e.layout||"reflowable",n=this.layoutOveride&&this.layoutOveride.spread||e.spread||"auto",o=this.layoutOveride&&this.layoutOveride.orientation||e.orientation||"auto";return this.globalLayoutProperties={layout:i,spread:n,orientation:o},this.globalLayoutProperties},EPUBJS.Rendition.prototype.current=function(){var t=this.visible();return t.length?t[t.length-1]:null},EPUBJS.Rendition.prototype.isVisible=function(t,e,i,n){var o=t.position(),r=n||this.container.getBoundingClientRect();return"horizontal"===this.settings.axis&&o.right>r.left-e&&!(o.left>=r.right+i)?!0:"vertical"===this.settings.axis&&o.bottom>r.top-e&&!(o.top>=r.bottom+i)?!0:!1},EPUBJS.Rendition.prototype.visible=function(){for(var t,e,i=this.bounds(),n=[],o=0;o=0;o--)if(t=this.views[o],e=t.bounds().top,ethis.settings.offsetDelta||this.scrollDeltaHorz>this.settings.offsetDelta)&&(this.q.enqueue(this.check),this.scrollDeltaVert=0,this.scrollDeltaHorz=0),this.scrollDeltaVert+=Math.abs(scrollTop-this.prevScrollTop),this.scrollDeltaHorz+=Math.abs(scrollLeft-this.prevScrollLeft),this.settings.height?(this.prevScrollTop=this.container.scrollTop,this.prevScrollLeft=this.container.scrollLeft):(this.prevScrollTop=window.scrollY,this.prevScrollLeft=window.scrollX),clearTimeout(this.scrollTimeout),this.scrollTimeout=setTimeout(function(){this.scrollDeltaVert=0,this.scrollDeltaHorz=0}.bind(this),150),this.scrolled=!1),this.tick.call(window,this.onScroll.bind(this)) +},EPUBJS.Continuous.prototype.resizeView=function(t){"horizontal"===this.settings.axis?t.lock("height",this.stage.width,this.stage.height):t.lock("width",this.stage.width,this.stage.height)},EPUBJS.Continuous.prototype.currentLocation=function(){{var t,e,i=this.visible();this.container.getBoundingClientRect()}return 1===i.length?this.map.page(i[0]):i.length>1?(t=this.map.page(i[0]),e=this.map.page(i[i.length-1]),{start:t.start,end:e.end}):void 0},EPUBJS.Paginate=function(t,e){EPUBJS.Continuous.apply(this,arguments),this.settings=EPUBJS.core.extend(this.settings||{},{width:600,height:400,axis:"horizontal",forceSingle:!1,minSpreadWidth:800,gap:"auto",overflow:"hidden",infinite:!1}),EPUBJS.core.extend(this.settings,e),this.isForcedSingle=this.settings.forceSingle,this.viewSettings={axis:this.settings.axis},this.start()},EPUBJS.Paginate.prototype=Object.create(EPUBJS.Continuous.prototype),EPUBJS.Paginate.prototype.constructor=EPUBJS.Paginate,EPUBJS.Paginate.prototype.determineSpreads=function(t){return this.isForcedSingle||!t||this.bounds().width1?(t=h.left-s[0].position().left,i=t+this.layout.column,e=h.left+this.layout.spread-s[s.length-1].position().left,n=e+this.layout.column,o=this.map.page(s[0],t,i),r=this.map.page(s[s.length-1],e,n),{start:o.start,end:r.end}):void 0},EPUBJS.Paginate.prototype.resize=function(t,e){this.q.clear(),this.stageSize(t,e),this.updateLayout(),this.display(this.location.start),this.trigger("resized",{width:this.stage.width,height:this.stage.height})},EPUBJS.Paginate.prototype.onResized=function(){this.clear(),clearTimeout(this.resizeTimeout),this.resizeTimeout=setTimeout(function(){this.resize()}.bind(this),150)},EPUBJS.Paginate.prototype.adjustImages=function(t){return t.addStylesheetRules([["img",["max-width",this.layout.spread+"px"],["max-height",this.layout.height+"px"]]]),new RSVP.Promise(function(t){setTimeout(function(){t()},1)})},EPUBJS.Map=function(t){this.layout=t},EPUBJS.Map.prototype.section=function(t){var e=this.findRanges(t),i=this.rangeListToCfiList(t,e);return i},EPUBJS.Map.prototype.page=function(t,e,i){var n=t.document.body;return this.rangePairToCfiPair(t.section,{start:this.findStart(n,e,i),end:this.findEnd(n,e,i)})},EPUBJS.Map.prototype.walk=function(t,e){for(var i,n,o=document.createTreeWalker(t,NodeFilter.SHOW_TEXT,{acceptNode:function(t){return t.data.trim().length>0?NodeFilter.FILTER_ACCEPT:NodeFilter.FILTER_REJECT}},!1);(i=o.nextNode())&&!(n=e(i)););return n},EPUBJS.Map.prototype.findRanges=function(t){for(var e,i,n=[],o=this.layout.count(t),r=this.layout.column,s=this.layout.gap,h=0;h=e&&i>=n?t:o>e?t:(s=t,r.push(t),void 0)}))return this.findTextStartRange(o,e,i);return this.findTextStartRange(s,e,i)},EPUBJS.Map.prototype.findEnd=function(t,e,i){for(var n,o,r=[t],s=t;r.length;)if(n=r.shift(),o=this.walk(n,function(t){var e,n,o,h;return t.nodeType==Node.TEXT_NODE?(h=document.createRange(),h.selectNodeContents(t),o=h.getBoundingClientRect()):o=t.getBoundingClientRect(),e=o.left,n=o.right,e>i&&s?s:n>i?t:(s=t,r.push(t),void 0)}))return this.findTextEndRange(o,e,i);return this.findTextEndRange(s,e,i)},EPUBJS.Map.prototype.findTextStartRange=function(t,e){for(var i,n,o,r=this.splitTextNodeIntoRanges(t),s=0;s=e)return n;i=n}return r[0]},EPUBJS.Map.prototype.findTextEndRange=function(t,e,i){for(var n,o,r,s=this.splitTextNodeIntoRanges(t),h=0;hi&&n)return n;if(r.right>i)return o;n=o}return s[s.length-1]},EPUBJS.Map.prototype.splitTextNodeIntoRanges=function(t,e){var i,n=[],o=t.textContent||"",r=o.trim(),s=t.ownerDocument,h=e||" ";if(pos=r.indexOf(h),-1===pos||t.nodeType!=Node.TEXT_NODE)return i=s.createRange(),i.selectNodeContents(t),[i];for(i=s.createRange(),i.setStart(t,0),i.setEnd(t,pos),n.push(i),i=!1;-1!=pos;)pos=r.indexOf(h,pos+1),pos>0&&(i&&(i.setEnd(t,pos),n.push(i)),i=s.createRange(),i.setStart(t,pos+1));return i&&(i.setEnd(t,r.length),n.push(i)),n},EPUBJS.Map.prototype.rangePairToCfiPair=function(t,e){var i=e.start,n=e.end;return i.collapse(!0),n.collapse(!0),startCfi=t.cfiFromRange(i),endCfi=t.cfiFromRange(n),{start:startCfi,end:endCfi}},EPUBJS.Map.prototype.rangeListToCfiList=function(t,e){for(var i,n=[],o=0;o var currentSectionIndex = 8; // Load the opf - var book = ePub("../books/moby-dick/OPS/package.opf"); + var book = ePub("../books/alice/OPS/package.opf"); var rendition = book.renderTo("viewer"); - + rendition.display("chapter_008.xhtml"); @@ -87,7 +87,7 @@ if(nextSection) { nextNav = book.navigation.get(nextSection.href); - + if(nextNav) { nextLabel = nextNav.label; } else { @@ -101,7 +101,7 @@ if(prevSection) { prevNav = book.navigation.get(prevSection.href); - + if(prevNav) { prevLabel = prevNav.label; } else { diff --git a/examples/spreads.html b/examples/spreads.html index 2492c34..3001f5e 100644 --- a/examples/spreads.html +++ b/examples/spreads.html @@ -23,9 +23,9 @@ -webkit-justify-content: center; } - #viewer { - /*width: 900px;*/ - width: 80%; + #viewer { + width: 900px; + /*width: 80%;*/ height: 600px; background: white; box-shadow: 0 0 4px #ccc; @@ -39,13 +39,13 @@ margin: 40px auto; } - + /* #viewer > iframe { overflow: scroll; height: 400px; - + -webkit-column-axis: horizontal; -webkit-column-fill: auto; -webkit-column-width: 400px; @@ -63,7 +63,7 @@ left: 40px; } - #next { + #next { right: 40px; } @@ -84,7 +84,7 @@ .arrow:hover { color: #777; } - + .arrow:active { color: #000; } @@ -106,7 +106,7 @@ top: 5%; opacity: .15; box-shadow: -2px 0 15px rgba(0, 0, 0, 1); - content: ""; + content: ""; } } @@ -119,15 +119,15 @@ diff --git a/src/continuous.js b/src/continuous.js index b99bcd2..417215e 100644 --- a/src/continuous.js +++ b/src/continuous.js @@ -1,5 +1,5 @@ EPUBJS.Continuous = function(book, options) { - + EPUBJS.Rendition.apply(this, arguments); // call super constructor. this.settings = EPUBJS.core.extend(this.settings || {}, { @@ -9,14 +9,14 @@ EPUBJS.Continuous = function(book, options) { offset: 500, offsetDelta: 250 }); - + EPUBJS.core.extend(this.settings, options); - + if(this.settings.hidden) { this.wrapper = this.wrap(this.container); } - - + + }; // subclass extends superclass @@ -26,7 +26,7 @@ EPUBJS.Continuous.prototype.constructor = EPUBJS.Continuous; EPUBJS.Continuous.prototype.attachListeners = function(){ // Listen to window for resize event if width or height is set to a percent - if(!EPUBJS.core.isNumber(this.settings.width) || + if(!EPUBJS.core.isNumber(this.settings.width) || !EPUBJS.core.isNumber(this.settings.height) ) { window.addEventListener("resize", this.onResized.bind(this), false); } @@ -40,14 +40,16 @@ EPUBJS.Continuous.prototype.attachListeners = function(){ }; EPUBJS.Continuous.prototype._display = function(target){ - + var displaying = new RSVP.defer(); var displayed = displaying.promise; - + var section; var view; var cfi, spinePos; + var visible; + if(this.epubcfi.isCfiString(target)) { cfi = this.epubcfi.parse(target); spinePos = cfi.spinePos; @@ -56,12 +58,48 @@ EPUBJS.Continuous.prototype._display = function(target){ section = this.book.spine.get(target); } - this.displaying = true; - this.hide(); + + + if(section){ + + this.displaying = true; + + // Check to make sure the section we want isn't already shown + visible = this.visible(); + for (var i = 0; i < visible.length; i++) { + if(visible.length && + section.index === visible[i].section.index){ + // Section already has a visible view + view = visible[i]; + // Move to target location + this.q.enqueue(function(){ + + var offset = view.locationOf(target); + + return this.moveTo(offset); + + }); + + this.q.enqueue(this.check); + // Trigger display hooks + this.hooks.display.trigger(view) + .then(function(){ + this.trigger("displayed", section); + displaying.resolve(this); + }.bind(this)); + + // Finished, no need to fill + return displayed; + + } + } + + this.hide(); + view = new EPUBJS.View(section, this.viewSettings); - + // This will clear all previous views this.q.enqueue(this.fill, view).then(function(){ @@ -73,18 +111,18 @@ EPUBJS.Continuous.prototype._display = function(target){ return this.moveTo(offset); }); - + this.q.enqueue(this.check); this.q.enqueue(this.show); - // This hook doesn't prevent showing, but waits to resolve until - // all the hooks have finished. Might want to block showing. - this.hooks.display.trigger(view) - .then(function(){ - this.trigger("displayed", section); - displaying.resolve(this); - }.bind(this)); + // // This hook doesn't prevent showing, but waits to resolve until + // // all the hooks have finished. Might want to block showing. + // this.hooks.display.trigger(view) + // .then(function(){ + // this.trigger("displayed", section); + // // displaying.resolve(this); + // }.bind(this)); }.bind(this)); @@ -93,7 +131,14 @@ EPUBJS.Continuous.prototype._display = function(target){ // this.displaying = false; // displaying.resolve(this); //}.bind(this)); - displaying.resolve(this); + + // This hook doesn't prevent showing, but waits to resolve until + // all the hooks have finished. Might want to block showing. + this.hooks.display.trigger(view) + .then(function(){ + this.trigger("displayed", section); + displaying.resolve(this); + }.bind(this)); } else { displaying.reject(new Error("No Section Found")); @@ -104,18 +149,20 @@ EPUBJS.Continuous.prototype._display = function(target){ EPUBJS.Continuous.prototype.moveTo = function(offset){ - var bounds = this.bounds(); - var dist = Math.floor(offset.top / bounds.height) * bounds.height; + // var bounds = this.bounds(); + // var dist = Math.floor(offset.top / bounds.height) * bounds.height; + return this.check( + offset.left+this.settings.offset, + offset.top+this.settings.offset) + .then(function(){ - return this.check(0, dist+this.settings.offset).then(function(){ - - if(this.settings.axis === "vertical") { - this.scrollBy(0, dist); - } else { - this.scrollBy(dist, 0); - } + if(this.settings.axis === "vertical") { + this.scrollBy(0, offset.top); + } else { + this.scrollBy(offset.left, 0); + } - }.bind(this)); + }.bind(this)); }; EPUBJS.Continuous.prototype.afterDisplayed = function(currView){ @@ -166,7 +213,7 @@ EPUBJS.Continuous.prototype.append = function(view){ EPUBJS.Continuous.prototype.prepend = function(view){ this.views.unshift(view); this.container.insertBefore(view.element, this.container.firstChild); - + // view.on("shown", this.afterDisplayedAbove.bind(this)); view.onDisplayed = this.afterDisplayed.bind(this); @@ -226,15 +273,15 @@ EPUBJS.Continuous.prototype.insert = function(view, index) { // } // this.container.removeChild(view.element); - + // view.off("resized"); // if(view.displayed){ // view.destroy(); // } - + // view = null; - + // }; EPUBJS.Continuous.prototype.first = function() { @@ -267,7 +314,7 @@ EPUBJS.Continuous.prototype.check = function(_offset){ } } else { - + if(view.displayed) { // console.log("destroy", view.section.index) this.q.enqueue(view.destroy.bind(view)); @@ -283,7 +330,7 @@ EPUBJS.Continuous.prototype.check = function(_offset){ }.bind(this)); - + if(promises.length){ return RSVP.all(promises) @@ -331,12 +378,12 @@ EPUBJS.Continuous.prototype.trim = function(){ var lastIndex = this.views.indexOf(last); var above = this.views.slice(0, firstIndex); var below = this.views.slice(lastIndex+1); - + // Erase all but last above for (var i = 0; i < above.length-1; i++) { this.erase(above[i], above); } - + // Erase all except first below for (var j = 1; j < below.length; j++) { this.erase(below[j]); @@ -347,7 +394,7 @@ EPUBJS.Continuous.prototype.trim = function(){ }; EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim - + var prevTop; var prevLeft; @@ -362,7 +409,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim var bounds = view.bounds(); this.remove(view); - + if(above) { if(this.settings.axis === "vertical") { @@ -371,7 +418,7 @@ EPUBJS.Continuous.prototype.erase = function(view, above){ //Trim this.scrollTo(prevLeft - bounds.width, 0, true); } } - + }; @@ -387,12 +434,12 @@ EPUBJS.Continuous.prototype.checkCurrent = function(position) { if(this.settings.axis === "horizontal") { // TODO: Check for current horizontal } else { - + for (var i = length; i >= 0; i--) { view = this.views[i]; top = view.bounds().top; if(top < container.bottom) { - + if(this.current == view.section) { break; } @@ -436,7 +483,7 @@ EPUBJS.Continuous.prototype.start = function() { this.ignore = false; } }.bind(this)); - + window.addEventListener('unload', function(e){ this.ignore = true; this.destroy(); @@ -467,13 +514,13 @@ EPUBJS.Continuous.prototype.onScroll = function(){ // left: scrollLeft // }); - if((this.scrollDeltaVert === 0 && + if((this.scrollDeltaVert === 0 && this.scrollDeltaHorz === 0) || this.scrollDeltaVert > this.settings.offsetDelta || this.scrollDeltaHorz > this.settings.offsetDelta) { this.q.enqueue(this.check); - + this.scrollDeltaVert = 0; this.scrollDeltaHorz = 0; @@ -509,43 +556,6 @@ EPUBJS.Continuous.prototype.onScroll = function(){ }; -EPUBJS.Continuous.prototype.scrollBy = function(x, y, silent){ - if(silent) { - this.ignore = true; - } - - if(this.settings.height) { - - if(x) this.container.scrollLeft += x; - if(y) this.container.scrollTop += y; - - } else { - window.scrollBy(x,y); - } - // console.log("scrollBy", x, y); - this.scrolled = true; -}; - -EPUBJS.Continuous.prototype.scrollTo = function(x, y, silent){ - if(silent) { - this.ignore = true; - } - - if(this.settings.height) { - this.container.scrollLeft = x; - this.container.scrollTop = y; - } else { - window.scrollTo(x,y); - } - // console.log("scrollTo", x, y); - this.scrolled = true; - // if(this.container.scrollLeft != x){ - // setTimeout(function() { - // this.scrollTo(x, y, silent); - // }.bind(this), 10); - // return; - // }; - }; EPUBJS.Continuous.prototype.resizeView = function(view) { @@ -562,7 +572,7 @@ EPUBJS.Continuous.prototype.currentLocation = function(){ var startPage, endPage; var container = this.container.getBoundingClientRect(); - + if(visible.length === 1) { return this.map.page(visible[0]); } @@ -577,7 +587,7 @@ EPUBJS.Continuous.prototype.currentLocation = function(){ end: endPage.end }; } - + }; /* @@ -587,13 +597,13 @@ EPUBJS.Continuous.prototype.current = function(what){ var length = this.views.length - 1; if(this.settings.axis === "horizontal") { - + for (var i = length; i >= 0; i--) { view = this.views[i]; left = view.position().left; if(left < container.right) { - + if(this._current == view) { break; } @@ -604,12 +614,12 @@ EPUBJS.Continuous.prototype.current = function(what){ } } else { - + for (var i = length; i >= 0; i--) { view = this.views[i]; top = view.bounds().top; if(top < container.bottom) { - + if(this._current == view) { break; } @@ -624,4 +634,4 @@ EPUBJS.Continuous.prototype.current = function(what){ return this._current; }; -*/ \ No newline at end of file +*/ diff --git a/src/rendition.js b/src/rendition.js index c966d38..191622c 100644 --- a/src/rendition.js +++ b/src/rendition.js @@ -1,5 +1,5 @@ EPUBJS.Rendition = function(book, options) { - + this.settings = EPUBJS.core.extend(this.settings || {}, { infinite: true, hidden: false, @@ -10,13 +10,13 @@ EPUBJS.Rendition = function(book, options) { }); EPUBJS.core.extend(this.settings, options); - + this.viewSettings = {}; this.book = book; - + this.views = []; - + //-- Adds Hook methods to the Rendition prototype this.hooks = {}; this.hooks.display = new EPUBJS.Hook(this); @@ -50,7 +50,7 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ var hidden = options.hidden || false; var container; var wrapper; - + if(options.height && EPUBJS.core.isNumber(options.height)) { height = options.height + "px"; } @@ -58,10 +58,10 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ if(options.width && EPUBJS.core.isNumber(options.width)) { width = options.width + "px"; } - + // Create new container element container = document.createElement("div"); - + container.id = "epubjs-container:" + EPUBJS.core.uuid(); container.classList.add("epub-container"); @@ -78,7 +78,7 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ if(width){ container.style.width = width; } - + if(height){ container.style.height = height; } @@ -90,21 +90,21 @@ EPUBJS.Rendition.prototype.initialize = function(_options){ EPUBJS.Rendition.wrap = function(container) { var wrapper = document.createElement("div"); - + wrapper.style.visibility = "hidden"; wrapper.style.overflow = "hidden"; wrapper.style.width = "0"; wrapper.style.height = "0"; - + wrapper.appendChild(container); return wrapper; }; - + // Call to attach the container to an element in the dom // Container must be attached before rendering can begin EPUBJS.Rendition.prototype.attachTo = function(_element){ var bounds; - + this.container = this.initialize({ "width" : this.settings.width, "height" : this.settings.height @@ -114,7 +114,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ this.element = _element; } else if (typeof _element === "string") { this.element = document.getElementById(_element); - } + } if(!this.element){ console.error("Not an Element"); @@ -127,7 +127,7 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ } else { this.element.appendChild(this.container); } - + // Attach Listeners this.attachListeners(); @@ -136,19 +136,19 @@ EPUBJS.Rendition.prototype.attachTo = function(_element){ // Add Layout method this.applyLayoutMethod(); - + // Trigger Attached this.trigger("attached"); // Start processing queue // this.q.run(); - + }; EPUBJS.Rendition.prototype.attachListeners = function(){ // Listen to window for resize event if width or height is set to 100% - if(!EPUBJS.core.isNumber(this.settings.width) || + if(!EPUBJS.core.isNumber(this.settings.width) || !EPUBJS.core.isNumber(this.settings.height) ) { window.addEventListener("resize", this.onResized.bind(this), false); } @@ -160,13 +160,13 @@ EPUBJS.Rendition.prototype.bounds = function() { }; EPUBJS.Rendition.prototype.display = function(target){ - + return this.q.enqueue(this._display, target); }; EPUBJS.Rendition.prototype._display = function(target){ - + var displaying = new RSVP.defer(); var displayed = displaying.promise; @@ -183,7 +183,7 @@ EPUBJS.Rendition.prototype._display = function(target){ } this.displaying = true; - + // Hide current views this.hide(); @@ -194,11 +194,19 @@ EPUBJS.Rendition.prototype._display = function(target){ this.q.enqueue(this.append, view); // Move to correct place within the section, if needed - // this.moveTo(what) + this.q.enqueue(function(){ + + var offset = view.locationOf(target); + + if(offset.top > 250 || offset.left > 250){ // Terrible temp fix to prevent unneeded jumps + return this.moveTo(offset); + } + + }); // Show views this.show(); - + // This hook doesn't prevent showing, but waits to resolve until // all the hooks have finished. Might want to block showing. this.hooks.display.trigger(view) @@ -215,14 +223,14 @@ EPUBJS.Rendition.prototype._display = function(target){ }; // Takes a cfi, fragment or page? -EPUBJS.Rendition.prototype.moveTo = function(what){ - +EPUBJS.Rendition.prototype.moveTo = function(offset){ + this.scrollBy(offset.left, offset.top); }; EPUBJS.Rendition.prototype.render = function(view, show) { view.create(); - + view.onLayout = this.layout.format.bind(this.layout); // Fit to size of the container, apply padding @@ -243,13 +251,13 @@ EPUBJS.Rendition.prototype.render = function(view, show) { return this.hooks.render.trigger(view, this); }.bind(this)) .then(function(){ - + if(show !== false && this.hidden === false) { this.q.enqueue(function(view){ view.show(); }, view); } - + // this.map = new EPUBJS.Map(view, this.layout); this.trigger("rendered", view.section); @@ -296,7 +304,7 @@ EPUBJS.Rendition.prototype.remove = function(view) { this.views.splice(index, 1); } - + this._remove(view); }; @@ -313,7 +321,7 @@ EPUBJS.Rendition.prototype._remove = function(view) { }; EPUBJS.Rendition.prototype.resizeView = function(view) { - + if(this.globalLayoutProperties.layout === "pre-paginated") { view.lock("both", this.stage.width, this.stage.height); } else { @@ -352,7 +360,7 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ width = bounds.width; //height = bounds.height; } - + if(height && !EPUBJS.core.isNumber(height)) { bounds = bounds || this.container.getBoundingClientRect(); //width = bounds.width; @@ -369,11 +377,11 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ }; this.stage = { - width: width - - this.containerPadding.left - + width: width - + this.containerPadding.left - this.containerPadding.right, - height: height - - this.containerPadding.top - + height: height - + this.containerPadding.top - this.containerPadding.bottom }; @@ -382,7 +390,7 @@ EPUBJS.Rendition.prototype.stageSize = function(_width, _height){ }; EPUBJS.Rendition.prototype.applyLayoutMethod = function() { - + this.layout = new EPUBJS.Layout.Scroll(); this.updateLayout(); @@ -390,7 +398,7 @@ EPUBJS.Rendition.prototype.applyLayoutMethod = function() { }; EPUBJS.Rendition.prototype.updateLayout = function() { - + this.layout.calculate(this.stage.width, this.stage.height); }; @@ -485,12 +493,12 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c var position = view.position(); var container = _container || this.container.getBoundingClientRect(); - if(this.settings.axis === "horizontal" && + if(this.settings.axis === "horizontal" && (position.right > container.left - offsetPrev) && !(position.left >= container.right + offsetNext)) { return true; - + } else if(this.settings.axis === "vertical" && (position.bottom > container.top - offsetPrev) && !(position.top >= container.bottom + offsetNext)) { @@ -499,7 +507,7 @@ EPUBJS.Rendition.prototype.isVisible = function(view, offsetPrev, offsetNext, _c } return false; - + }; EPUBJS.Rendition.prototype.visible = function(){ @@ -519,12 +527,12 @@ EPUBJS.Rendition.prototype.visible = function(){ } return visible; - + }; EPUBJS.Rendition.prototype.bounds = function(func) { var bounds; - + if(!this.settings.height) { bounds = EPUBJS.core.windowBounds(); } else { @@ -573,8 +581,8 @@ EPUBJS.Rendition.prototype.destroy = function(){ this.q.clear(); this.clear(); - - clearTimeout(this.trimTimeout); + + clearTimeout(this.trimTimeout); if(this.settings.hidden) { this.element.removeChild(this.wrapper); } else { @@ -593,7 +601,7 @@ EPUBJS.Rendition.prototype.reportLocation = function(){ EPUBJS.Rendition.prototype.currentLocation = function(){ var view; var start, end; - + if(this.views.length) { view = this.views[0]; // start = container.left - view.position().left; @@ -601,8 +609,46 @@ EPUBJS.Rendition.prototype.currentLocation = function(){ return this.map.page(view); } - + }; +EPUBJS.Rendition.prototype.scrollBy = function(x, y, silent){ + if(silent) { + this.ignore = true; + } + + if(this.settings.height) { + + if(x) this.container.scrollLeft += x; + if(y) this.container.scrollTop += y; + + } else { + window.scrollBy(x,y); + } + // console.log("scrollBy", x, y); + this.scrolled = true; +}; + +EPUBJS.Rendition.prototype.scrollTo = function(x, y, silent){ + if(silent) { + this.ignore = true; + } + + if(this.settings.height) { + this.container.scrollLeft = x; + this.container.scrollTop = y; + } else { + window.scrollTo(x,y); + } + // console.log("scrollTo", x, y); + this.scrolled = true; + // if(this.container.scrollLeft != x){ + // setTimeout(function() { + // this.scrollTo(x, y, silent); + // }.bind(this), 10); + // return; + // }; + }; + //-- Enable binding events to Renderer RSVP.EventTarget.mixin(EPUBJS.Rendition.prototype); diff --git a/src/view.js b/src/view.js index 60dbce2..8ecb695 100644 --- a/src/view.js +++ b/src/view.js @@ -7,7 +7,7 @@ EPUBJS.View = function(section, options) { this.element = document.createElement('div'); this.element.classList.add("epub-view"); - + // this.element.style.minHeight = "100px"; this.element.style.height = "0px"; @@ -17,7 +17,7 @@ EPUBJS.View = function(section, options) { this.added = false; this.displayed = false; this.rendered = false; - + //this.width = 0; //this.height = 0; @@ -29,7 +29,7 @@ EPUBJS.View = function(section, options) { } else { this.element.style.display = "block"; } - + }; EPUBJS.View.prototype.create = function() { @@ -45,13 +45,13 @@ 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.style.display = "none"; this.element.style.visibility = "hidden"; this.iframe.style.visibility = "hidden"; - + this.iframe.style.width = "0"; this.iframe.style.height = "0"; this._width = 0; @@ -59,7 +59,7 @@ EPUBJS.View.prototype.create = function() { this.element.appendChild(this.iframe); this.added = true; - + this.elementBounds = EPUBJS.core.bounds(this.element); // if(width || height){ @@ -69,7 +69,7 @@ EPUBJS.View.prototype.create = function() { // } else { // this.iframeBounds = EPUBJS.core.bounds(this.iframe); // } - + // Firefox has trouble with baseURI and srcdoc // Disabled for now /* @@ -86,7 +86,7 @@ EPUBJS.View.prototype.create = function() { EPUBJS.View.prototype.lock = function(what, width, height) { - + var elBorders = EPUBJS.core.borders(this.element); var iframeBorders; @@ -106,10 +106,10 @@ EPUBJS.View.prototype.lock = function(what, width, height) { this.resize(width, this.lockedHeight); } - if(what === "both" && + if(what === "both" && EPUBJS.core.isNumber(width) && EPUBJS.core.isNumber(height)){ - + this.lockedWidth = width - elBorders.width - iframeBorders.width; this.lockedHeight = height - elBorders.height - iframeBorders.height; @@ -123,14 +123,14 @@ EPUBJS.View.prototype.lock = function(what, width, height) { } - + }; EPUBJS.View.prototype.expand = function(force) { var width = this.lockedWidth; var height = this.lockedHeight; - + var textWidth, textHeight; // console.log("expanding a") if(!this.iframe || this._expanding) return; @@ -155,7 +155,7 @@ EPUBJS.View.prototype.expand = function(force) { width = this._contentWidth; } } - + // Expand Vertically if(width && !height) { textHeight = this.textHeight(); @@ -167,7 +167,7 @@ EPUBJS.View.prototype.expand = function(force) { height = this._contentHeight; } } - + // Only Resize if dimensions have changed or // if Frame is still hidden, so needs reframing if(this._needsReframe || width != this._width || height != this._height){ @@ -180,7 +180,7 @@ EPUBJS.View.prototype.expand = function(force) { EPUBJS.View.prototype.contentWidth = function(min) { var prev; var width; - + // Save previous width prev = this.iframe.style.width; // Set the iframe size to min, width will only ever be greater @@ -207,10 +207,10 @@ EPUBJS.View.prototype.contentHeight = function(min) { EPUBJS.View.prototype.textWidth = function() { var width; var range = this.document.createRange(); - + // Select the contents of frame range.selectNodeContents(this.document.body); - + // get the width of the text content width = range.getBoundingClientRect().width; return width; @@ -220,15 +220,15 @@ EPUBJS.View.prototype.textWidth = function() { EPUBJS.View.prototype.textHeight = function() { var height; var range = this.document.createRange(); - + range.selectNodeContents(this.document.body); - - height = range.getBoundingClientRect().height; + + height = range.getBoundingClientRect().height; return height; }; EPUBJS.View.prototype.resize = function(width, height) { - + if(!this.iframe) return; if(EPUBJS.core.isNumber(width)){ @@ -254,7 +254,7 @@ EPUBJS.View.prototype.reframe = function(width, height) { this._needsReframe = true; return; } - + if(EPUBJS.core.isNumber(width)){ this.element.style.width = width + "px"; } @@ -314,29 +314,29 @@ EPUBJS.View.prototype.load = function(contents) { } this.iframe.onload = function(event) { - + this.window = this.iframe.contentWindow; this.document = this.iframe.contentDocument; this.rendering = false; loading.resolve(this); - + }.bind(this); - + if(this.supportsSrcdoc){ this.iframe.srcdoc = contents; } else { - + this.document = this.iframe.contentDocument; - + if(!this.document) { loading.reject(new Error("No Document Available")); return loaded; } - + this.document.open(); this.document.write(contents); this.document.close(); - + } return loaded; @@ -351,7 +351,7 @@ EPUBJS.View.prototype.layout = function(layoutFunc) { this.document.body.style.margin = "0"; //this.document.body.style.display = "inline-block"; //this.document.documentElement.style.width = "auto"; - + if(layoutFunc){ layoutFunc(this); } @@ -374,7 +374,7 @@ EPUBJS.View.prototype.listeners = function() { // Wait for fonts to load to finish // http://dev.w3.org/csswg/css-font-loading/ // Not implemented fully except in chrome - + if(this.document.fonts && this.document.fonts.status === "loading") { // console.log("fonts unloaded"); this.document.fonts.onloadingdone = function(){ @@ -390,15 +390,15 @@ EPUBJS.View.prototype.listeners = function() { this.imageLoadListeners(); this.mediaQueryListeners(); - + this.resizeListenters(); - + }; EPUBJS.View.prototype.resizeListenters = function() { // Test size again clearTimeout(this.expanding); - // this.expanding = setTimeout(this.expand.bind(this), 350); + // this.expanding = setTimeout(this.expand.bind(this), 350); }; //https://github.com/tylergaw/media-query-events/blob/master/js/mq-events.js @@ -451,7 +451,7 @@ EPUBJS.View.prototype.observe = function(target) { // this.element = element; // this.element.appendChild(this.iframe); // }; -// +// // EPUBJS.View.prototype.prependTo = function(element) { // this.element = element; // element.insertBefore(this.iframe, element.firstChild); @@ -517,7 +517,7 @@ EPUBJS.View.prototype.onDisplayed = function(view) { // Stub, override with a custom functions }; -EPUBJS.View.prototype.bounds = function() { +EPUBJS.View.prototype.bounds = function() { if(!this.elementBounds) { this.elementBounds = EPUBJS.core.bounds(this.element); } @@ -535,7 +535,7 @@ EPUBJS.View.prototype.destroy = function() { this.element.removeChild(this.iframe); this.displayed = false; this.iframe = null; - + this._textWidth = null; this._textHeight = null; this._width = null; @@ -551,7 +551,9 @@ EPUBJS.View.prototype.root = function() { }; EPUBJS.View.prototype.locationOf = function(target) { - + var parentPos = this.iframe.getBoundingClientRect(); + var targetPos = {"left": 0, "top": 0}; + if(!this.document) return; if(this.epubcfi.isCfiString(target)) { @@ -563,38 +565,41 @@ EPUBJS.View.prototype.locationOf = function(target) { // Must Clean up Marker before going to page this.epubcfi.removeMarker(marker, this.document); - return marker.getBoundingClientRect(); + targetPos = marker.getBoundingClientRect(); } } else { range = this.epubcfi.generateRangeFromCfi(cfi, this.document); if(range) { - return range.getBoundingClientRect(); + targetPos = range.getBoundingClientRect(); } } } else if(typeof target === "string" && target.indexOf("#") > -1) { - id = target.substring(target.indexOf("#")); + id = target.substring(target.indexOf("#")+1); el = this.document.getElementById(id); if(el) { - return el.getBoundingClientRect(); + targetPos = el.getBoundingClientRect(); } } - return {"left": 0, "top": 0}; + return { + "left": window.scrollX + parentPos.left + targetPos.left, + "top": window.scrollY + parentPos.top + targetPos.top + }; }; EPUBJS.View.prototype.addCss = function(src) { var $stylesheet = document.createElement('link'); var ready = false; - + return new RSVP.Promise(function(resolve, reject){ if(!this.document) { resolve(false); return; } - + $stylesheet.type = 'text/css'; $stylesheet.rel = "stylesheet"; $stylesheet.href = src; @@ -607,7 +612,7 @@ EPUBJS.View.prototype.addCss = function(src) { }, 1); } }; - + this.document.head.appendChild($stylesheet); }.bind(this)); @@ -617,9 +622,9 @@ EPUBJS.View.prototype.addCss = function(src) { EPUBJS.View.prototype.addStylesheetRules = function(rules) { var styleEl = document.createElement('style'), styleSheet; - + if(!this.document) return; - + // Append style element to head this.document.head.appendChild(styleEl); @@ -644,4 +649,4 @@ EPUBJS.View.prototype.addStylesheetRules = function(rules) { } }; -RSVP.EventTarget.mixin(EPUBJS.View.prototype); \ No newline at end of file +RSVP.EventTarget.mixin(EPUBJS.View.prototype);