2
0
mirror of https://github.com/koreader/koreader synced 2024-11-04 12:00:25 +00:00
koreader/frontend/ui/reader/readertoc.lua

113 lines
2.2 KiB
Lua
Raw Normal View History

2012-05-27 11:47:22 +00:00
ReaderToc = InputContainer:new{
2012-12-31 00:13:46 +00:00
toc_menu_title = "Table of contents",
current_page = 0,
2012-06-05 07:23:36 +00:00
current_pos = 0,
2012-05-27 11:47:22 +00:00
}
2012-12-31 00:13:46 +00:00
function ReaderToc:init()
if not Device:hasNoKeyboard() then
self.key_events = {
ShowToc = {
{ "T" },
doc = "show Table of Content menu" },
}
end
self.ui.menu:registerToMainMenu(self)
end
2012-05-27 11:47:22 +00:00
function ReaderToc:cleanUpTocTitle(title)
return (title:gsub("\13", ""))
2012-05-27 11:47:22 +00:00
end
function ReaderToc:onSetDimensions(dimen)
self.dimen = dimen
end
--function ReaderToc:fillToc()
--self.toc = self.doc:getToc()
--end
-- getTocTitleByPage wrapper, so specific reader
-- can tranform pageno according its need
function ReaderToc:getTocTitleByPage(pageno)
return self:_getTocTitleByPage(pageno)
end
function ReaderToc:_getTocTitleByPage(pageno)
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()
return self:getTocTitleByPage(self.pageno)
end
2012-05-27 11:47:22 +00:00
function ReaderToc:onShowToc()
local items = self.ui.document:getToc()
-- build menu items
for _,v in ipairs(items) do
v.text = (" "):rep(v.depth-1)..self:cleanUpTocTitle(v.title)
end
2012-12-31 00:13:46 +00:00
local toc_menu = Menu:new{
2012-05-27 11:47:22 +00:00
title = "Table of Contents",
item_table = items,
2012-12-31 00:13:46 +00:00
ui = self.ui,
dimen = Geom:new{
w = Screen:getWidth()-20,
h = Screen:getHeight()-20
},
2012-05-27 11:47:22 +00:00
}
function toc_menu:onMenuChoice(item)
self.ui:handleEvent(Event:new("PageUpdate", item.page))
end
2012-05-27 11:47:22 +00:00
2012-12-31 00:13:46 +00:00
local menu_container = CenterContainer:new{
dimen = Screen:getSize(),
toc_menu,
}
toc_menu.close_callback = function()
UIManager:close(menu_container)
end
UIManager:show(menu_container)
2012-05-27 11:47:22 +00:00
end
2012-06-12 16:14:23 +00:00
function ReaderToc:onSetDimensions(dimen)
self.dimen = dimen
end
function ReaderToc:onPageUpdate(new_page_no)
self.current_page = new_page_no
end
2012-06-05 07:23:36 +00:00
function ReaderToc:onPosUpdate(new_pos)
self.current_pos = new_pos
end
2012-12-31 00:13:46 +00:00
function ReaderToc:addToMainMenu(item_table)
-- insert table to main reader menu
table.insert(item_table, {
text = self.toc_menu_title,
callback = function()
self:onShowToc()
end,
})
end