+
+ return state
+end
+
+
+--[[
+Delegator - Node controller
+]]--
+Delegator = class(Node)
+function Delegator.__init__(self, ...)
+ Node.__init__(self, ...)
+ self.nodes = {}
+ self.defaultpath = {}
+ self.pageaction = false
+ self.readinput = true
+ self.allow_reset = false
+ self.allow_back = false
+ self.allow_finish = false
+ self.template = "cbi/delegator"
+end
+
+function Delegator.set(self, name, node)
+ if type(node) == "table" and getmetatable(node) == nil then
+ node = Compound(unpack(node))
+ end
+ assert(type(node) == "function" or instanceof(node, Compound), "Invalid")
+ assert(not self.nodes[name], "Duplicate entry")
+
+ self.nodes[name] = node
+end
+
+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.set_route(self, ...)
+ local n, chain, route = 0, self.chain, {...}
+ for i = 1, #chain do
+ if chain[i] == self.current then
+ n = i
+ break
+ end
+ end
+ for i = 1, #route do
+ n = n + 1
+ chain[n] = route[i]
+ end
+ for i = n + 1, #chain do
+ chain[i] = nil
+ end
+end
+
+function Delegator.get(self, name)
+ return self.nodes[name]
+end
+
+function Delegator.parse(self, ...)
+ local newcurrent
+ self.chain = self.chain or self:get_chain()
+ self.current = self.current or self:get_active()
+ self.active = self.active or self:get(self.current)
+ assert(self.active, "Invalid state")
+
+ local stat = FORM_DONE
+ if type(self.active) ~= "function" then
+ self.active:populate_delegator(self)
+ stat = self.active:parse()
+ else
+ self:active()
+ end
+
+ if stat > 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
+ elseif stat < FORM_PROCEED then
+ return stat
+ end
+
+
+ if not Map.formvalue(self, "cbi.submit") then
+ return FORM_NODATA
+ elseif not newcurrent or not self:get(newcurrent) then
+ return FORM_DONE
+ else
+ self.current = newcurrent
+ self.active = self:get(self.current)
+ if type(self.active) ~= "function" then
+ self.active:parse(false)
+ return FROM_PROCEED
+ else
+ return self:parse(...)
+ end
+ end
+end
+
+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.get_active(self)
+ return Map.formvalue(self, "cbi.delg.current") or self.chain[1]
+end
+
+--[[
+Page - A simple node
+]]--
+
+Page = class(Node)
+Page.__init__ = Node.__init__
+Page.parse = function() end
+
+
+--[[
+SimpleForm - A Simple non-UCI form
+]]--
+SimpleForm = class(Node)
+
+function SimpleForm.__init__(self, config, title, description, data)
+ Node.__init__(self, title, description)
+ self.config = config
+ self.data = data or {}
+ self.template = "cbi/simpleform"
+ self.dorender = true
+ self.pageaction = false
+ self.readinput = true
+end
+
+SimpleForm.formvalue = Map.formvalue
+SimpleForm.formvaluetable = Map.formvaluetable
+
+function SimpleForm.parse(self, readinput, ...)
+ self.readinput = (readinput ~= false)
+
+ if self:formvalue("cbi.skip") then
+ return FORM_SKIP
+ end
+
+ if self:submitstate() then
+ Node.parse(self, 1, ...)
+ end
+
+ local valid = true
+ for k, j in ipairs(self.children) do
+ for i, v in ipairs(j.children) do
+ valid = valid
+ and (not v.tag_missing or not v.tag_missing[1])
+ and (not v.tag_invalid or not v.tag_invalid[1])
+ and (not v.error)
+ end
+ end
+
+ local state =
+ not self:submitstate() and FORM_NODATA
+ or valid and FORM_VALID
+ or FORM_INVALID
+
+ self.dorender = not self.handle
+ if self.handle then
+ local nrender, nstate = self:handle(state, self.data)
+ self.dorender = self.dorender or (nrender ~= false)
+ state = nstate or state
+ end
+ return state
+end
+
+function SimpleForm.render(self, ...)
+ if self.dorender then
+ Node.render(self, ...)
+ end
+end
+
+function SimpleForm.submitstate(self)
+ return self:formvalue("cbi.submit")
+end
+
+function SimpleForm.section(self, class, ...)
+ if instanceof(class, AbstractSection) then
+ local obj = class(self, ...)
+ self:append(obj)
+ return obj
+ else
+ error("class must be a descendent of AbstractSection")
+ end
+end
+
+-- Creates a child field
+function SimpleForm.field(self, class, ...)
+ local section
+ for k, v in ipairs(self.children) do
+ if instanceof(v, SimpleSection) then
+ section = v
+ break
+ end
+ end
+ if not section then
+ section = self:section(SimpleSection)
+ end
+
+ if instanceof(class, AbstractValue) then
+ local obj = class(self, section, ...)
+ obj.track_missing = true
+ section:append(obj)
+ return obj
+ else
+ error("class must be a descendent of AbstractValue")
+ end
+end
+
+function SimpleForm.set(self, section, option, value)
+ self.data[option] = value
+end
+
+
+function SimpleForm.del(self, section, option)
+ self.data[option] = nil
+end
+
+
+function SimpleForm.get(self, section, option)
+ return self.data[option]
+end
+
+
+function SimpleForm.get_scheme()
+ return nil
+end
+
+
+Form = class(SimpleForm)
+
+function Form.__init__(self, ...)
+ SimpleForm.__init__(self, ...)
+ self.embedded = true