5a5f421e9dc125066623dfefb40338a5ce401e81
[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 require("ffluci.config")
33
34 -- Default modelpath
35 modelpath = ffluci.config.path .. "/model/menu/"
36
37 -- Menu definition extra scope
38 scope = {
39         translate = ffluci.i18n.translate
40 }
41
42 -- Local menu database
43 local menu = {}
44
45 -- The current pointer
46 local menuc = {}
47
48 -- Adds a menu category to the current menu and selects it
49 function add(cat, controller, title, order)
50         order = order or 50
51         if not menu[cat] then
52                 menu[cat] = {}
53         end
54         
55         local entry = {}
56         entry[".descr"] = title
57         entry[".order"] = order
58         entry[".contr"] = controller
59         
60         menuc = entry
61
62         local i = 0                     
63         for k,v in ipairs(menu[cat]) do
64                 if v[".order"] > entry[".order"] then
65                         break
66                 end  
67                 i = k
68         end     
69         table.insert(menu[cat], i+1, entry)
70                 
71         return true
72 end
73
74 -- Adds an action to the current menu
75 function act(action, title)
76         table.insert(menuc, {action = action, descr = title})
77         return true
78 end
79
80 -- Selects a menu category
81 function sel(cat, controller)
82         if not menu[cat] then
83                 return nil
84         end
85         menuc = menu[cat]
86         
87         local stat = nil
88         for k,v in ipairs(menuc) do
89                 if v[".contr"] == controller then
90                         menuc = v
91                         stat = true
92                 end
93         end
94         
95         return stat
96 end
97
98
99 -- Collect all menu information provided in the model dir
100 function collect()
101         for k, menu in pairs(ffluci.fs.dir(modelpath)) do
102                 if menu:sub(1, 1) ~= "." then
103                         local f = loadfile(modelpath.."/"..menu)
104                         local env = ffluci.util.clone(scope)
105                         
106                         env.add = add
107                         env.sel = sel
108                         env.act = act
109                         
110                         setfenv(f, env)
111                         f()
112                 end
113         end
114 end
115
116 -- Returns the menu information
117 function get()
118         collect()
119         return menu
120 end