Format the pv number.

This commit is contained in:
Cotes Chung 2020-03-19 18:56:54 +08:00
parent b4daef2f59
commit 91bb726216
2 changed files with 22 additions and 27 deletions

View File

@ -11,9 +11,9 @@
* MIT License
*/
function countUp(min, max, dest) {
function countUp(min, max, destId) {
if (min < max) {
var numAnim = new CountUp(dest, min, max);
var numAnim = new CountUp(destId, min, max);
if (!numAnim.error) {
numAnim.start();
} else {
@ -22,6 +22,7 @@ function countUp(min, max, dest) {
}
}
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 */
@ -43,6 +44,21 @@ function countPV(path, rows) {
}
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;
@ -54,32 +70,12 @@ function displayPageviews(data) {
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'));
}
}
tacklePV(rows, path, $(this).find('.pageviews'), hasInit);
});
} 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');
}
}
tacklePV(rows, path, $('#pv'), hasInit);
}
}
@ -119,5 +115,4 @@ $(function() {
});
} // endif
});
});

View File

@ -1 +1 @@
function countUp(c,a,b){if(c<a){var d=new CountUp(b,c,a);if(!d.error){d.start()}else{console.error(d.error)}}}function countPV(f,e){var g=f.replace(/\/posts\//g,"").replace(/\//g,".html");var d=0;var c=f.replace(/posts\//g,"");for(var b=0;b<e.length;++b){var a=e[b][0];if(a==f||a==c||a.concat("/")==c||a.slice(a.lastIndexOf("/")+1)===g){d+=parseInt(e[b][1])}}return d}function displayPageviews(e){if(e===undefined){return}var b=getInitStatus();var d=e.rows;if($("#post-list").length>0){$(".post-preview").each(function(){var i=$(this).children("h1").children("a").attr("href");var h=countPV(i,d);h=(h==0?1:h);if(!b){$(this).find(".pageviews").text(h)}else{var g=parseInt($(this).find(".pageviews").text());if(h>g){countUp(g,h,$(this).find(".pageviews").attr("id"))}}})}else{if($(".post").length>0){var f=window.location.pathname;var c=countPV(f,d);c=(c==0?1:c);if(!b){$("#pv").text(c)}else{var a=parseInt($("#pv").text());if(c>a){countUp(a,c,"pv")}}}}}var getInitStatus=(function(){var a=false;return function(){if(a){return true}else{a=true;return false}}})();$(function(){if($(".pageviews").length>0){$.getJSON("/assets/data/pageviews.json",displayPageviews);$.getJSON("/assets/data/proxy.json",function(a){$.ajax({type:"GET",url:a.proxyUrl,dataType:"jsonp",jsonpCallback:"displayPageviews",error:function(b,d,c){console.log("Failed to load pageviews from proxy server: "+c)}})})}});
function countUp(b,a,d){if(b<a){var c=new CountUp(d,b,a);if(!c.error){c.start()}else{console.error(c.error)}}}function countPV(f,e){var g=f.replace(/\/posts\//g,"").replace(/\//g,".html");var d=0;var c=f.replace(/posts\//g,"");for(var b=0;b<e.length;++b){var a=e[b][0];if(a==f||a==c||a.concat("/")==c||a.slice(a.lastIndexOf("/")+1)===g){d+=parseInt(e[b][1])}}return d}function tacklePV(e,f,d,b){var c=countPV(f,e);c=(c==0?1:c);if(!b){d.text(new Intl.NumberFormat().format(c))}else{var a=parseInt(d.text().replace(/,/g,""));if(c>a){countUp(a,c,d.attr("id"))}}}function displayPageviews(c){if(c===undefined){return}var a=getInitStatus();var b=c.rows;if($("#post-list").length>0){$(".post-preview").each(function(){var e=$(this).children("h1").children("a").attr("href");tacklePV(b,e,$(this).find(".pageviews"),a)})}else{if($(".post").length>0){var d=window.location.pathname;tacklePV(b,d,$("#pv"),a)}}}var getInitStatus=(function(){var a=false;return function(){if(a){return true}else{a=true;return false}}})();$(function(){if($(".pageviews").length>0){$.getJSON("/assets/data/pageviews.json",displayPageviews);$.getJSON("/assets/data/proxy.json",function(a){$.ajax({type:"GET",url:a.proxyUrl,dataType:"jsonp",jsonpCallback:"displayPageviews",error:function(b,d,c){console.log("Failed to load pageviews from proxy server: "+c)}})})}});