* CBI updates
[project/luci.git] / src / ffluci / cbi.lua
index 149c1d70fae4b071079cb2ad5c78fd1601d28fcc..85b9f49e9dc56b46f8ce4614c1f883cff0094800 100644 (file)
@@ -25,22 +25,50 @@ 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 Template   = ffluci.template.Template
+local class      = ffluci.util.class
 local instanceof = ffluci.util.instanceof
 
 
+function load(cbimap)
+       require("ffluci.fs")
+       require("ffluci.i18n")
+       
+       local cbidir = ffluci.fs.dirname(ffluci.util.__file__()) .. "model/cbi/"
+       local func = loadfile(cbidir..cbimap..".lua")
+       
+       if not func then
+               error("Unable to load CBI map: " .. cbimap)
+               return nil
+       end
+       
+       ffluci.util.resfenv(func)
+       ffluci.util.updfenv(func, ffluci.cbi)
+       ffluci.util.extfenv(func, "translate", ffluci.i18n.translate)
+       
+       local map = func()
+       
+       if not instanceof(map, Map) then
+               error("CBI map returns no valid map object!")
+               return nil
+       end
+       
+       return map
+end
+
 -- Node pseudo abstract class
 Node = class()
 
 function Node.__init__(self, title, description)
        self.children = {}
-       self.title = title
-       self.description = description
+       self.title = title or ""
+       self.description = description or ""
        self.template = "cbi/node"
 end
 
@@ -55,7 +83,13 @@ function Node.parse(self)
 end
 
 function Node.render(self)
-       ffluci.template.render(self.template)
+       ffluci.template.render(self.template, {self=self})
+end
+
+function Node.render_children(self)
+       for k, node in ipairs(self.children) do
+               node:render()
+       end
 end
 
 
@@ -83,7 +117,7 @@ function Map.section(self, class, ...)
 end
 
 function Map.read(self)
-       self.ucidata = self.ucidata or ffluci.model.uci.show(self.config)
+       self.ucidata = self.ucidata or ffluci.model.uci.show(self.config)[self.config]
        return self.ucidata
 end
 
@@ -147,6 +181,23 @@ function TypedSection.__init__(self, ...)
        self.valid     = nil
 end
 
+function TypedSection.render_children(self, section)
+       for k, node in ipairs(self.children) do
+               node.section = section
+               node:render()
+       end
+end
+
+function TypedSection.ucisections(self)
+       local sections = {}
+       for k, v in pairs(self.map:read()) do
+               if v[".type"] == self.sectiontype then
+                       sections[k] = v
+               end
+       end
+       return sections 
+end
+
 
 --[[
 AbstractValue - An abstract Value Type
@@ -173,7 +224,7 @@ function AbstractValue.formvalue(self)
 end
 
 function AbstractValue.ucivalue(self)
-       return self.map.read()[self.section][self.option]
+       return self.map:read()[self.section][self.option]
 end
 
 function AbstractValue.validate(self, value)