@@ -12,7 +12,19 @@ var timerModule = angular.module('timer', [])
12
12
autoStart : '&autoStart' ,
13
13
language : '@?' ,
14
14
fallback : '@?' ,
15
- maxTimeUnit : '='
15
+ maxTimeUnit : '=' ,
16
+ seconds : '=?' ,
17
+ minutes : '=?' ,
18
+ hours : '=?' ,
19
+ days : '=?' ,
20
+ months : '=?' ,
21
+ years : '=?' ,
22
+ secondsS : '=?' ,
23
+ minutesS : '=?' ,
24
+ hoursS : '=?' ,
25
+ daysS : '=?' ,
26
+ monthsS : '=?' ,
27
+ yearsS : '=?'
16
28
} ,
17
29
controller : [ '$scope' , '$element' , '$attrs' , '$timeout' , 'I18nService' , '$interpolate' , 'progressBarService' , function ( $scope , $element , $attrs , $timeout , I18nService , $interpolate , progressBarService ) {
18
30
@@ -57,7 +69,7 @@ var timerModule = angular.module('timer', [])
57
69
$scope . startTime = null ;
58
70
$scope . endTime = null ;
59
71
$scope . timeoutId = null ;
60
- $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) >= 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
72
+ $scope . countdown = angular . isNumber ( $scope . countdownattr ) && parseInt ( $scope . countdownattr , 10 ) >= 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
61
73
$scope . isRunning = false ;
62
74
63
75
$scope . $on ( 'timer-start' , function ( ) {
@@ -102,49 +114,80 @@ var timerModule = angular.module('timer', [])
102
114
}
103
115
} ) ;
104
116
105
- $scope . start = $element [ 0 ] . start = function ( ) {
117
+ $scope . start = function ( ) {
106
118
$scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
107
119
$scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
108
- if ( ! $scope . countdown ) {
109
- $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
120
+ if ( ! angular . isNumber ( $scope . countdown ) ) {
121
+ $scope . countdown = angular . isNumber ( $scope . countdownattr ) && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
110
122
}
111
123
resetTimeout ( ) ;
112
124
tick ( ) ;
113
125
$scope . isRunning = true ;
126
+ $scope . $emit ( 'timer-started' , {
127
+ timeoutId : $scope . timeoutId ,
128
+ millis : $scope . millis ,
129
+ seconds : $scope . seconds ,
130
+ minutes : $scope . minutes ,
131
+ hours : $scope . hours ,
132
+ days : $scope . days
133
+ } ) ;
114
134
} ;
115
135
116
- $scope . resume = $element [ 0 ] . resume = function ( ) {
136
+ $scope . resume = function ( ) {
117
137
resetTimeout ( ) ;
118
138
if ( $scope . countdownattr ) {
119
139
$scope . countdown += 1 ;
120
140
}
121
141
$scope . startTime = moment ( ) . diff ( ( moment ( $scope . stoppedTime ) . diff ( moment ( $scope . startTime ) ) ) ) ;
122
142
tick ( ) ;
123
143
$scope . isRunning = true ;
144
+ $scope . $emit ( 'timer-started' , {
145
+ timeoutId : $scope . timeoutId ,
146
+ millis : $scope . millis ,
147
+ seconds : $scope . seconds ,
148
+ minutes : $scope . minutes ,
149
+ hours : $scope . hours ,
150
+ days : $scope . days
151
+ } ) ;
124
152
} ;
125
153
126
- $scope . stop = $scope . pause = $element [ 0 ] . stop = $element [ 0 ] . pause = function ( ) {
154
+ $scope . stop = $scope . pause = function ( ) {
127
155
var timeoutId = $scope . timeoutId ;
128
156
$scope . clear ( ) ;
129
- $scope . $emit ( 'timer-stopped' , { timeoutId : timeoutId , millis : $scope . millis , seconds : $scope . seconds , minutes : $scope . minutes , hours : $scope . hours , days : $scope . days } ) ;
157
+ $scope . $emit ( 'timer-stopped' , {
158
+ timeoutId : timeoutId ,
159
+ millis : $scope . millis ,
160
+ seconds : $scope . seconds ,
161
+ minutes : $scope . minutes ,
162
+ hours : $scope . hours ,
163
+ days : $scope . days
164
+ } ) ;
130
165
} ;
131
166
132
- $scope . clear = $element [ 0 ] . clear = function ( ) {
167
+ $scope . clear = function ( ) {
133
168
// same as stop but without the event being triggered
134
169
$scope . stoppedTime = moment ( ) ;
135
170
resetTimeout ( ) ;
136
171
$scope . timeoutId = null ;
137
172
$scope . isRunning = false ;
138
173
} ;
139
174
140
- $scope . reset = $element [ 0 ] . reset = function ( ) {
175
+ $scope . reset = function ( ) {
141
176
$scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
142
177
$scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
143
- $scope . countdown = $scope . countdownattr && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
178
+ $scope . countdown = angular . isNumber ( $scope . countdownattr ) && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
144
179
resetTimeout ( ) ;
145
180
tick ( ) ;
146
181
$scope . isRunning = false ;
147
182
$scope . clear ( ) ;
183
+ $scope . $emit ( 'timer-reset' , {
184
+ timeoutId : timeoutId ,
185
+ millis : $scope . millis ,
186
+ seconds : $scope . seconds ,
187
+ minutes : $scope . minutes ,
188
+ hours : $scope . hours ,
189
+ days : $scope . days
190
+ } ) ;
148
191
} ;
149
192
150
193
$element . bind ( '$destroy' , function ( ) {
@@ -237,18 +280,15 @@ var timerModule = angular.module('timer', [])
237
280
if ( $scope . countdownattr ) {
238
281
$scope . millis = $scope . countdownattr * 1000 ;
239
282
240
- $scope . addCDSeconds = $element [ 0 ] . addCDSeconds = function ( extraSeconds ) {
283
+ $scope . addCDSeconds = function ( extraSeconds ) {
241
284
$scope . countdown += extraSeconds ;
242
- $scope . $digest ( ) ;
243
285
if ( ! $scope . isRunning ) {
244
286
$scope . start ( ) ;
245
287
}
246
288
} ;
247
289
248
290
$scope . $on ( 'timer-add-cd-seconds' , function ( e , extraSeconds ) {
249
- $timeout ( function ( ) {
250
- $scope . addCDSeconds ( extraSeconds ) ;
251
- } ) ;
291
+ $scope . addCDSeconds ( extraSeconds ) ;
252
292
} ) ;
253
293
254
294
$scope . $on ( 'timer-set-countdown-seconds' , function ( e , countdownSeconds ) {
@@ -294,11 +334,20 @@ var timerModule = angular.module('timer', [])
294
334
295
335
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
296
336
$scope . timeoutId = setTimeout ( function ( ) {
297
- tick ( ) ;
298
- $scope . $digest ( ) ;
337
+ tick ( ) ;
338
+ // since you choose not to use $timeout, at least preserve angular cycle two way data binding
339
+ // by calling $scope.$apply() instead of $scope.$digest()
340
+ $scope . $apply ( ) ;
299
341
} , $scope . interval - adjustment ) ;
300
342
301
- $scope . $emit ( 'timer-tick' , { timeoutId : $scope . timeoutId , millis : $scope . millis } ) ;
343
+ $scope . $emit ( 'timer-tick' , {
344
+ timeoutId : $scope . timeoutId ,
345
+ millis : $scope . millis ,
346
+ seconds : $scope . seconds ,
347
+ minutes : $scope . minutes ,
348
+ hours : $scope . hours ,
349
+ days : $scope . days
350
+ } ) ;
302
351
303
352
if ( $scope . countdown > 0 ) {
304
353
$scope . countdown -- ;
@@ -325,7 +374,50 @@ var timerModule = angular.module('timer', [])
325
374
}
326
375
} ]
327
376
} ;
328
- } ] ) ;
377
+ } ] )
378
+ . directive ( 'timerControls' , function ( ) {
379
+ return {
380
+ restrict : 'EA' ,
381
+ scope : true ,
382
+ controller : [ '$scope' , function ( $scope ) {
383
+ $scope . timerStatus = "reset" ;
384
+ $scope . $on ( 'timer-started' , function ( ) {
385
+ $scope . timerStatus = "started" ;
386
+ } ) ;
387
+ $scope . $on ( 'timer-stopped' , function ( ) {
388
+ $scope . timerStatus = "stopped" ;
389
+ } ) ;
390
+ $scope . $on ( 'timer-reset' , function ( ) {
391
+ $scope . timerStatus = "reset" ;
392
+ } ) ;
393
+ $scope . timerStart = function ( ) {
394
+ $scope . $broadcast ( 'timer-start' ) ;
395
+ } ;
396
+ $scope . timerStop = function ( ) {
397
+ $scope . $broadcast ( 'timer-stop' ) ;
398
+ } ;
399
+ $scope . timerResume = function ( ) {
400
+ $scope . $broadcast ( 'timer-resume' ) ;
401
+ } ;
402
+ $scope . timerToggle = function ( ) {
403
+ switch ( $scope . timerStatus ) {
404
+ case "started" :
405
+ $scope . timerStop ( ) ;
406
+ break ;
407
+ case "stopped" :
408
+ $scope . timerResume ( ) ;
409
+ break ;
410
+ case "reset" :
411
+ $scope . timerStart ( ) ;
412
+ break ;
413
+ }
414
+ } ;
415
+ $scope . timerAddCDSeconds = function ( extraSeconds ) {
416
+ $scope . $broadcast ( 'timer-add-cd-seconds' , extraSeconds ) ;
417
+ } ;
418
+ } ]
419
+ } ;
420
+ } ) ;
329
421
330
422
/* commonjs package manager support (eg componentjs) */
331
423
if ( typeof module !== "undefined" && typeof exports !== "undefined" && module . exports === exports ) {
0 commit comments