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