libs/web: sent origin section id encoded in cbi.cts. requests
[project/luci.git] / libs / web / luasrc / cbi.lua
index 403935a..86658d7 100644 (file)
@@ -50,6 +50,8 @@ AUTO = true
 
 CREATE_PREFIX = "cbi.cts."
 REMOVE_PREFIX = "cbi.rts."
+RESORT_PREFIX = "cbi.sts."
+FEXIST_PREFIX = "cbi.cbe."
 
 -- Loads a CBI map from given file, creating an environment and returns it
 function load(cbimap, ...)
@@ -209,7 +211,9 @@ end
 
 -- Render the children
 function Node.render_children(self, ...)
+       local k, node
        for k, node in ipairs(self.children) do
+               node.last_child = (k == #self.children)
                node:render(...)
        end
 end
@@ -322,10 +326,10 @@ function Map.parse(self, readinput, ...)
                                self.uci:apply(self.parsechain)
                                self:_run_hooks("on_apply", "on_after_apply")
                        else
-                               self._apply = function()
-                                       local cmd = self.uci:apply(self.parsechain, true)
-                                       return io.popen(cmd)
-                               end
+                               -- This is evaluated by the dispatcher and delegated to the
+                               -- template which in turn fires XHR to perform the actual
+                               -- apply actions.
+                               self.apply_needed = true
                        end
 
                        -- Reparse sections
@@ -358,12 +362,6 @@ end
 function Map.render(self, ...)
        self:_run_hooks("on_init")
        Node.render(self, ...)
-       if false and self._apply then
-               local fp = self._apply()
-               fp:read("*a")
-               fp:close()
-               self:_run_hooks("on_apply")
-       end
 end
 
 -- Creates a child section
@@ -384,10 +382,14 @@ end
 
 -- UCI set
 function Map.set(self, section, option, value)
-       if option then
-               return self.uci:set(self.config, section, option, value)
+       if type(value) ~= "table" or #value > 0 then
+               if option then
+                       return self.uci:set(self.config, section, option, value)
+               else
+                       return self.uci:set(self.config, section, value)
+               end
        else
-               return self.uci:set(self.config, section, value)
+               return Map.del(self, section, option)
        end
 end
 
@@ -805,7 +807,9 @@ function AbstractSection.render_tab(self, tab, ...)
        assert(tab and self.tabs and self.tabs[tab],
                "Cannot render not existing tab %q" % tostring(tab))
 
-       for _, node in ipairs(self.tabs[tab].childs) do
+       local k, node
+       for k, node in ipairs(self.tabs[tab].childs) do
+               node.last_child = (k == #self.tabs[tab].childs)
                node:render(...)
        end
 end
@@ -1091,10 +1095,10 @@ function TypedSection.parse(self, novld)
                -- Create
                local created
                local crval = CREATE_PREFIX .. self.config .. "." .. self.sectiontype
-               local name  = self.map:formvalue(crval)
+               local origin, name = next(self.map:formvaluetable(crval))
                if self.anonymous then
                        if name then
-                               created = self:create()
+                               created = self:create(nil, origin)
                        end
                else
                        if name then
@@ -1110,7 +1114,7 @@ function TypedSection.parse(self, novld)
                                end
 
                                if name and #name > 0 then
-                                       created = self:create(name) and name
+                                       created = self:create(name, origin) and name
                                        if not created then
                                                self.invalid_cts = true
                                        end
@@ -1123,6 +1127,20 @@ function TypedSection.parse(self, novld)
                end
        end
 
+       if self.sortable then
+               local stval = RESORT_PREFIX .. self.config .. "." .. self.sectiontype
+               local order = self.map:formvalue(stval)
+               if order and #order > 0 then
+                       local sid
+                       local num = 0
+                       for sid in util.imatch(order) do
+                               self.map.uci:reorder(self.config, sid, num)
+                               num = num + 1
+                       end
+                       self.changed = (num > 0)
+               end
+       end
+
        if created or self.changed then
                self:push_events()
        end
@@ -1352,26 +1370,41 @@ function AbstractValue.cfgvalue(self, section)
                        return value[1]
                end
        elseif self.cast == "table" then
-               return luci.util.split(value, "%s+", nil, true)
+               return { value }
        end
 end
 
 -- Validate the form value
 function AbstractValue.validate(self, value)
-       if self.datatype and value and datatypes[self.datatype] then
-               if type(value) == "table" then
-                       local v
-                       for _, v in ipairs(value) do
-                               if v and #v > 0 and not datatypes[self.datatype](v) then
-                                       return nil
-                               end
+       if self.datatype and value then
+               local args = { }
+               local dt, ar = self.datatype:match("^(%w+)%(([^%(%)]+)%)")
+
+               if dt and ar then
+                       local a
+                       for a in ar:gmatch("[^%s,]+") do
+                               args[#args+1] = a
                        end
                else
-                       if not datatypes[self.datatype](value) then
-                               return nil
+                       dt = self.datatype
+               end
+
+               if dt and datatypes[dt] then
+                       if type(value) == "table" then
+                               local v
+                               for _, v in ipairs(value) do
+                                       if v and #v > 0 and not datatypes[dt](v, unpack(args)) then
+                                               return nil
+                                       end
+                               end
+                       else
+                               if not datatypes[dt](value, unpack(args)) then
+                                       return nil
+                               end
                        end
                end
        end
+
        return value
 end
 
@@ -1453,29 +1486,31 @@ function Flag.__init__(self, ...)
        AbstractValue.__init__(self, ...)
        self.template  = "cbi/fvalue"
 
-       self.enabled = "1"
+       self.enabled  = "1"
        self.disabled = "0"
+       self.default  = self.disabled
 end
 
 -- A flag can only have two states: set or unset
 function Flag.parse(self, section)
-       local fvalue = self:formvalue(section)
+       local fexists = self.map:formvalue(
+               FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option)
 
-       if fvalue then
-               fvalue = self.enabled
-       else
-               fvalue = self.disabled
-       end
-
-       if fvalue == self.enabled or (not self.optional and not self.rmempty) then
-               if not(fvalue == self:cfgvalue(section)) then
+       if fexists then
+               local fvalue = self:formvalue(section) and self.enabled or self.disabled
+               if fvalue ~= self.default or (not self.optional and not self.rmempty) then
                        self:write(section, fvalue)
+               else
+                       self:remove(section)
                end
        else
                self:remove(section)
        end
 end
 
+function Flag.cfgvalue(self, section)
+       return AbstractValue.cfgvalue(self, section) or self.default
+end
 
 
 --[[
@@ -1642,13 +1677,22 @@ function DynamicList.value(self, key, val)
 end
 
 function DynamicList.write(self, section, value)
-       if self.cast == "string" and type(value) == "table" then
-               value = table.concat(value, " ")
-       elseif self.cast == "table" and type(value) == "string" then
-               local x, t = { }
-               for x in value:gmatch("%S+") do
-                       t[#t+1] = x
+       local t = { }
+
+       if type(value) == "table" then
+               local x
+               for _, x in ipairs(value) do
+                       if x and #x > 0 then
+                               t[#t+1] = x
+                       end
                end
+       else
+               t = { value }
+       end
+
+       if self.cast == "string" then
+               value = table.concat(t, " ")
+       else
                value = t
        end
 
@@ -1662,7 +1706,9 @@ function DynamicList.cfgvalue(self, section)
                local x
                local t = { }
                for x in value:gmatch("%S+") do
-                       t[#t+1] = x
+                       if #x > 0 then
+                               t[#t+1] = x
+                       end
                end
                value = t
        end
@@ -1674,12 +1720,16 @@ function DynamicList.formvalue(self, section)
        local value = AbstractValue.formvalue(self, section)
 
        if type(value) == "string" then
-               local x
-               local t = { }
-               for x in value:gmatch("%S+") do
-                       t[#t+1] = x
+               if self.cast == "string" then
+                       local x
+                       local t = { }
+                       for x in value:gmatch("%S+") do
+                               t[#t+1] = x
+                       end
+                       value = t
+               else
+                       value = { value }
                end
-               value = t
        end
 
        return value