@@ -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 )
@@ -62,34 +62,34 @@ class RemoteInputElement extends HTMLElement {
62
62
set name ( name : string ) {
63
63
this . setAttribute ( 'name' , name )
64
64
}
65
+ }
65
66
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' ) )
67
+ async function fetchResults ( remoteInput : RemoteInputElement , checkCurrentQuery : boolean = true ) {
68
+ if ( ! remoteInput . input ) return
69
+ const query = remoteInput . input . value
70
+ if ( checkCurrentQuery && remoteInput . currentQuery === query ) return
71
+ remoteInput . currentQuery = query
72
+ const src = remoteInput . src
73
+ if ( ! src ) return
74
+ const resultsContainer = remoteInput . resultsContainer
75
+ if ( ! resultsContainer ) return
76
+
77
+ const url = new URL ( src , window . location . origin )
78
+ const params = new URLSearchParams ( url . search )
79
+ params . append ( remoteInput . name , query )
80
+ url . search = params . toString ( )
81
+
82
+ remoteInput . dispatchEvent ( new CustomEvent ( 'loadstart' ) )
83
+ remoteInput . setAttribute ( 'loading' , '' )
84
+ try {
85
+ const html = await fetch ( url ) . then ( data => data . text ( ) )
86
+ remoteInput . dispatchEvent ( new CustomEvent ( 'load' ) )
87
+ resultsContainer . innerHTML = html
88
+ } catch {
89
+ remoteInput . dispatchEvent ( new CustomEvent ( 'error' ) )
92
90
}
91
+ remoteInput . removeAttribute ( 'loading' )
92
+ remoteInput . dispatchEvent ( new CustomEvent ( 'loadend' ) )
93
93
}
94
94
95
95
function debounce ( callback ) {
0 commit comments