-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch_form.js
More file actions
112 lines (97 loc) · 4.19 KB
/
search_form.js
File metadata and controls
112 lines (97 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { trackSearch, trackCustomEvent } from 'matomo_events'
var keywordField = document.getElementById('basic-search-main');
var advancedPanel = document.getElementById('advanced-search-panel');
var geoboxPanel = document.getElementById('geobox-search-panel');
var geodistancePanel = document.getElementById('geodistance-search-panel');
var allPanels = document.getElementsByClassName('form-panel');
function togglePanelState(currentPanel) {
// Only the geoboxPanel and geodistancePanel inputs need to be required. Advanced search inputs do not.
if (currentPanel === geoboxPanel || currentPanel === geodistancePanel) {
toggleRequiredFieldset(currentPanel);
}
// These two functions are delayed to ensure that events have propagated first. Otherwise, they will fire before
// the `open` attribute has toggled on the currentPanel, resulting in unexpected behavior.
setTimeout(toggleKeywordRequired, 0);
setTimeout(updateKeywordPlaceholder, 0);
// Finally, enable or disable the search type of the current panel, based on whether it is open or not.
toggleSearch(currentPanel);
}
function toggleRequiredFieldset(panel) {
[...panel.getElementsByClassName('field')].forEach((field) => {
field.value = '';
field.classList.toggle('required');
field.toggleAttribute('required');
});
}
// Each panel has a hidden input that, when true, enables that type of search.
function toggleSearch(panel) {
let input = panel.querySelector('.fieldset-toggle');
if (panel.open) {
input.setAttribute('value', '');
} else {
input.setAttribute('value', 'true');
}
}
// The keyword field is required only if all panels are closed.
function toggleKeywordRequired() {
if (Array.from(allPanels).every((panel) => !panel.open)) {
keywordField.setAttribute('required', '');
keywordField.classList.add('required');
} else {
keywordField.removeAttribute('required');
keywordField.classList.remove('required');
}
}
// Placeholder text should be 'Keyword anywhere' if any panels are open, and 'Enter your search' otherwise.
function updateKeywordPlaceholder() {
if (Array.from(allPanels).some((panel) => panel.open)) {
keywordField.setAttribute('placeholder', 'Keyword anywhere');
} else {
keywordField.setAttribute('placeholder', 'Enter your search');
}
}
// Add event listeners for all panels in the DOM. For GeoData, this is currently both geospatial panels and the advanced
// panel. In all other TIMDEX UI apps, it's just the advanced panel.
if (Array.from(allPanels).includes(geoboxPanel && geodistancePanel)) {
document.getElementById('geobox-summary').addEventListener('click', () => {
trackCustomEvent('search_panel_toggled', { panel_type: 'geobox' });
togglePanelState(geoboxPanel);
});
document.getElementById('geodistance-summary').addEventListener('click', () => {
trackCustomEvent('search_panel_toggled', { panel_type: 'geodistance' });
togglePanelState(geodistancePanel);
});
document.getElementById('advanced-summary').addEventListener('click', () => {
trackCustomEvent('search_panel_toggled', { panel_type: 'advanced' });
togglePanelState(advancedPanel);
});
} else {
document.getElementById('advanced-summary').addEventListener('click', () => {
trackCustomEvent('search_panel_toggled', { panel_type: 'advanced' });
togglePanelState(advancedPanel);
});
}
// Track form submission
const searchForm = document.querySelector('form[data-turbo-confirm], form[action*="/results"]');
if (searchForm) {
searchForm.addEventListener('submit', (e) => {
const query = keywordField.value;
const searchType = determineSearchType();
if (query) {
trackSearch(query, {
searchType: searchType,
advanced_search: advancedPanel.open,
geobox_search: geoboxPanel ? geoboxPanel.open : false,
geodistance_search: geodistancePanel ? geodistancePanel.open : false
});
}
});
}
// Helper function to determine which search type is active
function determineSearchType() {
if (geoboxPanel && geoboxPanel.open) return 'geobox';
if (geodistancePanel && geodistancePanel.open) return 'geodistance';
if (advancedPanel && advancedPanel.open) return 'advanced';
return 'keyword';
}
console.log('search_form.js loaded');