|
1 | 1 | /* @flow strict */
|
2 | 2 |
|
3 |
| -class CustomElementElement extends HTMLElement { |
4 |
| - constructor() { |
5 |
| - super() |
| 3 | +class RemoteInputElement extends HTMLElement { |
| 4 | + currentQuery: ?string |
| 5 | + debounceInputChange: Event => void |
| 6 | + boundFetchResults: Event => mixed |
| 7 | + |
| 8 | + static get observedAttributes() { |
| 9 | + return ['src'] |
| 10 | + } |
| 11 | + |
| 12 | + attributeChangedCallback(name: string, oldValue: string) { |
| 13 | + if (oldValue && name === 'src') { |
| 14 | + this.fetchResults(false) |
| 15 | + } |
6 | 16 | }
|
7 | 17 |
|
8 | 18 | connectedCallback() {
|
9 |
| - this.textContent = ':wave:' |
| 19 | + const input = this.input |
| 20 | + if (!input) return |
| 21 | + |
| 22 | + input.setAttribute('autocomplete', 'off') |
| 23 | + input.setAttribute('spellcheck', 'false') |
| 24 | + |
| 25 | + this.debounceInputChange = debounce(this.fetchResults.bind(this)) |
| 26 | + this.boundFetchResults = this.fetchResults.bind(this) |
| 27 | + input.addEventListener('focus', this.boundFetchResults) |
| 28 | + input.addEventListener('change', this.boundFetchResults) |
| 29 | + input.addEventListener('input', this.debounceInputChange) |
| 30 | + } |
| 31 | + |
| 32 | + disconnectedCallback() { |
| 33 | + const input = this.input |
| 34 | + if (!input) return |
| 35 | + |
| 36 | + input.removeEventListener('focus', this.boundFetchResults) |
| 37 | + input.removeEventListener('change', this.boundFetchResults) |
| 38 | + input.removeEventListener('input', this.debounceInputChange) |
| 39 | + } |
| 40 | + |
| 41 | + get input(): ?(HTMLInputElement | HTMLTextAreaElement) { |
| 42 | + const input = this.querySelector('input, textarea') |
| 43 | + return input instanceof HTMLInputElement || input instanceof HTMLTextAreaElement ? input : null |
| 44 | + } |
| 45 | + |
| 46 | + get resultsContainer(): ?HTMLElement { |
| 47 | + return document.getElementById(this.getAttribute('aria-owns') || '') |
| 48 | + } |
| 49 | + |
| 50 | + get src(): string { |
| 51 | + return this.getAttribute('src') || '' |
| 52 | + } |
| 53 | + |
| 54 | + set src(url: string) { |
| 55 | + this.setAttribute('src', url) |
| 56 | + } |
| 57 | + |
| 58 | + get name(): string { |
| 59 | + return this.getAttribute('name') || 'q' |
| 60 | + } |
| 61 | + |
| 62 | + set name(name: string) { |
| 63 | + this.setAttribute('name', name) |
10 | 64 | }
|
11 | 65 |
|
12 |
| - disconnectedCallback() {} |
| 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')) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +function debounce(callback) { |
| 96 | + let timeout |
| 97 | + return function() { |
| 98 | + clearTimeout(timeout) |
| 99 | + timeout = setTimeout(() => { |
| 100 | + clearTimeout(timeout) |
| 101 | + callback() |
| 102 | + }, 300) |
| 103 | + } |
13 | 104 | }
|
14 | 105 |
|
15 |
| -export default CustomElementElement |
| 106 | +export default RemoteInputElement |
16 | 107 |
|
17 |
| -if (!window.customElements.get('custom-element')) { |
18 |
| - window.CustomElementElement = CustomElementElement |
19 |
| - window.customElements.define('custom-element', CustomElementElement) |
| 108 | +if (!window.customElements.get('remote-input')) { |
| 109 | + window.RemoteInputElement = RemoteInputElement |
| 110 | + window.customElements.define('remote-input', RemoteInputElement) |
20 | 111 | }
|
0 commit comments