CBI Delegators (Wizards)
authorSteven Barth <steven@midlink.org>
Sun, 24 May 2009 17:17:53 +0000 (17:17 +0000)
committerSteven Barth <steven@midlink.org>
Sun, 24 May 2009 17:17:53 +0000 (17:17 +0000)
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

libs/cbi/luasrc/cbi.lua
libs/cbi/luasrc/view/cbi/compound.htm [new file with mode: 0644]
libs/cbi/luasrc/view/cbi/delegator.htm [new file with mode: 0644]

index 18d84e52f57e62f43e08b96447fe344a9b7396c7..c176da0922f0e1b031c510d5b57ce7915acd2f06 100644 (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)
-
-       return state
+       self.nodes[name] = node
 end
 
-function Delegator.get(self, name)
-       return self.nodes[name]
+function Delegator.add(self, name, node)
+       node = self:set(name, node)
+       self.defaultpath[#self.defaultpath+1] = 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
+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
-       else
-               return state
        end
+       table.insert(self.chain, n, name)
 end
 
-function Delegator.parse(self, ...)
-       local active = self:getactive()
-       assert(active, "Invalid state")
+function Delegator.get(self, name)
+       return self.nodes[name]
+end
 
-       local cstate = active.node:parse()
-       self.active = active.transistor(self, active.node, cstate)
+function Delegator.parse(self, ...)
+       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
 
-       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.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.getactive(self)
-       return self:get(Map.formvalue(self, "cbi.delegated")
-               or (self.children[1] and self.children[1].name))
+function Delegator.get_active(self)
+       return Map.formvalue(self, "cbi.delg.current") or self.chain[1]
 end
 
 --[[
diff --git a/libs/cbi/luasrc/view/cbi/compound.htm b/libs/cbi/luasrc/view/cbi/compound.htm
new file mode 100644 (file)
index 0000000..fbd5a0a
--- /dev/null
@@ -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() %>
\ No newline at end of file
diff --git a/libs/cbi/luasrc/view/cbi/delegator.htm b/libs/cbi/luasrc/view/cbi/delegator.htm
new file mode 100644 (file)
index 0000000..523eebc
--- /dev/null
@@ -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>