@@ -10,9 +10,10 @@ var timerModule = angular.module('timer', [])
1010 countdownattr : '=countdown' ,
1111 finishCallback : '&finishCallback' ,
1212 autoStart : '&autoStart' ,
13+ language : '@?' ,
1314 maxTimeUnit : '='
1415 } ,
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 ) {
1617
1718 // Checking for trim function since IE8 doesn't have it
1819 // If not a function, create tirm with RegEx to mimic native trim
@@ -27,6 +28,18 @@ var timerModule = angular.module('timer', [])
2728 //backward and forward compatibility.
2829 $scope . autoStart = $attrs . autoStart || $attrs . autostart ;
2930
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+
3043 if ( $element . html ( ) . trim ( ) . length === 0 ) {
3144 $element . append ( $compile ( '<span>' + $interpolate . startSymbol ( ) + 'millis' + $interpolate . endSymbol ( ) + '</span>' ) ( $scope ) ) ;
3245 } else {
@@ -54,11 +67,11 @@ var timerModule = angular.module('timer', [])
5467 $scope . $on ( 'timer-clear' , function ( ) {
5568 $scope . clear ( ) ;
5669 } ) ;
57-
70+
5871 $scope . $on ( 'timer-reset' , function ( ) {
5972 $scope . reset ( ) ;
6073 } ) ;
61-
74+
6275 $scope . $on ( 'timer-set-countdown' , function ( e , countdown ) {
6376 $scope . countdown = countdown ;
6477 } ) ;
@@ -76,8 +89,8 @@ var timerModule = angular.module('timer', [])
7689 } ) ;
7790
7891 $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 ;
8194 if ( ! $scope . countdown ) {
8295 $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
8396 }
@@ -91,7 +104,7 @@ var timerModule = angular.module('timer', [])
91104 if ( $scope . countdownattr ) {
92105 $scope . countdown += 1 ;
93106 }
94- $scope . startTime = new Date ( ) - ( $scope . stoppedTime - $scope . startTime ) ;
107+ $scope . startTime = moment ( ) . diff ( ( moment ( $scope . stoppedTime ) . diff ( moment ( $scope . startTime ) ) ) ) ;
95108 tick ( ) ;
96109 $scope . isRunning = true ;
97110 } ;
@@ -104,31 +117,37 @@ var timerModule = angular.module('timer', [])
104117
105118 $scope . clear = $element [ 0 ] . clear = function ( ) {
106119 // same as stop but without the event being triggered
107- $scope . stoppedTime = new Date ( ) ;
120+ $scope . stoppedTime = moment ( ) ;
108121 resetTimeout ( ) ;
109122 $scope . timeoutId = null ;
110123 $scope . isRunning = false ;
111124 } ;
112125
113126 $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 ;
116129 $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
117130 resetTimeout ( ) ;
118131 tick ( ) ;
119132 $scope . isRunning = false ;
120133 $scope . clear ( ) ;
121134 } ;
122-
135+
123136 $element . bind ( '$destroy' , function ( ) {
124137 resetTimeout ( ) ;
125138 $scope . isRunning = false ;
126139 } ) ;
127140
141+
128142 function calculateTimeUnits ( ) {
143+ var timeUnits = { } ; //will contains time with units
144+
129145 if ( $attrs . startTime !== undefined ) {
130- $scope . millis = new Date ( ) - new Date ( $scope . startTimeAttr ) ;
146+ $scope . millis = moment ( ) . diff ( moment ( $scope . startTimeAttr ) ) ;
131147 }
148+
149+ timeUnits = i18nService . getTimeUnits ( $scope . millis ) ;
150+
132151 // compute time values based on maxTimeUnit specification
133152 if ( ! $scope . maxTimeUnit || $scope . maxTimeUnit === 'day' ) {
134153 $scope . seconds = Math . floor ( ( $scope . millis / 1000 ) % 60 ) ;
@@ -180,13 +199,16 @@ var timerModule = angular.module('timer', [])
180199 $scope . daysS = ( $scope . days === 1 ) ? '' : 's' ;
181200 $scope . monthsS = ( $scope . months === 1 ) ? '' : 's' ;
182201 $scope . yearsS = ( $scope . years === 1 ) ? '' : 's' ;
202+
203+
183204 // 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+
190212 //add leading zero if number is smaller than 10
191213 $scope . sseconds = $scope . seconds < 10 ? '0' + $scope . seconds : $scope . seconds ;
192214 $scope . mminutes = $scope . minutes < 10 ? '0' + $scope . minutes : $scope . minutes ;
@@ -229,17 +251,16 @@ var timerModule = angular.module('timer', [])
229251 }
230252 calculateTimeUnits ( ) ;
231253
232- var tick = function ( ) {
254+ var tick = function tick ( ) {
233255
234- $scope . millis = new Date ( ) - $scope . startTime ;
256+ $scope . millis = moment ( ) . diff ( $scope . startTime ) ;
235257 var adjustment = $scope . millis % 1000 ;
236258
237259 if ( $scope . endTimeAttr ) {
238- $scope . millis = $scope . endTime - new Date ( ) ;
260+ $scope . millis = moment ( $scope . endTime ) . diff ( moment ( ) ) ;
239261 adjustment = $scope . interval - $scope . millis % 1000 ;
240262 }
241263
242-
243264 if ( $scope . countdownattr ) {
244265 $scope . millis = $scope . countdown * 1000 ;
245266 }
@@ -279,7 +300,7 @@ var timerModule = angular.module('timer', [])
279300 }
280301 } ]
281302 } ;
282- } ] ) ;
303+ } ] ) ;
283304
284305/* commonjs package manager support (eg componentjs) */
285306if ( typeof module !== "undefined" && typeof exports !== "undefined" && module . exports === exports ) {
0 commit comments