1
+ // Allow to use the Rails UJS events
2
+ // UJS code source: https://github.com/rails/rails/blob/main/actionview/app/assets/javascripts/rails-ujs.js
3
+ export type WindowWithRailsUJSEventMap = WindowEventMap & {
4
+ "rails:attachBindings" : CustomEvent
5
+ "ujs:everythingStopped" : CustomEvent
6
+ confirm : CustomEvent
7
+ "confirm:complete" : CustomEvent < [ boolean ] >
8
+ "ajax:before" : CustomEvent
9
+ "ajax:stopped" : CustomEvent
10
+ "ajax:beforeSend" : CustomEvent <
11
+ [
12
+ XMLHttpRequest ,
13
+ {
14
+ url : string
15
+ type : string
16
+ data : string
17
+ dataType : string
18
+ accept : string
19
+ }
20
+ ]
21
+ >
22
+ "ajax:send" : CustomEvent < [ XMLHttpRequest ] >
23
+ // response has to be casted afterwards by the event handler
24
+ "ajax:success" : CustomEvent <
25
+ [ ( response : unknown , textStatus : string , xhr : XMLHttpRequest ) => void ]
26
+ >
27
+ "ajax:error" : CustomEvent <
28
+ [ ( response : unknown , textStatus : string , xhr : XMLHttpRequest ) => void ]
29
+ >
30
+ "ajax:complete" : CustomEvent <
31
+ [ ( xhr : XMLHttpRequest , textStatus : string ) => void ]
32
+ >
33
+ }
34
+
1
35
export interface EnhancedHTMLElement {
2
36
isEnhancedHTMLElement : true
3
- on : < K extends keyof WindowEventMap > (
37
+ on : < K extends keyof WindowWithRailsUJSEventMap > (
4
38
this : HTMLElement & EnhancedHTMLElement ,
5
39
type : K ,
6
- callback : ( event : WindowEventMap [ K ] ) => void ,
40
+ callback : ( event : WindowWithRailsUJSEventMap [ K ] ) => void ,
7
41
options ?: AddEventListenerOptions
8
42
) => ( ) => void
9
- onDelegate : < K extends keyof WindowEventMap > (
43
+ onDelegate : < K extends keyof WindowWithRailsUJSEventMap > (
10
44
this : HTMLElement & EnhancedHTMLElement ,
11
45
childSelector : string ,
12
46
type : K ,
13
- callback : ( event : WindowEventMap [ K ] ) => void ,
47
+ callback : ( event : WindowWithRailsUJSEventMap [ K ] ) => void ,
14
48
options ?: AddEventListenerOptions
15
49
) => ( ) => void
16
50
query : typeof query
@@ -31,7 +65,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
31
65
on ( type , callback , options ) {
32
66
const attachedCallback = ( e : Event ) => {
33
67
// wrapped in a function to mimic the once configuration of the native option, which is not well supported (IE 11)
34
- callback . call ( e . target , e as WindowEventMap [ typeof type ] )
68
+ callback . call ( e . target , e as WindowWithRailsUJSEventMap [ typeof type ] )
35
69
if ( options && options . once ) {
36
70
this . removeEventListener ( type , attachedCallback )
37
71
}
@@ -54,7 +88,7 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
54
88
55
89
if ( target ) {
56
90
overrideEventCurrentTarget ( e , target )
57
- callback . call ( target , e as WindowEventMap [ typeof type ] )
91
+ callback . call ( target , e as WindowWithRailsUJSEventMap [ typeof type ] )
58
92
}
59
93
}
60
94
@@ -73,17 +107,17 @@ const enhancedHTMLElementImpl: EnhancedHTMLElement = {
73
107
74
108
export interface EnhancedHTMLElementList {
75
109
isEnhancedHTMLElementList : true
76
- on : < K extends keyof WindowEventMap > (
110
+ on : < K extends keyof WindowWithRailsUJSEventMap > (
77
111
this : ( HTMLElement & EnhancedHTMLElement ) [ ] & EnhancedHTMLElementList ,
78
112
type : K ,
79
- callback : ( event : WindowEventMap [ K ] ) => void ,
113
+ callback : ( event : WindowWithRailsUJSEventMap [ K ] ) => void ,
80
114
options ?: AddEventListenerOptions
81
115
) => ( ) => void
82
- onDelegate : < K extends keyof WindowEventMap > (
116
+ onDelegate : < K extends keyof WindowWithRailsUJSEventMap > (
83
117
this : ( HTMLElement & EnhancedHTMLElement ) [ ] & EnhancedHTMLElementList ,
84
118
childSelector : string ,
85
119
type : K ,
86
- callback : ( event : WindowEventMap [ K ] ) => void ,
120
+ callback : ( event : WindowWithRailsUJSEventMap [ K ] ) => void ,
87
121
options ?: AddEventListenerOptions
88
122
) => ( ) => void
89
123
}
0 commit comments