@@ -11,7 +11,7 @@ class RemoteInputElement extends HTMLElement {
11
11
12
12
attributeChangedCallback ( name : string , oldValue : string ) {
13
13
if ( oldValue && name === 'src' ) {
14
- this . fetchResults ( false )
14
+ fetchResults ( this , false )
15
15
}
16
16
}
17
17
@@ -22,8 +22,8 @@ class RemoteInputElement extends HTMLElement {
22
22
input . setAttribute ( 'autocomplete' , 'off' )
23
23
input . setAttribute ( 'spellcheck' , 'false' )
24
24
25
- this . debounceInputChange = debounce ( this . fetchResults . bind ( this ) )
26
- this . boundFetchResults = this . fetchResults . bind ( this )
25
+ this . debounceInputChange = debounce ( ( ) => fetchResults ( this ) )
26
+ this . boundFetchResults = ( ) => fetchResults ( this )
27
27
input . addEventListener ( 'focus' , this . boundFetchResults )
28
28
input . addEventListener ( 'change' , this . boundFetchResults )
29
29
input . addEventListener ( 'input' , this . debounceInputChange )
@@ -43,53 +43,42 @@ class RemoteInputElement extends HTMLElement {
43
43
return input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement ? input : null
44
44
}
45
45
46
- get resultsContainer ( ) : ?HTMLElement {
47
- return document . getElementById ( this . getAttribute ( 'aria-owns' ) || '' )
48
- }
49
-
50
46
get src ( ) : string {
51
47
return this . getAttribute ( 'src' ) || ''
52
48
}
53
49
54
50
set src ( url : string ) {
55
51
this . setAttribute ( 'src' , url )
56
52
}
53
+ }
57
54
58
- get name ( ) : string {
59
- return this . getAttribute ( 'name' ) || 'q'
60
- }
61
-
62
- set name ( name : string ) {
63
- this . setAttribute ( 'name' , name )
64
- }
65
-
66
- async fetchResults ( checkCurrentQuery : boolean = true ) {
67
- if ( ! this . input ) return
68
- const query = this . input . value
69
- if ( checkCurrentQuery && this . currentQuery === query ) return
70
- this . currentQuery = query
71
- const src = this . src
72
- if ( ! src ) return
73
- const resultsContainer = this . resultsContainer
74
- if ( ! resultsContainer ) return
75
-
76
- const url = new URL ( src , window . location . origin )
77
- const params = new URLSearchParams ( url . search )
78
- params . append ( this . name , query )
79
- url . search = params . toString ( )
80
-
81
- this . dispatchEvent ( new CustomEvent ( 'loadstart' ) )
82
- this . setAttribute ( 'loading' , '' )
83
- try {
84
- const html = await fetch ( url ) . then ( data => data . text ( ) )
85
- this . dispatchEvent ( new CustomEvent ( 'load' ) )
86
- resultsContainer . innerHTML = html
87
- } catch {
88
- this . dispatchEvent ( new CustomEvent ( 'error' ) )
89
- }
90
- this . removeAttribute ( 'loading' )
91
- this . dispatchEvent ( new CustomEvent ( 'loadend' ) )
55
+ async function fetchResults ( remoteInput : RemoteInputElement , checkCurrentQuery : boolean = true ) {
56
+ const input = remoteInput . input
57
+ if ( ! input ) return
58
+ const query = input . value
59
+ if ( checkCurrentQuery && remoteInput . currentQuery === query ) return
60
+ remoteInput . currentQuery = query
61
+ const src = remoteInput . src
62
+ if ( ! src ) return
63
+ const resultsContainer = document . getElementById ( remoteInput . getAttribute ( 'aria-owns' ) || '' )
64
+ if ( ! resultsContainer ) return
65
+
66
+ const url = new URL ( src , window . location . origin )
67
+ const params = new URLSearchParams ( url . search )
68
+ params . append ( remoteInput . getAttribute ( 'param' ) || 'q' , query )
69
+ url . search = params . toString ( )
70
+
71
+ remoteInput . dispatchEvent ( new CustomEvent ( 'loadstart' ) )
72
+ remoteInput . setAttribute ( 'loading' , '' )
73
+ try {
74
+ const html = await fetch ( url ) . then ( data => data . text ( ) )
75
+ remoteInput . dispatchEvent ( new CustomEvent ( 'load' ) )
76
+ resultsContainer . innerHTML = html
77
+ } catch {
78
+ remoteInput . dispatchEvent ( new CustomEvent ( 'error' ) )
92
79
}
80
+ remoteInput . removeAttribute ( 'loading' )
81
+ remoteInput . dispatchEvent ( new CustomEvent ( 'loadend' ) )
93
82
}
94
83
95
84
function debounce ( callback ) {
0 commit comments