-
Notifications
You must be signed in to change notification settings - Fork 4
/
lui.lua
215 lines (177 loc) · 6.85 KB
/
lui.lua
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
require "iuplua"
lo = require "lockout"
---------------------------------------------------------------------------------------------------------------
lo.utils = lo.utils or {}
lo.utils.unwrapObservable = function(observaleOrValue)
if lo.isObservable(observaleOrValue) then
return observaleOrValue()
else
return observaleOrValue
end
end
---------------------------------------------------------------------------------------------------------------
lo.nestContext = function(bindingContext, parentContext)
local bindingContext_mt = {
__index = parentContext
}
setmetatable(bindingContext, bindingContext_mt)
return bindingContext
end
---------------------------------------------------------------------------------------------------------------
lo.applyBindings = function(viewModel, template, bindingContext)
if not bindingContext then
bindingContext = lo.nestContext({
_root = viewModel,
_data = viewModel
}, viewModel)
end
local controlsDescendantBindings = false
local options = {}
for k, v in pairs(template.options) do
options[k] = v
end
local bindings = options.databind
local control = {
realControl = lo.observable()
}
local control_mt = {
__index = options,
__newindex = options,
__len = function() return #options end
}
setmetatable(control, control_mt)
if bindings then
for bindingName, bindingValue in pairs(bindings) do
local bindingHandler = lo.bindingHandlers[bindingName]
if not bindingHandler then
error("Unable to parse binding " .. bindingName)
end
local valueAccessorContext = lo.nestContext({
_context = bindingContext,
_element = control
}, bindingContext)
local valueAccessor = load("return " .. bindingValue, nil, nil, valueAccessorContext)
if bindingHandler.init then
local bindingOptions = bindingHandler:init(control, valueAccessor, viewModel, bindingContext)
controlsDescendantBindings = controlsDescendantBindings or (bindingOptions and bindingOptions.controlsDescendantBindings)
end
if bindingHandler.update then
lo.computed(function() bindingHandler:update(control, valueAccessor, viewModel, bindingContext) end)
end
end
end
if not controlsDescendantBindings then
for i = 1, #options do
options[i] = lo.applyBindings(viewModel, options[i], bindingContext)
end
end
local realControl = lo.createControl(template.name, options)
control_mt.__index = realControl
control_mt.__newindex = realControl
control_mt.__len = function() return iup.GetChildCount(realControl) end
control.realControl(realControl)
return realControl
end
---------------------------------------------------------------------------------------------------------------
lo.createControl = function(name, options)
return iup[name](options)
end
---------------------------------------------------------------------------------------------------------------
lo.bindingHandlers = lo.bindingHandlers or {}
lo.bindingHandlers.title = {
update = function(self, control, valueAccessor, viewModel)
control.title = lo.utils.unwrapObservable(valueAccessor())
end
}
lo.bindingHandlers.action = {
update = function(self, control, valueAccessor, viewModel, bindingContext)
local action = lo.utils.unwrapObservable(valueAccessor())
control.action = function() action(bindingContext._data) end
end
}
lo.bindingHandlers.foreach = {
template = {},
controls = {},
init = function(self, control, valueAccessor, viewModel, bindingContext)
local childCount = #control
local template = {}
self.controls[control] = {}
self.template[control] = template
for i = 1, childCount do
template[i] = control[i]
control[i] = nil
end
return { controlsDescendantBindings = true }
end,
update = function(self, control, valueAccessor, viewModel, bindingContext)
local realControl = control.realControl()
if not realControl then return end
local items = lo.utils.unwrapObservable(valueAccessor())
local newParents = { [1] = viewModel }
if bindingContext._parents then
for i, value in ipairs(bindingContext._parents) do
newParents[i + 1] = value
end
end
local commonBindingContext = lo.nestContext({
_parent = viewModel,
_parents = newParents,
_parentContext = bindingContext
}, bindingContext)
local template = self.template[control]
local controls = {}
for _, controls in pairs(self.controls[control]) do
for i = 1, #controls do
iup.Destroy(controls[i])
end
end
local index = 1
for key, item in pairs(items) do
local itemBindingContext = {
_index = index,
_key = key,
_data = item,
}
local itemBindingContext_mt
if type(item) == "table" or type(item) == "userdata" then
itemBindingContext_mt = {
__index = function(t, k) return item[k] or commonBindingContext[k] end
}
else
itemBindingContext_mt = {
__index = commonBindingContext
}
end
setmetatable(itemBindingContext, itemBindingContext_mt)
local newControls = {}
for i = 1, #template do
local newControl = lo.applyBindings(item, template[i], itemBindingContext)
iup.Append(realControl, newControl)
iup.Map(newControl)
newControls[i] = newControl
end
controls[key] = newControls
index = index + 1
end
self.controls[control] = controls
iup.Refresh(realControl)
end
}
---------------------------------------------------------------------------------------------------------------
local lui
do
lui = {}
local lui_mt = {
__index = function(self, name)
return function(options)
return {
name = name,
options = options
}
end
end
}
setmetatable(lui, lui_mt)
end
------------------------------------------------------------------------------------------------------------------
return lui