mirror of
https://github.com/futurepress/epub.js.git
synced 2025-10-04 15:09:16 +02:00
Views -> Controllers, Moved previousLocationCfi logic to Reader, Bookmarks in Reader
This commit is contained in:
parent
3bfadb2abc
commit
e4be17a428
24 changed files with 1363 additions and 1003 deletions
114
reader/controllers/toc_controller.js
Normal file
114
reader/controllers/toc_controller.js
Normal file
|
@ -0,0 +1,114 @@
|
|||
EPUBJS.reader.TocController = function(toc) {
|
||||
var book = this.book;
|
||||
|
||||
var $list = $("#tocView"),
|
||||
docfrag = document.createDocumentFragment();
|
||||
|
||||
var currentChapter = false;
|
||||
|
||||
var generateTocItems = function(toc, level) {
|
||||
var container = document.createElement("ul");
|
||||
|
||||
if(!level) level = 1;
|
||||
|
||||
toc.forEach(function(chapter) {
|
||||
var listitem = document.createElement("li"),
|
||||
link = document.createElement("a");
|
||||
toggle = document.createElement("a");
|
||||
|
||||
var subitems;
|
||||
|
||||
listitem.id = "toc-"+chapter.id;
|
||||
listitem.classList.add('list_item');
|
||||
|
||||
link.textContent = chapter.label;
|
||||
link.href = chapter.href;
|
||||
|
||||
link.classList.add('toc_link');
|
||||
|
||||
listitem.appendChild(link);
|
||||
|
||||
if(chapter.subitems) {
|
||||
level++;
|
||||
subitems = generateTocItems(chapter.subitems, level);
|
||||
toggle.classList.add('toc_toggle');
|
||||
|
||||
listitem.insertBefore(toggle, link);
|
||||
listitem.appendChild(subitems);
|
||||
}
|
||||
|
||||
|
||||
container.appendChild(listitem);
|
||||
|
||||
});
|
||||
|
||||
return container;
|
||||
};
|
||||
|
||||
var onShow = function() {
|
||||
$list.show();
|
||||
};
|
||||
|
||||
var onHide = function() {
|
||||
$list.hide();
|
||||
};
|
||||
|
||||
var chapterChange = function(e) {
|
||||
var id = e.id,
|
||||
$item = $list.find("#toc-"+id),
|
||||
$current = $list.find(".currentChapter"),
|
||||
$open = $list.find('.openChapter');
|
||||
|
||||
if($item.length){
|
||||
|
||||
if($item != $current && $item.has(currentChapter).length > 0) {
|
||||
$current.removeClass("currentChapter");
|
||||
}
|
||||
|
||||
$item.addClass("currentChapter");
|
||||
|
||||
// $open.removeClass("openChapter");
|
||||
$item.parents('li').addClass("openChapter");
|
||||
}
|
||||
};
|
||||
|
||||
book.on('renderer:chapterDisplayed', chapterChange);
|
||||
|
||||
var tocitems = generateTocItems(toc);
|
||||
|
||||
docfrag.appendChild(tocitems);
|
||||
|
||||
$list.append(docfrag);
|
||||
$list.find(".toc_link").on("click", function(event){
|
||||
var url = this.getAttribute('href');
|
||||
|
||||
//-- Provide the Book with the url to show
|
||||
// The Url must be found in the books manifest
|
||||
book.goto(url);
|
||||
|
||||
$list.find(".currentChapter")
|
||||
.addClass("openChapter")
|
||||
.removeClass("currentChapter");
|
||||
|
||||
$(this).parent('li').addClass("currentChapter");
|
||||
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
$list.find(".toc_toggle").on("click", function(event){
|
||||
var $el = $(this).parent('li'),
|
||||
open = $el.hasClass("openChapter");
|
||||
|
||||
if(open){
|
||||
$el.removeClass("openChapter");
|
||||
} else {
|
||||
$el.addClass("openChapter");
|
||||
}
|
||||
event.preventDefault();
|
||||
});
|
||||
|
||||
return {
|
||||
"show" : onShow,
|
||||
"hide" : onHide
|
||||
};
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue