2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/apps/reader/modules/readertoc.lua

175 lines
4.3 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local CenterContainer = require("ui/widget/container/centercontainer")
local GestureRange = require("ui/gesturerange")
2013-10-18 20:38:07 +00:00
local Menu = require("ui/widget/menu")
local Geom = require("ui/geometry")
2013-10-18 20:38:07 +00:00
local Screen = require("ui/screen")
local Device = require("ui/device")
local UIManager = require("ui/uimanager")
local Event = require("ui/event")
2014-06-10 13:32:49 +00:00
local Font = require("ui/font")
local DEBUG = require("dbg")
2013-10-18 20:38:07 +00:00
local _ = require("gettext")
local ReaderToc = InputContainer:new{
2014-03-13 13:52:43 +00:00
toc = nil,
toc_menu_title = _("Table of contents"),
2012-05-27 11:47:22 +00:00
}
2012-12-31 00:13:46 +00:00
function ReaderToc:init()
2014-03-13 13:52:43 +00:00
if Device:hasKeyboard() then
self.key_events = {
ShowToc = {
{ "T" },
doc = "show Table of Content menu" },
2014-03-13 13:52:43 +00:00
}
end
if Device:isTouchDevice() then
self.ges_events = {
ShowToc = {
GestureRange:new{
ges = "two_finger_swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
direction = "east"
}
},
}
end
self.ui.menu:registerToMainMenu(self)
2012-12-31 00:13:46 +00:00
end
2012-05-27 11:47:22 +00:00
function ReaderToc:cleanUpTocTitle(title)
2014-03-13 13:52:43 +00:00
return (title:gsub("\13", ""))
2012-05-27 11:47:22 +00:00
end
function ReaderToc:onSetDimensions(dimen)
2014-03-13 13:52:43 +00:00
self.dimen = dimen
2012-05-27 11:47:22 +00:00
end
function ReaderToc:onUpdateToc()
2014-03-13 13:52:43 +00:00
self.toc = nil
return true
end
2014-04-04 15:02:29 +00:00
function ReaderToc:onPageUpdate(pageno)
self.pageno = pageno
end
function ReaderToc:fillToc()
2014-03-13 13:52:43 +00:00
self.toc = self.ui.document:getToc()
end
-- _getTocTitleByPage wrapper, so specific reader
-- can tranform pageno according its need
function ReaderToc:getTocTitleByPage(pn_or_xp)
2014-03-13 13:52:43 +00:00
local page = pn_or_xp
if type(pn_or_xp) == "string" then
page = self.ui.document:getPageFromXPointer(pn_or_xp)
end
return self:_getTocTitleByPage(page)
end
function ReaderToc:_getTocTitleByPage(pageno)
2014-03-13 13:52:43 +00:00
if not self.toc then
-- build toc when needed.
self:fillToc()
end
-- no table of content
if #self.toc == 0 then
return ""
end
local pre_entry = self.toc[1]
for _k,_v in ipairs(self.toc) do
if _v.page > pageno then
break
end
pre_entry = _v
end
return self:cleanUpTocTitle(pre_entry.title)
end
function ReaderToc:getTocTitleOfCurrentPage()
2014-03-13 13:52:43 +00:00
return self:getTocTitleByPage(self.pageno)
end
2012-05-27 11:47:22 +00:00
function ReaderToc:onShowToc()
2014-03-13 13:52:43 +00:00
if not self.toc then
self:fillToc()
end
-- build menu items
if #self.toc > 0 and not self.toc[1].text then
for _,v in ipairs(self.toc) do
2014-06-08 06:08:18 +00:00
v.text = (" "):rep(v.depth-1)..self:cleanUpTocTitle(v.title)
2014-03-13 13:52:43 +00:00
v.mandatory = v.page
end
end
2014-04-04 15:02:29 +00:00
-- update current entry
if #self.toc > 0 then
for i=1, #self.toc do
v = self.toc[i]
if v.page > self.pageno then
self.toc.current = i > 1 and i - 1 or 1
break
end
end
end
2014-03-13 13:52:43 +00:00
local toc_menu = Menu:new{
title = _("Table of Contents"),
item_table = self.toc,
ui = self.ui,
2014-06-10 13:32:49 +00:00
is_borderless = true,
width = Screen:getWidth(),
height = Screen:getHeight(),
2014-06-10 13:32:49 +00:00
cface = Font:getFace("cfont", 20),
on_close_ges = {
GestureRange:new{
ges = "two_finger_swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
},
direction = "west"
}
}
2014-03-13 13:52:43 +00:00
}
local menu_container = CenterContainer:new{
dimen = Screen:getSize(),
toc_menu,
}
2014-03-13 13:52:43 +00:00
function toc_menu:onMenuChoice(item)
self.ui:handleEvent(Event:new("PageUpdate", item.page))
end
toc_menu.close_callback = function()
UIManager:close(menu_container)
2014-03-13 13:52:43 +00:00
end
toc_menu.show_parent = menu_container
UIManager:show(menu_container)
2014-05-30 14:11:46 +00:00
2014-03-13 13:52:43 +00:00
return true
2012-05-27 11:47:22 +00:00
end
2013-03-14 05:58:42 +00:00
function ReaderToc:addToMainMenu(tab_item_table)
2014-03-13 13:52:43 +00:00
-- insert table to main reader menu
table.insert(tab_item_table.navi, {
text = self.toc_menu_title,
callback = function()
self:onShowToc()
end,
})
2012-12-31 00:13:46 +00:00
end
2013-10-18 20:38:07 +00:00
return ReaderToc