parseevent.lua

main 198 lines · 6.3 KB Raw
Alex Alex Commit Client Upload Part 2 19/10/2026 00:06
1local EVENTS_TRANSLATED = {
2 onstyleapply = 'onStyleApply',
3 ondestroy = 'onDestroy',
4 onidchange = 'onIdChange',
5 onwidthchange = 'onWidthChange',
6 onheightchange = 'onHeightChange',
7 onresize = 'onResize',
8 onenabled = 'onEnabled',
9 onpropertychange = 'onPropertyChange',
10 ongeometrychange = 'onGeometryChange',
11 onlayoutupdate = 'onLayoutUpdate',
12 onfocus = 'onFocusChange',
13 onchildfocus = 'onChildFocusChange',
14 onhover = 'onHoverChange',
15 onvisibility = 'onVisibilityChange',
16 ondragenter = 'onDragEnter',
17 ondragleave = 'onDragLeave',
18 ondragmove = 'onDragMove',
19 ondrop = 'onDrop',
20 onkeytext = 'onKeyText',
21 onkeydown = 'onKeyDown',
22 onkeypress = 'onKeyPress',
23 onkeyup = 'onKeyUp',
24 onmousepress = 'onMousePress',
25 onmouserelease = 'onMouseRelease',
26 onmousemove = 'onMouseMove',
27 onmousewheel = 'onMouseWheel',
28 onclick = 'onClick',
29 ondoubleclick = 'onDoubleClick',
30 oncreate = 'onCreate',
31 onsetup = 'onSetup',
32 ontextareaupdate = 'onTextAreaUpdate',
33 onfontchange = 'onFontChange',
34 ontextchange = 'onTextChange',
35}
36
37local parseEvents = function(el, widget, eventName, callStr, controller)
38 local eventCall = loadstring('return function(self, event) ' .. callStr .. ' end')()
39 local event = { target = widget }
40 local function execEventCall()
41 eventCall(controller, event)
42 end
43
44 if eventName == 'onchange' then
45 if widget.__class == 'UIComboBox' then
46 controller:registerEvents(widget, {
47 onOptionChange = function(widget, text, data)
48 event.name = 'onOptionChange'
49 event.text = text
50 event.data = data
51 execEventCall()
52 end
53 })
54 elseif widget.__class == 'UIRadioGroup' then
55 controller:registerEvents(widget, {
56 onSelectionChange = function(widget, selectedWidget, previousSelectedWidget)
57 event.name = 'onSelectionChange'
58 event.selectedWidget = selectedWidget
59 event.previousSelectedWidget = previousSelectedWidget
60 execEventCall()
61 end
62 })
63 elseif widget.__class == 'UICheckBox' then
64 controller:registerEvents(widget, {
65 onCheckChange = function(widget, checked)
66 event.name = 'onCheckChange'
67 event.checked = checked
68 execEventCall()
69 end
70 })
71 elseif widget.__class == 'UIScrollBar' then
72 controller:registerEvents(widget, {
73 onValueChange = function(widget, value, delta)
74 event.name = 'onValueChange'
75 event.value = value
76 event.delta = delta
77 execEventCall()
78 end
79 })
80 elseif widget.setValue then
81 controller:registerEvents(widget, {
82 onValueChange = function(widget, value)
83 event.name = 'onValueChange'
84 event.value = value
85 execEventCall()
86 end
87 })
88 else
89 controller:registerEvents(widget, {
90 onTextChange = function(widget, value)
91 event.name = 'onTextChange'
92 event.value = value
93 execEventCall()
94 end
95 })
96 end
97
98 return
99 end
100
101 local trEventName = EVENTS_TRANSLATED[eventName]
102 if not trEventName then
103 pwarning('[' .. HTML_PATH .. ']:' .. el.name .. ' Event ' .. eventName .. ' does not exist.')
104 return
105 end
106
107 local data = {}
108 data[trEventName] = function(widget, value)
109 event.name = trEventName
110 event.value = value
111 execEventCall()
112 end
113
114 controller:registerEvents(widget, data)
115end
116
117local onCreateWidget = function(el, widget, controller)
118 local getFncSet = function(exp)
119 local f = loadstring('return function(self, value) ' .. exp .. '=value end')
120 return f and f() or nil
121 end
122
123 if el.attributes['*checked'] then
124 local set = getFncSet(el.attributes['*checked'])
125 if set then
126 controller:registerEvents(widget, {
127 onCheckChange = function(widget, value)
128 set(controller, value)
129 end
130 })
131 end
132 end
133
134 if el.attributes['*value'] then
135 local set = getFncSet(el.attributes['*value'])
136 if set then
137 if widget.getCurrentOption then
138 controller:registerEvents(widget, {
139 onOptionChange = function(widget, text, data)
140 set(controller, data)
141 end
142 })
143 else
144 controller:registerEvents(widget, {
145 onTextChange = function(widget, value)
146 set(controller, value)
147 end
148 })
149 end
150 end
151 end
152end
153
154local function setText(el, text)
155 if text then
156 local whiteSpace = el.style and el.style['white-space'] or 'nowrap'
157 el.widget:setTextWrap(true)
158 if whiteSpace == 'normal' then
159 text = text:trim()
160 elseif whiteSpace == 'nowrap' then
161 text = text:gsub(" ", ""):gsub("[\n\r\t]", " ")
162 el.widget:setTextWrap(false)
163 elseif whiteSpace == 'pre' then
164 el.widget:setTextWrap(false)
165 end
166
167 el.widget:setText(text)
168 end
169end
170
171local createRadioGroup = function(el, groups, controller)
172 local name = el.attributes.name
173 if not name then
174 return
175 end
176
177 if not groups[name] then
178 groups[name] = UIRadioGroup.create()
179 end
180
181 groups[name]:addWidget(el.widget)
182end
183
184local function afterLoadElement(el)
185 if el.name == 'hr' then
186 if el.widget:hasAnchoredLayout() then
187 el.widget:addAnchor(AnchorLeft, 'parent', AnchorLeft)
188 el.widget:addAnchor(AnchorRight, 'parent', AnchorRight)
189 end
190 end
191
192 if #el.nodes == 0 then
193 setText(el, el:getcontent())
194 end
195end
196
197return parseEvents, onCreateWidget, setText, createRadioGroup, afterLoadElement
198
Top