@@ -114,7 +114,7 @@ var timerModule = angular.module('timer', [])
114
114
}
115
115
} ) ;
116
116
117
- $scope . start = $element [ 0 ] . start = function ( ) {
117
+ $scope . start = function ( ) {
118
118
$scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
119
119
$scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
120
120
if ( ! angular . isNumber ( $scope . countdown ) ) {
@@ -123,40 +123,71 @@ var timerModule = angular.module('timer', [])
123
123
resetTimeout ( ) ;
124
124
tick ( ) ;
125
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
+ } ) ;
126
134
} ;
127
135
128
- $scope . resume = $element [ 0 ] . resume = function ( ) {
136
+ $scope . resume = function ( ) {
129
137
resetTimeout ( ) ;
130
138
if ( $scope . countdownattr ) {
131
139
$scope . countdown += 1 ;
132
140
}
133
141
$scope . startTime = moment ( ) . diff ( ( moment ( $scope . stoppedTime ) . diff ( moment ( $scope . startTime ) ) ) ) ;
134
142
tick ( ) ;
135
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
+ } ) ;
136
152
} ;
137
153
138
- $scope . stop = $scope . pause = $element [ 0 ] . stop = $element [ 0 ] . pause = function ( ) {
154
+ $scope . stop = $scope . pause = function ( ) {
139
155
var timeoutId = $scope . timeoutId ;
140
156
$scope . clear ( ) ;
141
- $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
+ } ) ;
142
165
} ;
143
166
144
- $scope . clear = $element [ 0 ] . clear = function ( ) {
167
+ $scope . clear = function ( ) {
145
168
// same as stop but without the event being triggered
146
169
$scope . stoppedTime = moment ( ) ;
147
170
resetTimeout ( ) ;
148
171
$scope . timeoutId = null ;
149
172
$scope . isRunning = false ;
150
173
} ;
151
174
152
- $scope . reset = $element [ 0 ] . reset = function ( ) {
175
+ $scope . reset = function ( ) {
153
176
$scope . startTime = $scope . startTimeAttr ? moment ( $scope . startTimeAttr ) : moment ( ) ;
154
177
$scope . endTime = $scope . endTimeAttr ? moment ( $scope . endTimeAttr ) : null ;
155
178
$scope . countdown = angular . isNumber ( $scope . countdownattr ) && parseInt ( $scope . countdownattr , 10 ) > 0 ? parseInt ( $scope . countdownattr , 10 ) : undefined ;
156
179
resetTimeout ( ) ;
157
180
tick ( ) ;
158
181
$scope . isRunning = false ;
159
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
+ } ) ;
160
191
} ;
161
192
162
193
$element . bind ( '$destroy' , function ( ) {
@@ -249,18 +280,15 @@ var timerModule = angular.module('timer', [])
249
280
if ( $scope . countdownattr ) {
250
281
$scope . millis = $scope . countdownattr * 1000 ;
251
282
252
- $scope . addCDSeconds = $element [ 0 ] . addCDSeconds = function ( extraSeconds ) {
283
+ $scope . addCDSeconds = function ( extraSeconds ) {
253
284
$scope . countdown += extraSeconds ;
254
- $scope . $digest ( ) ;
255
285
if ( ! $scope . isRunning ) {
256
286
$scope . start ( ) ;
257
287
}
258
288
} ;
259
289
260
290
$scope . $on ( 'timer-add-cd-seconds' , function ( e , extraSeconds ) {
261
- $timeout ( function ( ) {
262
- $scope . addCDSeconds ( extraSeconds ) ;
263
- } ) ;
291
+ $scope . addCDSeconds ( extraSeconds ) ;
264
292
} ) ;
265
293
266
294
$scope . $on ( 'timer-set-countdown-seconds' , function ( e , countdownSeconds ) {
@@ -310,7 +338,14 @@ var timerModule = angular.module('timer', [])
310
338
$scope . $digest ( ) ;
311
339
} , $scope . interval - adjustment ) ;
312
340
313
- $scope . $emit ( 'timer-tick' , { timeoutId : $scope . timeoutId , millis : $scope . millis , timerElement : $element [ 0 ] } ) ;
341
+ $scope . $emit ( 'timer-tick' , {
342
+ timeoutId : $scope . timeoutId ,
343
+ millis : $scope . millis ,
344
+ seconds : $scope . seconds ,
345
+ minutes : $scope . minutes ,
346
+ hours : $scope . hours ,
347
+ days : $scope . days
348
+ } ) ;
314
349
315
350
if ( $scope . countdown > 0 ) {
316
351
$scope . countdown -- ;
@@ -337,7 +372,50 @@ var timerModule = angular.module('timer', [])
337
372
}
338
373
} ]
339
374
} ;
340
- } ] ) ;
375
+ } ] )
376
+ . directive ( 'timerControls' , function ( ) {
377
+ return {
378
+ restrict : 'EA' ,
379
+ scope : true ,
380
+ controller : [ '$scope' , function ( $scope ) {
381
+ $scope . timerStatus = "reset" ;
382
+ $scope . $on ( 'timer-started' , function ( ) {
383
+ $scope . timerStatus = "started" ;
384
+ } ) ;
385
+ $scope . $on ( 'timer-stopped' , function ( ) {
386
+ $scope . timerStatus = "stopped" ;
387
+ } ) ;
388
+ $scope . $on ( 'timer-reset' , function ( ) {
389
+ $scope . timerStatus = "reset" ;
390
+ } ) ;
391
+ $scope . timerStart = function ( ) {
392
+ $scope . $broadcast ( 'timer-start' ) ;
393
+ } ;
394
+ $scope . timerStop = function ( ) {
395
+ $scope . $broadcast ( 'timer-stop' ) ;
396
+ } ;
397
+ $scope . timerResume = function ( ) {
398
+ $scope . $broadcast ( 'timer-resume' ) ;
399
+ } ;
400
+ $scope . timerToggle = function ( ) {
401
+ switch ( $scope . timerStatus ) {
402
+ case "started" :
403
+ $scope . timerStop ( ) ;
404
+ break ;
405
+ case "stopped" :
406
+ $scope . timerResume ( ) ;
407
+ break ;
408
+ case "reset" :
409
+ $scope . timerStart ( ) ;
410
+ break ;
411
+ }
412
+ } ;
413
+ $scope . timerAddCDSeconds = function ( extraSeconds ) {
414
+ $scope . $broadcast ( 'timer-add-cd-seconds' , extraSeconds ) ;
415
+ } ;
416
+ } ]
417
+ } ;
418
+ } ) ;
341
419
342
420
/* commonjs package manager support (eg componentjs) */
343
421
if ( typeof module !== "undefined" && typeof exports !== "undefined" && module . exports === exports ) {
0 commit comments