+Delegator - Node controller
+]]--
+Delegator = class(Node)
+function Delegator.__init__(self, ...)
+ Node.__init__(self, ...)
+ self.nodes = {}
+ 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")
+ assert(not self.nodes[name], "Duplicate entry")
+
+ self.nodes[name] = state
+ self:append(state)
+
+ return state
+end
+
+function Delegator.get(self, name)
+ return self.nodes[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
+ end
+ else
+ return state
+ end
+end
+
+function Delegator.parse(self, ...)
+ local active = self:getactive()
+ assert(active, "Invalid state")
+
+ local cstate = active.node:parse()
+ self.active = active.transistor(self, active.node, cstate)
+
+ if not self.active then
+ return FORM_DONE
+ else
+ self.active:parse(false)
+ return FROM_PROCEED
+ end
+end
+
+function Delegator.render(self, ...)
+ self.active.node:render(...)
+end
+
+function Delegator.getactive(self)
+ return self:get(Map.formvalue(self, "cbi.delegated")
+ or (self.children[1] and self.children[1].name))
+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: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 or self:handle(state, self.data) ~= false
+ 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
+
+
+
+--[[