* Rewritten menu builder
authorSteven Barth <steven@midlink.org>
Mon, 31 Mar 2008 08:52:24 +0000 (08:52 +0000)
committerSteven Barth <steven@midlink.org>
Mon, 31 Mar 2008 08:52:24 +0000 (08:52 +0000)
* Moved menu definitions from controllers to model/menu

Makefile
src/ffluci/controller/admin/index.lua
src/ffluci/controller/admin/mesh.lua
src/ffluci/controller/admin/network.lua
src/ffluci/controller/admin/system.lua
src/ffluci/controller/admin/wifi.lua
src/ffluci/controller/public/index.lua
src/ffluci/fs.lua
src/ffluci/menu.lua
src/ffluci/model/menu/00main.lua [new file with mode: 0644]

index 79adba0..0aefd19 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -8,7 +8,7 @@ ffluci/sys.lua ffluci/model/uci.lua ffluci/model/ipkg.lua \
 ffluci/config.lua ffluci/i18n.lua ffluci/template.lua \
 ffluci/cbi.lua ffluci/dispatcher.lua ffluci/menu.lua ffluci/init.lua 
 
 ffluci/config.lua ffluci/i18n.lua ffluci/template.lua \
 ffluci/cbi.lua ffluci/dispatcher.lua ffluci/menu.lua ffluci/init.lua 
 
-DIRECTORIES = dist/ffluci/model/cbi dist/ffluci/controller dist/ffluci/i18n dist/ffluci/view
+DIRECTORIES = dist/ffluci/model/cbi dist/ffluci/model/menu dist/ffluci/controller dist/ffluci/i18n dist/ffluci/view
 
 INFILES = $(CFILES:%=src/%)
 OUTFILE = ffluci/init.lua
 
 INFILES = $(CFILES:%=src/%)
 OUTFILE = ffluci/init.lua
