text: /*! JS group for layout Home or Post Chirpy v2.3 https://github.com/cotes2020/jekyll-theme-chirpy © 2020 Cotes Chung MIT License */ /*! 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 */ $(window).scroll(function() { if ($(this).scrollTop() > 50 && $('#sidebar-trigger').css('display') == 'none') { $('#back-to-top').fadeIn(); } else { $('#back-to-top').fadeOut(); } }); $(function() { $('#back-to-top').click(function() { $('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: function() { offset = $(window).scrollTop(); }, release: function() { $('html,body').scrollTop(offset); }, getOffset: function() { return offset; } } })(); var mobileSearchBar = (function() { return { on: function() { btnSbTrigger.addClass('unloaded'); topbarTitle.addClass('unloaded'); btnSearchTrigger.addClass('unloaded'); searchWrapper.addClass('d-flex'); btnCancel.addClass('loaded'); }, off: function() { btnCancel.removeClass('loaded'); searchWrapper.removeClass('d-flex'); btnSbTrigger.removeClass('unloaded'); topbarTitle.removeClass('unloaded'); btnSearchTrigger.removeClass('unloaded'); } } })(); var resultSwitch = (function() { var visable = false; return { on: function() { if (!visable) { resultWrapper.removeClass('unloaded'); main.addClass('hidden'); visable = true; scrollBlocker.block(); } }, off: function() { 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: function() { 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: function() { 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(); $(window).scroll(function(event) { if ($("#topbar-title").is(":hidden")) { /* Not in small screens */ didScroll = true; } }); setInterval(function() { if (didScroll) { hasScrolled(); didScroll = false; } }, 250); 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; } }); /* * 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 '<title> <count_of_posts>' */ if (/\s/.test(title)) { title = title.replace(/[0-9]/g, '').trim(); } } /* Replace topbar title while scroll screens. */ $(window).scroll(function () { if ($("#post-list").length /* in Home page */ || $("div.post>h1").is(":hidden") /* is tab pages */ || $("#topbar-title").is(":hidden") /* not mobile screens */ || $("#sidebar.sidebar-expand").length) { /* when the sidebar trigger is clicked */ return false; } if ($(this).scrollTop() >= 95) { if ($("#topbar-title").text() != title) { $("#topbar-title").text(title); } } else { if ($("#topbar-title").text() != DEFAULT) { $("#topbar-title").text(DEFAULT); } } }); /* Click title remove hover effect. */ $('#topbar-title').click(function() { $('body,html').animate({scrollTop: 0}, 800); }); }); /* * Copy current page url to clipboard. * v2.1 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2020 Cotes Chung * MIT License */ function copyLink(url) { if (!url || 0 === url.length) url = window.location.href; var $temp = $("<input>"); $("body").append($temp); $temp.val(url).select(); document.execCommand("copy"); $temp.remove(); alert("Link copied successfully!"); } /** * Initial Bootstrap Tooltip. * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2019 Cotes Chung * MIT License */ $(function () { $('[data-toggle="tooltip"]').tooltip(); }); /* * Caculate the Timeago * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2019 Cotes Chung * MIT Licensed */ $(function() { function timeago(iso, isLastmod) { let now = new Date(); let past = new Date(iso); if (past.getFullYear() != now.getFullYear()) { toRefresh -= 1; return past.toLocaleString("en-US", { year: 'numeric', month: 'short', day: 'numeric' }); } if (past.getMonth() != now.getMonth()) { toRefresh -= 1; return past.toLocaleString("en-US", { month: 'short', day: 'numeric' }); } let seconds = Math.floor((now - past) / 1000); let day = Math.floor(seconds / 86400); if (day >= 1) { toRefresh -= 1; return day + " day" + (day > 1 ? "s" : "") + " ago"; } let hour = Math.floor(seconds / 3600); if (hour >= 1) { return hour + " hour" + (hour > 1 ? "s" : "") + " ago"; } let minute = Math.floor(seconds / 60); if (minute >= 1) { return minute + " minute" + (minute > 1 ? "s" : "") + " ago"; } return (isLastmod? "just" : "Just") + " now"; } function updateTimeago() { $(".timeago").each(function() { if ($(this).children("i").length > 0) { var basic = $(this).text(); var isLastmod = $(this).hasClass('lastmod'); var node = $(this).children("i"); var date = node.text(); /* ISO Date: 'YYYY-MM-DDTHH:MM:SSZ' */ $(this).text(timeago(date, isLastmod)); $(this).append(node); } }); if (toRefresh == 0 && intervalId != undefined) { clearInterval(intervalId); /* stop interval */ } return toRefresh; } var toRefresh = $(".timeago").length; if (toRefresh == 0) { return; } if (updateTimeago() > 0) { /* run immediately */ var intervalId = setInterval(updateTimeago, 60000); /* run every minute */ } }); /*! * Bootstrap Table of Contents v1.0.1 (http://afeld.github.io/bootstrap-toc/) * Copyright 2015 Aidan Feldman * Licensed under MIT (https://github.com/afeld/bootstrap-toc/blob/gh-pages/LICENSE.md) */ !function(a){"use strict";window.Toc={helpers:{findOrFilter:function(e,t){var n=e.find(t);return e.filter(t).add(n).filter(":not([data-toc-skip])")},generateUniqueIdBase:function(e){return a(e).text().trim().replace(/\'/gi,"").replace(/[& +$,:;=?@"#{}|^~[`%!'<>\]\.\/\(\)\*\\\n\t\b\v]/g,"-").replace(/-{2,}/g,"-").substring(0,64).replace(/^-+|-+$/gm,"").toLowerCase()||e.tagName.toLowerCase()},generateUniqueId:function(e){for(var t=this.generateUniqueIdBase(e),n=0;;n++){var r=t;if(0<n&&(r+="-"+n),!document.getElementById(r))return r}},generateAnchor:function(e){if(e.id)return e.id;var t=this.generateUniqueId(e);return e.id=t},createNavList:function(){return a('<ul class="nav navbar-nav"></ul>')},createChildNavList:function(e){var t=this.createNavList();return e.append(t),t},generateNavEl:function(e,t){var n=a('<a class="nav-link"></a>');n.attr("href","#"+e),n.text(t);var r=a("<li></li>");return r.append(n),r},generateNavItem:function(e){var t=this.generateAnchor(e),n=a(e),r=n.data("toc-text")||n.text();return this.generateNavEl(t,r)},getTopLevel:function(e){for(var t=1;t<=6;t++){if(1<this.findOrFilter(e,"h"+t).length)return t}return 1},getHeadings:function(e,t){var n="h"+t,r="h"+(t+1);return this.findOrFilter(e,n+","+r)},getNavLevel:function(e){return parseInt(e.tagName.charAt(1),10)},populateNav:function(r,a,e){var i,s=r,c=this;e.each(function(e,t){var n=c.generateNavItem(t);c.getNavLevel(t)===a?s=r:i&&s===r&&(s=c.createChildNavList(i)),s.append(n),i=n})},parseOps:function(e){var t;return(t=e.jquery?{$nav:e}:e).$scope=t.$scope||a(document.body),t}},init:function(e){(e=this.helpers.parseOps(e)).$nav.attr("data-toggle","toc");var t=this.helpers.createChildNavList(e.$nav),n=this.helpers.getTopLevel(e.$scope),r=this.helpers.getHeadings(e.$scope,n);this.helpers.populateNav(t,n,r)}},a(function(){a('nav[data-toggle="toc"]').each(function(e,t){var n=a(t);Toc.init(n)})})}(jQuery); /* * Hide the empty ToC in posts. * v2.0 * https://github.com/cotes2020/jekyll-theme-chirpy * © 2019 Cotes Chung * MIT Licensed */ $(function() { if ($("#post-wrapper .post-content h1").length == 0 && $("#post-wrapper .post-content h2").length == 0) { $("#toc-wrapper").addClass("unloaded"); } });
  