libs/web: align interface related widgets to changed network model api
[oweals/luci.git] / libs / web / luasrc / cbi.lua
index f4bf18d1e7a5bb0bf5b671c0e3746bdf0f52d4e9..5aa2f5a092ef604c8628d1e1b96ec1455fd70ff9 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
@@ -803,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
@@ -1121,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
@@ -1466,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
 
 
 --[[