frontend.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var FLBuilderCountdown;
  2. var FLBuilderCountdownIntervals = FLBuilderCountdownIntervals || [];
  3. (function($) {
  4. /**
  5. * Class for Countdown Module
  6. *
  7. * @since 1.6.4
  8. */
  9. FLBuilderCountdown = function( settings ){
  10. // set params
  11. this.nodeID = settings.id;
  12. this.nodeClass = '.fl-node-' + settings.id;
  13. this.wrapperClass = this.nodeClass + ' .fl-countdown';
  14. this.dateWrapper = this.nodeClass + ' .fl-countdown-days';
  15. this.dateLabel = $( this.dateWrapper + ' .fl-countdown-unit-label' ).data( 'label' );
  16. this.hoursWrapper = this.nodeClass + ' .fl-countdown-hours';
  17. this.hoursLabel = $( this.hoursWrapper + ' .fl-countdown-unit-label' ).data( 'label' );
  18. this.minutesWrapper = this.nodeClass + ' .fl-countdown-minutes';
  19. this.minutesLabel = $( this.minutesWrapper + ' .fl-countdown-unit-label' ).data( 'label' );
  20. this.secondsWrapper = this.nodeClass + ' .fl-countdown-seconds';
  21. this.secondsLabel = $( this.secondsWrapper + ' .fl-countdown-unit-label' ).data( 'label' );
  22. this.timestamp = settings.time;
  23. this.type = settings.type;
  24. // initialize the countdown
  25. this._initCountdown();
  26. };
  27. FLBuilderCountdown.prototype = {
  28. nodeClass : '',
  29. wrapperClass : '',
  30. countdown : '',
  31. dateWrapper : '',
  32. dateLabel : '',
  33. hoursWrapper : '',
  34. hoursLabel : '',
  35. minutesWrapper : '',
  36. minutesLabel : '',
  37. secondsWrapper : '',
  38. secondsLabel : '',
  39. timestamp : '',
  40. _timeInterval : '',
  41. /**
  42. * Gets the defined timestamp and return the remaining time.
  43. *
  44. * @since 1.6.4
  45. * @return {Object}
  46. */
  47. _getTimeRemaining: function( endtime ){
  48. var t = Date.parse( endtime ) - Date.parse( new Date() );
  49. var seconds = Math.floor( (t / 1000) % 60 );
  50. var minutes = Math.floor( (t / 1000 / 60) % 60 );
  51. var hours = Math.floor( (t / (1000 * 60 * 60)) % 24 );
  52. var days = Math.floor( t / (1000 * 60 * 60 * 24) );
  53. return {
  54. 'total' : t,
  55. 'days' : ( days < 10 ) ? ( '0' + days ) : days,
  56. 'hours' : ('0' + hours).slice( -2 ),
  57. 'minutes': ('0' + minutes).slice( -2 ),
  58. 'seconds': ('0' + seconds).slice( -2 )
  59. };
  60. },
  61. /**
  62. * Gets the remaining time and updates the respective DOM elements.
  63. *
  64. * @see _getTimeRemaining()
  65. * @since 1.6.4
  66. * @return void
  67. */
  68. _setTimeRemaining: function(){
  69. var t = this._getTimeRemaining( this.timestamp ),
  70. wrappers = {
  71. days : $( this.dateWrapper ),
  72. hours : $( this.hoursWrapper ),
  73. minutes : $( this.minutesWrapper ),
  74. seconds : $( this.secondsWrapper ),
  75. },
  76. labels = {
  77. days : this.dateLabel,
  78. hours : this.hoursLabel,
  79. minutes : this.minutesLabel,
  80. seconds : this.secondsLabel,
  81. };
  82. if ( t.total <= 0 ) {
  83. clearInterval( this._timeInterval );
  84. $.each( wrappers, function( type, element ){
  85. element.find( '.fl-countdown-unit-number' ).html( '00' );
  86. } );
  87. } else {
  88. $.each( wrappers, function( type, element ){
  89. element.find( '.fl-countdown-unit-number' ).html( t[type] );
  90. var $el = element.find( '.fl-countdown-unit-label' );
  91. var label = parseInt( t[type] ) != 1 ? labels[type].plural : labels[type].singular;
  92. $el.html( label );
  93. } );
  94. }
  95. },
  96. _setCircleCount: function(){
  97. var t = this._getTimeRemaining( this.timestamp ),
  98. max = {
  99. days : 365,
  100. hours : 24,
  101. minutes : 60,
  102. seconds : 60
  103. },
  104. circles = {
  105. days : $( this.dateWrapper ).find( 'svg' ),
  106. hours : $( this.hoursWrapper ).find( 'svg' ),
  107. minutes : $( this.minutesWrapper ).find( 'svg' ),
  108. seconds : $( this.secondsWrapper ).find( 'svg' ),
  109. }
  110. $.each( circles, function( type, element ){
  111. var $circle = element.find( '.fl-number' ),
  112. r = $circle.attr( 'r' ),
  113. circle = Math.PI * (r * 2),
  114. val = t[type],
  115. total = max[type],
  116. stroke = ( 1 - ( val / total ) ) * circle;
  117. $circle.css( { strokeDashoffset: stroke } );
  118. } );
  119. },
  120. /**
  121. * Initialize the logic for the countdown.
  122. *
  123. * @see _setTimeRemaining()
  124. * @since 1.6.4
  125. * @return void
  126. */
  127. _initCountdown: function(){
  128. var self = this;
  129. this._setTimeRemaining();
  130. if ( this.type == 'circle' ) {
  131. this._setCircleCount();
  132. }
  133. clearInterval( FLBuilderCountdownIntervals[ this.nodeID ] );
  134. FLBuilderCountdownIntervals[ this.nodeID ] = setInterval( function(){
  135. self._setTimeRemaining();
  136. if ( self.type == 'circle' ) {
  137. self._setCircleCount();
  138. }
  139. }, 1000 );
  140. },
  141. };
  142. })(jQuery);