2013-10-18 20:38:07 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local Menu = require("ui/widget/menu")
|
|
|
|
local Screen = require("ui/screen")
|
|
|
|
local Device = require("ui/device")
|
|
|
|
local UIManager = require("ui/uimanager")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2013-10-18 20:38:07 +00:00
|
|
|
local _ = require("gettext")
|
|
|
|
|
|
|
|
local ReaderToc = InputContainer:new{
|
2013-01-09 08:53:32 +00:00
|
|
|
toc = nil,
|
2013-04-07 08:36:16 +00:00
|
|
|
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()
|
2013-10-16 12:56:18 +00:00
|
|
|
if Device:hasKeyboard() then
|
2012-12-31 00:13:46 +00:00
|
|
|
self.key_events = {
|
|
|
|
ShowToc = {
|
|
|
|
{ "T" },
|
2013-04-07 08:36:16 +00:00
|
|
|
doc = _("show Table of Content menu") },
|
2012-12-31 00:13:46 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
self.ui.menu:registerToMainMenu(self)
|
|
|
|
end
|
|
|
|
|
2012-05-27 11:47:22 +00:00
|
|
|
function ReaderToc:cleanUpTocTitle(title)
|
2012-09-18 00:21:03 +00:00
|
|
|
return (title:gsub("\13", ""))
|
2012-05-27 11:47:22 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderToc:onSetDimensions(dimen)
|
|
|
|
self.dimen = dimen
|
|
|
|
end
|
|
|
|
|
2013-01-09 08:53:32 +00:00
|
|
|
function ReaderToc:onUpdateToc()
|
|
|
|
self.toc = nil
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2013-01-01 19:37:36 +00:00
|
|
|
function ReaderToc:fillToc()
|
|
|
|
self.toc = self.ui.document:getToc()
|
|
|
|
end
|
2012-05-27 13:24:33 +00:00
|
|
|
|
2013-01-01 19:37:36 +00:00
|
|
|
-- _getTocTitleByPage wrapper, so specific reader
|
2012-05-27 13:24:33 +00:00
|
|
|
-- can tranform pageno according its need
|
2013-01-01 19:37:36 +00:00
|
|
|
function ReaderToc:getTocTitleByPage(pn_or_xp)
|
|
|
|
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)
|
2012-05-27 13:24:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ReaderToc:_getTocTitleByPage(pageno)
|
|
|
|
if not self.toc then
|
2013-01-01 19:37:36 +00:00
|
|
|
-- build toc when needed.
|
|
|
|
self:fillToc()
|
2012-05-27 13:24:33 +00:00
|
|
|
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()
|
2013-01-09 08:53:32 +00:00
|
|
|
if not self.toc then
|
|
|
|
self:fillToc()
|
|
|
|
end
|
2012-05-27 11:47:22 +00:00
|
|
|
-- build menu items
|
2013-02-16 23:17:31 +00:00
|
|
|
if #self.toc > 0 and not self.toc[1].text then
|
2013-01-09 08:53:32 +00:00
|
|
|
for _,v in ipairs(self.toc) do
|
|
|
|
v.text = (" "):rep(v.depth-1)..self:cleanUpTocTitle(v.title)
|
2013-12-19 15:14:27 +00:00
|
|
|
v.mandatory = v.page
|
2013-01-09 08:53:32 +00:00
|
|
|
end
|
2012-05-27 11:47:22 +00:00
|
|
|
end
|
2012-12-31 00:13:46 +00:00
|
|
|
|
2013-03-16 18:19:51 +00:00
|
|
|
local menu_container = CenterContainer:new{
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
}
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
local toc_menu = Menu:new{
|
2013-04-07 08:36:16 +00:00
|
|
|
title = _("Table of Contents"),
|
2013-01-09 08:53:32 +00:00
|
|
|
item_table = self.toc,
|
2012-12-31 00:13:46 +00:00
|
|
|
ui = self.ui,
|
2013-03-24 10:28:54 +00:00
|
|
|
width = Screen:getWidth()-50,
|
|
|
|
height = Screen:getHeight()-50,
|
2013-03-16 18:19:51 +00:00
|
|
|
show_parent = menu_container,
|
2012-05-27 11:47:22 +00:00
|
|
|
}
|
2013-03-16 18:19:51 +00:00
|
|
|
|
|
|
|
table.insert(menu_container, toc_menu)
|
|
|
|
|
2012-06-10 15:36:19 +00:00
|
|
|
function toc_menu:onMenuChoice(item)
|
|
|
|
self.ui:handleEvent(Event:new("PageUpdate", item.page))
|
|
|
|
end
|
2012-05-27 11:47:22 +00:00
|
|
|
|
2013-03-14 05:58:42 +00:00
|
|
|
toc_menu.close_callback = function()
|
2012-12-31 00:13:46 +00:00
|
|
|
UIManager:close(menu_container)
|
|
|
|
end
|
|
|
|
|
|
|
|
UIManager:show(menu_container)
|
2013-01-01 19:37:36 +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)
|
2012-12-31 00:13:46 +00:00
|
|
|
-- insert table to main reader menu
|
2013-03-14 05:58:42 +00:00
|
|
|
table.insert(tab_item_table.navi, {
|
2012-12-31 00:13:46 +00:00
|
|
|
text = self.toc_menu_title,
|
|
|
|
callback = function()
|
|
|
|
self:onShowToc()
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return ReaderToc
|