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
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
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
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")
--[[
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
]]--
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
function ListValue.__init__(self, ...)
AbstractValue.__init__(self, ...)
- self.template = "cbi/value"
+ self.template = "cbi/lvalue"
self.list = {}
end
<%
-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
end
end
%>
-<div class="cbi-tsection" id="cbi-<%=map%>-<%=sectiontype%>">
-<h2><%=title%></h2>
-<div class="cbi-tsection-descr"><%=description%></div>
+<div class="cbi-tsection" id="cbi-<%=self.map.config%>-<%=self.sectiontype%>">
+<h2><%=self.title%></h2>
+<div class="cbi-tsection-descr"><%=self.description%></div>
<% for k, v in pairs(sections) do %>
-<div class="cbi-tsection-node" id="cbi-<%=map%>-<%=k%>">
-<% for k, node in ipairs(children) do
+<div class="cbi-tsection-node" id="cbi-<%=self.map.config%>-<%=k%>">
+<% for k, node in ipairs(self.children) do
node.section = k
node:render(k)
end %>