libs/web: improve cbi handling of Flag values, support usage of .default attribute to signalize default-on checkboxes, this helps to deal with the reversed logic of various uci bools within OpenWrt configs

This commit is contained in:
Jo-Philipp Wich 2011-01-29 17:54:00 +00:00
parent ece62bd9ab
commit be5b1015b2
2 changed files with 16 additions and 10 deletions

View file

@ -51,6 +51,7 @@ 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, ...)
@ -1481,29 +1482,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
--[[

View file

@ -13,6 +13,9 @@ $Id$
-%>
<%+cbi/valueheader%>
<input type="hidden" value="1"<%=
attr("name", "cbi.cbe." .. self.config .. "." .. section .. "." .. self.option)
%> />
<input class="cbi-input-checkbox" onclick="cbi_d_update(this.id)" onchange="cbi_d_update(this.id)" type="checkbox"<%=
attr("id", cbid) .. attr("name", cbid) .. attr("value", self.enabled or 1) ..
ifattr((self:cfgvalue(section) or self.default) == self.enabled, "checked", "checked")