gluon-web-model: Add support for "Supramaps"

It was not possible to modularize single sections of forms in the config mode.
This commit adds support for ´Supramaps´. Using supramaps it is possible to create an additional directory where inframaps reside.
E.g. a Lua model file with a same-named directory (excluding the file extension) defines a Form element and returns it. This is a supramap.
If Lua files exist in that directoryn the Form element is available in their environment as "Supramap".
Thus these files can add additional sections which are "inframaps" and must return the modified supramap.
This allows e.g. modularizing the password field in the gluon-web-admin package as an own package without hampering usability.
This commit is contained in:
CodeFetch 2019-03-07 16:38:39 +01:00
parent b2f249e995
commit 2c9504408b

View File

@ -4,6 +4,7 @@
module('gluon.web.model', package.seeall)
local glob = require 'posix.glob'
local unistd = require 'posix.unistd'
local classes = require 'gluon.web.model.classes'
@ -11,12 +12,13 @@ local util = require 'gluon.web.util'
local instanceof = util.instanceof
-- Loads a model from given file, creating an environment and returns it
local function load(filename, i18n)
local function load(filename, i18n, supra)
supra = supra or {}
local func = assert(loadfile(filename))
setfenv(func, setmetatable({}, {__index =
function(tbl, key)
return classes[key] or i18n[key] or _G[key]
return supra[key] or classes[key] or i18n[key] or _G[key]
end
}))
@ -37,6 +39,8 @@ return function(config, http, renderer, name, pkg)
local modeldir = config.base_path .. '/model/'
local filename = modeldir .. name .. '.lua'
local infradir = modeldir .. name .. '/'
local infrafnames = glob.glob(infradir .. '*.lua') or {}
if not unistd.access(filename) then
error("Model '" .. name .. "' not found!")
@ -50,6 +54,19 @@ return function(config, http, renderer, name, pkg)
local maps = load(filename, i18n)
if #infrafnames > 0 and #maps ~= 1 then
error("'" .. name .. "' has got inframaps. It must return a single supramap!")
end
for _, f in ipairs(infrafnames) do
local m = load(f, i18n, { Supramap = maps[1] })
if #m ~= 1 then
error("Inframodule '" .. f:sub(#modeldir) .. "' must return the inherited supramap!")
end
hidenav = hidenav or maps[1].hidenav or m[1].hidenav
maps[1] = m[1]
end
for _, map in ipairs(maps) do
map:parse(http)
end