From ee690abb0f482f0fd3f0b744f98b05699c08c8e4 Mon Sep 17 00:00:00 2001 From: Steven Barth Date: Sun, 24 May 2009 17:17:53 +0000 Subject: [PATCH] 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 --- libs/cbi/luasrc/cbi.lua | 105 +++++++++++++++++++++++---------- libs/cbi/luasrc/view/cbi/compound.htm | 14 +++++ libs/cbi/luasrc/view/cbi/delegator.htm | 30 ++++++++++ 3 files changed, 117 insertions(+), 32 deletions(-) create mode 100644 libs/cbi/luasrc/view/cbi/compound.htm create mode 100644 libs/cbi/luasrc/view/cbi/delegator.htm diff --git a/libs/cbi/luasrc/cbi.lua b/libs/cbi/luasrc/cbi.lua index 18d84e52f..c176da092 100644 --- a/libs/cbi/luasrc/cbi.lua +++ b/libs/cbi/luasrc/cbi.lua @@ -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 index 000000000..fbd5a0a07 --- /dev/null +++ b/libs/cbi/luasrc/view/cbi/compound.htm @@ -0,0 +1,14 @@ +<%# +LuCI - Lua Configuration Interface +Copyright 2009 Steven Barth + +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 index 000000000..523eebcda --- /dev/null +++ b/libs/cbi/luasrc/view/cbi/delegator.htm @@ -0,0 +1,30 @@ +<%# +LuCI - Lua Configuration Interface +Copyright 2009 Steven Barth + +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() %> +
+ +<% for _, x in ipairs(self.chain) do %> + +<% end %> +<% if self.allow_back and self:get_prev(self.current) then %> + +<% end %> + +<% if self.allow_finish and not self:get_next(self.current) then %> + +<% elseif self:get_next(self.current) then %> + +<% end %> + +
-- 2.11.0