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:
parent
d41f3de31e
commit
ee690abb0f
3 changed files with 118 additions and 33 deletions
|
@ -40,6 +40,7 @@ local instanceof = util.instanceof
|
||||||
FORM_NODATA = 0
|
FORM_NODATA = 0
|
||||||
FORM_PROCEED = 0
|
FORM_PROCEED = 0
|
||||||
FORM_VALID = 1
|
FORM_VALID = 1
|
||||||
|
FORM_DONE = 1
|
||||||
FORM_INVALID = -1
|
FORM_INVALID = -1
|
||||||
FORM_CHANGED = 2
|
FORM_CHANGED = 2
|
||||||
FORM_SKIP = 4
|
FORM_SKIP = 4
|
||||||
|
@ -320,7 +321,7 @@ function Map.formvalue(self, key)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Map.formvaluetable(self, key)
|
function Map.formvaluetable(self, key)
|
||||||
return self.readinput and luci.http.formvaluetable(key)
|
return self.readinput and luci.http.formvaluetable(key) or {}
|
||||||
end
|
end
|
||||||
|
|
||||||
function Map.get_scheme(self, sectiontype, option)
|
function Map.get_scheme(self, sectiontype, option)
|
||||||
|
@ -464,11 +465,18 @@ Compound = class(Node)
|
||||||
|
|
||||||
function Compound.__init__(self, ...)
|
function Compound.__init__(self, ...)
|
||||||
Node.__init__(self)
|
Node.__init__(self)
|
||||||
|
self.template = "cbi/compound"
|
||||||
self.children = {...}
|
self.children = {...}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Compound.populate_delegator(self, delegator)
|
||||||
|
for _, v in ipairs(self.children) do
|
||||||
|
v.delegator = delegator
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
function Compound.parse(self, ...)
|
function Compound.parse(self, ...)
|
||||||
local cstate, state = 0, 0
|
local cstate, state = 0
|
||||||
|
|
||||||
for k, child in ipairs(self.children) do
|
for k, child in ipairs(self.children) do
|
||||||
cstate = child:parse(...)
|
cstate = child:parse(...)
|
||||||
|
@ -486,60 +494,93 @@ Delegator = class(Node)
|
||||||
function Delegator.__init__(self, ...)
|
function Delegator.__init__(self, ...)
|
||||||
Node.__init__(self, ...)
|
Node.__init__(self, ...)
|
||||||
self.nodes = {}
|
self.nodes = {}
|
||||||
|
self.defaultpath = {}
|
||||||
|
self.pageaction = false
|
||||||
|
self.readinput = true
|
||||||
|
self.allow_back = false
|
||||||
|
self.allow_finish = false
|
||||||
self.template = "cbi/delegator"
|
self.template = "cbi/delegator"
|
||||||
end
|
end
|
||||||
|
|
||||||
function Delegator.state(self, name, node, transitor)
|
function Delegator.set(self, name, node)
|
||||||
transitor = transitor or self.transistor_linear
|
if type(node) == "table" and getmetatable(node) == nil then
|
||||||
local state = {node=node, name=name, transitor=transitor}
|
node = Compound(unpack(node))
|
||||||
|
end
|
||||||
assert(instanceof(node, Node), "Invalid node")
|
assert(instanceof(node, Compound), "Invalid node")
|
||||||
assert(not self.nodes[name], "Duplicate entry")
|
assert(not self.nodes[name], "Duplicate entry")
|
||||||
|
|
||||||
self.nodes[name] = state
|
self.nodes[name] = node
|
||||||
self:append(state)
|
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
|
end
|
||||||
|
|
||||||
function Delegator.get(self, name)
|
function Delegator.get(self, name)
|
||||||
return self.nodes[name]
|
return self.nodes[name]
|
||||||
end
|
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, ...)
|
function Delegator.parse(self, ...)
|
||||||
local active = self:getactive()
|
local newcurrent
|
||||||
assert(active, "Invalid state")
|
self.chain = self:get_chain()
|
||||||
|
self.current = self:get_active()
|
||||||
|
self.active = self:get(self.current)
|
||||||
|
assert(self.active, "Invalid state")
|
||||||
|
|
||||||
local cstate = active.node:parse()
|
self.active:populate_delegator(self)
|
||||||
self.active = active.transistor(self, active.node, cstate)
|
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
|
||||||
|
|
||||||
if not self.active then
|
if not newcurrent or not self:get(newcurrent) then
|
||||||
return FORM_DONE
|
return FORM_DONE
|
||||||
else
|
else
|
||||||
|
self.current = newcurrent
|
||||||
|
self.active = self:get(self.current)
|
||||||
self.active:parse(false)
|
self.active:parse(false)
|
||||||
return FROM_PROCEED
|
return FROM_PROCEED
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Delegator.render(self, ...)
|
function Delegator.get_next(self, state)
|
||||||
self.active.node:render(...)
|
for k, v in ipairs(self.chain) do
|
||||||
|
if v == state then
|
||||||
|
return self.chain[k+1]
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function Delegator.getactive(self)
|
function Delegator.get_prev(self, state)
|
||||||
return self:get(Map.formvalue(self, "cbi.delegated")
|
for k, v in ipairs(self.chain) do
|
||||||
or (self.children[1] and self.children[1].name))
|
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
|
end
|
||||||
|
|
||||||
--[[
|
--[[
|
||||||
|
|
14
libs/cbi/luasrc/view/cbi/compound.htm
Normal file
14
libs/cbi/luasrc/view/cbi/compound.htm
Normal 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() %>
|
30
libs/cbi/luasrc/view/cbi/delegator.htm
Normal file
30
libs/cbi/luasrc/view/cbi/delegator.htm
Normal 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 < 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 >%>" />
|
||||||
|
<% end %>
|
||||||
|
<script type="text/javascript">cbi_d_update();</script>
|
||||||
|
</div>
|
Loading…
Reference in a new issue