diff options
Diffstat (limited to 'plugins/jetpack/modules/widgets/milestone/milestone.js')
-rw-r--r-- | plugins/jetpack/modules/widgets/milestone/milestone.js | 67 |
1 files changed, 25 insertions, 42 deletions
diff --git a/plugins/jetpack/modules/widgets/milestone/milestone.js b/plugins/jetpack/modules/widgets/milestone/milestone.js index 9dafafc2..01c761f1 100644 --- a/plugins/jetpack/modules/widgets/milestone/milestone.js +++ b/plugins/jetpack/modules/widgets/milestone/milestone.js @@ -2,54 +2,37 @@ var Milestone = ( function( $ ) { var Milestone = function ( args ) { - var num, - labels = MilestoneConfig.labels; - - this.id = args.id; - this.diff = args.diff; - this.message = args.message; - this.widget = $( '#' + this.id ); - this.widgetContent = this.widget.find( '.milestone-content' ); + var $widget = $( '#' + args.id ), + id = args.id, + refresh = args.refresh * 1000; this.timer = function() { - this.diff = this.diff - 1; - - if ( 63113852 < this.diff ) { // more than 2 years - show in years, one decimal point - num = ( this.diff / 60 / 60 / 24 / 365 ).toFixed( 1 ); - if ( 0 === num.charAt( num.length - 1 ) ) { - num = Math.floor( num ); + var instance = this; + + $.ajax( { + url: MilestoneConfig.api_root + 'jetpack/v4/widgets/' + id, + success: function( result ) { + $widget.find( '.milestone-countdown' ).replaceWith( result.message ); + refresh = result.refresh * 1000; + + if ( ! refresh ) { + return; + } + + setTimeout( + function() { + instance.timer(); + }, + refresh + ); } - this.number = num; - this.label = labels.years; - } else if ( 7775999 < this.diff ) { // fewer than 2 years - show in months - this.number = Math.floor( this.diff / 60 / 60 / 24 / 30 ); - this.label = ( 1 === this.number ) ? labels.month : labels.months; - } else if ( 86399 < this.diff ) { // fewer than 3 months - show in days - this.number = Math.floor( this.diff / 60 / 60 / 24 ) + 1; - this.label = ( 1 === this.number ) ? labels.day : labels.days; - } else if ( 3599 < this.diff ) { // less than 1 day - show in hours - this.number = Math.floor( this.diff / 60 / 60 ); - this.label = ( 1 === this.number ) ? labels.hour : labels.hours; - } else if ( 59 < this.diff ) { // less than 1 hour - show in minutes - this.number = Math.floor( this.diff / 60 ) + 1; - this.label = ( 1 === this.number ) ? labels.minute : labels.minutes; - } else { // less than 1 minute - show in seconds - this.number = this.diff; - this.label = ( 1 === this.number ) ? labels.second : labels.seconds; - } - - this.widget.find( '.difference' ).html( this.number ); - this.widget.find( '.label' ).html( this.label ); + } ); - if ( 1 > this.diff ) { - this.widget.find( '.milestone-countdown' ).replaceWith( '<div class="milestone-message">' + this.message + '</div>' ); - } else { - var instance = this; - setTimeout( function() { instance.timer(); }, 1000 ); - } }; - this.timer(); + if ( refresh > 0 ) { + this.timer(); + } }; return function ( args ) { return new Milestone( args ); |