mirror of
https://github.com/OpenMW/openmw.git
synced 2025-06-25 20:41:33 +00:00
Define order of groups in a page
This commit is contained in:
parent
26154c85a1
commit
5e90b1db0d
2 changed files with 27 additions and 5 deletions
|
@ -40,6 +40,9 @@ local function validateGroupOptions(options)
|
||||||
if type(options.page) ~= 'string' then
|
if type(options.page) ~= 'string' then
|
||||||
error('Group must belong to a page')
|
error('Group must belong to a page')
|
||||||
end
|
end
|
||||||
|
if type(options.order) ~= 'number' and type(options.order) ~= 'nil' then
|
||||||
|
error('Group order must be a number')
|
||||||
|
end
|
||||||
if type(options.l10n) ~= 'string' then
|
if type(options.l10n) ~= 'string' then
|
||||||
error('Group must have a localization context')
|
error('Group must have a localization context')
|
||||||
end
|
end
|
||||||
|
@ -78,6 +81,7 @@ local function registerGroup(options)
|
||||||
local group = {
|
local group = {
|
||||||
key = options.key,
|
key = options.key,
|
||||||
page = options.page,
|
page = options.page,
|
||||||
|
order = options.order or 0,
|
||||||
l10n = options.l10n,
|
l10n = options.l10n,
|
||||||
name = options.name,
|
name = options.name,
|
||||||
description = options.description,
|
description = options.description,
|
||||||
|
|
|
@ -154,6 +154,12 @@ local function renderGroup(group, global)
|
||||||
return layout
|
return layout
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function pageGroupComparator(a, b)
|
||||||
|
return a.order < b.order or (
|
||||||
|
a.order == b.order and a.key < b.key
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
local function renderPage(page)
|
local function renderPage(page)
|
||||||
local l10n = core.l10n(page.l10n)
|
local l10n = core.l10n(page.l10n)
|
||||||
local layout = {
|
local layout = {
|
||||||
|
@ -197,7 +203,11 @@ local function renderPage(page)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
local groupsContent = layout.content.groups.content
|
local groupsContent = layout.content.groups.content
|
||||||
for _, pageGroup in ipairs(groups[page.key]) do
|
local pageGroups = groups[page.key]
|
||||||
|
local sortedGroups = {}
|
||||||
|
for i, v in ipairs(pageGroups) do sortedGroups[i] = v end
|
||||||
|
table.sort(sortedGroups, pageGroupComparator)
|
||||||
|
for _, pageGroup in ipairs(sortedGroups) do
|
||||||
local group = common.getSection(pageGroup.global, common.groupSectionKey):get(pageGroup.key)
|
local group = common.getSection(pageGroup.global, common.groupSectionKey):get(pageGroup.key)
|
||||||
groupsContent:add(renderGroup(group, pageGroup.global))
|
groupsContent:add(renderGroup(group, pageGroup.global))
|
||||||
end
|
end
|
||||||
|
@ -224,17 +234,25 @@ end
|
||||||
local function onGroupRegistered(global, key)
|
local function onGroupRegistered(global, key)
|
||||||
local group = common.getSection(global, common.groupSectionKey):get(key)
|
local group = common.getSection(global, common.groupSectionKey):get(key)
|
||||||
groups[group.page] = groups[group.page] or {}
|
groups[group.page] = groups[group.page] or {}
|
||||||
table.insert(groups[group.page], {
|
local pageGroup = {
|
||||||
key = group.key,
|
key = group.key,
|
||||||
global = global,
|
global = global,
|
||||||
})
|
order = group.order,
|
||||||
|
}
|
||||||
|
table.insert(groups[group.page], pageGroup)
|
||||||
common.getSection(global, group.key):subscribe(onSettingChanged(global))
|
common.getSection(global, group.key):subscribe(onSettingChanged(global))
|
||||||
|
|
||||||
|
local index = 1
|
||||||
|
for _, g in ipairs(groups[group.page]) do
|
||||||
|
if pageGroupComparator(pageGroup, g) then
|
||||||
|
index = index + 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
if not pageOptions[group.page] then return end
|
if not pageOptions[group.page] then return end
|
||||||
local element = pageOptions[group.page].element
|
local element = pageOptions[group.page].element
|
||||||
local groupsLayout = element.layout.content.groups
|
local groupsLayout = element.layout.content.groups
|
||||||
-- TODO: make group order deterministic
|
groupsLayout.content:insert(index, renderGroup(group, global))
|
||||||
groupsLayout.content:add(renderGroup(group, global))
|
|
||||||
element:update()
|
element:update()
|
||||||
end
|
end
|
||||||
local globalGroups = storage.globalSection(common.groupSectionKey)
|
local globalGroups = storage.globalSection(common.groupSectionKey)
|
||||||
|
|
Loading…
Reference in a new issue