2023-03-13 15:20:59 +03:00
|
|
|
/**
|
|
|
|
* This script make #search-result-wrapper switch to unloaded or shown automatically.
|
|
|
|
*/
|
|
|
|
const $btnSbTrigger = $('#sidebar-trigger');
|
|
|
|
const $btnSearchTrigger = $('#search-trigger');
|
|
|
|
const $btnCancel = $('#search-cancel');
|
2023-09-08 17:48:37 +03:00
|
|
|
const $content = $('#main-wrapper>.container>.row');
|
2023-03-13 15:20:59 +03:00
|
|
|
const $topbarTitle = $('#topbar-title');
|
2023-09-08 17:48:37 +03:00
|
|
|
const $search = $('search');
|
2023-03-13 15:20:59 +03:00
|
|
|
const $resultWrapper = $('#search-result-wrapper');
|
|
|
|
const $results = $('#search-results');
|
|
|
|
const $input = $('#search-input');
|
|
|
|
const $hints = $('#search-hints');
|
|
|
|
const $viewport = $('html,body');
|
|
|
|
|
|
|
|
// class names
|
|
|
|
const C_LOADED = 'loaded';
|
|
|
|
const C_UNLOADED = 'unloaded';
|
|
|
|
const C_FOCUS = 'input-focus';
|
|
|
|
const C_FLEX = 'd-flex';
|
|
|
|
|
|
|
|
class ScrollBlocker {
|
|
|
|
static offset = 0;
|
|
|
|
static resultVisible = false;
|
|
|
|
|
|
|
|
static on() {
|
|
|
|
ScrollBlocker.offset = window.scrollY;
|
|
|
|
$viewport.scrollTop(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static off() {
|
|
|
|
$viewport.scrollTop(ScrollBlocker.offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--- Actions in mobile screens (Sidebar hidden) ---*/
|
|
|
|
class MobileSearchBar {
|
|
|
|
static on() {
|
|
|
|
$btnSbTrigger.addClass(C_UNLOADED);
|
|
|
|
$topbarTitle.addClass(C_UNLOADED);
|
|
|
|
$btnSearchTrigger.addClass(C_UNLOADED);
|
2023-09-08 17:48:37 +03:00
|
|
|
$search.addClass(C_FLEX);
|
2023-03-13 15:20:59 +03:00
|
|
|
$btnCancel.addClass(C_LOADED);
|
|
|
|
}
|
|
|
|
|
|
|
|
static off() {
|
|
|
|
$btnCancel.removeClass(C_LOADED);
|
2023-09-08 17:48:37 +03:00
|
|
|
$search.removeClass(C_FLEX);
|
2023-03-13 15:20:59 +03:00
|
|
|
$btnSbTrigger.removeClass(C_UNLOADED);
|
|
|
|
$topbarTitle.removeClass(C_UNLOADED);
|
|
|
|
$btnSearchTrigger.removeClass(C_UNLOADED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class ResultSwitch {
|
|
|
|
static on() {
|
|
|
|
if (!ScrollBlocker.resultVisible) {
|
2023-09-08 17:48:37 +03:00
|
|
|
// the block method must be called before $(#main-wrapper>.container) unloaded.
|
2023-03-13 15:20:59 +03:00
|
|
|
ScrollBlocker.on();
|
|
|
|
$resultWrapper.removeClass(C_UNLOADED);
|
2023-05-16 20:59:34 +03:00
|
|
|
$content.addClass(C_UNLOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
ScrollBlocker.resultVisible = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static off() {
|
|
|
|
if (ScrollBlocker.resultVisible) {
|
|
|
|
$results.empty();
|
|
|
|
if ($hints.hasClass(C_UNLOADED)) {
|
|
|
|
$hints.removeClass(C_UNLOADED);
|
|
|
|
}
|
|
|
|
$resultWrapper.addClass(C_UNLOADED);
|
2023-05-16 20:59:34 +03:00
|
|
|
$content.removeClass(C_UNLOADED);
|
2023-03-13 15:20:59 +03:00
|
|
|
|
2023-09-08 17:48:37 +03:00
|
|
|
// now the release method must be called after $(#main-wrapper>.container) display
|
2023-03-13 15:20:59 +03:00
|
|
|
ScrollBlocker.off();
|
|
|
|
|
|
|
|
$input.val('');
|
|
|
|
ScrollBlocker.resultVisible = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMobileView() {
|
|
|
|
return $btnCancel.hasClass(C_LOADED);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function displaySearch() {
|
|
|
|
$btnSearchTrigger.on('click', function () {
|
|
|
|
MobileSearchBar.on();
|
|
|
|
ResultSwitch.on();
|
|
|
|
$input.trigger('focus');
|
|
|
|
});
|
|
|
|
|
|
|
|
$btnCancel.on('click', function () {
|
|
|
|
MobileSearchBar.off();
|
|
|
|
ResultSwitch.off();
|
|
|
|
});
|
|
|
|
|
|
|
|
$input.on('focus', function () {
|
2023-09-08 17:48:37 +03:00
|
|
|
$search.addClass(C_FOCUS);
|
2023-03-13 15:20:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$input.on('focusout', function () {
|
2023-09-08 17:48:37 +03:00
|
|
|
$search.removeClass(C_FOCUS);
|
2023-03-13 15:20:59 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
$input.on('input', () => {
|
|
|
|
if ($input.val() === '') {
|
|
|
|
if (isMobileView()) {
|
|
|
|
$hints.removeClass(C_UNLOADED);
|
|
|
|
} else {
|
|
|
|
ResultSwitch.off();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ResultSwitch.on();
|
|
|
|
if (isMobileView()) {
|
|
|
|
$hints.addClass(C_UNLOADED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|