-
-
Notifications
You must be signed in to change notification settings - Fork 162
/
Copy pathComponent.mjs
297 lines (255 loc) · 9.05 KB
/
Component.mjs
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
288
289
290
291
292
293
294
295
296
297
import Base from './Base.mjs';
import {resolveCallback} from '../util/Function.mjs';
import Logger from '../util/Logger.mjs';
/**
* @class Neo.controller.Component
* @extends Neo.controller.Base
*/
class Component extends Base {
static config = {
/**
* @member {String} className='Neo.controller.Component'
* @protected
*/
className: 'Neo.controller.Component',
/**
* @member {String} ntype='component-controller'
* @protected
*/
ntype: 'component-controller',
/**
* @member {Neo.component.Base|null} component=null
* @protected
*/
component: null,
/**
* @member {Neo.controller.Component|null} parent_=null
*/
parent_: null,
/**
* @member {Object} references=null
* @protected
*/
references: null,
/**
* @member {Number|null} windowId=null
*/
windowId: null
}
/**
* @param {Object} config
*/
construct(config) {
super.construct(config);
let me = this,
{component} = me;
me.references = {};
if (component.isConstructed) {
me.onComponentConstructed()
} else {
component.on('constructed', () => {
me.onComponentConstructed()
}, me, {once: true})
}
}
/**
* Triggered before the parent config gets changed
* @param {Neo.controller.Component|null} value
* @param {Neo.controller.Component|null} oldValue
* @protected
*/
beforeSetParent(value, oldValue) {
return value || this.getParent()
}
/**
* @param {String} handlerName
* @param {Neo.component.Base} [component]
* @returns {Neo.controller.Component|Boolean|null}
*/
getHandlerScope(handlerName, component) {
let me = this,
{parent} = me;
if (component) {
// Look for ths function *name* first in the Component itself.
// If we find it, return true so calling code knows not to continue to search.
const handlerCb = resolveCallback(handlerName, component);
// Handler fn is resolved in the Component or its own parent chain.
// Return a status indicating that we do not need an early binding
if (handlerCb.fn) {
return true
}
}
return Neo.isFunction(me[handlerName]) ?
me : parent?.getHandlerScope(handlerName) || null
}
/**
* Get the closest controller inside the components parent tree
* @returns {Neo.controller.Component|null}
*/
getParent() {
let me = this,
{parent} = me;
if (parent) {
return parent
}
return me.component.parent?.getController() || null
}
/**
* todo: update changed references (e.g. container.remove() then container.add() using the same key)
* @param {String} name
* @returns {*}
*/
getReference(name) {
let me = this,
component = me.references[name];
if (!component) {
component = me.component.down({reference: name});
if (component) {
me.references[name] = component
}
}
return component || null
}
/**
* Convenience shortcut
* @param args
* @returns {*}
*/
getState(...args) {
return this.getStateProvider().getData(...args)
}
/**
* sameLevelOnly=false will return the closest stateProvider inside the component parent tree,
* in case there is none on the same level.
* @param {Boolean} [sameLevelOnly=false]
*/
getStateProvider(sameLevelOnly=false) {
let {component} = this;
return sameLevelOnly ? component.stateProvider : component.getStateProvider()
}
/**
* Convenience shortcut for accessing state.Provider based data.Stores
* @param {String} key
* @returns {Neo.data.Store}
*/
getStore(key) {
return this.getStateProvider().getStore(key)
}
/**
* Override this method inside your view controllers as a starting point in case you need references
* (instead of using onConstructed() inside your controller)
*/
onComponentConstructed() {}
/**
* @param {Neo.component.Base} component=this.component
*/
parseConfig(component=this.component) {
let me = this,
{handler, listeners, reference, renderer, validator} = component,
eventHandler, handlerScope;
if (handler && Neo.isString(handler)) {
handlerScope = me.getHandlerScope(handler, component);
// If the handler name was not resolved in the Component itself, bind it
if (handlerScope !== true) {
component.handler = handlerScope[handler].bind(component.handlerScope || handlerScope);
}
}
listeners && Object.entries(listeners).forEach(([key, value]) => {
if (key !== 'scope' && key !== 'delegate') {
if (Neo.isString(value)) {
eventHandler = value;
handlerScope = me.getHandlerScope(eventHandler, component);
if (!handlerScope) {
Logger.logError('Unknown event handler for', eventHandler, component)
} else if (handlerScope !== true) {
listeners[key] = {};
listeners[key].fn = handlerScope[eventHandler].bind(handlerScope)
}
} else {
value?.forEach?.(listener => {
if (Neo.isObject(listener) && listener.hasOwnProperty('fn') && Neo.isString(listener.fn)) {
eventHandler = listener.fn;
handlerScope = me.getHandlerScope(eventHandler, component);
if (!handlerScope) {
console.error('Unknown event handler for', eventHandler, component)
} else if (handlerScope !== true) {
listener.fn = handlerScope[eventHandler].bind(handlerScope)
}
}
})
}
}
});
if (renderer && Neo.isString(renderer)) {
handlerScope = me.getHandlerScope(renderer);
if (!handlerScope) {
Logger.logError('Unknown renderer for', component.id, component)
} else {
component.renderer = handlerScope[renderer].bind(handlerScope)
}
}
if (validator && Neo.isString(validator)) {
handlerScope = me.getHandlerScope(validator);
if (!handlerScope) {
Logger.logError('Unknown validator for', component.id, component)
} else {
component.validator = handlerScope[validator].bind(handlerScope)
}
}
if (reference) {
me.references[reference] = component
}
}
/**
* @param {Neo.component.Base} component=this.component
*/
parseDomListeners(component=this.component) {
let me = this,
{domListeners} = component,
eventHandler, scope;
domListeners?.forEach(domListener => {
Object.entries(domListener).forEach(([key, value]) => {
eventHandler = null;
if (key !== 'scope' && key !== 'delegate') {
if (Neo.isString(value)) {
eventHandler = value;
} else if (Neo.isObject(value) && value.hasOwnProperty('fn') && Neo.isString(value.fn)) {
eventHandler = value.fn;
}
if (eventHandler) {
scope = me.getHandlerScope(eventHandler);
// There can be string based listeners like 'up.onClick', which will resolved inside manager.DomEvents
// => Do nothing in case there is no match inside the controller hierarchy.
if (scope) {
domListener[key] = scope[eventHandler].bind(scope)
}
}
}
})
})
}
/**
* Will get called by component.Base: destroy() in case the component has a reference config
* @param {Neo.component.Base} component
*/
removeReference(component) {
let me = this,
{references} = me,
key;
for (key in references) {
if (component === references[key]) {
delete references[key];
break
}
}
me.getParent()?.removeReference(component)
}
/**
* Convenience shortcut
* @param args
*/
setState(...args) {
this.getStateProvider().setData(...args)
}
}
export default Neo.setupClass(Component);