@@ -10,9 +10,10 @@ var timerModule = angular.module('timer', [])
10
10
countdownattr : '=countdown' ,
11
11
finishCallback : '&finishCallback' ,
12
12
autoStart : '&autoStart' ,
13
+ language : '@?' ,
13
14
maxTimeUnit : '='
14
15
} ,
15
- controller : [ '$scope' , '$element' , '$attrs' , '$timeout' , '$interpolate' , function ( $scope , $element , $attrs , $timeout , $interpolate ) {
16
+ controller : [ '$scope' , '$element' , '$attrs' , '$timeout' , 'I18nService' , ' $interpolate', function ( $scope , $element , $attrs , $timeout , I18nService , $interpolate ) {
16
17
17
18
// Checking for trim function since IE8 doesn't have it
18
19
// If not a function, create tirm with RegEx to mimic native trim
@@ -27,6 +28,18 @@ var timerModule = angular.module('timer', [])
27
28
//backward and forward compatibility.
28
29
$scope . autoStart = $attrs . autoStart || $attrs . autostart ;
29
30
31
+
32
+ $scope . language = $scope . language || 'en' ;
33
+
34
+ //allow to change the language of the directive while already launched
35
+ $scope . $watch ( 'language' , function ( ) {
36
+ i18nService . init ( $scope . language ) ;
37
+ } ) ;
38
+
39
+ //init momentJS i18n, default english
40
+ var i18nService = new I18nService ( ) ;
41
+ i18nService . init ( $scope . language ) ;
42
+
30
43
if ( $element . html ( ) . trim ( ) . length === 0 ) {
31
44
$element . append ( $compile ( '<span>' + $interpolate . startSymbol ( ) + 'millis' + $interpolate . endSymbol ( ) + '</span>' ) ( $scope ) ) ;
32
45
} else {
@@ -54,11 +67,11 @@ var timerModule = angular.module('timer', [])
54
67
$scope . $on ( 'timer-clear' , function ( ) {
55
68
$scope . clear ( ) ;
56
69
} ) ;
57
-
70
+
58
71
$scope . $on ( 'timer-reset' , function ( ) {
59
72
$scope . reset ( ) ;
60
73
} ) ;
61
-
74
+
62
75
$scope . $on ( 'timer-set-countdown' , function ( e , countdown ) {
63
76
$scope . countdown = countdown ;
64
77
} ) ;
@@ -76,8 +89,8 @@ var timerModule = angular.module('timer', [])
76
89
} ) ;
77
90
78
91
$scope . start = $element [ 0 ] . start = function ( ) {
79
- $scope . startTime = $scope . startTimeAttr ? new Date ( $scope . startTimeAttr ) : new Date ( ) ;
80
- $scope . endTime = $scope . endTimeAttr ? new Date ( $scope . endTimeAttr ) : null ;
92
+ $scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
93
+ $scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
81
94
if ( ! $scope . countdown ) {
82
95
$scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
83
96
}
@@ -91,7 +104,7 @@ var timerModule = angular.module('timer', [])
91
104
if ( $scope . countdownattr ) {
92
105
$scope . countdown += 1 ;
93
106
}
94
- $scope . startTime = new Date ( ) - ( $scope . stoppedTime - $scope . startTime ) ;
107
+ $scope . startTime = moment ( ) . diff ( ( moment ( $scope . stoppedTime ) . diff ( moment ( $scope . startTime ) ) ) ) ;
95
108
tick ( ) ;
96
109
$scope . isRunning = true ;
97
110
} ;
@@ -104,31 +117,37 @@ var timerModule = angular.module('timer', [])
104
117
105
118
$scope . clear = $element [ 0 ] . clear = function ( ) {
106
119
// same as stop but without the event being triggered
107
- $scope . stoppedTime = new Date ( ) ;
120
+ $scope . stoppedTime = moment ( ) ;
108
121
resetTimeout ( ) ;
109
122
$scope . timeoutId = null ;
110
123
$scope . isRunning = false ;
111
124
} ;
112
125
113
126
$scope . reset = $element [ 0 ] . reset = function ( ) {
114
- $scope . startTime = $scope . startTimeAttr ? new Date ( $scope . startTimeAttr ) : new Date ( ) ;
115
- $scope . endTime = $scope . endTimeAttr ? new Date ( $scope . endTimeAttr ) : null ;
127
+ $scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
128
+ $scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
116
129
$scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
117
130
resetTimeout ( ) ;
118
131
tick ( ) ;
119
132
$scope . isRunning = false ;
120
133
$scope . clear ( ) ;
121
134
} ;
122
-
135
+
123
136
$element . bind ( '$destroy' , function ( ) {
124
137
resetTimeout ( ) ;
125
138
$scope . isRunning = false ;
126
139
} ) ;
127
140
141
+
128
142
function calculateTimeUnits ( ) {
143
+ var timeUnits = { } ; //will contains time with units
144
+
129
145
if ( $attrs . startTime !== undefined ) {
130
- $scope . millis = new Date ( ) - new Date ( $scope . startTimeAttr ) ;
146
+ $scope . millis = moment ( ) . diff ( moment ( $scope . startTimeAttr ) ) ;
131
147
}
148
+
149
+ timeUnits = i18nService . getTimeUnits ( $scope . millis ) ;
150
+
132
151
// compute time values based on maxTimeUnit specification
133
152
if ( ! $scope . maxTimeUnit || $scope . maxTimeUnit === 'day' ) {
134
153
$scope . seconds = Math . floor ( ( $scope . millis / 1000 ) % 60 ) ;
@@ -180,13 +199,16 @@ var timerModule = angular.module('timer', [])
180
199
$scope . daysS = ( $scope . days === 1 ) ? '' : 's' ;
181
200
$scope . monthsS = ( $scope . months === 1 ) ? '' : 's' ;
182
201
$scope . yearsS = ( $scope . years === 1 ) ? '' : 's' ;
202
+
203
+
183
204
// new plural-singular unit decision functions (for custom units and multilingual support)
184
- $scope . secondUnit = function ( singleSecond , pluralSecond ) { if ( $scope . seconds === 1 ) { if ( singleSecond ) { return singleSecond ; } return 'second' ; } if ( pluralSecond ) { return pluralSecond ; } return 'seconds' ; } ;
185
- $scope . minuteUnit = function ( singleMinute , pluralMinute ) { if ( $scope . minutes === 1 ) { if ( singleMinute ) { return singleMinute ; } return 'minute' ; } if ( pluralMinute ) { return pluralMinute ; } return 'minutes' ; } ;
186
- $scope . hourUnit = function ( singleHour , pluralHour ) { if ( $scope . hours === 1 ) { if ( singleHour ) { return singleHour ; } return 'hour' ; } if ( pluralHour ) { return pluralHour ; } return 'hours' ; } ;
187
- $scope . dayUnit = function ( singleDay , pluralDay ) { if ( $scope . days === 1 ) { if ( singleDay ) { return singleDay ; } return 'day' ; } if ( pluralDay ) { return pluralDay ; } return 'days' ; } ;
188
- $scope . monthUnit = function ( singleMonth , pluralMonth ) { if ( $scope . months === 1 ) { if ( singleMonth ) { return singleMonth ; } return 'month' ; } if ( pluralMonth ) { return pluralMonth ; } return 'months' ; } ;
189
- $scope . yearUnit = function ( singleYear , pluralYear ) { if ( $scope . years === 1 ) { if ( singleYear ) { return singleYear ; } return 'year' ; } if ( pluralYear ) { return pluralYear ; } return 'years' ; } ;
205
+ $scope . secondUnit = timeUnits . seconds ;
206
+ $scope . minuteUnit = timeUnits . minutes ;
207
+ $scope . hourUnit = timeUnits . hours ;
208
+ $scope . dayUnit = timeUnits . days ;
209
+ $scope . monthUnit = timeUnits . months ;
210
+ $scope . yearUnit = timeUnits . years ;
211
+
190
212
//add leading zero if number is smaller than 10
191
213
$scope . sseconds = $scope . seconds < 10 ? '0' + $scope . seconds : $scope . seconds ;
192
214
$scope . mminutes = $scope . minutes < 10 ? '0' + $scope . minutes : $scope . minutes ;
@@ -229,17 +251,16 @@ var timerModule = angular.module('timer', [])
229
251
}
230
252
calculateTimeUnits ( ) ;
231
253
232
- var tick = function ( ) {
254
+ var tick = function tick ( ) {
233
255
234
- $scope . millis = new Date ( ) - $scope . startTime ;
256
+ $scope . millis = moment ( ) . diff ( $scope . startTime ) ;
235
257
var adjustment = $scope . millis % 1000 ;
236
258
237
259
if ( $scope . endTimeAttr ) {
238
- $scope . millis = $scope . endTime - new Date ( ) ;
260
+ $scope . millis = moment ( $scope . endTime ) . diff ( moment ( ) ) ;
239
261
adjustment = $scope . interval - $scope . millis % 1000 ;
240
262
}
241
263
242
-
243
264
if ( $scope . countdownattr ) {
244
265
$scope . millis = $scope . countdown * 1000 ;
245
266
}
@@ -279,7 +300,7 @@ var timerModule = angular.module('timer', [])
279
300
}
280
301
} ]
281
302
} ;
282
- } ] ) ;
303
+ } ] ) ;
283
304
284
305
/* commonjs package manager support (eg componentjs) */
285
306
if ( typeof module !== "undefined" && typeof exports !== "undefined" && module . exports === exports ) {
0 commit comments