1
0
mirror of https://github.com/cotes2020/jekyll-theme-chirpy.git synced 2025-12-18 21:53:26 +00:00

Combine local JS.

’yui-compressor’ is no longer used.
This commit is contained in:
Cotes Chung
2020-05-30 00:48:10 +08:00
parent cfb67a1cb9
commit 1a2767ca24
44 changed files with 213 additions and 195 deletions

View File

@@ -0,0 +1,33 @@
/*
* Tab 'Categories' expand/close effect.
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2018-2019 Cotes Chung
* MIT License
*/
$(function() {
var child_prefix = "l_";
var parent_prefix = "h_";
/* close up top-category */
$(".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").addClass("rotate");
$("#" + parent_id).removeClass("hide-border-bottom");
}
});
/* expand the 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").removeClass("rotate");
$("#" + parent_id).addClass("hide-border-bottom");
}
});
});

View File

@@ -0,0 +1,225 @@
/**
* Count pageviews form GA or local cache file.
*
* Dependences:
* - jQuery
* - countUp.js <https://github.com/inorganik/countUp.js>
*
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2018-2019 Cotes Chung
* MIT License
*/
function countUp(min, max, destId) {
if (min < max) {
var numAnim = new CountUp(destId, 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 tacklePV(rows, path, elem, hasInit) {
var count = countPV(path, rows);
count = (count == 0 ? 1 : count);
if (!hasInit) {
elem.text(new Intl.NumberFormat().format(count));
} else {
var initCount = parseInt(elem.text().replace(/,/g, ''));
if (count > initCount) {
countUp(initCount, count, elem.attr('id'));
}
}
}
function displayPageviews(data) {
if (data === undefined) {
return;
}
var hasInit = getInitStatus();
var rows = data.rows;
if ($("#post-list").length > 0) { /* the Home page */
$(".post-preview").each(function() {
var path = $(this).children("h1").children("a").attr("href");
tacklePV(rows, path, $(this).find('.pageviews'), hasInit);
});
} else if ($(".post").length > 0) { /* the post */
var path = window.location.pathname;
tacklePV(rows, path, $('#pv'), hasInit);
}
}
var getInitStatus = (function() {
var hasInit = false;
return function() {
let ret = hasInit;
if (!hasInit) {
hasInit = true;
}
return ret;
}
})();
var PvCache = (function() {
const KEY_PV = "pv";
const KEY_CREATION = "pv-created-date";
const KEY_PV_TYPE = "pv-type";
var PvType = {
ORIGIN: "origin",
PROXY: "proxy"
};
function get(key) {
return localStorage.getItem(key);
}
function set(key, val) {
localStorage.setItem(key, val);
}
return {
getData: function() {
return JSON.parse(localStorage.getItem(KEY_PV) );
},
saveOriginCache: function(pv) {
set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.ORIGIN );
set(KEY_CREATION, new Date().toJSON() );
},
saveProxyCache: function(pv) {
set(KEY_PV, pv);
set(KEY_PV_TYPE, PvType.PROXY );
set(KEY_CREATION, new Date().toJSON() );
},
isOriginCache: function() {
return get(KEY_PV_TYPE) == PvType.ORIGIN;
},
isProxyCache: function() {
return get(KEY_PV_TYPE) == PvType.PROXY;
},
isExpired: function() {
if (PvCache.isOriginCache() ) {
let date = new Date(get(KEY_CREATION));
date.setDate(date.getDate() + 1); /* fetch origin-data every day */
return Date.now() >= date.getTime();
} else if (PvCache.isProxyCache() ) {
let date = new Date(get(KEY_CREATION) );
date.setHours(date.getHours() + 1); /* proxy-data is updated every hour */
return Date.now() >= date.getTime();
}
return false;
},
getAllPagevies: function() {
return PvCache.getData().totalsForAllResults["ga:pageviews"];
},
newerThan: function(pv) {
return PvCache.getAllPagevies() > pv.totalsForAllResults["ga:pageviews"];
}
};
})(); /* PvCache */
function fetchOriginPageviews(pvData) {
if (pvData === undefined) {
return;
}
displayPageviews(pvData);
PvCache.saveOriginCache(JSON.stringify(pvData));
}
function fetchProxyPageviews() {
let proxy = JSON.parse(proxyData); /* see file '/assets/data/pv-data.json' */
$.ajax({
type: 'GET',
url: proxy.url,
dataType: 'jsonp',
jsonpCallback: "displayPageviews",
success: function(data, textStatus, jqXHR) {
PvCache.saveProxyCache(JSON.stringify(data));
},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Failed to load pageviews from proxy server: " + errorThrown);
}
});
}
$(function() {
if ($('.pageviews').length > 0) {
let cache = PvCache.getData();
if (cache) {
if (PvCache.isExpired()) {
if (PvCache.isProxyCache() ) {
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
if (originPvData) {
if (PvCache.newerThan(originPvData)) {
displayPageviews(cache);
} else {
fetchOriginPageviews(originPvData);
}
}
fetchProxyPageviews();
} else if (PvCache.isOriginCache() ) {
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
}
} else { /* still valid */
displayPageviews(cache);
if (PvCache.isOriginCache() ) {
fetchProxyPageviews();
}
}
} else {
let originPvData = pageviews ? JSON.parse(pageviews) : undefined;
fetchOriginPageviews(originPvData);
fetchProxyPageviews();
}
}
});

View File

@@ -0,0 +1,78 @@
/*
* Caculate the Timeago
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 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 */
}
});

14
assets/js/_utils/toc.js Normal file
View File

@@ -0,0 +1,14 @@
/*
* 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");
}
});

View File

@@ -0,0 +1,10 @@
/**
* Initial Bootstrap Tooltip.
* v2.0
* https://github.com/cotes2020/jekyll-theme-chirpy
* © 2019 Cotes Chung
* MIT License
*/
$(function () {
$('[data-toggle="tooltip"]').tooltip();
});