d6f65116e48f9641b60ddc0e6d4fe2e4b4decc59
[project/luci.git] / core / src / 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.sys")
31
32 -- Default modelpath
33 modelpattern = ffluci.sys.libpath() .. "/model/menu/*.lua"
34
35 -- Menu definition extra scope
36 scope = {
37         translate = function(...) return require("ffluci.i18n").translate(...) end,
38         loadtrans = function(...) return require("ffluci.i18n").loadc(...) end,
39         isfile    = ffluci.fs.isfile
40 }
41
42 -- Local menu database
43 local menu = nil
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 100
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         local generators = {}
102         
103         local m = ffluci.fs.glob(modelpattern) or {}
104         for k, menu in pairs(m) do
105                 local f = loadfile(menu)
106                 if f then
107                         table.insert(generators, f)
108                 end
109         end
110         
111         return generators
112 end
113
114 -- Parse the collected information
115 function parse(generators)
116         menu = {}
117         for i, f in pairs(generators) do
118                 local env = ffluci.util.clone(scope)
119                 
120                 env.add = add
121                 env.sel = sel
122                 env.act = act
123                 
124                 setfenv(f, env)
125                 f()
126         end
127         return menu
128 end
129
130 -- Returns the menu information
131 function get()
132         if not menu then
133                 menu = parse(collect())
134         end
135         return menu
136 end