* Rewritten menu builder
[project/luci.git] / src / ffluci / menu.lua
1 --[[
2 FFLuCI - Menu Builder
3
4 Description:
5 Collects menu building information from controllers
6
7 FileId:
8 $Id$
9
10 License:
11 Copyright 2008 Steven Barth <steven@midlink.org>
12
13 Licensed under the Apache License, Version 2.0 (the "License");
14 you may not use this file except in compliance with the License.
15 You may obtain a copy of the License at 
16
17         http://www.apache.org/licenses/LICENSE-2.0 
18
19 Unless required by applicable law or agreed to in writing, software
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24
25 ]]--
26 module("ffluci.menu", package.seeall)
27
28 require("ffluci.fs")
29 require("ffluci.util")
30 require("ffluci.template")
31 require("ffluci.i18n")
32
33 -- Default modelpath
34 modelpath = ffluci.fs.dirname(ffluci.util.__file__()) .. "/model/menu/"
35
36 -- Menu definition extra scope
37 scope = {
38         translate = ffluci.i18n.translate
39 }
40
41 -- Local menu database
42 local menu = {}
43
44 -- The current pointer
45 local menuc = {}
46
47 -- Adds a menu category to the current menu and selects it
48 function add(cat, controller, title, order)
49         order = order or 50
50         if not menu[cat] then
51                 menu[cat] = {}
52         end
53         
54         local entry = {}
55         entry[".descr"] = title
56         entry[".order"] = order
57         entry[".contr"] = controller
58         
59         menuc = entry
60
61         local i = 0                     
62         for k,v in ipairs(menu[cat]) do
63                 if v[".order"] > entry[".order"] then
64                         break
65                 end  
66                 i = k
67         end     
68         table.insert(menu[cat], i+1, entry)
69                 
70         return true
71 end
72
73 -- Adds an action to the current menu
74 function act(action, title)
75         table.insert(menuc, {action = action, descr = title})
76         return true
77 end
78
79 -- Selects a menu category
80 function sel(cat, controller)
81         if not menu[cat] then
82                 return nil
83         end
84         menuc = menu[cat]
85         
86         local stat = nil
87         for k,v in ipairs(menuc) do
88                 if v[".contr"] == controller then
89                         menuc = v
90                         stat = true
91                 end
92         end
93         
94         return stat
95 end
96
97
98 -- Collect all menu information provided in the model dir
99 function collect()
100         for k, menu in pairs(ffluci.fs.dir(modelpath)) do
101                 if menu:sub(1, 1) ~= "." then
102                         local f = loadfile(modelpath.."/"..menu)
103                         local env = ffluci.util.clone(scope)
104                         
105                         env.add = add
106                         env.sel = sel
107                         env.act = act
108                         
109                         setfenv(f, env)
110                         f()
111                 end
112         end
113 end
114
115 -- Returns the menu information
116 function get()
117         collect()
118         return menu
119 end