2023-03-13 15:20:59 +03:00
|
|
|
/**
|
|
|
|
* This script make #search-result-wrapper switch to unloaded or shown automatically.
|
|
|
|
*/
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
const btnSbTrigger = document.getElementById('sidebar-trigger');
|
|
|
|
const btnSearchTrigger = document.getElementById('search-trigger');
|
|
|
|
const btnCancel = document.getElementById('search-cancel');
|
|
|
|
const content = document.querySelectorAll('#main-wrapper>.container>.row');
|
|
|
|
const topbarTitle = document.getElementById('topbar-title');
|
|
|
|
const search = document.getElementById('search');
|
|
|
|
const resultWrapper = document.getElementById('search-result-wrapper');
|
|
|
|
const results = document.getElementById('search-results');
|
|
|
|
const input = document.getElementById('search-input');
|
|
|
|
const hints = document.getElementById('search-hints');
|
|
|
|
|
|
|
|
// CSS class names
|
2024-04-20 19:00:50 +03:00
|
|
|
const LOADED = 'd-block';
|
|
|
|
const UNLOADED = 'd-none';
|
2024-04-17 01:10:01 +03:00
|
|
|
const FOCUS = 'input-focus';
|
|
|
|
const FLEX = 'd-flex';
|
|
|
|
|
|
|
|
/* Actions in mobile screens (Sidebar hidden) */
|
2023-03-13 15:20:59 +03:00
|
|
|
class MobileSearchBar {
|
|
|
|
static on() {
|
2024-04-17 01:10:01 +03:00
|
|
|
btnSbTrigger.classList.add(UNLOADED);
|
|
|
|
topbarTitle.classList.add(UNLOADED);
|
|
|
|
btnSearchTrigger.classList.add(UNLOADED);
|
|
|
|
search.classList.add(FLEX);
|
|
|
|
btnCancel.classList.add(LOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static off() {
|
2024-04-17 01:10:01 +03:00
|
|
|
btnCancel.classList.remove(LOADED);
|
|
|
|
search.classList.remove(FLEX);
|
|
|
|
btnSbTrigger.classList.remove(UNLOADED);
|
|
|
|
topbarTitle.classList.remove(UNLOADED);
|
|
|
|
btnSearchTrigger.classList.remove(UNLOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSwitch {
|
2024-04-17 01:10:01 +03:00
|
|
|
static resultVisible = false;
|
|
|
|
|
2023-03-13 15:20:59 +03:00
|
|
|
static on() {
|
2024-04-17 01:10:01 +03:00
|
|
|
if (!this.resultVisible) {
|
|
|
|
resultWrapper.classList.remove(UNLOADED);
|
|
|
|
content.forEach((el) => {
|
|
|
|
el.classList.add(UNLOADED);
|
|
|
|
});
|
|
|
|
this.resultVisible = true;
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static off() {
|
2024-04-17 01:10:01 +03:00
|
|
|
if (this.resultVisible) {
|
|
|
|
results.innerHTML = '';
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
if (hints.classList.contains(UNLOADED)) {
|
|
|
|
hints.classList.remove(UNLOADED);
|
|
|
|
}
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
resultWrapper.classList.add(UNLOADED);
|
|
|
|
content.forEach((el) => {
|
|
|
|
el.classList.remove(UNLOADED);
|
|
|
|
});
|
|
|
|
input.textContent = '';
|
|
|
|
this.resultVisible = false;
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMobileView() {
|
2024-04-17 01:10:01 +03:00
|
|
|
return btnCancel.classList.contains(LOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export function displaySearch() {
|
2024-04-17 01:10:01 +03:00
|
|
|
btnSearchTrigger.addEventListener('click', () => {
|
2023-03-13 15:20:59 +03:00
|
|
|
MobileSearchBar.on();
|
|
|
|
ResultSwitch.on();
|
2024-04-17 01:10:01 +03:00
|
|
|
input.focus();
|
2023-03-13 15:20:59 +03:00
|
|
|
});
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
btnCancel.addEventListener('click', () => {
|
2023-03-13 15:20:59 +03:00
|
|
|
MobileSearchBar.off();
|
|
|
|
ResultSwitch.off();
|
|
|
|
});
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
input.addEventListener('focus', () => {
|
|
|
|
search.classList.add(FOCUS);
|
2023-03-13 15:20:59 +03:00
|
|
|
});
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
input.addEventListener('focusout', () => {
|
|
|
|
search.classList.remove(FOCUS);
|
2023-03-13 15:20:59 +03:00
|
|
|
});
|
|
|
|
|
2024-04-17 01:10:01 +03:00
|
|
|
input.addEventListener('input', () => {
|
|
|
|
if (input.value === '') {
|
2023-03-13 15:20:59 +03:00
|
|
|
if (isMobileView()) {
|
2024-04-17 01:10:01 +03:00
|
|
|
hints.classList.remove(UNLOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
} else {
|
|
|
|
ResultSwitch.off();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ResultSwitch.on();
|
|
|
|
if (isMobileView()) {
|
2024-04-17 01:10:01 +03:00
|
|
|
hints.classList.add(UNLOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|