/*! Aggregation of common JS. Chirpy@2.3 https://github.com/cotes2020/jekyll-theme-chirpy © 2020 Cotes Chung MIT License */ /* Reference: https://bootsnipp.com/snippets/featured/link-to-top-page */ $(function() { $(window).scroll(() => { if ($(this).scrollTop() > 50 && $("#sidebar-trigger").css("display") === "none") { $("#back-to-top").fadeIn(); } else { $("#back-to-top").fadeOut(); } }); $("#back-to-top").click(() => { $("body,html").animate({ scrollTop: 0 }, 800); return false; }); }); /* * This script make #search-result-wrapper switch to unloaded or shown automatically. * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2018-2019 Cotes Chung * MIT License */ $(function() { var btnSbTrigger = $("#sidebar-trigger"); var btnSearchTrigger = $("#search-trigger"); var btnCancel = $("#search-cancel"); var btnClear = $("#search-cleaner"); 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"); /*--- Actions in small screens (Sidebar unloaded) ---*/ var scrollBlocker = (function() { var offset = 0; return { block() { offset = $(window).scrollTop(); }, release() { $("html,body").scrollTop(offset); }, getOffset() { return offset; } }; }()); var 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"); } }; }()); var resultSwitch = (function() { var visable = false; return { on() { if (!visable) { resultWrapper.removeClass("unloaded"); main.addClass("hidden"); visable = true; scrollBlocker.block(); } }, off() { if (visable) { results.empty(); if (hints.hasClass("unloaded")) { hints.removeClass("unloaded"); } resultWrapper.addClass("unloaded"); btnClear.removeClass("visable"); main.removeClass("hidden"); input.val(""); visable = false; scrollBlocker.release(); } }, isVisable() { return visable; } }; }()); function isMobileView() { return btnCancel.hasClass("loaded"); } btnSearchTrigger.click(function() { mobileSearchBar.on(); resultSwitch.on(); input.focus(); }); btnCancel.click(function() { mobileSearchBar.off(); resultSwitch.off(); }); input.focus(function() { searchWrapper.addClass("input-focus"); }); input.focusout(function() { searchWrapper.removeClass("input-focus"); }); input.on("keyup", function(e) { if (e.keyCode === 8 && input.val() === "") { if (!isMobileView()) { resultSwitch.off(); } else { hints.removeClass("unloaded"); } } else { if (input.val() !== "") { resultSwitch.on(); if (!btnClear.hasClass("visible")) { btnClear.addClass("visable"); } if (isMobileView()) { hints.addClass("unloaded"); } } } }); btnClear.on("click", function() { input.val(""); if (isMobileView()) { hints.removeClass("unloaded"); results.empty(); } else { resultSwitch.off(); } input.focus(); btnClear.removeClass("visable"); }); }); /** * Expand or close the sidebar in mobile screens. * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2018-2019 Cotes Chung * MIT License */ $(function() { var sidebarUtil = (function() { const ATTR_DISPLAY = "sidebar-display"; var isExpanded = false; var body = $("body"); return { toggle() { if (isExpanded === false) { body.attr(ATTR_DISPLAY, ""); } else { body.removeAttr(ATTR_DISPLAY); } isExpanded = !isExpanded; } }; }()); $("#sidebar-trigger").click(sidebarUtil.toggle); $("#mask").click(sidebarUtil.toggle); }); /* * Hide Header on scroll down * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2018-2019 Cotes Chung * MIT License */ $(function() { var didScroll; var lastScrollTop = 0; var delta = 5; var topbarHeight = $("#topbar-wrapper").outerHeight(); function hasScrolled() { var st = $(this).scrollTop(); /* Make sure they scroll more than delta */ if (Math.abs(lastScrollTop - st) <= delta) { return; } if (st > lastScrollTop && st > topbarHeight) { /* Scroll Down */ $("#topbar-wrapper").removeClass("topbar-down").addClass("topbar-up"); if ($("#toc-wrapper").length > 0) { $("#toc-wrapper").removeClass("topbar-down"); } if ($(".access").length > 0) { $(".access").removeClass("topbar-down"); } if ($("#search-input").is(":focus")) { $("#search-input").blur(); /* remove focus */ } } else { /* Scroll Up */ if (st + $(window).height() < $(document).height()) { $("#topbar-wrapper").removeClass("topbar-up").addClass("topbar-down"); if ($("#toc-wrapper").length > 0) { $("#toc-wrapper").addClass("topbar-down"); } if ($(".access").length > 0) { $(".access").addClass("topbar-down"); } } } lastScrollTop = st; } $(window).scroll(function(event) { if ($("#topbar-title").is(":hidden")) { /* Not in small screens */ didScroll = true; } }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 250); }); /* * Topbar title auto change while scrolling in mobile screens. * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2018-2019 Cotes Chung * MIT License */ $(function() { var DEFAULT = $("#topbar-title").text().trim(); var title = ($("div.post>h1").length > 0) ? $("div.post>h1").text().trim() : $("h1").text().trim(); if ($("#page-category").length || $("#page-tag").length) { /* The title in Category or Tag page will be "