web/_javascript/commons/search-display.js

130 lines
3 KiB
JavaScript
Raw Normal View History

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