From: Steven Barth Date: Thu, 20 Mar 2008 20:33:43 +0000 (+0000) Subject: * CBI updates X-Git-Tag: 0.8.0~1207 X-Git-Url: http://git.openwrt.org/?p=project%2Fluci.git;a=commitdiff_plain;h=e9461f898cdd8f740accdf8c0c99dd1cfe48d1ea * CBI updates --- diff --git a/src/ffluci/cbi.lua b/src/ffluci/cbi.lua index a346dd7072..149c1d70fa 100644 --- a/src/ffluci/cbi.lua +++ b/src/ffluci/cbi.lua @@ -27,6 +27,8 @@ limitations under the License. module("ffluci.cbi", package.seeall) require("ffluci.template") require("ffluci.util") +require("ffluci.http") +require("ffluci.model.uci") local Template = ffluci.template.Template local class = ffluci.util.class local instanceof = ffluci.util.instanceof @@ -46,8 +48,14 @@ function Node.append(self, obj) table.insert(self.children, obj) end +function Node.parse(self) + for k, child in ipairs(self.children) do + child:parse() + end +end + function Node.render(self) - ffluci.template.render(self.template, self) + ffluci.template.render(self.template) end @@ -64,15 +72,20 @@ end function Map.section(self, class, ...) if instanceof(class, AbstractSection) then - local obj = class(...) - obj.map = self.config - table.insert(self.children, obj) + local obj = class(...) + obj.map = self + obj.config = self.config + self:append(obj) return obj else error("class must be a descendent of AbstractSection") end end +function Map.read(self) + self.ucidata = self.ucidata or ffluci.model.uci.show(self.config) + return self.ucidata +end --[[ AbstractSection @@ -86,9 +99,10 @@ end function AbstractSection.option(self, class, ...) if instanceof(class, AbstractValue) then - local obj = class(...) - obj.map = self.map - table.insert(self.children, obj) + local obj = class(...) + obj.map = self.map + obj.config = self.config + self:append(obj) return obj else error("class must be a descendent of AbstractValue") @@ -137,7 +151,7 @@ end --[[ AbstractValue - An abstract Value Type null: Value can be empty - valid: A function returning nil if invalid + valid: A function returning the value if it is valid otherwise nil depends: A table of option => value pairs of which one must be true default: The default value ]]-- @@ -151,7 +165,25 @@ function AbstractValue.__init__(self, option, ...) self.depends = nil self.default = nil end - + + +function AbstractValue.formvalue(self) + local key = "uci."..self.map.config.."."..self.section.."."..self.option + return ffluci.http.formvalue(key) +end + +function AbstractValue.ucivalue(self) + return self.map.read()[self.section][self.option] +end + +function AbstractValue.validate(self, value) + return ffluci.util.validate(value, nil, nil, self.valid) +end + +function AbstractValue.write(self, value) + ffluci.model.uci.set(self.config, self.section, self.option, value) +end + --[[ Value - A one-line value @@ -178,7 +210,7 @@ ListValue = class(AbstractValue) function ListValue.__init__(self, ...) AbstractValue.__init__(self, ...) - self.template = "cbi/value" + self.template = "cbi/lvalue" self.list = {} end diff --git a/src/ffluci/http.lua b/src/ffluci/http.lua index 5e177195e1..bf94105734 100644 --- a/src/ffluci/http.lua +++ b/src/ffluci/http.lua @@ -54,6 +54,7 @@ function request_redirect(category, module, action) redirect(pattern:format(category, module, action)) end + -- Gets form value from key function formvalue(key, default) local c = formvalues() diff --git a/src/ffluci/view/cbi/lvalue.htm b/src/ffluci/view/cbi/lvalue.htm new file mode 100644 index 0000000000..b7db0def96 --- /dev/null +++ b/src/ffluci/view/cbi/lvalue.htm @@ -0,0 +1,11 @@ +
+
<%=self.title%>
+
+ +
+
<%=self.description%>
+
\ No newline at end of file diff --git a/src/ffluci/view/cbi/map.htm b/src/ffluci/view/cbi/map.htm index 918e455d75..e4f493bd09 100644 --- a/src/ffluci/view/cbi/map.htm +++ b/src/ffluci/view/cbi/map.htm @@ -1,7 +1,7 @@ -
+
"> -

<%=title%>

-
<%=description%>
-<% for k, node in ipairs(children) do node:render() end %> +

<%=self.title%>

+
<%=self.description%>
+<% for k, node in ipairs(self.children) do node:render() end %>
diff --git a/src/ffluci/view/cbi/nsection.htm b/src/ffluci/view/cbi/nsection.htm index 4a47c7dad4..4607a872cc 100644 --- a/src/ffluci/view/cbi/nsection.htm +++ b/src/ffluci/view/cbi/nsection.htm @@ -1,5 +1,5 @@ -
-

<%=title%>

-
<%=description%>
-<% for k, node in ipairs(children) do node:render() end %> +
+

<%=self.title%>

+
<%=self.description%>
+<% for k, node in ipairs(self.children) do node:render() end %>
diff --git a/src/ffluci/view/cbi/tsection.htm b/src/ffluci/view/cbi/tsection.htm index 2c25500f8a..420bfd14a8 100644 --- a/src/ffluci/view/cbi/tsection.htm +++ b/src/ffluci/view/cbi/tsection.htm @@ -1,6 +1,5 @@ <% -require("ffluci.model.uci") -local allsections = ffluci.model.uci.show(map) +local allsections = self.map:read() local sections = {} for k, v in pairs(allsections) do if v[".type"] == sectiontype then @@ -8,12 +7,12 @@ for k, v in pairs(allsections) do end end %> -
-

<%=title%>

-
<%=description%>
+
+

<%=self.title%>

+
<%=self.description%>
<% for k, v in pairs(sections) do %> -
-<% for k, node in ipairs(children) do +
+<% for k, node in ipairs(self.children) do node.section = k node:render(k) end %> diff --git a/src/ffluci/view/cbi/value.htm b/src/ffluci/view/cbi/value.htm new file mode 100644 index 0000000000..a39010ac8d --- /dev/null +++ b/src/ffluci/view/cbi/value.htm @@ -0,0 +1,7 @@ +
+
<%=self.title%>
+
+" value="<%=self:ucivalue()%>" /> +
+
<%=self.description%>
+
\ No newline at end of file