X-Git-Url: https://git.librecmc.org/?a=blobdiff_plain;f=libs%2Fweb%2Fluasrc%2Fcbi.lua;h=5aa2f5a092ef604c8628d1e1b96ec1455fd70ff9;hb=bf49f78599a006a9d136d9fd83cecc5d8a5afb1a;hp=0fc93b25cd3dce1fa8f504fe743d0fccbfe3c405;hpb=be385bfcc63a4e75698ea7b564b0c1f5e50ba4ed;p=oweals%2Fluci.git diff --git a/libs/web/luasrc/cbi.lua b/libs/web/luasrc/cbi.lua index 0fc93b25c..5aa2f5a09 100644 --- a/libs/web/luasrc/cbi.lua +++ b/libs/web/luasrc/cbi.lua @@ -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 @@ -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,7 +1370,7 @@ 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 @@ -1468,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) - - if fvalue then - fvalue = self.enabled - else - fvalue = self.disabled - end + local fexists = self.map:formvalue( + FEXIST_PREFIX .. self.config .. "." .. section .. "." .. self.option) - 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 --[[ @@ -1666,11 +1686,6 @@ function DynamicList.write(self, section, value) t[#t+1] = x end end - elseif self.cast == "table" then - local x - for x in util.imatch(value) do - t[#t+1] = x - end else t = { value } end @@ -1705,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