-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheswidget.lua
More file actions
287 lines (287 loc) · 8.16 KB
/
eswidget.lua
File metadata and controls
287 lines (287 loc) · 8.16 KB
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
local Widget, is_mixins_class
do
local _obj_0 = require("lapis.html")
Widget, is_mixins_class = _obj_0.Widget, _obj_0.is_mixins_class
end
local underscore
underscore = require("lapis.util").underscore
local types, is_type
do
local _obj_0 = require("tableshape")
types, is_type = _obj_0.types, _obj_0.is_type
end
if not (is_mixins_class) then
is_mixins_class = function(cls)
return rawget(cls, "_mixins_class") == true
end
end
local to_json
to_json = require("lapis.util").to_json
local convert_prop_types
do
local resolve_prop_type
resolve_prop_type = function(fn, ...)
if type(fn) == "function" then
return fn(...)
else
return fn
end
end
convert_prop_types = function(cls, tbl)
local resolved_types
do
local _tbl_0 = { }
for k, v in pairs(tbl) do
_tbl_0[k] = resolve_prop_type(v, k, cls)
end
resolved_types = _tbl_0
end
local t = types.shape(resolved_types, {
check_all = true
})
return types.annotate(t, {
format_error = function(self, value, err)
return tostring(cls.__name) .. ": " .. tostring(err)
end
})
end
end
local RENDER_PROPS_KEY
RENDER_PROPS_KEY = require("lapis.eswidget.prop_types").RENDER_PROPS_KEY
local ESWidget
do
local _class_0
local _parent_0 = Widget
local _base_0 = {
widget_enclosing_element = "div",
render = function(self, ...)
local props = self.props
do
local render_props = self[RENDER_PROPS_KEY]
if render_props then
local helper_scope = setmetatable({ }, {
__index = function(helper_scope, name)
do
local v = self:_find_helper(name)
helper_scope[name] = v
local _ = v
return v
end
end
})
self.props = assert(render_props:transform(helper_scope))
if self.props == helper_scope then
setmetatable(self.props, nil)
end
if props then
for k, v in pairs(props) do
self.props[k] = v
end
end
end
end
_class_0.__parent.__base.render(self, ...)
self.props = props
end,
widget_id = function(self)
if not (self._widget_id) then
self._widget_id = tostring(self.__class:widget_name()) .. "_" .. tostring(math.random(0, 10000000))
end
return self._widget_id
end,
widget_selector = function(self)
return "'#" .. tostring(self:widget_id()) .. "'"
end,
js_init = function(self, widget_params)
if widget_params == nil then
widget_params = nil
end
if not (rawget(self.__class, "es_module")) then
return nil, "widget does not have an @@es_module"
end
local method_name = self.__class:es_module_init_function_name()
if not (method_name) then
return nil, "no init method name"
end
return tostring(method_name) .. "(" .. tostring(self:widget_selector()) .. ", " .. tostring(to_json(widget_params)) .. ");"
end,
widget_enclosing_attributes = function(self)
return {
id = self:widget_id(),
class = {
self.__class:widget_class_list()
}
}
end,
content = function(self, fn)
if fn == nil then
fn = self.inner_content
end
if self.widget_enclosing_element == false then
fn(self)
else
element(self.widget_enclosing_element, self:widget_enclosing_attributes(), function()
return fn(self)
end)
end
return self:render_js_init()
end,
render_js_init = function(self)
local js = self:js_init()
if not (js) then
return
end
if self.layout_opts then
return self:content_for("js_init", function()
raw(js)
if not (js:match(";%s*$")) then
return raw(";")
end
end)
else
return script({
type = "text/javascript"
}, function()
return raw(js)
end)
end
end,
inner_content = function(self) end
}
_base_0.__index = _base_0
setmetatable(_base_0, _parent_0.__base)
_class_0 = setmetatable({
__init = function(self, props, ...)
if self.__class.prop_types then
local state
if is_type(self.__class.prop_types) then
self.props, state = assert(self.__class.prop_types:transform(props or { }))
elseif type(self.__class.prop_types) == "table" then
self.__class.prop_types = convert_prop_types(self.__class, self.__class.prop_types)
self.props, state = assert(self.__class.prop_types:transform(props or { }))
else
self.props, state = error("Got prop_types of unknown type")
end
do
local render_props = state and state[RENDER_PROPS_KEY]
if render_props then
self[RENDER_PROPS_KEY] = convert_prop_types(self.__class, render_props)
end
end
else
return _class_0.__parent.__init(self, props, ...)
end
end,
__base = _base_0,
__name = "ESWidget",
__parent = _parent_0
}, {
__index = function(cls, name)
local val = rawget(_base_0, name)
if val == nil then
local parent = rawget(cls, "__parent")
if parent then
return parent[name]
end
else
return val
end
end,
__call = function(cls, ...)
local _self_0 = setmetatable({}, _base_0)
cls.__init(_self_0, ...)
return _self_0
end
})
_base_0.__class = _class_0
local self = _class_0
self.widget_name = function(self)
return underscore(self.__name or "some_widget")
end
self.widget_class_name = function(self)
return tostring(self:widget_name()) .. "_widget"
end
self.widget_class_list = function(self)
if self == ESWidget then
return
end
if is_mixins_class(self) then
return self.__parent:widget_class_list()
end
return self:widget_class_name(), self.__parent:widget_class_list()
end
self.asset_packages = { }
self.es_module_init_function_name = function(self)
return "init_" .. tostring(self.__name)
end
self.require = function(self, path)
local required = require(path)
if rawget(required, "es_module") then
if not (rawget(self, "es_module")) then
self.es_module = ""
end
local deps = rawget(self, "es_module_dependencies")
if not (deps) then
if self.es_module_dependencies then
deps = {
unpack(self.es_module_dependencies)
}
else
deps = { }
end
self.es_module_dependencies = deps
end
table.insert(deps, path)
end
return required
end
self.compile_es_module = function(self)
local deps = self.es_module_dependencies
if not (rawget(self, "es_module") or deps) then
return nil, "no @@es_module or @@es_module_dependencies"
end
local import_lines = { }
local code_lines = { }
if deps then
for _index_0 = 1, #deps do
local path = deps[_index_0]
local import_path = path:gsub("%.", "/")
table.insert(import_lines, "import \"" .. tostring(import_path) .. "\"")
end
end
if self.es_module then
assert(type(self.es_module) == "string", "@es_module must be a string")
local trim
trim = require("lapis.util").trim
for line in self.es_module:gmatch("([^\r\n]+)") do
local _continue_0 = false
repeat
if line:match("^%s*$") then
_continue_0 = true
break
end
if line:match("^%s*import[^(]") then
table.insert(import_lines, trim(line))
else
table.insert(code_lines, line)
end
_continue_0 = true
until true
if not _continue_0 then
break
end
end
end
return table.concat({
table.concat(import_lines, "\n"),
"window." .. tostring(self:es_module_init_function_name()) .. " = function(widget_selector, widget_params) {",
table.concat(code_lines, "\n"),
"}"
}, "\n")
end
if _parent_0.__inherited then
_parent_0.__inherited(_parent_0, _class_0)
end
ESWidget = _class_0
return _class_0
end