From 58893a88b4548849f8fe9ddc920f8ca279521580 Mon Sep 17 00:00:00 2001 From: Fred Chasen Date: Mon, 12 Dec 2016 21:06:34 +0100 Subject: [PATCH] move link replacement into Contents, make sure afterDisplayed is triggered --- examples/spreads.html | 2 +- src/contents.js | 20 +++++++++++---- src/managers/continuous/index.js | 6 +++++ src/managers/default/index.js | 8 ++++++ src/rendition.js | 9 +++++-- src/utils/replacements.js | 42 +++++++++----------------------- src/utils/url.js | 4 ++- 7 files changed, 51 insertions(+), 40 deletions(-) diff --git a/examples/spreads.html b/examples/spreads.html index 0b520ff..308bc51 100644 --- a/examples/spreads.html +++ b/examples/spreads.html @@ -26,7 +26,7 @@ height: 600 }); - rendition.display("chapter_007.xhtml"); + rendition.display(); var title = document.getElementById("title"); diff --git a/src/contents.js b/src/contents.js index 0fd723f..6247a0b 100644 --- a/src/contents.js +++ b/src/contents.js @@ -2,7 +2,7 @@ import EventEmitter from "event-emitter"; import {isNumber, prefixed} from "./utils/core"; import EpubCFI from "./epubcfi"; import Mapping from "./mapping"; - +import {replaceLinks} from "./utils/replacements"; class Contents { constructor(doc, content, cfiBase) { @@ -396,8 +396,7 @@ class Contents { } else { position = range.getBoundingClientRect(); } - targetPos.left = position.left; - targetPos.top = position.top; + } } @@ -409,11 +408,14 @@ class Contents { if(el) { position = el.getBoundingClientRect(); - targetPos.left = position.left; - targetPos.top = position.top; } } + if (position) { + targetPos.left = position.left; + targetPos.top = position.top; + } + return targetPos; } @@ -696,6 +698,14 @@ class Contents { return mapping.page(this, cfiBase, start, end); } + linksHandler(fn) { + replaceLinks(this.content, fn); + } + + passEvents() { + + } + destroy() { // Stop observing if(this.observer) { diff --git a/src/managers/continuous/index.js b/src/managers/continuous/index.js index 64c73e2..26bde74 100644 --- a/src/managers/continuous/index.js +++ b/src/managers/continuous/index.js @@ -181,6 +181,9 @@ class ContinuousViewManager extends DefaultViewManager { append(section){ var view = this.createView(section); this.views.append(view); + + view.onDisplayed = this.afterDisplayed.bind(this); + return view; } @@ -190,6 +193,9 @@ class ContinuousViewManager extends DefaultViewManager { view.on("resized", this.counter.bind(this)); this.views.prepend(view); + + view.onDisplayed = this.afterDisplayed.bind(this); + return view; } diff --git a/src/managers/default/index.js b/src/managers/default/index.js index 127fa44..6c5d4a3 100644 --- a/src/managers/default/index.js +++ b/src/managers/default/index.js @@ -239,6 +239,10 @@ class DefaultViewManager { append(section){ var view = this.createView(section); this.views.append(view); + + view.onDisplayed = this.afterDisplayed.bind(this); + view.onResize = this.afterResized.bind(this); + return view.display(this.request); } @@ -246,6 +250,10 @@ class DefaultViewManager { var view = this.createView(section); this.views.prepend(view); + + view.onDisplayed = this.afterDisplayed.bind(this); + view.onResize = this.afterResized.bind(this); + return view.display(this.request); } // resizeView(view) { diff --git a/src/rendition.js b/src/rendition.js index d906315..dfc406f 100644 --- a/src/rendition.js +++ b/src/rendition.js @@ -1,6 +1,5 @@ import EventEmitter from "event-emitter"; import { extend, defer, isFloat } from "./utils/core"; -import {replaceLinks} from "./utils/replacements"; import Hook from "./utils/hook"; import EpubCFI from "./epubcfi"; import Queue from "./utils/queue"; @@ -67,7 +66,7 @@ class Rendition { this.hooks.render = new Hook(this); this.hooks.show = new Hook(this); - this.hooks.content.register(replaceLinks.bind(this)); + this.hooks.content.register(this.handleLinks.bind(this)); this.hooks.content.register(this.passViewEvents.bind(this)); // this.hooks.display.register(this.afterDisplay.bind(this)); @@ -589,6 +588,12 @@ class Rendition { getContents () { return this.manager ? this.manager.getContents() : []; } + + handleLinks(view) { + view.contents.linksHandler((href) => { + this.display(href); + }); + } } //-- Enable binding events to Renderer diff --git a/src/utils/replacements.js b/src/utils/replacements.js index b10a69f..06ac137 100644 --- a/src/utils/replacements.js +++ b/src/utils/replacements.js @@ -1,5 +1,4 @@ import { qs } from "./core"; -import Url from "./url"; export function replaceBase(doc, section){ var base; @@ -43,10 +42,11 @@ export function replaceCanonical(doc, section){ head.appendChild(link); } } +// TODO: move me to Contents +export function replaceLinks(contents, fn) { -export function replaceLinks(view, renderer) { + var links = contents.querySelectorAll("a[href]"); - var links = view.document.querySelectorAll("a[href]"); var replaceLink = function(link){ var href = link.getAttribute("href"); @@ -54,39 +54,19 @@ export function replaceLinks(view, renderer) { return; } - var linkUrl = Url(href); - var relative = this.book.resolve(href, false); + var absolute = (href.indexOf("://") > -1); - if(linkUrl && linkUrl.protocol){ + + if(absolute){ link.setAttribute("target", "_blank"); }else{ - /* - if(baseDirectory) { - // We must ensure that the file:// protocol is preserved for - // local file links, as in certain contexts (such as under - // Titanium), file links without the file:// protocol will not - // work - if (baseUri.protocol === "file") { - relative = core.resolveUrl(baseUri.base, href); - } else { - relative = core.resolveUrl(baseDirectory, href); - } - } else { - relative = href; - } - */ - - if(linkUrl.fragment) { - // do nothing with fragment yet - } else { - link.onclick = function(){ - renderer.display(relative); - return false; - }; - } - + link.onclick = function(){ + // renderer.display(relative); + fn(href); + return false; + }; } }.bind(this); diff --git a/src/utils/url.js b/src/utils/url.js index 5887874..e5ccbf3 100644 --- a/src/utils/url.js +++ b/src/utils/url.js @@ -22,7 +22,9 @@ class Url { this.search = ""; this.base = baseString; - if (!absolute && (typeof(baseString) !== "string") && + if (!absolute && + baseString !== false && + typeof(baseString) !== "string" && window && window.location) { this.base = window.location.href; }