|
1 | | -const gallySearchFormHandler = function (event) { |
2 | | - const gallySearchFormContainer = document.querySelector('#searchFormContainer'); |
3 | | - if (null !== gallySearchFormContainer) { |
4 | | - const gallyPreviewUrl = gallySearchFormContainer.dataset.previewUrl; |
5 | | - const gallySearchForm = gallySearchFormContainer.querySelector('form'); |
6 | | - const gallySearchInput = gallySearchForm.querySelector('input'); |
7 | | - const gallySearchResult = gallySearchFormContainer.querySelector('#collapsedSearchResults'); |
| 1 | +const gallySearchFormHandler = function () { |
| 2 | + const gallySearchFormContainers = document.querySelectorAll('.searchFormContainer'); |
8 | 3 |
|
9 | | - let abortController = null; |
10 | | - const gallySearchResultCollapsible = bootstrap.Collapse.getOrCreateInstance(gallySearchResult, { |
11 | | - toggle: false |
12 | | - }); |
| 4 | + gallySearchFormContainers.forEach(container => { |
| 5 | + const gallyPreviewUrl = container.dataset.previewUrl; |
| 6 | + const gallySearchForm = container.querySelector('form'); |
| 7 | + const gallySearchInput = gallySearchForm.querySelector('input'); |
| 8 | + const gallySearchResult = container.querySelector('.collapsedSearchResults'); |
13 | 9 |
|
14 | | - gallySearchInput.addEventListener('input', (event) => { |
15 | | - const queryText = event.target.value; |
| 10 | + let abortController = null; |
16 | 11 |
|
17 | | - if (queryText.length >= 3) { |
18 | | - let formData = new FormData(gallySearchForm); |
19 | | - const plainFormData = Object.fromEntries(formData.entries()); |
| 12 | + gallySearchInput.addEventListener('input', (event) => { |
| 13 | + const queryText = event.target.value; |
20 | 14 |
|
21 | | - let formDataArray = []; |
22 | | - for (const [key, value] of Object.entries(plainFormData)) { |
23 | | - formDataArray.push(`${encodeURIComponent(key)}=${value}`); |
24 | | - } |
25 | | - let formDataString = formDataArray.join('&'); |
| 15 | + if (queryText.length >= 3) { |
| 16 | + const formData = new FormData(gallySearchForm); |
| 17 | + const plainFormData = Object.fromEntries(formData.entries()); |
| 18 | + const formDataString = new URLSearchParams(plainFormData).toString(); |
26 | 19 |
|
27 | | - gallySearchResult.querySelector('.loading-results').classList.remove('d-none') |
28 | | - gallySearchResult.querySelector('.results').classList.add('d-none') |
29 | | - gallySearchResult.querySelector('.results').textContent = ''; |
30 | | - gallySearchResultCollapsible.show(); |
| 20 | + gallySearchResult.querySelector('.loading-results').classList.remove('d-none'); |
| 21 | + gallySearchResult.querySelector('.results').classList.add('d-none'); |
| 22 | + gallySearchResult.querySelector('.results').textContent = ''; |
| 23 | + gallySearchResult.classList.add('show'); |
31 | 24 |
|
32 | | - if (null !== abortController) { |
33 | | - abortController.abort(); |
34 | | - abortController = null; |
35 | | - } |
| 25 | + if (abortController) { |
| 26 | + abortController.abort(); |
| 27 | + } |
36 | 28 |
|
37 | | - abortController = new AbortController(); |
| 29 | + abortController = new AbortController(); |
38 | 30 |
|
39 | | - (async () => { |
40 | | - const rawResponse = await fetch(gallyPreviewUrl, { |
41 | | - method: 'POST', |
42 | | - headers: { |
43 | | - 'Content-Type': 'application/x-www-form-urlencoded' |
44 | | - }, |
45 | | - body: formDataString, |
46 | | - signal: abortController.signal |
47 | | - }).catch((error) => { |
48 | | - // If the request was aborted, do nothing |
49 | | - if (error.name === 'AbortError') { |
50 | | - return; |
51 | | - } |
| 31 | + (async () => { |
| 32 | + try { |
| 33 | + const rawResponse = await fetch(gallyPreviewUrl, { |
| 34 | + method: 'POST', |
| 35 | + headers: { |
| 36 | + 'Content-Type': 'application/x-www-form-urlencoded' |
| 37 | + }, |
| 38 | + body: formDataString, |
| 39 | + signal: abortController.signal |
| 40 | + }); |
52 | 41 |
|
53 | | - // Otherwise, handle the error here or throw it back to the console |
54 | | - throw error; |
55 | | - }); |
| 42 | + const content = await rawResponse.json(); |
56 | 43 |
|
57 | | - // On aborted calls, rawResponse will be undefined |
58 | | - if (rawResponse !== undefined) { |
59 | | - const content = await rawResponse.json(); |
60 | | - // Do something with the results here |
| 44 | + gallySearchResult.querySelector('.loading-results').classList.add('d-none'); |
| 45 | + gallySearchResult.querySelector('.results').classList.remove('d-none'); |
| 46 | + gallySearchResult.querySelector('.results').innerHTML = content.htmlResults; |
61 | 47 |
|
62 | | - gallySearchResult.querySelector('.loading-results').classList.add('d-none'); |
63 | | - gallySearchResult.querySelector('.results').classList.remove('d-none'); |
64 | | - gallySearchResult.querySelector('.results').innerHTML = content.htmlResults; |
65 | | - } |
66 | | - })(); |
67 | | - } else { |
68 | | - gallySearchResultCollapsible.hide(); |
| 48 | + if (!content.htmlResults) { |
| 49 | + gallySearchResult.classList.remove('show'); |
69 | 50 | } |
70 | | - }); |
71 | 51 |
|
72 | | - // |
73 | | - gallySearchInput.addEventListener('focus', (event) => { |
74 | | - const queryText = event.target.value; |
75 | | - if (queryText.length >= 3) { |
76 | | - // If there are already some results, display them |
77 | | - if (gallySearchResult.querySelector('.loading-results').classList.contains('d-none')) { |
78 | | - gallySearchResultCollapsible.show(); |
79 | | - } |
80 | | - // Otherwise, trigger a search |
81 | | - else { |
82 | | - gallySearchInput.dispatchEvent(new Event('input')); |
83 | | - } |
| 52 | + } catch (error) { |
| 53 | + if (error.name !== 'AbortError') { |
| 54 | + console.error(error); |
84 | 55 | } |
85 | | - }); |
| 56 | + } |
| 57 | + })(); |
| 58 | + } else { |
| 59 | + gallySearchResult.classList.remove('show'); |
| 60 | + } |
| 61 | + }); |
86 | 62 |
|
87 | | - // Add a listener to close the searchResult container |
88 | | - document.addEventListener('click', function (event) { |
89 | | - const gallySearchResult = event.target.closest('#collapsedSearchResults'); |
90 | | - if (null === gallySearchResult) { |
91 | | - gallySearchResultCollapsible.hide(); |
92 | | - } |
93 | | - }); |
| 63 | + gallySearchInput.addEventListener('focus', (event) => { |
| 64 | + const queryText = event.target.value; |
| 65 | + if (queryText.length >= 3) { |
| 66 | + if (gallySearchResult.querySelector('.loading-results').classList.contains('d-none')) { |
| 67 | + gallySearchResult.classList.add('show'); |
| 68 | + } else { |
| 69 | + gallySearchInput.dispatchEvent(new Event('input')); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + // Close when clicking outside |
| 76 | + document.addEventListener('click', function (event) { |
| 77 | + if (!event.target.closest('.collapsedSearchResults')) { |
| 78 | + document.querySelectorAll('.collapsedSearchResults').forEach(element => element.classList.remove('show')); |
94 | 79 | } |
95 | | -} |
| 80 | + }); |
| 81 | +}; |
96 | 82 |
|
97 | 83 | window.addEventListener("DOMContentLoaded", gallySearchFormHandler); |
0 commit comments