gluon-web-model: add state tracking to sections

This add support for a write() method on sections, in addition to the
value and form level write(). write() is only called when the section is
valid and visible. In addition, write() is empty by default, so it can be
overridden more easily.
This commit is contained in:
Matthias Schiffer 2021-05-14 21:08:27 +02:00
parent bb920eaefc
commit df9fa32d58
No known key found for this signature in database
GPG Key ID: 16EF3F64CB201D9C

View File

@ -53,6 +53,7 @@ function Node:__init__(name, title, description)
self.name = name self.name = name
self.index = nil self.index = nil
self.parent = nil self.parent = nil
self.state = M.FORM_NODATA
self.package = 'gluon-web-model' self.package = 'gluon-web-model'
end end
@ -73,17 +74,32 @@ function Node:id()
end end
function Node:reset_node() function Node:reset_node()
self.state = M.FORM_NODATA
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
child:reset_node() child:reset_node()
end end
end end
function Node:parse(http) function Node:parse(http)
self.state = M.FORM_VALID
for _, child in ipairs(self.children) do for _, child in ipairs(self.children) do
child:parse(http) child:parse(http)
end end
end end
function Node:propagate_state()
if self.state == M.FORM_NODATA then
return
end
for _, child in ipairs(self.children) do
child:propagate_state()
if child.state == M.FORM_INVALID then
self.state = M.FORM_INVALID
end
end
end
function Node:render(renderer, scope) function Node:render(renderer, scope)
if self.template then if self.template then
local env = setmetatable({ local env = setmetatable({
@ -158,9 +174,16 @@ function Node:resolve_node_depends()
return true return true
end end
-- will be overridden: write(value)
function Node:write()
end
function Node:handle() function Node:handle()
for _, node in ipairs(self.children) do if self.state == M.FORM_VALID then
node:handle() for _, node in ipairs(self.children) do
node:handle()
end
self:write(self.data)
end end
end end
@ -187,7 +210,6 @@ function AbstractValue:__init__(...)
self.template = "model/valuewrapper" self.template = "model/valuewrapper"
self.error = false self.error = false
self.state = M.FORM_NODATA
end end
function AbstractValue:defaultvalue() function AbstractValue:defaultvalue()
@ -250,16 +272,6 @@ function AbstractValue:validate()
end end
function AbstractValue:handle()
if self.state == M.FORM_VALID then
self:write(self.data)
end
end
-- will be overridden: write(value)
function AbstractValue:write()
end
local Value = class(AbstractValue) local Value = class(AbstractValue)
M.Value = Value M.Value = Value
@ -438,26 +450,7 @@ function Form:parse(http)
while self:resolve_depends() do end while self:resolve_depends() do end
for _, s in ipairs(self.children) do self:propagate_state()
for _, v in ipairs(s.children) do
if v.state == M.FORM_INVALID then
self.state = M.FORM_INVALID
return
end
end
end
self.state = M.FORM_VALID
end
function Form:handle()
if self.state == M.FORM_VALID then
Node.handle(self)
self:write()
end
end
function Form:write()
end end
function Form:section(t, ...) function Form:section(t, ...)