1111 * MIT License
1212 */
1313
14- function countUp ( min , max , dest ) {
14+ function countUp ( min , max , destId ) {
1515 if ( min < max ) {
16- var numAnim = new CountUp ( dest , min , max ) ;
16+ var numAnim = new CountUp ( destId , min , max ) ;
1717 if ( ! numAnim . error ) {
1818 numAnim . start ( ) ;
1919 } else {
@@ -22,6 +22,7 @@ function countUp(min, max, dest) {
2222 }
2323}
2424
25+
2526function countPV ( path , rows ) {
2627 /* path permalink looks like: '/posts/post-title/' */
2728 var fileName = path . replace ( / \/ p o s t s \/ / g, '' ) . replace ( / \/ / g, '.html' ) ; /* e.g. post-title.html */
@@ -43,6 +44,21 @@ function countPV(path, rows) {
4344}
4445
4546
47+ function tacklePV ( rows , path , elem , hasInit ) {
48+ var count = countPV ( path , rows ) ;
49+ count = ( count == 0 ? 1 : count ) ;
50+
51+ if ( ! hasInit ) {
52+ elem . text ( new Intl . NumberFormat ( ) . format ( count ) ) ;
53+ } else {
54+ var initCount = parseInt ( elem . text ( ) . replace ( / , / g, '' ) ) ;
55+ if ( count > initCount ) {
56+ countUp ( initCount , count , elem . attr ( 'id' ) ) ;
57+ }
58+ }
59+ }
60+
61+
4662function displayPageviews ( data ) {
4763 if ( data === undefined ) {
4864 return ;
@@ -54,32 +70,12 @@ function displayPageviews(data) {
5470 if ( $ ( "#post-list" ) . length > 0 ) { // the Home page
5571 $ ( ".post-preview" ) . each ( function ( ) {
5672 var path = $ ( this ) . children ( "h1" ) . children ( "a" ) . attr ( "href" ) ;
57- var count = countPV ( path , rows ) ;
58- count = ( count == 0 ? 1 : count ) ;
59-
60- if ( ! hasInit ) {
61- $ ( this ) . find ( '.pageviews' ) . text ( count ) ;
62- } else {
63- var initCount = parseInt ( $ ( this ) . find ( '.pageviews' ) . text ( ) ) ;
64- if ( count > initCount ) {
65- countUp ( initCount , count , $ ( this ) . find ( '.pageviews' ) . attr ( 'id' ) ) ;
66- }
67- }
73+ tacklePV ( rows , path , $ ( this ) . find ( '.pageviews' ) , hasInit ) ;
6874 } ) ;
6975
7076 } else if ( $ ( ".post" ) . length > 0 ) { // the post
7177 var path = window . location . pathname ;
72- var count = countPV ( path , rows ) ;
73- count = ( count == 0 ? 1 : count ) ;
74-
75- if ( ! hasInit ) {
76- $ ( '#pv' ) . text ( count ) ;
77- } else {
78- var initCount = parseInt ( $ ( '#pv' ) . text ( ) ) ;
79- if ( count > initCount ) {
80- countUp ( initCount , count , 'pv' ) ;
81- }
82- }
78+ tacklePV ( rows , path , $ ( '#pv' ) , hasInit ) ;
8379 }
8480
8581}
@@ -119,5 +115,4 @@ $(function() {
119115 } ) ;
120116
121117 } // endif
122-
123- } ) ;
118+ } ) ;
0 commit comments