@@ -25,6 +25,7 @@ dist:
        cp src/ffluci/i18n/* dist/ffluci/i18n/
        cp src/ffluci/view/* dist/ffluci/view/ -R
        cp src/ffluci/model/cbi/* dist/ffluci/model/cbi/ -R
        cp src/ffluci/i18n/* dist/ffluci/i18n/
        cp src/ffluci/view/* dist/ffluci/view/ -R
        cp src/ffluci/model/cbi/* dist/ffluci/model/cbi/ -R
+       cp src/ffluci/model/menu/* dist/ffluci/model/menu/ -R
 
 compile:
        mkdir -p $(DIRECTORIES)
 
 compile:
        mkdir -p $(DIRECTORIES)
index 43e680a..b4a7720 100644 (file)
@@ -1,10 +1 @@
-module("ffluci.controller.admin.index", package.seeall)
-
-menu = {
-       descr   = "Übersicht",
-       order   = 10,
-       entries = {
-               {action = "luci", descr = "FFLuCI"},
-               {action = "contact", descr = "Kontakt"}
-       }
-}
\ No newline at end of file
+module("ffluci.controller.admin.index", package.seeall)
\ No newline at end of file
index fcdcdab..3dbe98c 100644 (file)
@@ -1,9 +1 @@
-module("ffluci.controller.admin.mesh", package.seeall)
-
-menu = {
-       descr   = "Mesh",
-       order   = 50,
-       entries = {
-               {action = "olsrd", descr = "OLSR"},
-       }
-}
\ No newline at end of file
+module("ffluci.controller.admin.mesh", package.seeall)
\ No newline at end of file
index 5ffe641..4f8160a 100644 (file)
@@ -1,12 +1 @@
-module(..., package.seeall)
-
-menu = {
-       descr   = "Netzwerk",
-       order   = 30,
-       entries = {
-               {action = "vlan", descr = "Switch"},
-               {action = "ifaces", descr = "Schnittstellen"},
-               {action = "ptp", descr = "PPPoE / PPTP"},
-               {action = "routes", descr = "Statische Routen"},
-       }
-}
\ No newline at end of file
+module(..., package.seeall)
\ No newline at end of file
index 455424e..c4ba027 100644 (file)
@@ -5,16 +5,6 @@ require("ffluci.http")
 require("ffluci.util")
 require("ffluci.fs")
 
 require("ffluci.util")
 require("ffluci.fs")
 
-menu = {
-       descr   = "System",
-       order   = 20,
-       entries = {
-               {action = "passwd", descr = "Passwort ändern"},
-               {action = "sshkeys", descr = "SSH-Schlüssel"},
-               {action = "reboot", descr = "Neu starten"},
-       }
-}
-
 function action_editor()
        local file = ffluci.http.formvalue("file", "")
        local data = ffluci.http.formvalue("data")
 function action_editor()
        local file = ffluci.http.formvalue("file", "")
        local data = ffluci.http.formvalue("data")
index 54b6bfb..bc1040c 100644 (file)
@@ -1,10 +1 @@
-module("ffluci.controller.admin.wifi", package.seeall)
-
-menu = {
-       descr   = "Drahtlos",
-       order   = 40,
-       entries = {
-               {action = "devices", descr = "Geräte"},
-               {action = "networks", descr = "Netze"},
-       }
-}
\ No newline at end of file
+module("ffluci.controller.admin.wifi", package.seeall)
\ No newline at end of file
index 3ee564f..4f8160a 100644 (file)
@@ -1,9 +1 @@
-module(..., package.seeall)
-
-menu = {
-       descr   = "Übersicht",
-       order   = 10,
-       entries = {
-               {action = "contact", descr = "Kontakt"}
-       }
-}
\ No newline at end of file
+module(..., package.seeall)
\ No newline at end of file
index 897308c..6e8859a 100644 (file)
@@ -93,12 +93,13 @@ basename = posix.basename
 -- dirname wrapper
 dirname = posix.dirname
 
 -- dirname wrapper
 dirname = posix.dirname
 
--- Diriterator - alias for lfs.dir - filter . and ..
+-- dir wrapper
 function dir(path)
 function dir(path)
-       local e = posix.dir(path)
-       table.remove(e, 1)
-       table.remove(e, 1)
-       return e
+       local dir = {}
+       for node in posix.files(path) do
+               table.insert(dir, 1, node)
+       end 
+       return dir
 end
 
 -- Alias for lfs.mkdir
 end
 
 -- Alias for lfs.mkdir
index 724faae..cbf47e2 100644 (file)
@@ -28,97 +28,92 @@ module("ffluci.menu", package.seeall)
 require("ffluci.fs")
 require("ffluci.util")
 require("ffluci.template")
 require("ffluci.fs")
 require("ffluci.util")
 require("ffluci.template")
+require("ffluci.i18n")
 
 
-ctrldir   = ffluci.fs.dirname(ffluci.util.__file__()) .. "/controller/"
-modelpath = ffluci.fs.dirname(ffluci.util.__file__()) .. "/model/menudata.lua"
+-- Default modelpath
+modelpath = ffluci.fs.dirname(ffluci.util.__file__()) .. "/model/menu/"
 
 
--- Cache menudata into a Luafile instead of recollecting it at every pageload
--- Warning: Make sure the menudata cache gets deleted everytime you update
--- the menu information of any module or add or remove a module
-builder_enable_cache = false
+-- Menu definition extra scope
+scope = {
+       translate = ffluci.i18n.translate
+}
 
 
+-- Local menu database
+local menu = {}
 
 
--- Builds the menudata file
-function build()
-       local data = collect()
-       ffluci.fs.writefile(modelpath, dump(data, "m"))
-       return data
+-- The current pointer
+local menuc = {}
+
+-- Adds a menu category to the current menu and selects it
+function add(cat, controller, title, order)
+       order = order or 50
+       if not menu[cat] then
+               menu[cat] = {}
+       end
+       
+       local entry = {}
+       entry[".descr"] = title
+       entry[".order"] = order
+       entry[".contr"] = controller
+       
+       menuc = entry
+
+       local i = 0                     
+       for k,v in ipairs(menu[cat]) do
+               if v[".order"] > entry[".order"] then
+                       break
+               end  
+               i = k
+       end     
+       table.insert(menu[cat], i+1, entry)
+               
+       return true
 end
 
 end
 
+-- Adds an action to the current menu
+function act(action, title)
+       table.insert(menuc, {action = action, descr = title})
+       return true
+end
 
 
--- Collect all menu information provided in the controller modules
-function collect()
-       local m = {}
-       for k,cat in pairs(ffluci.fs.dir(ctrldir)) do
-               m[cat] = {}
-               for k,con in pairs(ffluci.fs.dir(ctrldir .. "/" .. cat)) do
-                       if con:sub(-4) == ".lua" then
-                               con = con:sub(1, con:len()-4)
-                               local mod = require("ffluci.controller." .. cat .. "." .. con)
-                               if mod.menu and mod.menu.descr
-                               and mod.menu.entries and mod.menu.order then
-                                       local entry = {}
-                                       entry[".descr"] = mod.menu.descr
-                                       entry[".order"] = mod.menu.order
-                                       entry[".contr"] = con
-                                       for k,v in pairs(mod.menu.entries) do
-                                               entry[k] = v
-                                       end
-                                       local i = 0                     
-                                       for k,v in ipairs(m[cat]) do
-                                               if v[".order"] > entry[".order"] then
-                                                       break
-                                               end  
-                                               i = k
-                                       end     
-                                       table.insert(m[cat], i+1, entry)
-                               end
-                       end
+-- Selects a menu category
+function sel(cat, controller)
+       if not menu[cat] then
+               return nil
+       end
+       menuc = menu[cat]
+       
+       local stat = nil
+       for k,v in ipairs(menuc) do
+               if v[".contr"] == controller then
+                       menuc = v
+                       stat = true
                end
        end
                end
        end
-       return m
+       
+       return stat
 end
 
 
 end
 
 
--- Dumps a table into a string of Lua code
-function dump(tbl, name)
-       local src = name .. "={}\n"
-       for k,v in pairs(tbl) do
-               if type(k) == "string" then
-                       k = ffluci.util.escape(k)
-                       k = "'" .. ffluci.util.escape(k, "'") .. "'"
-               end             
-               if type(v) == "string" then
-                       v = ffluci.util.escape(v)
-                       v = ffluci.util.escape(v, "'")
-                       src = src .. name .. "[" .. k .. "]='" .. v .. "'\n"
-               elseif type(v) == "number" then
-                       src = src .. name .. "[" .. k .. "]=" .. v .. "\n"
-               elseif type(v) == "table" then
-                       src = src .. dump(v, name .. "[" .. k .. "]")
+-- Collect all menu information provided in the model dir
+function collect()
+       for k, menu in pairs(ffluci.fs.dir(modelpath)) do
+               if menu:sub(1, 1) ~= "." then
+                       local f = loadfile(modelpath.."/"..menu)
+                       local env = ffluci.util.clone(scope)
+                       
+                       env.add = add
+                       env.sel = sel
+                       env.act = act
+                       
+                       setfenv(f, env)
+                       f()
                end
        end
                end
        end
-       return src
 end
 
 -- Returns the menu information
 function get()
 end
 
 -- Returns the menu information
 function get()
-       if builder_enable_cache then
-               local cachemt = ffluci.fs.mtime(modelpath)
-               local data = nil
-               
-               if cachemt == nil then
-                       data = build()
-               else
-                       local fenv = {}
-                       local f    = loadfile(modelpath)
-                       setfenv(f, fenv)
-                       f()
-                       data = fenv.m
-               end
-               
-               return data
-       else
-               return collect()
-       end
+       collect()
+       return menu
 end
\ No newline at end of file
 end
\ No newline at end of file
diff --git a/src/ffluci/model/menu/00main.lua b/src/ffluci/model/menu/00main.lua
new file mode 100644 (file)
index 0000000..64fc364
--- /dev/null
@@ -0,0 +1,26 @@
+-- General menu definition
+add("public", "index", "Übersicht", 10)
+act("contact", "Kontakt")
+
+
+add("admin", "index", "Übersicht", 10)
+act("luci", "FFLuCI")
+act("contact", "Kontakt")
+
+add("admin", "system", "System", 20)
+act("passwd", "Passwort ändern")
+act("sshkeys", "SSH-Schlüssel")
+act("reboot", "Neu starten")
+
+add("admin", "network", "Netzwerk", 30)
+act("vlan", "Switch")
+act("ifaces", "Schnittstellen")
+act("ptp", "PPPoE / PPTP")
+act("routes", "Statische Routen")
+
+add("admin", "wifi", "Drahtlos", 40)
+act("devices", "Geräte")
+act("networks", "Netze")
+
+add("admin", "mesh", "Mesh", 50)
+act("olsrd", "OLSR")
\ No newline at end of file