@@ -22,15 +22,23 @@ export class LiveTemplateElement extends HTMLElement {
22
22
}
23
23
this . connectLiveState ( ) ;
24
24
}
25
+ directive . sendclick = this . sendEventDirective ( 'click' ) ;
26
+ directive . sendsubmit = this . sendEventDirective ( 'submit' ) ;
27
+ directive . sendinput = this . sendEventDirective ( 'input' ) ;
28
+ if ( this . getAttribute ( 'custom-events' ) ) {
29
+ const customEvents = this . getAttribute ( 'custom-events' ) . split ( ',' ) ;
30
+ for ( const customEvent of customEvents ) {
31
+ console . log ( 'adding directive for' , customEvent ) ;
32
+ directive [ `send${ customEvent } ` ] = this . sendEventDirective ( customEvent ) ;
33
+ }
34
+ }
25
35
}
26
36
27
37
connectLiveState ( ) {
28
38
this . liveState . connect ( ) ;
29
39
this . liveState . addEventListener ( 'livestate-change' , ( { detail : { state } } ) => {
40
+ console . log ( 'got state' , state ) ;
30
41
this . buildTemplate ( ) ;
31
- directive . sendclick = this . sendEventDirective ( 'click' ) ;
32
- directive . sendsubmit = this . sendEventDirective ( 'submit' ) ;
33
- directive . sendinput = this . sendEventDirective ( 'input' ) ;
34
42
sprae ( this , { ...state , sendEvent : ( n ) => ( e ) => this . sendEvent ( n , e ) } ) ;
35
43
} ) ;
36
44
}
@@ -57,18 +65,22 @@ export class LiveTemplateElement extends HTMLElement {
57
65
dir . parse = ( value ) => value ;
58
66
return dir ;
59
67
}
60
-
68
+
61
69
sendEvent ( eventName , e ) {
62
70
if ( e instanceof SubmitEvent ) {
63
71
const form = e . target ;
64
72
const formData = new FormData ( form ) ;
65
73
const data = Object . fromEntries ( formData . entries ( ) ) ;
66
74
this [ 'liveState' ] . pushEvent ( eventName , data ) ;
67
75
} else if ( e instanceof InputEvent ) {
68
- const form = e . target . form ;
69
- const formData = new FormData ( form ) ;
70
- const data = Object . fromEntries ( formData . entries ( ) ) ;
71
- this [ 'liveState' ] . pushEvent ( eventName , data ) ;
76
+ const input = e . target ;
77
+ const payload = { } ;
78
+ payload [ input . getAttribute ( 'name' ) ] = input . value ;
79
+ this [ 'liveState' ] . pushEvent ( eventName , payload ) ;
80
+ } else if ( e instanceof CustomEvent ) {
81
+ const detail = e . detail ;
82
+ const payload = Object . assign ( { } , e . target . dataset , detail ) ;
83
+ this [ 'liveState' ] . pushEvent ( eventName , payload ) ;
72
84
} else {
73
85
this [ 'liveState' ] . pushEvent ( eventName , e . target . dataset )
74
86
}
0 commit comments