CBI Delegators (Wizards)

Example CBI-Map:

d = Delegator()
d.allow_back = true	-- Back-button
d:add("step1", load("mywizard/form1")) -- model/cbi/mywizard/form1
d:add("step2", load("mywizard/form2"))

return d
This commit is contained in:
Steven Barth 2009-05-24 17:17:53 +00:00
parent d41f3de31e
commit ee690abb0f
3 changed files with 118 additions and 33 deletions

View file

@ -40,6 +40,7 @@ local instanceof = util.instanceof
FORM_NODATA = 0
FORM_PROCEED = 0
FORM_VALID = 1
FORM_DONE = 1
FORM_INVALID = -1
FORM_CHANGED = 2
FORM_SKIP = 4
@ -320,7 +321,7 @@ function Map.formvalue(self, key)
end
function Map.formvaluetable(self, key)
return self.readinput and luci.http.formvaluetable(key)
return self.readinput and luci.http.formvaluetable(key) or {}
end
function Map.get_scheme(self, sectiontype, option)
@ -464,11 +465,18 @@ Compound = class(Node)
function Compound.__init__(self, ...)
Node.__init__(self)
self.template = "cbi/compound"
self.children = {...}
end
function Compound.populate_delegator(self, delegator)
for _, v in ipairs(self.children) do
v.delegator = delegator
end
end
function Compound.parse(self, ...)
local cstate, state = 0, 0
local cstate, state = 0
for k, child in ipairs(self.children) do
cstate = child:parse(...)
@ -486,60 +494,93 @@ Delegator = class(Node)
function Delegator.__init__(self, ...)
Node.__init__(self, ...)
self.nodes = {}
self.defaultpath = {}
self.pageaction = false
self.readinput = true
self.allow_back = false
self.allow_finish = false
self.template = "cbi/delegator"
end
function Delegator.state(self, name, node, transitor)
transitor = transitor or self.transistor_linear
local state = {node=node, name=name, transitor=transitor}
assert(instanceof(node, Node), "Invalid node")
function Delegator.set(self, name, node)
if type(node) == "table" and getmetatable(node) == nil then
node = Compound(unpack(node))
end
assert(instanceof(node, Compound), "Invalid node")
assert(not self.nodes[name], "Duplicate entry")
self.nodes[name] = state
self:append(state)
self.nodes[name] = node
end
return state
function Delegator.add(self, name, node)
node = self:set(name, node)
self.defaultpath[#self.defaultpath+1] = name
end
function Delegator.insert_after(self, name, after)
local n = #self.chain
for k, v in ipairs(self.chain) do
if v == state then
n = k + 1
break
end
end
table.insert(self.chain, n, name)
end
function Delegator.get(self, name)
return self.nodes[name]
end
function Delegator.transistor_linear(self, state, cstate)
if cstate > 0 then
for i, child in ipairs(self.children) do
if state == child then
return self.children[i+1]
end
end
else
return state
end
end
function Delegator.parse(self, ...)
local active = self:getactive()
assert(active, "Invalid state")
local newcurrent
self.chain = self:get_chain()
self.current = self:get_active()
self.active = self:get(self.current)
assert(self.active, "Invalid state")
self.active:populate_delegator(self)
if self.active:parse() > FORM_PROCEED then
if Map.formvalue(self, "cbi.delg.back") then
newcurrent = self:get_prev(self.current)
else
newcurrent = self:get_next(self.current)
end
end
local cstate = active.node:parse()
self.active = active.transistor(self, active.node, cstate)
if not self.active then
if not newcurrent or not self:get(newcurrent) then
return FORM_DONE
else
self.current = newcurrent
self.active = self:get(self.current)
self.active:parse(false)
return FROM_PROCEED
end
end
function Delegator.render(self, ...)
self.active.node:render(...)
function Delegator.get_next(self, state)
for k, v in ipairs(self.chain) do
if v == state then
return self.chain[k+1]
end
end
end
function Delegator.getactive(self)
return self:get(Map.formvalue(self, "cbi.delegated")
or (self.children[1] and self.children[1].name))
function Delegator.get_prev(self, state)
for k, v in ipairs(self.chain) do
if v == state then
return self.chain[k-1]
end
end
end
function Delegator.get_chain(self)
local x = Map.formvalue(self, "cbi.delg.path") or self.defaultpath
return type(x) == "table" and x or {x}
end
function Delegator.get_active(self)
return Map.formvalue(self, "cbi.delg.current") or self.chain[1]
end
--[[

View file

@ -0,0 +1,14 @@
<%#
LuCI - Lua Configuration Interface
Copyright 2009 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
-%>
<%- self:render_children() %>

View file

@ -0,0 +1,30 @@
<%#
LuCI - Lua Configuration Interface
Copyright 2009 Steven Barth <steven@midlink.org>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
$Id$
-%>
<%- self.active:render() %>
<div class="cbi-page-actions">
<input type="hidden" name="cbi.delg.current" value="<%=self.current%>" />
<% for _, x in ipairs(self.chain) do %>
<input type="hidden" name="cbi.delg.path" value="<%=x%>" />
<% end %>
<% if self.allow_back and self:get_prev(self.current) then %>
<input class="cbi-button cbi-button-back" type="submit" name="cbi.delg.back" value="<%:cbi_back &lt; Back%>" />
<% end %>
<input class="cbi-button cbi-button-reset" type="reset" value="<%:reset%>" />
<% if self.allow_finish and not self:get_next(self.current) then %>
<input class="cbi-button cbi-button-finish" type="submit" value="<%:cbi_finish Finish%>" />
<% elseif self:get_next(self.current) then %>
<input class="cbi-button cbi-button-next" type="submit" value="<%:cbi_next Next &gt;%>" />
<% end %>
<script type="text/javascript">cbi_d_update();</script>
</div>