2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00

Merge pull request #984 from chrox/collapsible_toc

count only leaf nodes of toc tree when level is zero
This commit is contained in:
Qingping Hou 2014-10-11 11:14:53 -07:00
commit 101a7f6d96
2 changed files with 31 additions and 14 deletions

View File

@ -101,7 +101,8 @@ end
--[[ --[[
TOC ticks is a list of page number in ascending order of TOC nodes at certain level TOC ticks is a list of page number in ascending order of TOC nodes at certain level
positive level counts nodes of the depth level (level 1 for depth 1) positive level counts nodes of the depth level (level 1 for depth 1)
non-positive level counts nodes of reversed depth level (level -1 for max_depth-1) negative level counts nodes of reversed depth level (level -1 for max_depth)
zero level counts leaf nodes of the toc tree
--]] --]]
function ReaderToc:getTocTicks(level) function ReaderToc:getTocTicks(level)
if self.ticks[level] then return self.ticks[level] end if self.ticks[level] then return self.ticks[level] end
@ -110,15 +111,26 @@ function ReaderToc:getTocTicks(level)
local ticks = {} local ticks = {}
if #self.toc > 0 then if #self.toc > 0 then
local depth = nil if level == 0 then
if level > 0 then local depth = 0
depth = level for i = #self.toc, 1, -1 do
local v = self.toc[i]
if v.depth >= depth then
table.insert(ticks, v.page)
end
depth = v.depth
end
else else
depth = self:getMaxDepth() + level local depth = nil
end if level > 0 then
for _, v in ipairs(self.toc) do depth = level
if v.depth == depth then else
table.insert(ticks, v.page) depth = self:getMaxDepth() + level + 1
end
for _, v in ipairs(self.toc) do
if v.depth == depth then
table.insert(ticks, v.page)
end
end end
end end
-- normally the ticks are sorted already but in rare cases -- normally the ticks are sorted already but in rare cases

View File

@ -25,21 +25,26 @@ describe("Readertoc module", function()
it("should get ticks of level 0", function() it("should get ticks of level 0", function()
ticks_level_0 = toc:getTocTicks(0) ticks_level_0 = toc:getTocTicks(0)
DEBUG("ticks", ticks_level_0) DEBUG("ticks", ticks_level_0)
assert.are.same(26, #ticks_level_0) assert.are.same(28, #ticks_level_0)
end) end)
local ticks_level_1 = nil local ticks_level_1 = nil
it("should get ticks of level 1", function() it("should get ticks of level 1", function()
ticks_level_1 = toc:getTocTicks(1) ticks_level_1 = toc:getTocTicks(1)
assert.are.same(7, #ticks_level_1) assert.are.same(7, #ticks_level_1)
end) end)
local ticks_level_2 = nil
it("should get ticks of level 2", function()
ticks_level_2 = toc:getTocTicks(2)
assert.are.same(26, #ticks_level_2)
end)
local ticks_level_m1 = nil local ticks_level_m1 = nil
it("should get ticks of level -1", function() it("should get ticks of level -1", function()
ticks_level_m1 = toc:getTocTicks(1) ticks_level_m1 = toc:getTocTicks(-1)
assert.are.same(7, #ticks_level_m1) assert.are.same(26, #ticks_level_m1)
end) end)
it("should get the same ticks of level -1 and level 1", function() it("should get the same ticks of level -1 and level 2", function()
if toc_max_depth == 2 then if toc_max_depth == 2 then
assert.are.same(ticks_level_1, ticks_level_m1) assert.are.same(ticks_level_2, ticks_level_m1)
end end
end) end)
end) end)