summaryrefslogtreecommitdiffstats
path: root/lib/menubuilder.lua
diff options
context:
space:
mode:
authorTed Trask <ttrask01@yahoo.com>2020-03-04 17:03:44 +0000
committerTed Trask <ttrask01@yahoo.com>2020-03-04 17:03:44 +0000
commit499cc5e2a8e1a4a0b8adbf2184d4bad6bf3182f8 (patch)
treed4fee3d378eeb21d11f62c3c5635b624a1992082 /lib/menubuilder.lua
parenta8fc4bf73eb00191e99d594b3a98535db8a83a3e (diff)
downloadacf-core-499cc5e2a8e1a4a0b8adbf2184d4bad6bf3182f8.tar.bz2
acf-core-499cc5e2a8e1a4a0b8adbf2184d4bad6bf3182f8.tar.xz
Allow sorting of tabs using the .menu file
Based on patch by Marc-Andre Parent Ticket: https://gitlab.alpinelinux.org/acf/acf-core/issues/1
Diffstat (limited to 'lib/menubuilder.lua')
-rw-r--r--lib/menubuilder.lua33
1 files changed, 20 insertions, 13 deletions
diff --git a/lib/menubuilder.lua b/lib/menubuilder.lua
index b4f69ed..10aee9c 100644
--- a/lib/menubuilder.lua
+++ b/lib/menubuilder.lua
@@ -40,23 +40,28 @@ local parse_menu_line = function (line)
result = {}
result.cat, result.cat_prio = parse_menu_entry(item[1])
if (item[2]) then result.group, result.group_prio = parse_menu_entry(item[2]) end
- if (item[3]) then result.tab = parse_menu_entry(item[3]) end
+ if (item[3]) then result.tab, result.tab_prio = parse_menu_entry(item[3]) end
if (item[4]) then result.action = parse_menu_entry(item[4]) end
end
end
return result
end
--- Function to compare priorities, missing priority moves to the front, same priority sorted alphabetically
-local prio_compare = function(x,y)
- if x.priority == y.priority then
- if x.name < y.name then return true end
- return false
+-- Function to sort by priority, missing priority moves to the end, same priority maintains initial order
+local sort_by_prio = function(tab)
+ local reversetab = {}
+ for i,t in ipairs(tab) do
+ reversetab[t.name] = i
end
- if nil == x.priority then return true end
- if nil == y.priority then return false end
- if tonumber(x.priority) < tonumber(y.priority) then return true end
- return false
+ prio_compare = function(x,y)
+ if x.priority == y.priority then
+ return reversetab[x.name] < reversetab[y.name]
+ end
+ if nil == x.priority then return false end
+ if nil == y.priority then return true end
+ return tonumber(x.priority) < tonumber(y.priority)
+ end
+ table.sort(tab, prio_compare)
end
-- returns a table of all the menu items found, sorted by priority
@@ -106,7 +111,8 @@ mymodule.get_menuitems = function (self)
local tab = { name = result.tab,
controller = controller,
prefix = prefix,
- action = result.action }
+ action = result.action,
+ priority = result.tab_prio}
table.insert(group.tabs, tab)
if group.reversetabs[tab.name] then
-- Flag for two tabs of same name in different controllers
@@ -130,7 +136,7 @@ mymodule.get_menuitems = function (self)
-- Now that we have the entire menu, sort by priority
-- Categories first
- table.sort(cats, prio_compare)
+ sort_by_prio(cats)
-- Then groups
for x, cat in ipairs(cats) do
@@ -185,9 +191,10 @@ mymodule.get_menuitems = function (self)
group.tabs = {}
end
group.reversetabs = nil -- don't need reverse table anymore
+ sort_by_prio(group.tabs)
end
cat.reversegroups = nil -- don't need reverse table anymore
- table.sort(cat.groups, prio_compare)
+ sort_by_prio(cat.groups)
end
return cats