MenuSorter: avoid recursion in findById

pull/2677/head
Frans de Jonge 7 years ago
parent b7b5950e98
commit 56659ddb20

@ -6,7 +6,6 @@ local MenuSorter = {
separator = { separator = {
text = "KOMenu:separator", text = "KOMenu:separator",
}, },
sub_menu_position,
} }
-- thanks to http://stackoverflow.com/a/4991602/2470572 -- thanks to http://stackoverflow.com/a/4991602/2470572
@ -106,10 +105,9 @@ function MenuSorter:magic(item_table, order)
DEBUG("SUBMENUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUS") DEBUG("SUBMENUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUS")
DEBUG("self.sub_menus", sub_menus) DEBUG("self.sub_menus", sub_menus)
for i,sub_menu in ipairs(sub_menus) do for i,sub_menu in ipairs(sub_menus) do
self.sub_menu_position = {} local sub_menu_position = self:findById(self.menu_table["KOMenu:menu_buttons"], sub_menu) or nil
self:findById(self.menu_table["KOMenu:menu_buttons"], sub_menu) if sub_menu_position and sub_menu_position.id then
if self.sub_menu_position and self.sub_menu_position.id then sub_menu_position.sub_item_table = self.menu_table[sub_menu]
self.sub_menu_position.sub_item_table = self.menu_table[sub_menu]
self.menu_table[sub_menu] = nil self.menu_table[sub_menu] = nil
end end
end end
@ -122,29 +120,25 @@ function MenuSorter:magic(item_table, order)
end end
function MenuSorter:findById(tbl, needle_id) function MenuSorter:findById(tbl, needle_id)
local items = {}
for _,item in pairs(tbl) do
table.insert(items, item)
end
--DEBUG("TBL given",tbl) local k, v
for k,v in pairs(tbl) do k, v = next(items, nil)
if #self.sub_menu_position == 1 then while k do
if self.sub_menu_position.id == needle_id then
return self.sub_menu_position
else
self.sub_menu_position = nil
end
end
--DEBUG("FINDBYID:", needle_id, "current:", k,v)
if type(k) == "number" or k == "sub_item_table" then if type(k) == "number" or k == "sub_item_table" then
if v.id == needle_id then if v.id == needle_id then
DEBUG("FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT ", v.id) DEBUG("FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT FOUND IT ", v.id)
self.sub_menu_position = v return v
return self.sub_menu_position
elseif type(v) == "table" and v.id then elseif type(v) == "table" and v.id then
DEBUG("GOING DEEPER", v.id) DEBUG("GOING DEEPER", v.id)
self:findById(v, needle_id) table.insert(items, v)
end end
end end
k, v = next(items, k)
end end
end end

@ -7,6 +7,7 @@ describe("Readerfooter module", function()
DocumentRegistry = require("document/documentregistry") DocumentRegistry = require("document/documentregistry")
ReaderUI = require("apps/reader/readerui") ReaderUI = require("apps/reader/readerui")
ReaderUI = require("apps/reader/readerui") ReaderUI = require("apps/reader/readerui")
DocSettings = require("docsettings")
UIManager = require("ui/uimanager") UIManager = require("ui/uimanager")
MenuSorter = require("ui/menusorter") MenuSorter = require("ui/menusorter")
DEBUG = require("dbg") DEBUG = require("dbg")

Loading…
Cancel
Save