From 2c9504408b0bbe91a485f1ea3571fe1ef4077639 Mon Sep 17 00:00:00 2001 From: CodeFetch Date: Thu, 7 Mar 2019 16:38:39 +0100 Subject: [PATCH] gluon-web-model: Add support for "Supramaps" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../luasrc/usr/lib/lua/gluon/web/model.lua | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/package/gluon-web-model/luasrc/usr/lib/lua/gluon/web/model.lua b/package/gluon-web-model/luasrc/usr/lib/lua/gluon/web/model.lua index 513fb65e..c40fcd1e 100644 --- a/package/gluon-web-model/luasrc/usr/lib/lua/gluon/web/model.lua +++ b/package/gluon-web-model/luasrc/usr/lib/lua/gluon/web/model.lua @@ -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 })) @@ -36,7 +38,9 @@ return function(config, http, renderer, name, pkg) local hidenav = false local modeldir = config.base_path .. '/model/' - local filename = modeldir..name..'.lua' + 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