|
1 | | -const states = new WeakMap(); |
| 1 | +const states = new WeakMap() |
2 | 2 |
|
3 | 3 | class RemoteInputElement extends HTMLElement { |
4 | 4 | constructor() { |
5 | | - super(); |
6 | | - const fetch = fetchResults.bind(null, this, true); |
| 5 | + super() |
| 6 | + const fetch = fetchResults.bind(null, this, true) |
7 | 7 | const state = { |
8 | 8 | currentQuery: null, |
9 | 9 | oninput: debounce<Event>((e) => fetch(e)), |
10 | 10 | fetch, |
11 | | - controller: null, |
12 | | - }; |
13 | | - states.set(this, state); |
| 11 | + controller: null |
| 12 | + } |
| 13 | + states.set(this, state) |
14 | 14 | } |
15 | 15 |
|
16 | 16 | static get observedAttributes() { |
17 | | - return ["src"]; |
| 17 | + return ['src'] |
18 | 18 | } |
19 | 19 |
|
20 | 20 | attributeChangedCallback(name: string, oldValue: string) { |
21 | | - if (oldValue && name === "src") { |
22 | | - fetchResults(this, false); |
| 21 | + if (oldValue && name === 'src') { |
| 22 | + fetchResults(this, false) |
23 | 23 | } |
24 | 24 | } |
25 | 25 |
|
26 | 26 | connectedCallback() { |
27 | | - const input = this.input; |
28 | | - if (!input) return; |
| 27 | + const input = this.input |
| 28 | + if (!input) return |
29 | 29 |
|
30 | | - input.setAttribute("autocomplete", "off"); |
31 | | - input.setAttribute("spellcheck", "false"); |
| 30 | + input.setAttribute('autocomplete', 'off') |
| 31 | + input.setAttribute('spellcheck', 'false') |
32 | 32 |
|
33 | | - const state = states.get(this); |
34 | | - if (!state) return; |
| 33 | + const state = states.get(this) |
| 34 | + if (!state) return |
35 | 35 |
|
36 | | - input.addEventListener("focus", state.fetch); |
37 | | - input.addEventListener("change", state.fetch); |
38 | | - input.addEventListener("input", state.oninput); |
| 36 | + input.addEventListener('focus', state.fetch) |
| 37 | + input.addEventListener('change', state.fetch) |
| 38 | + input.addEventListener('input', state.oninput) |
39 | 39 | } |
40 | 40 |
|
41 | 41 | disconnectedCallback() { |
42 | | - const input = this.input; |
43 | | - if (!input) return; |
| 42 | + const input = this.input |
| 43 | + if (!input) return |
44 | 44 |
|
45 | | - const state = states.get(this); |
46 | | - if (!state) return; |
| 45 | + const state = states.get(this) |
| 46 | + if (!state) return |
47 | 47 |
|
48 | | - input.removeEventListener("focus", state.fetch); |
49 | | - input.removeEventListener("change", state.fetch); |
50 | | - input.removeEventListener("input", state.oninput); |
| 48 | + input.removeEventListener('focus', state.fetch) |
| 49 | + input.removeEventListener('change', state.fetch) |
| 50 | + input.removeEventListener('input', state.oninput) |
51 | 51 | } |
52 | 52 |
|
53 | 53 | get input(): HTMLInputElement | HTMLTextAreaElement | null { |
54 | | - const input = this.querySelector("input, textarea"); |
| 54 | + const input = this.querySelector('input, textarea') |
55 | 55 | return input instanceof HTMLInputElement || |
56 | 56 | input instanceof HTMLTextAreaElement |
57 | 57 | ? input |
58 | | - : null; |
| 58 | + : null |
59 | 59 | } |
60 | 60 |
|
61 | 61 | get src(): string { |
62 | | - return this.getAttribute("src") || ""; |
| 62 | + return this.getAttribute('src') || '' |
63 | 63 | } |
64 | 64 |
|
65 | 65 | set src(url: string) { |
66 | | - this.setAttribute("src", url); |
| 66 | + this.setAttribute('src', url) |
67 | 67 | } |
68 | 68 | } |
69 | 69 |
|
70 | 70 | function makeAbortController() { |
71 | | - if ("AbortController" in window) { |
72 | | - return new AbortController(); |
| 71 | + if ('AbortController' in window) { |
| 72 | + return new AbortController() |
73 | 73 | } |
74 | 74 |
|
75 | | - return { signal: null, abort() {} }; |
| 75 | + return { signal: null, abort() {} } |
76 | 76 | } |
77 | 77 |
|
78 | 78 | async function fetchResults( |
79 | 79 | remoteInput: RemoteInputElement, |
80 | 80 | checkCurrentQuery: boolean, |
81 | | - event?: Event, |
| 81 | + event?: Event |
82 | 82 | ) { |
83 | | - const input = remoteInput.input; |
84 | | - if (!input) return; |
| 83 | + const input = remoteInput.input |
| 84 | + if (!input) return |
85 | 85 |
|
86 | | - const state = states.get(remoteInput); |
87 | | - if (!state) return; |
| 86 | + const state = states.get(remoteInput) |
| 87 | + if (!state) return |
88 | 88 |
|
89 | | - const query = input.value; |
90 | | - if (checkCurrentQuery && state.currentQuery === query) return; |
| 89 | + const query = input.value |
| 90 | + if (checkCurrentQuery && state.currentQuery === query) return |
91 | 91 |
|
92 | | - state.currentQuery = query; |
| 92 | + state.currentQuery = query |
93 | 93 |
|
94 | | - const src = remoteInput.src; |
95 | | - if (!src) return; |
| 94 | + const src = remoteInput.src |
| 95 | + if (!src) return |
96 | 96 |
|
97 | 97 | const resultsContainer = document.getElementById( |
98 | | - remoteInput.getAttribute("aria-owns") || "", |
99 | | - ); |
100 | | - if (!resultsContainer) return; |
| 98 | + remoteInput.getAttribute('aria-owns') || '' |
| 99 | + ) |
| 100 | + if (!resultsContainer) return |
101 | 101 |
|
102 | | - const url = new URL(src, window.location.href); |
103 | | - const params = new URLSearchParams(url.search); |
104 | | - params.append(remoteInput.getAttribute("param") || "q", query); |
105 | | - url.search = params.toString(); |
| 102 | + const url = new URL(src, window.location.href) |
| 103 | + const params = new URLSearchParams(url.search) |
| 104 | + params.append(remoteInput.getAttribute('param') || 'q', query) |
| 105 | + url.search = params.toString() |
106 | 106 |
|
107 | 107 | if (state.controller) { |
108 | | - state.controller.abort(); |
| 108 | + state.controller.abort() |
109 | 109 | } else { |
110 | | - remoteInput.dispatchEvent(new CustomEvent("loadstart")); |
111 | | - remoteInput.setAttribute("loading", ""); |
| 110 | + remoteInput.dispatchEvent(new CustomEvent('loadstart')) |
| 111 | + remoteInput.setAttribute('loading', '') |
112 | 112 | } |
113 | 113 |
|
114 | | - state.controller = makeAbortController(); |
| 114 | + state.controller = makeAbortController() |
115 | 115 |
|
116 | | - let response; |
117 | | - let html = ""; |
| 116 | + let response |
| 117 | + let html = '' |
118 | 118 | try { |
119 | 119 | response = await fetchWithNetworkEvents(remoteInput, url.toString(), { |
120 | 120 | signal: state.controller.signal, |
121 | | - credentials: "same-origin", |
122 | | - headers: { accept: "text/fragment+html" }, |
123 | | - }); |
124 | | - html = await response.text(); |
125 | | - remoteInput.removeAttribute("loading"); |
126 | | - state.controller = null; |
| 121 | + credentials: 'same-origin', |
| 122 | + headers: { accept: 'text/fragment+html' } |
| 123 | + }) |
| 124 | + html = await response.text() |
| 125 | + remoteInput.removeAttribute('loading') |
| 126 | + state.controller = null |
127 | 127 | } catch (error) { |
128 | | - if (error instanceof Error && error.name !== "AbortError") { |
129 | | - remoteInput.removeAttribute("loading"); |
130 | | - state.controller = null; |
| 128 | + if (error instanceof Error && error.name !== 'AbortError') { |
| 129 | + remoteInput.removeAttribute('loading') |
| 130 | + state.controller = null |
131 | 131 | } |
132 | | - return; |
| 132 | + return |
133 | 133 | } |
134 | 134 |
|
135 | 135 | if (response && response.ok) { |
136 | | - resultsContainer.innerHTML = html; |
| 136 | + resultsContainer.innerHTML = html |
137 | 137 | remoteInput.dispatchEvent( |
138 | | - new CustomEvent("remote-input-success", { |
| 138 | + new CustomEvent('remote-input-success', { |
139 | 139 | bubbles: true, |
140 | | - detail: { eventType: event ? event.type : undefined }, |
141 | | - }), |
142 | | - ); |
| 140 | + detail: { eventType: event ? event.type : undefined } |
| 141 | + }) |
| 142 | + ) |
143 | 143 | } else { |
144 | 144 | remoteInput.dispatchEvent( |
145 | | - new CustomEvent("remote-input-error", { bubbles: true }), |
146 | | - ); |
| 145 | + new CustomEvent('remote-input-error', { bubbles: true }) |
| 146 | + ) |
147 | 147 | } |
148 | 148 | } |
149 | 149 |
|
150 | 150 | async function fetchWithNetworkEvents( |
151 | 151 | el: Element, |
152 | 152 | url: string, |
153 | | - options: RequestInit, |
| 153 | + options: RequestInit |
154 | 154 | ): Promise<Response> { |
155 | 155 | try { |
156 | | - const response = await fetch(url, options); |
157 | | - el.dispatchEvent(new CustomEvent("load")); |
158 | | - el.dispatchEvent(new CustomEvent("loadend")); |
159 | | - return response; |
| 156 | + const response = await fetch(url, options) |
| 157 | + el.dispatchEvent(new CustomEvent('load')) |
| 158 | + el.dispatchEvent(new CustomEvent('loadend')) |
| 159 | + return response |
160 | 160 | } catch (error) { |
161 | | - if (error instanceof Error && error?.name !== "AbortError") { |
162 | | - el.dispatchEvent(new CustomEvent("error")); |
163 | | - el.dispatchEvent(new CustomEvent("loadend")); |
| 161 | + if (error instanceof Error && error?.name !== 'AbortError') { |
| 162 | + el.dispatchEvent(new CustomEvent('error')) |
| 163 | + el.dispatchEvent(new CustomEvent('loadend')) |
164 | 164 | } |
165 | | - throw error; |
| 165 | + throw error |
166 | 166 | } |
167 | 167 | } |
168 | 168 |
|
169 | 169 | function debounce<T>(callback: (_args: T) => void) { |
170 | | - let timeout: ReturnType<typeof setTimeout>; |
| 170 | + let timeout: ReturnType<typeof setTimeout> |
171 | 171 | return function (args: T) { |
172 | | - clearTimeout(timeout); |
| 172 | + clearTimeout(timeout) |
173 | 173 | timeout = setTimeout(() => { |
174 | | - clearTimeout(timeout); |
175 | | - callback(args); |
176 | | - }, 300); |
177 | | - }; |
| 174 | + clearTimeout(timeout) |
| 175 | + callback(args) |
| 176 | + }, 300) |
| 177 | + } |
178 | 178 | } |
179 | 179 |
|
180 | | -export default RemoteInputElement; |
| 180 | +export default RemoteInputElement |
181 | 181 |
|
182 | 182 | declare global { |
183 | 183 | // eslint-disable-next-line no-unused-vars |
184 | 184 | interface Window { |
185 | | - RemoteInputElement: typeof RemoteInputElement; |
| 185 | + RemoteInputElement: typeof RemoteInputElement |
186 | 186 | } |
187 | 187 | } |
188 | 188 |
|
189 | | -if (!window.customElements.get("remote-input")) { |
190 | | - window.RemoteInputElement = RemoteInputElement; |
191 | | - window.customElements.define("remote-input", RemoteInputElement); |
| 189 | +if (!window.customElements.get('remote-input')) { |
| 190 | + window.RemoteInputElement = RemoteInputElement |
| 191 | + window.customElements.define('remote-input', RemoteInputElement) |
192 | 192 | } |
0 commit comments