diff options
Diffstat (limited to 'libs/cbi/luasrc/cbi.lua')
-rw-r--r-- | libs/cbi/luasrc/cbi.lua | 105 |
1 files changed, 73 insertions, 32 deletions
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 --[[ |