Skip to content

Commit

Permalink
Internal link handling:
Browse files Browse the repository at this point in the history
* Intercept internal link clicks.
* Attach internal link clicks to proper reader navigation within the spine.
* Open external links in a new browser window, separate from the reader.

Change-Id: I14bb17a493e678e8a6331b99b7bb90c4aab4aa69
  • Loading branch information
aadamowski committed Oct 15, 2013
1 parent 987b229 commit ee36f2f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 12 deletions.
65 changes: 55 additions & 10 deletions epub-modules/epub-renderer/end.frag
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,57 @@
return new SpineItem();
};

var loadIframeFunctionGenerator = function(epubFetch) {
return function(iframe, src, callback, context) {
var loadIframeFunctionGenerator = function(epubFetch, reader) {
return function(iframe, src, origCallback, context) {
var callback = function(success) {
var epubContentDocument = this.$iframe[0].contentDocument;
$('a', epubContentDocument).click(function (clickEvent) {
// Check for both href and xlink:href attribute and get value
var href;
if (clickEvent.currentTarget.attributes["xlink:href"]) {
href = clickEvent.currentTarget.attributes["xlink:href"].value;
}
else {
href = clickEvent.currentTarget.attributes["href"].value;
}
var hrefUri = new URI(href);
var hrefIsRelative = hrefUri.is('relative');
var hrefUriHasFilename = hrefUri.filename();
var overrideClickEvent = false;

if (hrefIsRelative) {
// TODO:
if (hrefUriHasFilename /* TODO: && check whether href actually resolves to a spine item */) {

var currentSpineItemUri = new URI(context.currentSpineItem.href);
var openedSpineItemUri = hrefUri.absoluteTo(currentSpineItemUri);
var idref = openedSpineItemUri.pathname();
var hashFrag = openedSpineItemUri.fragment();
var spineItem = context.spine.getItemByHref(idref);
var pageData = new ReadiumSDK.Models.PageOpenRequest(spineItem);
if (hashFrag) {
pageData.setElementId(hashFrag);
}
reader.openPage(pageData);
overrideClickEvent = true;
} // otherwise it's probably just a hash frag that needs to be handled by browser's default handling
} else {
// It's an absolute URL to a remote site - open it in a separate window outside the reader
window.open(href, '_blank');
overrideClickEvent = true;
}

if (overrideClickEvent) {
clickEvent.preventDefault();
clickEvent.stopPropagation();
}
});
origCallback.call(this, success);
}
if (epubFetch.isPackageExploded()) {
return origLoadIframeFunction(iframe, src, callback, context);
} else {
var onLoadWrapperFunction = function(boolArg) {
var onLoadWrapperFunction = function(success) {
var context = this;
var itemHref = context.currentSpineItem.href;
epubFetch.relativeToPackageFetchFileContents(itemHref, 'text', function(contentDocumentText) {
Expand All @@ -25,14 +70,14 @@
var contentDocument = iframe.contentDocument;
contentDocument.replaceChild(resolvedContentDocumentDom.documentElement,
contentDocument.documentElement);
callback.call(context, boolArg);
callback.call(context, success);
});
}, function(err) {
if (err.message) {
console.error(err.message);
};
console.error(err);
callback.call(context, boolArg);
callback.call(context, success);
});
};
// Feed an artificial empty HTML document to the IFRAME, then let the wrapper onload function
Expand All @@ -46,15 +91,15 @@
};
var EpubRendererModule = function (epubFetch, elementToBindReaderTo, packageData) {

/*
* Patch the ReadiumSDK.Helpers.LoadIframe global function to support zipped EPUB packages:
*/
ReadiumSDK.Helpers.LoadIframe = loadIframeFunctionGenerator(epubFetch);

var reader = new ReadiumSDK.Views.ReaderView({
el: elementToBindReaderTo
});

/*
* Patch the ReadiumSDK.Helpers.LoadIframe global function to support zipped EPUB packages:
*/
ReadiumSDK.Helpers.LoadIframe = loadIframeFunctionGenerator(epubFetch, reader);

// Description: The public interface
return {

Expand Down
4 changes: 2 additions & 2 deletions epub-modules/epub-renderer/start.frag
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
define('epub_renderer_module', ['require', 'module', 'jquery', 'underscore', 'backbone'],
define('epub_renderer_module', ['require', 'module', 'jquery', 'underscore', 'backbone', 'URIjs/URI'],

function (require, module, $, _, Backbone) {
function (require, module, $, _, Backbone, URI) {

0 comments on commit ee36f2f

Please sign in to comment.