2019-09-30 15:38:41 +03:00
|
|
|
/*
|
2019-12-31 18:17:27 +03:00
|
|
|
* This script make #search-result-wrapper switch to unloaded or shown automatically.
|
2020-01-02 16:17:49 +03:00
|
|
|
* v2.0
|
|
|
|
* https://github.com/cotes2020/jekyll-theme-chirpy
|
2019-09-30 15:38:41 +03:00
|
|
|
* © 2018-2019 Cotes Chung
|
|
|
|
* MIT License
|
|
|
|
*/
|
|
|
|
|
|
|
|
$(function() {
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
var btnSbTrigger = $("#sidebar-trigger");
|
|
|
|
var btnSearchTrigger = $("#search-trigger");
|
|
|
|
var btnCancel = $("#search-cancel");
|
|
|
|
var btnClear = $("#search-cleaner");
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
var main = $("#main");
|
|
|
|
var topbarTitle = $("#topbar-title");
|
|
|
|
var searchWrapper = $("#search-wrapper");
|
|
|
|
var resultWrapper = $("#search-result-wrapper");
|
|
|
|
var results = $("#search-results");
|
|
|
|
var input = $("#search-input");
|
|
|
|
var hints = $("#search-hints");
|
2019-12-31 18:17:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
/*--- Actions in small screens (Sidebar unloaded) ---*/
|
|
|
|
|
|
|
|
var scrollBlocker = (function() {
|
|
|
|
var offset = 0;
|
|
|
|
return {
|
2020-08-19 07:26:45 +03:00
|
|
|
block() {
|
2019-12-31 18:17:27 +03:00
|
|
|
offset = $(window).scrollTop();
|
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
release() {
|
|
|
|
$("html,body").scrollTop(offset);
|
2019-12-31 18:17:27 +03:00
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
getOffset() {
|
2019-12-31 18:17:27 +03:00
|
|
|
return offset;
|
|
|
|
}
|
2020-08-19 07:26:45 +03:00
|
|
|
};
|
|
|
|
}());
|
2019-12-31 18:17:27 +03:00
|
|
|
|
|
|
|
var 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");
|
2019-12-31 18:17:27 +03:00
|
|
|
},
|
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");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
2020-08-19 07:26:45 +03:00
|
|
|
};
|
|
|
|
}());
|
2019-12-31 18:17:27 +03:00
|
|
|
|
|
|
|
var resultSwitch = (function() {
|
|
|
|
var visable = false;
|
|
|
|
|
|
|
|
return {
|
2020-08-19 07:26:45 +03:00
|
|
|
on() {
|
2019-12-31 18:17:27 +03:00
|
|
|
if (!visable) {
|
2020-08-19 07:26:45 +03:00
|
|
|
resultWrapper.removeClass("unloaded");
|
|
|
|
main.addClass("hidden");
|
2019-12-31 18:17:27 +03:00
|
|
|
|
|
|
|
visable = true;
|
|
|
|
scrollBlocker.block();
|
|
|
|
}
|
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
off() {
|
2019-12-31 18:17:27 +03:00
|
|
|
if (visable) {
|
|
|
|
results.empty();
|
2020-08-19 07:26:45 +03:00
|
|
|
if (hints.hasClass("unloaded")) {
|
|
|
|
hints.removeClass("unloaded");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
2020-08-19 07:26:45 +03:00
|
|
|
resultWrapper.addClass("unloaded");
|
2021-01-23 08:21:40 +03:00
|
|
|
btnClear.removeClass("visible");
|
2020-08-19 07:26:45 +03:00
|
|
|
main.removeClass("hidden");
|
2019-12-31 18:17:27 +03:00
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
input.val("");
|
2019-12-31 18:17:27 +03:00
|
|
|
visable = false;
|
|
|
|
|
|
|
|
scrollBlocker.release();
|
|
|
|
}
|
|
|
|
},
|
2020-08-19 07:26:45 +03:00
|
|
|
isVisable() {
|
2019-12-31 18:17:27 +03:00
|
|
|
return visable;
|
|
|
|
}
|
2020-08-19 07:26:45 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}());
|
2019-09-30 15:38:41 +03:00
|
|
|
|
|
|
|
|
2019-12-31 18:17:27 +03:00
|
|
|
function isMobileView() {
|
2020-08-19 07:26:45 +03:00
|
|
|
return btnCancel.hasClass("loaded");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-12-31 18:17:27 +03:00
|
|
|
btnSearchTrigger.click(function() {
|
|
|
|
mobileSearchBar.on();
|
|
|
|
resultSwitch.on();
|
|
|
|
input.focus();
|
2019-09-30 15:38:41 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
btnCancel.click(function() {
|
2019-12-31 18:17:27 +03:00
|
|
|
mobileSearchBar.off();
|
|
|
|
resultSwitch.off();
|
2019-09-30 15:38:41 +03:00
|
|
|
});
|
|
|
|
|
2019-12-31 18:17:27 +03:00
|
|
|
input.focus(function() {
|
2020-08-19 07:26:45 +03:00
|
|
|
searchWrapper.addClass("input-focus");
|
2019-12-31 18:17:27 +03:00
|
|
|
});
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2019-12-31 18:17:27 +03:00
|
|
|
input.focusout(function() {
|
2020-08-19 07:26:45 +03:00
|
|
|
searchWrapper.removeClass("input-focus");
|
2019-12-31 18:17:27 +03:00
|
|
|
});
|
2019-09-30 15:38:41 +03:00
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
input.on("keyup", function(e) {
|
|
|
|
if (e.keyCode === 8 && input.val() === "") {
|
2019-12-31 18:17:27 +03:00
|
|
|
if (!isMobileView()) {
|
|
|
|
resultSwitch.off();
|
|
|
|
} else {
|
2020-08-19 07:26:45 +03:00
|
|
|
hints.removeClass("unloaded");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
|
|
|
} else {
|
2020-08-19 07:26:45 +03:00
|
|
|
if (input.val() !== "") {
|
2019-12-31 18:17:27 +03:00
|
|
|
resultSwitch.on();
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
if (!btnClear.hasClass("visible")) {
|
2021-01-23 08:21:40 +03:00
|
|
|
btnClear.addClass("visible");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isMobileView()) {
|
2020-08-19 07:26:45 +03:00
|
|
|
hints.addClass("unloaded");
|
2019-12-31 18:17:27 +03:00
|
|
|
}
|
|
|
|
}
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
btnClear.on("click", function() {
|
|
|
|
input.val("");
|
2019-12-31 18:17:27 +03:00
|
|
|
if (isMobileView()) {
|
2020-08-19 07:26:45 +03:00
|
|
|
hints.removeClass("unloaded");
|
2019-12-31 18:17:27 +03:00
|
|
|
results.empty();
|
|
|
|
} else {
|
|
|
|
resultSwitch.off();
|
2019-09-30 15:38:41 +03:00
|
|
|
}
|
2019-12-31 18:17:27 +03:00
|
|
|
input.focus();
|
2021-01-23 08:21:40 +03:00
|
|
|
btnClear.removeClass("visible");
|
2019-09-30 15:38:41 +03:00
|
|
|
});
|
|
|
|
|
2020-08-19 07:26:45 +03:00
|
|
|
});
|