1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

update toc to use displayChapter and added events for book

This commit is contained in:
Fred Chasen 2012-12-20 12:37:43 -08:00
parent 501b55e0bf
commit f26bdc8afc
9 changed files with 192 additions and 38 deletions

BIN
.DS_Store vendored

Binary file not shown.

View file

@ -155,10 +155,23 @@ input:-moz-placeholder {
#toc { #toc {
margin-top: 50px; margin-top: 50px;
padding-left: 25px;
display: block;
width: 100%;
height: 100%;
} }
#toc li { #toc li {
margin-bottom: 10px; margin-bottom: 10px;
width: 225px;
font-family: Georgia, "Times New Roman", Times, serif;
list-style: none;
}
#toc li:active,
#toc li.current
{
list-style: square;
} }
#toc a { #toc a {
@ -169,3 +182,4 @@ input:-moz-placeholder {
#toc a:hover { #toc a:hover {
text-decoration: underline; text-decoration: underline;
} }

View file

@ -5,22 +5,26 @@ FP.app.init = (function($){
var Book; var Book;
function init(){ function init(){
//-- Setup the browser prefixes
FP.core.crossBrowserColumnCss(); FP.core.crossBrowserColumnCss();
//-- Temp set the previos position to section 6,
// since moby-dick has lots of crap before the test
// Might want to make a way to skip to first chapter
if (localStorage.getItem("spinePos") === null) {
localStorage.setItem("spinePos", 6);
}
Book = new FP.Book("area", "/moby-dick/"); Book = new FP.Book("area", "/moby-dick/");
//Book = new FP.Book("area", "/the-hound-of-the-baskervilles/"); //Book = new FP.Book("area", "/the-hound-of-the-baskervilles/");
Book.listen("book:metadataReady", meta);
Book.listen("book:tocReady", toc);
//-- Wait for Dom ready to handle jquery //-- Wait for Dom ready to handle jquery
$(function() { $(function() {
controls(); controls();
//-- Replace with event
setTimeout(function(){
meta();
toc();
}, 500);
setTimeout(function(){
toc();
}, 1000);
}); });
@ -42,10 +46,25 @@ FP.app.init = (function($){
var contents = Book.getTOC(); var contents = Book.getTOC();
$toc = $("#toc"); $toc = $("#toc");
$toc.empty();
contents.forEach(function(item){ contents.forEach(function(item){
$toc.append("<li><a href='"+item.href+"'>"+item.label+"</a></li>"); $wrapper = $("<li>");
$item = $("<a href='#"+item.href+"' data-spinepos='"+item.spinePos+"'>"+item.label+"</a>");
$item.on("click", function(e){
$this = $(this);
console.log(item.spinePos)
Book.displayChapter($this.data("spinepos"));
e.preventDefault();
});
$wrapper.append($item);
$toc.append($wrapper);
}); });
} }
function controls(){ function controls(){

View file

@ -7,6 +7,14 @@ FP.Book = function(elem, bookUrl){
this.el = elem; this.el = elem;
} }
this.events = {};
this.createEvent("book:tocReady");
this.createEvent("book:metadataReady");
this.createEvent("book:spineReady");
this.createEvent("book:bookReady");
this.createEvent("book:chapterReady");
this.createEvent("book:resized");
this.initialize(this.el); this.initialize(this.el);
this.listeners(); this.listeners();
@ -21,19 +29,21 @@ FP.Book = function(elem, bookUrl){
//-- Build up any html needed //-- Build up any html needed
FP.Book.prototype.initialize = function(el){ FP.Book.prototype.initialize = function(el){
this.iframe = document.createElement('iframe'); this.iframe = document.createElement('iframe');
this.onResized(); this.resizeIframe(false, this.el.clientWidth, this.el.clientHeight);
this.listen("book:resized", this.resizeIframe, this);
//this.listen("book:bookReady", function(){console.log("rready")});
this.el.appendChild(this.iframe); this.el.appendChild(this.iframe);
} }
FP.Book.prototype.listeners = function(){ FP.Book.prototype.listeners = function(){
var that = this; var that = this;
window.addEventListener("resize", function(){ window.addEventListener("resize", that.onResized.bind(this), false);
//-- Need a better way to unbind this
that.onResized();
}, false);
} }
@ -70,8 +80,25 @@ FP.Book.prototype.isContained = function(bookUrl){
FP.Book.prototype.onResized = function(){ FP.Book.prototype.onResized = function(){
var width = this.el.clientWidth; this.tell("book:resized", {
this.iframe.height = this.el.clientHeight; width: this.el.clientWidth,
height: this.el.clientHeight
});
}
FP.Book.prototype.resizeIframe = function(e, cWidth, cHeight){
var width, height;
//-- Can be resized by the window resize event, or by passed height
if(!e){
width = cWidth;
height = cHeight;
}else{
width = e.msg.width;
height = e.msg.height;
}
this.iframe.height = height;
if(width % 2 != 0){ if(width % 2 != 0){
width += 1; width += 1;
@ -98,7 +125,7 @@ FP.Book.prototype.parseContainer = function(callback){
that.contentsPath = fullpath[1]; that.contentsPath = fullpath[1];
//-- Now that we have the path we can parse the contents //-- Now that we have the path we can parse the contents
//-- TODO: move this //-- TODO: move this
that.parseContents(that.contentsPath) that.parseContents(that.contentsPath);
}); });
} }
@ -129,6 +156,8 @@ FP.Book.prototype.parseMetadata = function(metadata){
.childNodes[0].nodeValue; .childNodes[0].nodeValue;
this.metadata["creator"] = metadata.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","creator")[0] this.metadata["creator"] = metadata.getElementsByTagNameNS("http://purl.org/dc/elements/1.1/","creator")[0]
.childNodes[0].nodeValue; .childNodes[0].nodeValue;
this.tell("book:metadataReady");
} }
FP.Book.prototype.parseManifest = function(manifest){ FP.Book.prototype.parseManifest = function(manifest){
@ -155,16 +184,20 @@ FP.Book.prototype.parseSpine = function(spine){
this.spine = []; this.spine = [];
this.spineIndex = {}; //-- For quick reference by id, might be a better way
//-- Turn items into an array //-- Turn items into an array
items = Array.prototype.slice.call(spine.getElementsByTagName("itemref")); items = Array.prototype.slice.call(spine.getElementsByTagName("itemref"));
//-- Add to array to mantain ordering and cross reference with manifest //-- Add to array to mantain ordering and cross reference with manifest
items.forEach(function(item){ items.forEach(function(item, index){
var id = item.getAttribute('idref'), var id = item.getAttribute('idref'),
href = that.assets[id]; href = that.assets[id];
that.spine.push({"id": id, "href": href}); that.spine.push({"id": id, "href": href});
that.spineIndex[id] = index;
}); });
this.tell("book:spineReady");
} }
FP.Book.prototype.parseTOC = function(path){ FP.Book.prototype.parseTOC = function(path){
@ -185,9 +218,16 @@ FP.Book.prototype.parseTOC = function(path){
href = that.assets[id], href = that.assets[id],
navLabel = item.getElementsByTagName("navLabel")[0].childNodes[0].childNodes[0].nodeValue; navLabel = item.getElementsByTagName("navLabel")[0].childNodes[0].childNodes[0].nodeValue;
that.toc.push({"id": id, "href": href, "label": navLabel}); that.toc.push({
"id": id,
"href": href,
"label": navLabel,
"spinePos": that.spineIndex[id]
});
}); });
that.tell("book:tocReady");
/* /*
<navPoint class="chapter" id="xtitlepage" playOrder="1"> <navPoint class="chapter" id="xtitlepage" playOrder="1">
<navLabel><text>Moby-Dick</text></navLabel> <navLabel><text>Moby-Dick</text></navLabel>
@ -216,10 +256,13 @@ FP.Book.prototype.chapterTitle = function(){
} }
FP.Book.prototype.startDisplay = function(){ FP.Book.prototype.startDisplay = function(){
//-- TODO: get previous saved positions? //-- get previous saved positions
var spinePos = 6; var spinePos = localStorage.getItem("spinePos") || 0;
this.tell("book:bookReady");
this.displayChapter(spinePos); this.displayChapter(spinePos);
} }
FP.Book.prototype.displayChapter = function(pos, end){ FP.Book.prototype.displayChapter = function(pos, end){
@ -235,6 +278,7 @@ FP.Book.prototype.displayChapter = function(pos, end){
return false; return false;
} }
localStorage.setItem("spinePos", pos);
this.spinePos = pos; this.spinePos = pos;
this.chapterPos = 1; this.chapterPos = 1;
@ -245,23 +289,22 @@ FP.Book.prototype.displayChapter = function(pos, end){
this.iframe.onload = function() { this.iframe.onload = function() {
//-- TODO: Choose between single and spread //-- TODO: Choose between single and spread
that.formatSpread(end); that.formatSpread();
if(end) that.goToChapterEnd();
window.addEventListener("resize", function(){ that.listen("book:resized", that.formatSpread, that);
that.formatSpread();
//-- need to adjust position in book that.tell("book:chapterReady");
}, false);
} }
} }
FP.Book.prototype.formatSpread = function(end){ FP.Book.prototype.formatSpread = function(){
this.bodyEl = this.iframe.contentDocument.documentElement.getElementsByTagName('body')[0]; this.bodyEl = this.iframe.contentDocument.documentElement.getElementsByTagName('body')[0];
//this.bodyEl.setAttribute("style", "background: #777"); //this.bodyEl.setAttribute("style", "background: #777");
//-- Check the width and decied on columns //-- Check the width and decied on columns
//-- Todo: a better place for this? //-- Todo: a better place for this?
this.elWidth = this.iframe.width; this.elWidth = this.iframe.width;
this.gap = this.gap || this.elWidth / 8; this.gap = this.gap || this.elWidth / 8;
@ -291,9 +334,10 @@ FP.Book.prototype.formatSpread = function(end){
this.calcPages(); this.calcPages();
if(end){ }
this.chapterEnd();
} FP.Book.prototype.goToChapterEnd = function(){
this.chapterEnd();
} }
FP.Book.prototype.calcPages = function(){ FP.Book.prototype.calcPages = function(){
@ -351,3 +395,43 @@ FP.Book.prototype.getTOC = function(){
} }
FP.Book.prototype.createEvent = function(evt){
var e = new CustomEvent(evt);
this.events[evt] = e;
return e;
}
FP.Book.prototype.tell = function(evt, msg){
var e;
if(!this.events[evt]){
console.warn("No event:", evt, "defined yet, creating.");
e = this.createEvent(evt)
}else{
e = this.events[evt];
}
if(msg) e.msg = msg;
this.el.dispatchEvent(e);
}
FP.Book.prototype.listen = function(evt, func, bindto){
if(!this.events[evt]){
console.warn("No event:", evt, "defined yet, creating.");
this.createEvent(evt);
return;
}
if(bindto){
this.el.addEventListener(evt, func.bind(bindto), false);
}else{
this.el.addEventListener(evt, func, false);
}
}
FP.Book.prototype.deafen = function(evt, func){
this.el.removeEventListener(evt, func, false);
}

BIN
img/.DS_Store vendored Normal file

Binary file not shown.

BIN
img/settings-s.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

BIN
img/settings.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 KiB

View file

@ -44,8 +44,10 @@
<div id="controls"> <div id="controls">
<input id="search" placeholder="search"> <input id="search" placeholder="search">
<img id="open" src="/img/menu-icon.png"> <img id="open" src="/img/menu-icon.png">
<img id="settings" src="/img/settings.png">
</div> </div>
<ol id="toc"></ol> <ul id="toc"></ul>
</div> </div>
<div id="main"> <div id="main">

View file

@ -46,5 +46,40 @@
<content src="chapter_003.xhtml"/> <content src="chapter_003.xhtml"/>
</navPoint> </navPoint>
<navPoint class="chapter" id="xchapter_004" playOrder="1">
<navLabel><text>Chapter 4. The Counterpane.</text></navLabel>
<content src="chapter_004.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_005" playOrder="1">
<navLabel><text>Chapter 5. Breakfast.</text></navLabel>
<content src="chapter_005.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_006" playOrder="1">
<navLabel><text>Chapter 6. The Street.</text></navLabel>
<content src="chapter_006.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_007" playOrder="1">
<navLabel><text>Chapter 7. The Chapel.</text></navLabel>
<content src="chapter_007.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_008" playOrder="1">
<navLabel><text>Chapter 8. The Pulpit.</text></navLabel>
<content src="chapter_008.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_009" playOrder="1">
<navLabel><text>Chapter 9. The Sermon.</text></navLabel>
<content src="chapter_009.xhtml"/>
</navPoint>
<navPoint class="chapter" id="xchapter_010" playOrder="1">
<navLabel><text>Chapter 10. A Bosom Friend.</text></navLabel>
<content src="chapter_010.xhtml"/>
</navPoint>
</navMap> </navMap>
</ncx> </ncx>