1
1
import { class_create } from '@utils/class' ;
2
- import { DomObjectTransport } from './DomObjectTransport' ;
2
+ import { DomObjectTransport , IDomWay , IObjectWay } from './DomObjectTransport' ;
3
3
import { signal_parse } from './utils/signal' ;
4
4
import { log_error , log_warn } from '@core/util/reporters' ;
5
5
@@ -18,23 +18,57 @@ import { Component } from '@compo/exports';
18
18
19
19
export const CustomProviders = { } ;
20
20
21
- export const BindingProvider = class_create ( {
22
- validations : null ,
23
- constructor : function BindingProvider ( model , element , ctr , bindingType ) {
21
+ export class BindingProvider {
22
+ validations = null
23
+ ctx = null
24
+
25
+ value : string
26
+ property : string
27
+
28
+ expression : string
29
+
30
+ domSetter : string
31
+ domGetter : string
32
+ objSetter : string
33
+ objGetter : string
34
+ mapToObj : string
35
+ mapToDom : string
36
+ changeEvent : string
37
+ typeof : string
38
+
39
+ slots : any
40
+ pipes : any
41
+
42
+ parent : any
43
+
44
+
45
+ dismiss : number = 0
46
+ bindingType : string
47
+ log = false
48
+ logExpression : string
49
+ signal_domChanged : string
50
+ signal_objectChanged : string
51
+
52
+ pipe_domChanged : { pipe : string , signal : string }
53
+ pipe_objectChanged : { pipe : string , signal : string }
54
+ locked = false
55
+
56
+ domWay : IDomWay = DomObjectTransport . domWay
57
+ objectWay : IObjectWay = DomObjectTransport . objectWay
58
+
59
+ binder : Function
60
+
61
+ constructor ( public model , public element : HTMLElement , public ctr , bindingType ?: string ) {
24
62
if ( bindingType == null ) {
25
63
bindingType = 'dual' ;
26
64
27
- var name = ctr . compoName ;
65
+ let name = ctr . compoName ;
28
66
if ( name === ':bind' || name === 'bind' ) {
29
67
bindingType = 'single' ;
30
68
}
31
69
}
32
- var attr = ctr . attr ;
70
+ let attr = ctr . attr ;
33
71
34
- this . ctr = ctr ;
35
- this . ctx = null ;
36
- this . model = model ;
37
- this . element = element ;
38
72
this . value = attr . value ;
39
73
this . property = attr . property ;
40
74
this . domSetter = attr [ 'dom-setter' ] || attr . setter ;
@@ -48,14 +82,9 @@ export const BindingProvider = class_create({
48
82
/* Convert to an instance, e.g. Number, on domchange event */
49
83
this [ 'typeof' ] = attr [ 'typeof' ] || null ;
50
84
51
- this . dismiss = 0 ;
52
85
this . bindingType = bindingType ;
53
- this . log = false ;
54
- this . signal_domChanged = null ;
55
- this . signal_objectChanged = null ;
56
- this . locked = false ;
57
-
58
- var isCompoBinder = ctr . node . parent . tagName === ctr . parent . compoName ;
86
+
87
+ let isCompoBinder = ctr . node . parent . tagName === ctr . parent . compoName ;
59
88
if (
60
89
isCompoBinder &&
61
90
( element . nodeType !== 1 || element . tagName !== 'INPUT' )
@@ -88,14 +117,14 @@ export const BindingProvider = class_create({
88
117
this . domWay = x . domWay ;
89
118
break ;
90
119
}
91
- this . changeEvent = attr [ 'change-event' ] || 'input' ;
120
+ this . changeEvent = attr [ 'change-event' ] || 'change, input' ;
92
121
this . property = 'element.value' ;
93
122
break ;
94
123
case 'TEXTAREA' :
95
124
this . property = 'element.value' ;
96
125
break ;
97
126
case 'SELECT' :
98
- this . domWay = element . multiple
127
+ this . domWay = ( element as HTMLSelectElement ) . multiple
99
128
? DomObjectTransport . SELECT_MULT
100
129
: DomObjectTransport . SELECT ;
101
130
break ;
@@ -191,11 +220,11 @@ export const BindingProvider = class_create({
191
220
}
192
221
193
222
this . expression = this . value ;
194
- } ,
195
- dispose : function ( ) {
223
+ }
224
+ dispose ( ) {
196
225
expression_unbind ( this . expression , this . model , this . ctr , this . binder ) ;
197
- } ,
198
- objectChanged : function ( x ) {
226
+ }
227
+ objectChanged ( val ? ) {
199
228
if ( this . dismiss -- > 0 ) {
200
229
return ;
201
230
}
@@ -205,61 +234,61 @@ export const BindingProvider = class_create({
205
234
}
206
235
this . locked = true ;
207
236
208
- if ( x == null || this . objGetter != null ) {
209
- x = this . objectWay . get ( this , this . expression ) ;
237
+ if ( val == null || this . objGetter != null ) {
238
+ val = this . objectWay . get ( this , this . expression ) ;
210
239
}
211
240
if ( this . mapToDom != null ) {
212
- x = expression_callFn ( this . mapToDom , this . model , null , this . ctr , [
213
- x
241
+ val = expression_callFn ( this . mapToDom , this . model , null , this . ctr , [
242
+ val
214
243
] ) ;
215
244
}
216
245
217
- this . domWay . set ( this , x ) ;
246
+ this . domWay . set ( this , val ) ;
218
247
219
248
if ( this . log ) {
220
- console . log ( '[BindingProvider] objectChanged -' , x ) ;
249
+ console . log ( '[BindingProvider] objectChanged -' , val ) ;
221
250
}
222
251
if ( this . signal_objectChanged ) {
223
252
Component . signal . emitOut (
224
253
this . ctr ,
225
254
this . signal_objectChanged ,
226
255
this . ctr ,
227
- [ x ]
256
+ [ val ]
228
257
) ;
229
258
}
230
- if ( this . pipe_objectChanged ) {
259
+ if ( this . pipe_objectChanged != null ) {
231
260
var pipe = this . pipe_objectChanged ;
232
261
Component . pipe ( pipe . pipe ) . emit ( pipe . signal ) ;
233
- }
234
-
262
+ }
235
263
this . locked = false ;
236
- } ,
237
- domChanged : function ( event , val_ ) {
264
+ }
265
+
266
+ domChanged ( event ?, val ?) {
238
267
if ( this . locked === true ) {
239
268
log_warn ( 'Concurance change detected' , this ) ;
240
269
return ;
241
270
}
242
271
this . locked = true ;
243
272
244
- var value = val_ ;
245
- if ( value == null ) value = this . domWay . get ( this ) ;
246
-
247
- var typeof_ = this [ 'typeof' ] ;
273
+ if ( val == null ) {
274
+ val = this . domWay . get ( this ) ;
275
+ }
276
+ let typeof_ = this [ 'typeof' ] ;
248
277
if ( typeof_ != null ) {
249
- var Converter = window [ typeof_ ] ;
250
- value = Converter ( value ) ;
278
+ let Converter = window [ typeof_ ] ;
279
+ val = Converter ( val ) ;
251
280
}
252
281
if ( this . mapToObj != null ) {
253
- value = expression_callFn (
282
+ val = expression_callFn (
254
283
this . mapToObj ,
255
284
this . model ,
256
285
null ,
257
286
this . ctr ,
258
- [ value ]
287
+ [ val ]
259
288
) ;
260
289
}
261
290
262
- var error = this . validate ( value ) ;
291
+ var error = this . validate ( val ) ;
263
292
if ( error == null ) {
264
293
this . dismiss = 1 ;
265
294
@@ -272,19 +301,19 @@ export const BindingProvider = class_create({
272
301
if ( tuple != null ) {
273
302
var obj = tuple [ 0 ] ,
274
303
prop = tuple [ 1 ] ;
275
- this . objectWay . set ( obj , prop , value , this ) ;
304
+ this . objectWay . set ( obj , prop , val , this ) ;
276
305
}
277
306
278
307
this . dismiss = 0 ;
279
308
if ( this . log ) {
280
- console . log ( '[BindingProvider] domChanged -' , value ) ;
309
+ console . log ( '[BindingProvider] domChanged -' , val ) ;
281
310
}
282
311
if ( this . signal_domChanged != null ) {
283
312
Component . signal . emitOut (
284
313
this . ctr ,
285
314
this . signal_domChanged ,
286
315
this . ctr ,
287
- [ value ]
316
+ [ val ]
288
317
) ;
289
318
}
290
319
if ( this . pipe_domChanged != null ) {
@@ -293,8 +322,8 @@ export const BindingProvider = class_create({
293
322
}
294
323
}
295
324
this . locked = false ;
296
- } ,
297
- addValidation : function ( mix ) {
325
+ }
326
+ addValidation ( mix ) {
298
327
if ( this . validations == null ) {
299
328
this . validations = [ ] ;
300
329
}
@@ -303,8 +332,8 @@ export const BindingProvider = class_create({
303
332
return ;
304
333
}
305
334
this . validations . push ( mix ) ;
306
- } ,
307
- validate : function ( val ) {
335
+ }
336
+ validate ( val ) {
308
337
var fns = this . validations ,
309
338
ctr = this . ctr ,
310
339
el = this . element ;
@@ -320,13 +349,9 @@ export const BindingProvider = class_create({
320
349
el ,
321
350
this . objectChanged . bind ( this )
322
351
) ;
323
- } ,
324
- objectWay : DomObjectTransport . objectWay ,
325
- domWay : DomObjectTransport . domWay
326
- } ) ;
352
+ }
327
353
328
- export const BindingProviderStatics = {
329
- create : function ( model , el , ctr , bindingType ?) {
354
+ static create ( model , el , ctr , bindingType ?) {
330
355
/* Initialize custom provider */
331
356
var type = ctr . attr . bindingProvider ,
332
357
CustomProvider = type == null ? null : CustomProviders [ type ] ,
@@ -342,14 +367,15 @@ export const BindingProviderStatics = {
342
367
obj_extend ( provider , CustomProvider ) ;
343
368
}
344
369
return provider ;
345
- } ,
370
+ }
346
371
347
- bind : function ( provider ) {
372
+ static bind ( provider ) {
348
373
return apply_bind ( provider ) ;
349
374
}
350
- } ;
375
+ }
351
376
352
- function apply_bind ( provider ) {
377
+
378
+ function apply_bind ( provider : BindingProvider ) {
353
379
var expr = provider . expression ,
354
380
model = provider . model ,
355
381
onObjChanged = ( provider . objectChanged = provider . objectChanged . bind (
@@ -372,9 +398,16 @@ function apply_bind(provider) {
372
398
if ( ! attr [ 'dom-slot' ] && ! attr [ 'change-pipe-event' ] ) {
373
399
var element = provider . element ,
374
400
eventType = provider . changeEvent ,
375
- onDomChange = provider . domChanged . bind ( provider ) ;
401
+ onDomChange = provider . domChanged . bind ( provider ) ,
402
+ doListen = Component . Dom . addEventListener ;
376
403
377
- Component . Dom . addEventListener ( element , eventType , onDomChange ) ;
404
+ if ( eventType . indexOf ( ',' ) !== - 1 ) {
405
+ let arr = eventType . split ( ',' ) ;
406
+ for ( let i = 0 ; i < arr . length ; i ++ ) {
407
+ doListen ( element , arr [ i ] . trim ( ) , onDomChange ) ;
408
+ }
409
+ }
410
+ doListen ( element , eventType , onDomChange ) ;
378
411
}
379
412
380
413
if ( provider . objectWay . get ( provider , provider . expression ) == null ) {
0 commit comments