mirror of
https://github.com/cotes2020/jekyll-theme-chirpy.git
synced 2025-12-18 05:41:31 +00:00
Import the framework.
This commit is contained in:
18
assets/js/_src/_commons/back-to-top.js
Normal file
18
assets/js/_src/_commons/back-to-top.js
Normal file
@@ -0,0 +1,18 @@
|
||||
/*
|
||||
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;
|
||||
});
|
||||
});
|
||||
88
assets/js/_src/_commons/search-display.js
Normal file
88
assets/js/_src/_commons/search-display.js
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* This script make #search-result-wrap switch to hidden or shown automatically.
|
||||
* © 2018-2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
|
||||
$(function() {
|
||||
|
||||
var offset = 0;
|
||||
|
||||
var btnCancel = $('#search-wrap + a');
|
||||
var btnSbTrigger = $('#sidebar-trigger');
|
||||
var btnSearchTrigger = $('#search-trigger');
|
||||
var btnCloseSearch = $('#search-wrap + a');
|
||||
var topbarTitle = $('#topbar-title');
|
||||
var searchWrap = $('#search-wrap');
|
||||
|
||||
/*--- Actions in small screens ---*/
|
||||
|
||||
btnSearchTrigger.click(function() {
|
||||
|
||||
offset = $(window).scrollTop();
|
||||
|
||||
$('body').addClass('no-scroll');
|
||||
// $('body').css('top', -offset + 'px');
|
||||
// $('html,body').addClass('input-focus');
|
||||
|
||||
btnSbTrigger.addClass('hidden');
|
||||
topbarTitle.addClass('hidden');
|
||||
btnSearchTrigger.addClass('hidden');
|
||||
|
||||
searchWrap.addClass('shown flex-grow-1');
|
||||
btnCancel.addClass('shown');
|
||||
|
||||
$('#main').addClass('hidden');
|
||||
$('#search-result-wrap').addClass('shown');
|
||||
$('#search-input').focus();
|
||||
|
||||
});
|
||||
|
||||
btnCancel.click(function() {
|
||||
|
||||
btnCancel.removeClass('shown');
|
||||
|
||||
$('#search-input').val('');
|
||||
$('#search-results').empty();
|
||||
|
||||
searchWrap.removeClass('shown flex-grow-1');
|
||||
|
||||
btnSbTrigger.removeClass('hidden');
|
||||
topbarTitle.removeClass('hidden');
|
||||
btnSearchTrigger.removeClass('hidden');
|
||||
|
||||
$('#main').removeClass('hidden');
|
||||
$('#search-result-wrap').removeClass('shown');
|
||||
|
||||
$('body').removeClass('no-scroll');
|
||||
// $('html,body').removeClass('input-focus');
|
||||
|
||||
$('html,body').scrollTop(offset);
|
||||
|
||||
});
|
||||
|
||||
/*--- Actions in large screens. ---*/
|
||||
|
||||
var isShown = false;
|
||||
|
||||
$('#search-input').on('input', function(){
|
||||
if (isShown == false) {
|
||||
offset = $(window).scrollTop();
|
||||
$('body,html').scrollTop(0);
|
||||
$('#search-result-wrap').addClass('shown');
|
||||
$('#main').addClass('hidden');
|
||||
isShown = true;
|
||||
}
|
||||
});
|
||||
|
||||
$('#search-input').on('keyup', function(e){
|
||||
var input = $('#search-input').val();
|
||||
if (e.keyCode == 8 && input == '' && btnCloseSearch.css('display') == 'none') {
|
||||
$('#main').removeClass('hidden');
|
||||
$('#search-result-wrap').removeClass('shown');
|
||||
$('body,html').scrollTop(offset);
|
||||
isShown = false;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
54
assets/js/_src/_commons/sidebar.js
Normal file
54
assets/js/_src/_commons/sidebar.js
Normal file
@@ -0,0 +1,54 @@
|
||||
/**
|
||||
* Expand or close the sidebar in mobile screens.
|
||||
* © 2018-2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
$(function(){
|
||||
|
||||
var isExpanded = false;
|
||||
|
||||
$("#sidebar-trigger").click(function() {
|
||||
if (isExpanded == false) {
|
||||
$("#sidebar").addClass("sidebar-expand");
|
||||
openModal();
|
||||
isExpanded = true;
|
||||
}
|
||||
});
|
||||
|
||||
$("#mask").click(function() {
|
||||
$("#sidebar").removeClass("sidebar-expand");
|
||||
closeModal();
|
||||
isExpanded = false;
|
||||
});
|
||||
|
||||
/**
|
||||
* ModalHelper helpers resolve the modal scrolling issue on mobile devices
|
||||
* https://github.com/twbs/bootstrap/issues/15852
|
||||
* requires document.scrollingElement polyfill https://github.com/yangg/scrolling-element
|
||||
*/
|
||||
var ModalHelper = (function(bodyCls) {
|
||||
var scrollTop;
|
||||
return {
|
||||
afterOpen: function() {
|
||||
scrollTop = document.scrollingElement.scrollTop;
|
||||
document.body.classList.add(bodyCls);
|
||||
document.body.style.top = -scrollTop + 'px';
|
||||
},
|
||||
beforeClose: function() {
|
||||
document.body.classList.remove(bodyCls);
|
||||
// scrollTop lost after set position:fixed, restore it back.
|
||||
document.scrollingElement.scrollTop = scrollTop;
|
||||
document.body.style.top = '';
|
||||
}
|
||||
};
|
||||
})('no-scroll');
|
||||
|
||||
function openModal() {
|
||||
ModalHelper.afterOpen();
|
||||
}
|
||||
|
||||
function closeModal() {
|
||||
ModalHelper.beforeClose();
|
||||
}
|
||||
|
||||
});
|
||||
8
assets/js/_src/_commons/tooltip.js
Normal file
8
assets/js/_src/_commons/tooltip.js
Normal file
@@ -0,0 +1,8 @@
|
||||
/**
|
||||
* Initial Bootstrap Tooltip.
|
||||
* © 2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
$(function () {
|
||||
$('[data-toggle="tooltip"]').tooltip({placement: "auto"});
|
||||
})
|
||||
64
assets/js/_src/_commons/topbar-switch.js
Normal file
64
assets/js/_src/_commons/topbar-switch.js
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* Hide Header on scroll down
|
||||
* © 2018-2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
$(function() {
|
||||
|
||||
var didScroll;
|
||||
var lastScrollTop = 0;
|
||||
var delta = 5;
|
||||
var topbarHeight = $('#topbar').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').removeClass('topbar-down').addClass('topbar-up');
|
||||
|
||||
if ( $('#toc-wrap').length > 0) {
|
||||
$('#toc-wrap').removeClass('topbar-down');
|
||||
}
|
||||
|
||||
if ( $('.panel-group').length > 0) {
|
||||
$('.panel-group').removeClass('topbar-down');
|
||||
}
|
||||
|
||||
if ($('#search-input').is(':focus')) {
|
||||
$('#search-input').blur(); // remove focus
|
||||
}
|
||||
|
||||
} else {
|
||||
// Scroll Up
|
||||
if (st + $(window).height() < $(document).height()) {
|
||||
$('#topbar').removeClass('topbar-up').addClass('topbar-down');
|
||||
if ( $('#toc-wrap').length > 0) {
|
||||
$('#toc-wrap').addClass('topbar-down');
|
||||
}
|
||||
if ( $('.panel-group').length > 0) {
|
||||
$('.panel-group').addClass('topbar-down');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastScrollTop = st;
|
||||
}
|
||||
});
|
||||
44
assets/js/_src/_commons/topbar-title.js
Normal file
44
assets/js/_src/_commons/topbar-title.js
Normal file
@@ -0,0 +1,44 @@
|
||||
/*
|
||||
* Topbar title auto change while scrolling in mobile screens.
|
||||
* © 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);
|
||||
});
|
||||
|
||||
});
|
||||
31
assets/js/_src/category-collapse.js
Normal file
31
assets/js/_src/category-collapse.js
Normal file
@@ -0,0 +1,31 @@
|
||||
/*
|
||||
* Tab 'Categories' expand/close effect.
|
||||
* © 2018-2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
|
||||
$(function() {
|
||||
var child_prefix = "l_";
|
||||
var parent_prefix = "h_";
|
||||
|
||||
// close up Top
|
||||
$(".collapse").on("hide.bs.collapse", function() { // Bootstrap collapse events.
|
||||
var parent_id = parent_prefix + $(this).attr('id').substring(child_prefix.length);
|
||||
if (parent_id) {
|
||||
$("#" + parent_id + " .far.fa-folder-open").attr("class", "far fa-folder fa-fw");
|
||||
$("#" + parent_id + " i.fas.fa-angle-up").addClass("flip");
|
||||
$("#" + parent_id).removeClass("hide-border-bottom");
|
||||
}
|
||||
});
|
||||
|
||||
// expand Top Category
|
||||
$(".collapse").on("show.bs.collapse", function() {
|
||||
var parent_id = parent_prefix + $(this).attr('id').substring(child_prefix.length);
|
||||
if (parent_id) {
|
||||
$("#" + parent_id + " .far.fa-folder").attr("class", "far fa-folder-open fa-fw");
|
||||
$("#" + parent_id + " i.fas.fa-angle-up").removeClass("flip");
|
||||
$("#" + parent_id).addClass("hide-border-bottom");
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
111
assets/js/_src/pageviews.js
Normal file
111
assets/js/_src/pageviews.js
Normal file
@@ -0,0 +1,111 @@
|
||||
/**
|
||||
* Count pageviews form GA or local cache file.
|
||||
*
|
||||
* Dependences:
|
||||
* - jQuery
|
||||
* - countUp.js(https://github.com/inorganik/countUp.js)
|
||||
*
|
||||
* © 2018-2019 Cotes Chung
|
||||
* MIT License
|
||||
*/
|
||||
|
||||
function countUp(min, max, dest) {
|
||||
if (min < max) {
|
||||
var numAnim = new CountUp(dest, min, max);
|
||||
if (!numAnim.error) {
|
||||
numAnim.start();
|
||||
} else {
|
||||
console.error(numAnim.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function countPV(path, rows) {
|
||||
/* path permalink looks like: '/posts/post-title/' */
|
||||
var fileName = path.replace(/\/posts\//g, '').replace(/\//g, '.html'); /* e.g. post-title.html */
|
||||
var count = 0;
|
||||
|
||||
var _v2_url = path.replace(/posts\//g, ''); /* the v2.0+ blog permalink: "/post-title/" */
|
||||
|
||||
for (var i = 0; i < rows.length; ++i) {
|
||||
var gaPath = rows[i][0];
|
||||
if (gaPath == path ||
|
||||
gaPath == _v2_url ||
|
||||
gaPath.concat('/') == _v2_url ||
|
||||
gaPath.slice(gaPath.lastIndexOf('/') + 1) === fileName) { // old permalink record
|
||||
count += parseInt(rows[i][1]);
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
function displayPageviews(rows, hasInit) {
|
||||
if (rows === undefined) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($("#post-list").length > 0) { // the Home page
|
||||
$(".post-preview").each(function() {
|
||||
var path = $(this).children("h1").children("a").attr("href");
|
||||
var count = countPV(path, rows);
|
||||
count = (count == 0 ? 1 : count);
|
||||
|
||||
if (!hasInit) {
|
||||
$(this).find('.pageviews').text(count);
|
||||
} else {
|
||||
var initCount = parseInt($(this).find('.pageviews').text());
|
||||
if (count > initCount) {
|
||||
countUp(initCount, count, $(this).find('.pageviews').attr('id'));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
} else if ($(".post").length > 0) { // the post
|
||||
var path = window.location.pathname;
|
||||
var count = countPV(path, rows);
|
||||
count = (count == 0 ? 1 : count);
|
||||
|
||||
if (!hasInit) {
|
||||
$('#pv').text(count);
|
||||
} else {
|
||||
var initCount = parseInt($('#pv').text());
|
||||
if (count > initCount) {
|
||||
countUp(initCount, count, 'pv');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
$(function() {
|
||||
// load pageview if this page has .pageviews
|
||||
if ($('.pageviews').length > 0) {
|
||||
var hasInit = false;
|
||||
|
||||
// Get data from daily cache.
|
||||
$.getJSON('/assets/data/pageviews.json', function(data) {
|
||||
displayPageviews(data.rows, hasInit);
|
||||
hasInit = true;
|
||||
});
|
||||
|
||||
$.getJSON('/assets/data/proxy.json', function(data) {
|
||||
$.ajax({
|
||||
url: data.proxyUrl,
|
||||
dataType: 'jsonp',
|
||||
timeout: 2000,
|
||||
success: function(data) {
|
||||
displayPageviews(data.rows, hasInit);
|
||||
},
|
||||
error: function(xhr, status, err) {
|
||||
console.log("Failed to load pageviews from proxy server.");
|
||||
xhr.abort();
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
} // endif
|
||||
|
||||
});
|
||||
77
assets/js/_src/timeago.js
Normal file
77
assets/js/_src/timeago.js
Normal file
@@ -0,0 +1,77 @@
|
||||
/*
|
||||
* Caculate the Timeago
|
||||
*
|
||||
* © 2019 Cotes Chung
|
||||
* MIT Licensed
|
||||
*/
|
||||
|
||||
$(function() {
|
||||
|
||||
function timeago(date, isLastmod) {
|
||||
var now = new Date();
|
||||
var past = new Date(date);
|
||||
var seconds = Math.floor((now - past) / 1000);
|
||||
|
||||
var year = Math.floor(seconds / 31536000);
|
||||
if (year >= 1) {
|
||||
return year + " year" + (year > 1 ? "s" : "") + " ago";
|
||||
}
|
||||
|
||||
var month = Math.floor(seconds / 2592000);
|
||||
if (month >= 1) {
|
||||
return month + " month" + (month > 1 ? "s" : "") + " ago";
|
||||
}
|
||||
|
||||
var week = Math.floor(seconds / 604800);
|
||||
if (week >= 1) {
|
||||
return week + " week" + (week > 1 ? "s" : "") + " ago";
|
||||
}
|
||||
|
||||
var day = Math.floor(seconds / 86400);
|
||||
if (day >= 1) {
|
||||
return day + " day" + (day > 1 ? "s" : "") + " ago";
|
||||
}
|
||||
|
||||
var hour = Math.floor(seconds / 3600);
|
||||
if (hour >= 1) {
|
||||
return hour + " hour" + (hour > 1 ? "s" : "") + " ago";
|
||||
}
|
||||
|
||||
var 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 isLastmod = $(this).hasClass('lastmod');
|
||||
var node = $(this).children("i");
|
||||
var date = node.text(); /* ISO Dates: 'YYYY-MM-DDTHH:MM:SSZ' */
|
||||
$(this).text(timeago(date, isLastmod));
|
||||
$(this).append(node);
|
||||
}
|
||||
});
|
||||
|
||||
if (vote == 0 && intervalId != undefined) {
|
||||
clearInterval(intervalId); /* stop interval */
|
||||
}
|
||||
return vote;
|
||||
}
|
||||
|
||||
|
||||
var vote = $(".timeago").length;
|
||||
if (vote == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (updateTimeago() > 0) { /* run immediately */
|
||||
vote = $(".timeago").length; /* resume */
|
||||
var intervalId = setInterval(updateTimeago, 60000); /* loop every minutes */
|
||||
}
|
||||
|
||||
});
|
||||
Reference in New Issue
Block a user