5724b2cb9e6f8d4c909d018efa848a3d6e0ffd3e
[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 modelpath = ffluci.sys.libpath() .. "/model/menu/"
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         for k, menu in pairs(ffluci.fs.dir(modelpath)) do
104                 if menu:sub(1, 1) ~= "." then
105                         local f = loadfile(modelpath.."/"..menu)
106                         if f then
107                                 table.insert(generators, f)
108                         end
109                 end
110         end
111         
112         return generators
113 end
114
115 -- Parse the collected information
116 function parse(generators)
117         menu = {}
118         for i, f in pairs(generators) do
119                 local env = ffluci.util.clone(scope)
120                 
121                 env.add = add
122                 env.sel = sel
123                 env.act = act
124                 
125                 setfenv(f, env)
126                 f()
127         end
128         return menu
129 end
130
131 -- Returns the menu information
132 function get()
133         if not menu then
134                 menu = parse(collect())
135         end
136         return menu
137 end