Skip to content

Commit

Permalink
Simplify field definitions for custom events. Allow using LWScriptCal…
Browse files Browse the repository at this point in the history
…lback (with debounce etc) as handler.
  • Loading branch information
tatut committed Jan 20, 2024
1 parent a50fce5 commit 47e3eb4
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
17 changes: 10 additions & 7 deletions src/LiveWeb-Core/LWCustomElement.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,17 @@ LWCustomElement >> renderCustomEvents: h [
h script: [
h raw: 'window.addEventListener("load",function(){ const '; raw: c; raw: ' = _lw.get('; raw: id asString; raw: ');'.
eventListeners do: [ :e |
| cb event callback |
| cb event callback block |
event := e key.
callback := e value.
cb := ctx registerCallback: [ :d | |evt|
evt := event fromDictionary: d.
callback value: evt ] for: self.
h raw: c; raw: '.addEventListener("'; raw: event type; raw: '",e=>_lws(';
raw: cb asString; raw: ', ['; raw: event eventJS; raw: ']));'
callback := e value asLWScriptCallback.
callback jsParams: { '[{1}]' format: {(',' join: (event eventFields collect: #value))} }.
block := callback callback.
cb := ctx registerCallback: [ :arr | |evt|
evt := event fromArray: arr.
block value: evt ] for: self.
h raw: c; raw: '.addEventListener("'; raw: event type; raw: '",e=>';
raw: (callback asJS: cb);
raw: ');'
].
h raw: '})'
]
Expand Down
14 changes: 3 additions & 11 deletions src/LiveWeb-Core/LWCustomEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,12 @@ LWCustomEvent class >> eventFields [
self subclassResponsibility
]

{ #category : #'as yet unclassified' }
LWCustomEvent class >> eventJS [
"Build JS to create the event object in the browser side, using the event 'e'.
For example: ^ '{''value'': e.target.value}'
"
self subclassResponsibility.
]

{ #category : #'instance creation' }
LWCustomEvent class >> fromDictionary: d [
LWCustomEvent class >> fromArray: arr [
| e |
e := self new.
self eventFields do: [ :f |
e at: f put: (d at: f)
self eventFields doWithIndex: [ :f :i |
e at: f key put: (arr at: i)
].
^ e
]
Expand Down
3 changes: 2 additions & 1 deletion src/LiveWeb-Core/LWScriptCallback.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ LWScriptCallback >> asJS: cb [

{ #category : #converting }
LWScriptCallback >> asLWScriptCallback [
^ self
^ self

]

{ #category : #accessing }
Expand Down
3 changes: 2 additions & 1 deletion src/LiveWeb-Developer/LWDevMain.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ LWDevMain >> initialize [
spotterDialog := SlDialog new
className: 'spotter';
label: 'Jump to class';
add: (LWBlockContainer new block: [ :h | h input: { #placeholder -> 'type classname part' } ]);
add: (SlInput new placeholder: 'type class name'; autofocus: true;
on: SlInputEvent do: (Js debounced: [ :e | e inspect ] wait: (Duration milliSeconds: 250)));
yourself.

]
Expand Down
7 changes: 1 addition & 6 deletions src/LiveWeb-Shoelace/SlChangeEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,13 @@ SlChangeEvent class >> eventFields [
^ #( 'value' )
]

{ #category : #'as yet unclassified' }
SlChangeEvent class >> eventJS [
^ '{"value": e.target.value}'
]

{ #category : #accessing }
SlChangeEvent class >> type [
^ 'sl-change'
]

{ #category : #evaluating }
SlChangeEvent >> value [
^ self at: 'value'
^ self at: #value

]
5 changes: 0 additions & 5 deletions src/LiveWeb-Shoelace/SlCheckboxEvent.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ SlCheckboxEvent class >> eventFields [
^ #('checked')
]

{ #category : #'as yet unclassified' }
SlCheckboxEvent class >> eventJS [
^'{"checked":e.target.checked}'
]

{ #category : #accessing }
SlCheckboxEvent class >> type [
^'sl-change'
Expand Down
20 changes: 20 additions & 0 deletions src/LiveWeb-Shoelace/SlInputEvent.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"
I am a form input event. I provide the current value of the target.
"
Class {
#name : #SlInputEvent,
#superclass : #LWCustomEvent,
#category : #'LiveWeb-Shoelace'
}

{ #category : #accessing }
SlInputEvent class >> type [
^ 'sl-input'

]

{ #category : #evaluating }
SlInputEvent >> value [
^ self at: #value

]

0 comments on commit 47e3eb4

Please sign in to comment.