|
1 | | -function disableAdvanced() { |
2 | | - advanced_field.setAttribute('value', ''); |
3 | | - if (geobox_label.classList.contains('closed') && geodistance_label.classList.contains('closed')) { |
4 | | - keyword_field.toggleAttribute('required'); |
5 | | - keyword_field.classList.toggle('required'); |
6 | | - keyword_field.setAttribute('placeholder', 'Enter your search'); |
7 | | - } |
8 | | - [...details_panel.getElementsByClassName('field')].forEach( |
9 | | - field => field.value = '' |
10 | | - ); |
11 | | - advanced_label.classList = 'closed'; |
12 | | -}; |
| 1 | +var keywordField = document.getElementById('basic-search-main'); |
| 2 | +var advancedPanel = document.getElementById('advanced-search-panel'); |
| 3 | +var geoboxPanel = document.getElementById('geobox-search-panel'); |
| 4 | +var geodistancePanel = document.getElementById('geodistance-search-panel'); |
| 5 | +var allPanels = document.getElementsByClassName('form-panel'); |
13 | 6 |
|
14 | | -function enableAdvanced() { |
15 | | - advanced_field.setAttribute('value', 'true'); |
16 | | - if (geobox_label.classList.contains('closed') && geodistance_label.classList.contains('closed')) { |
17 | | - keyword_field.toggleAttribute('required'); |
18 | | - keyword_field.classList.toggle('required'); |
19 | | - keyword_field.setAttribute('placeholder', 'Keyword anywhere'); |
| 7 | +function togglePanelState(currentPanel) { |
| 8 | + // Only the geoboxPanel and geodistancePanel inputs need to be required. Advanced search inputs do not. |
| 9 | + if (currentPanel === geoboxPanel || currentPanel === geodistancePanel) { |
| 10 | + toggleRequiredFieldset(currentPanel); |
20 | 11 | } |
21 | | - advanced_label.classList = 'open'; |
22 | | -}; |
23 | 12 |
|
24 | | -function disableGeobox() { |
25 | | - if (advanced_label.classList.contains('closed') && geodistance_label.classList.contains('closed')) { |
26 | | - keyword_field.toggleAttribute('required'); |
27 | | - keyword_field.classList.toggle('required'); |
28 | | - keyword_field.setAttribute('placeholder', 'Enter your search'); |
29 | | - } |
30 | | - geobox_field.setAttribute('value', ''); |
31 | | - [...geobox_details_panel.getElementsByClassName('field')].forEach(function(field) { |
32 | | - field.value = ''; |
33 | | - field.classList.toggle('required'); |
34 | | - field.toggleAttribute('required'); |
35 | | - }); |
36 | | - geobox_label.classList = 'closed'; |
37 | | -}; |
| 13 | + // These two functions are delayed to ensure that events have propagated first. Otherwise, they will fire before |
| 14 | + // the `open` attribute has toggled on the currentPanel, resulting in unexpected behavior. |
| 15 | + setTimeout(toggleKeywordRequired, 0); |
| 16 | + setTimeout(updateKeywordPlaceholder, 0); |
38 | 17 |
|
39 | | -function enableGeobox() { |
40 | | - if (advanced_label.classList.contains('closed') && geodistance_label.classList.contains('closed')) { |
41 | | - keyword_field.toggleAttribute('required'); |
42 | | - keyword_field.classList.toggle('required'); |
43 | | - keyword_field.setAttribute('placeholder', 'Keyword anywhere'); |
44 | | - } |
45 | | - geobox_field.setAttribute('value', 'true'); |
46 | | - [...geobox_details_panel.getElementsByClassName('field')].forEach(function(field) { |
47 | | - field.value = ''; |
48 | | - field.classList.toggle('required'); |
49 | | - field.toggleAttribute('required'); |
50 | | - }); |
51 | | - geobox_label.classList = 'open'; |
52 | | -}; |
| 18 | + // Finally, enable or disable the search type of the current panel, based on whether it is open or not. |
| 19 | + toggleSearch(currentPanel); |
| 20 | +} |
53 | 21 |
|
54 | | -function disableGeodistance() { |
55 | | - if (advanced_label.classList.contains('closed') && geobox_label.classList.contains('closed')) { |
56 | | - keyword_field.toggleAttribute('required'); |
57 | | - keyword_field.classList.toggle('required'); |
58 | | - keyword_field.setAttribute('placeholder', 'Enter your search'); |
59 | | - } |
60 | | - geodistance_field.setAttribute('value', ''); |
61 | | - [...geodistance_details_panel.getElementsByClassName('field')].forEach(function(field) { |
| 22 | +function toggleRequiredFieldset(panel) { |
| 23 | + [...panel.getElementsByClassName('field')].forEach((field) => { |
62 | 24 | field.value = ''; |
63 | 25 | field.classList.toggle('required'); |
64 | 26 | field.toggleAttribute('required'); |
65 | 27 | }); |
66 | | - geodistance_label.classList = 'closed'; |
67 | | -}; |
| 28 | +} |
68 | 29 |
|
69 | | -function enableGeodistance() { |
70 | | - if (advanced_label.classList.contains('closed') && geobox_label.classList.contains('closed')) { |
71 | | - keyword_field.toggleAttribute('required'); |
72 | | - keyword_field.classList.toggle('required'); |
73 | | - keyword_field.setAttribute('placeholder', 'Keyword anywhere'); |
74 | | - } |
75 | | - geodistance_field.setAttribute('value', 'true'); |
76 | | - [...geodistance_details_panel.getElementsByClassName('field')].forEach(function(field) { |
77 | | - field.value = ''; |
78 | | - field.classList.toggle('required'); |
79 | | - field.toggleAttribute('required'); |
80 | | - }); |
81 | | - geodistance_label.classList = 'open'; |
82 | | -}; |
83 | | - |
84 | | - |
85 | | -var advanced_field = document.getElementById('advanced-search-field'); |
86 | | -var advanced_label = document.getElementById('advanced-search-label'); |
87 | | -var advanced_toggle = document.getElementById('advanced-summary'); |
88 | | -var details_panel = document.getElementById('advanced-search-panel'); |
89 | | -var keyword_field = document.getElementById('basic-search-main'); |
90 | | -var geobox_field = document.getElementById('geobox-search-field'); |
91 | | -var geobox_label = document.getElementById('geobox-search-label'); |
92 | | -var geobox_toggle = document.getElementById('geobox-summary'); |
93 | | -var geobox_details_panel = document.getElementById('geobox-search-panel'); |
94 | | -var geodistance_field = document.getElementById('geodistance-search-field'); |
95 | | -var geodistance_label = document.getElementById('geodistance-search-label'); |
96 | | -var geodistance_toggle = document.getElementById('geodistance-summary'); |
97 | | -var geodistance_details_panel = document.getElementById('geodistance-search-panel'); |
98 | | - |
99 | | -geobox_toggle.addEventListener('click', event => { |
100 | | - if (geobox_details_panel.attributes.hasOwnProperty('open')) { |
101 | | - disableGeobox(); |
| 30 | +// Each panel has a hidden input that, when true, enables that type of search. |
| 31 | +function toggleSearch(panel) { |
| 32 | + let input = panel.querySelector('.fieldset-toggle'); |
| 33 | + if (panel.open) { |
| 34 | + input.setAttribute('value', ''); |
102 | 35 | } else { |
103 | | - enableGeobox(); |
| 36 | + input.setAttribute('value', 'true'); |
104 | 37 | } |
105 | | -}); |
| 38 | +} |
106 | 39 |
|
107 | | -geodistance_toggle.addEventListener('click', event => { |
108 | | - if (geodistance_details_panel.attributes.hasOwnProperty('open')) { |
109 | | - disableGeodistance(); |
| 40 | +// The keyword field is required only if all panels are closed. |
| 41 | +function toggleKeywordRequired() { |
| 42 | + if (Array.from(allPanels).every((panel) => !panel.open)) { |
| 43 | + keywordField.setAttribute('required', ''); |
| 44 | + keywordField.classList.add('required'); |
110 | 45 | } else { |
111 | | - enableGeodistance(); |
| 46 | + keywordField.removeAttribute('required'); |
| 47 | + keywordField.classList.remove('required'); |
112 | 48 | } |
113 | | -}); |
| 49 | +} |
114 | 50 |
|
115 | | -advanced_toggle.addEventListener('click', event => { |
116 | | - if (details_panel.attributes.hasOwnProperty('open')) { |
117 | | - disableAdvanced(); |
| 51 | +// Placeholder text should be 'Keyword anywhere' if any panels are open, and 'Enter your search' otherwise. |
| 52 | +function updateKeywordPlaceholder() { |
| 53 | + if (Array.from(allPanels).some((panel) => panel.open)) { |
| 54 | + keywordField.setAttribute('placeholder', 'Keyword anywhere'); |
118 | 55 | } else { |
119 | | - enableAdvanced(); |
| 56 | + keywordField.setAttribute('placeholder', 'Enter your search'); |
120 | 57 | } |
121 | | -}); |
| 58 | +} |
| 59 | + |
| 60 | +// Add event listeners for all panels in the DOM. For GDT, this is currently both geospatial panels and the advanced |
| 61 | +// panel. In all other TIMDEX UI apps, it's just the advanced panel. |
| 62 | +if (Array.from(allPanels).includes(geoboxPanel && geodistancePanel)) { |
| 63 | + document.getElementById('geobox-summary').addEventListener('click', () => { |
| 64 | + togglePanelState(geoboxPanel); |
| 65 | + }); |
| 66 | + |
| 67 | + document.getElementById('geodistance-summary').addEventListener('click', () => { |
| 68 | + togglePanelState(geodistancePanel); |
| 69 | + }); |
| 70 | + |
| 71 | + document.getElementById('advanced-summary').addEventListener('click', () => { |
| 72 | + togglePanelState(advancedPanel); |
| 73 | + }); |
| 74 | +} else { |
| 75 | + document.getElementById('advanced-summary').addEventListener('click', () => { |
| 76 | + togglePanelState(advancedPanel); |
| 77 | + }); |
| 78 | +} |
122 | 79 |
|
123 | 80 | console.log('search_form.js loaded'); |
0 commit comments