web/_javascript/commons/search-display.js

127 lines
3.5 KiB
JavaScript
Raw Normal View History

2022-10-25 14:26:44 +03:00
/**
* This script make #search-result-wrapper switch to unloaded or shown automatically.
*/
$(function () {
const btnSbTrigger = $("#sidebar-trigger");
const btnSearchTrigger = $("#search-trigger");
const btnCancel = $("#search-cancel");
const main = $("#main");
const topbarTitle = $("#topbar-title");
const searchWrapper = $("#search-wrapper");
const resultWrapper = $("#search-result-wrapper");
const results = $("#search-results");
const input = $("#search-input");
const hints = $("#search-hints");
const scrollBlocker = (function () {
let offset = 0;
return {
block() {
offset = window.scrollY;
$("html,body").scrollTop(0);
},
release() {
$("html,body").scrollTop(offset);
},
getOffset() {
return offset;
}
};
}());
/*--- Actions in mobile screens (Sidebar hidden) ---*/
const mobileSearchBar = (function () {
return {
on() {
btnSbTrigger.addClass("unloaded");
topbarTitle.addClass("unloaded");
btnSearchTrigger.addClass("unloaded");
searchWrapper.addClass("d-flex");
btnCancel.addClass("loaded");
},
off() {
btnCancel.removeClass("loaded");
searchWrapper.removeClass("d-flex");
btnSbTrigger.removeClass("unloaded");
topbarTitle.removeClass("unloaded");
btnSearchTrigger.removeClass("unloaded");
}
};
}());
const resultSwitch = (function () {
let visible = false;
return {
on() {
if (!visible) {
// the block method must be called before $(#main) unloaded.
scrollBlocker.block();
resultWrapper.removeClass("unloaded");
main.addClass("unloaded");
visible = true;
}
},
off() {
if (visible) {
results.empty();
if (hints.hasClass("unloaded")) {
hints.removeClass("unloaded");
}
resultWrapper.addClass("unloaded");
main.removeClass("unloaded");
// now the release method must be called after $(#main) display
scrollBlocker.release();
input.val("");
visible = false;
}
}
};
}());
function isMobileView() {
return btnCancel.hasClass("loaded");
2019-09-30 15:38:41 +03:00
}
2022-10-25 14:26:44 +03:00
btnSearchTrigger.on('click',function () {
2022-10-25 14:26:44 +03:00
mobileSearchBar.on();
resultSwitch.on();
input.trigger('focus');
2022-10-25 14:26:44 +03:00
});
btnCancel.on('click',function () {
2022-10-25 14:26:44 +03:00
mobileSearchBar.off();
resultSwitch.off();
});
input.on('focus',function () {
2022-10-25 14:26:44 +03:00
searchWrapper.addClass("input-focus");
});
input.on('focusout', function () {
2022-10-25 14:26:44 +03:00
searchWrapper.removeClass("input-focus");
});
input.on("input", () => {
if (input.val() === "") {
if (isMobileView()) {
hints.removeClass("unloaded");
} else {
resultSwitch.off();
}
} else {
resultSwitch.on();
if (isMobileView()) {
hints.addClass("unloaded");
}
}
});
2019-09-30 15:38:41 +03:00
2020-08-19 07:26:45 +03:00
});