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