web/assets/js/dist/pageviews.min.js

1 line
1.3 KiB
JavaScript
Raw Normal View History

2019-09-30 15:38:41 +03:00
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(d,b){if(d===undefined){return}if($("#post-list").length>0){$(".post-preview").each(function(){var h=$(this).children("h1").children("a").attr("href");var g=countPV(h,d);g=(g==0?1:g);if(!b){$(this).find(".pageviews").text(g)}else{var f=parseInt($(this).find(".pageviews").text());if(g>f){countUp(f,g,$(this).find(".pageviews").attr("id"))}}})}else{if($(".post").length>0){var e=window.location.pathname;var c=countPV(e,d);c=(c==0?1:c);if(!b){$("#pv").text(c)}else{var a=parseInt($("#pv").text());if(c>a){countUp(a,c,"pv")}}}}}$(function(){if($(".pageviews").length>0){var a=false;$.getJSON("/assets/data/pageviews.json",function(b){displayPageviews(b.rows,a);a=true});$.getJSON("/assets/data/proxy.json",function(b){$.ajax({url:b.proxyUrl,dataType:"jsonp",timeout:2000,success:function(c){displayPageviews(c.rows,a)},error:function(e,c,d){console.log("Failed to load pageviews from proxy server.");e.abort();return}})})}});