Skip to content

Commit 131d641

Browse files
committed
Add timerControls and fix tests
1 parent 6c51bce commit 131d641

File tree

8 files changed

+227
-103
lines changed

8 files changed

+227
-103
lines changed

app/js/_timer.js

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ var timerModule = angular.module('timer', [])
123123
resetTimeout();
124124
tick();
125125
$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+
});
126134
};
127135

128136
$scope.resume = function () {
@@ -133,12 +141,27 @@ var timerModule = angular.module('timer', [])
133141
$scope.startTime = moment().diff((moment($scope.stoppedTime).diff(moment($scope.startTime))));
134142
tick();
135143
$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+
});
136152
};
137153

138154
$scope.stop = $scope.pause = function () {
139155
var timeoutId = $scope.timeoutId;
140156
$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+
});
142165
};
143166

144167
$scope.clear = function () {
@@ -157,6 +180,14 @@ var timerModule = angular.module('timer', [])
157180
tick();
158181
$scope.isRunning = false;
159182
$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+
});
160191
};
161192

162193
$element.bind('$destroy', function () {
@@ -251,16 +282,13 @@ var timerModule = angular.module('timer', [])
251282

252283
$scope.addCDSeconds = function (extraSeconds) {
253284
$scope.countdown += extraSeconds;
254-
$scope.$digest();
255285
if (!$scope.isRunning) {
256286
$scope.start();
257287
}
258288
};
259289

260290
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
261-
$timeout(function () {
262-
$scope.addCDSeconds(extraSeconds);
263-
});
291+
$scope.addCDSeconds(extraSeconds);
264292
});
265293

266294
$scope.$on('timer-set-countdown-seconds', function (e, countdownSeconds) {
@@ -310,7 +338,14 @@ var timerModule = angular.module('timer', [])
310338
$scope.$digest();
311339
}, $scope.interval - adjustment);
312340

313-
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
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+
});
314349

315350
if ($scope.countdown > 0) {
316351
$scope.countdown--;
@@ -337,7 +372,50 @@ var timerModule = angular.module('timer', [])
337372
}
338373
}]
339374
};
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.start = function() {
392+
$scope.$broadcast('timer-start');
393+
};
394+
$scope.stop = function() {
395+
$scope.$broadcast('timer-stop');
396+
};
397+
$scope.resume = function() {
398+
$scope.$broadcast('timer-resume');
399+
};
400+
$scope.toggle = function() {
401+
switch ($scope.timerStatus) {
402+
case "started":
403+
$scope.stop();
404+
break;
405+
case "stopped":
406+
$scope.resume();
407+
break;
408+
case "reset":
409+
$scope.start();
410+
break;
411+
}
412+
};
413+
$scope.addCDSeconds = function(extraSeconds) {
414+
$scope.$broadcast('timer-add-cd-seconds', extraSeconds);
415+
};
416+
}]
417+
};
418+
});
341419

342420
/* commonjs package manager support (eg componentjs) */
343421
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){

dist/angular-timer.js

Lines changed: 86 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.3.5 - 2017-02-14 1:19 PM
2+
* angular-timer - v1.3.5 - 2017-02-15 4:06 PM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2017 Adrian Wardell
@@ -130,6 +130,14 @@ var timerModule = angular.module('timer', [])
130130
resetTimeout();
131131
tick();
132132
$scope.isRunning = true;
133+
$scope.$emit('timer-started', {
134+
timeoutId: $scope.timeoutId,
135+
millis: $scope.millis,
136+
seconds: $scope.seconds,
137+
minutes: $scope.minutes,
138+
hours: $scope.hours,
139+
days: $scope.days
140+
});
133141
};
134142

135143
$scope.resume = function () {
@@ -140,12 +148,27 @@ var timerModule = angular.module('timer', [])
140148
$scope.startTime = moment().diff((moment($scope.stoppedTime).diff(moment($scope.startTime))));
141149
tick();
142150
$scope.isRunning = true;
151+
$scope.$emit('timer-started', {
152+
timeoutId: $scope.timeoutId,
153+
millis: $scope.millis,
154+
seconds: $scope.seconds,
155+
minutes: $scope.minutes,
156+
hours: $scope.hours,
157+
days: $scope.days
158+
});
143159
};
144160

145161
$scope.stop = $scope.pause = function () {
146162
var timeoutId = $scope.timeoutId;
147163
$scope.clear();
148-
$scope.$emit('timer-stopped', {timeoutId: timeoutId, millis: $scope.millis, seconds: $scope.seconds, minutes: $scope.minutes, hours: $scope.hours, days: $scope.days});
164+
$scope.$emit('timer-stopped', {
165+
timeoutId: timeoutId,
166+
millis: $scope.millis,
167+
seconds: $scope.seconds,
168+
minutes: $scope.minutes,
169+
hours: $scope.hours,
170+
days: $scope.days
171+
});
149172
};
150173

151174
$scope.clear = function () {
@@ -164,6 +187,14 @@ var timerModule = angular.module('timer', [])
164187
tick();
165188
$scope.isRunning = false;
166189
$scope.clear();
190+
$scope.$emit('timer-reset', {
191+
timeoutId: timeoutId,
192+
millis: $scope.millis,
193+
seconds: $scope.seconds,
194+
minutes: $scope.minutes,
195+
hours: $scope.hours,
196+
days: $scope.days
197+
});
167198
};
168199

169200
$element.bind('$destroy', function () {
@@ -258,16 +289,13 @@ var timerModule = angular.module('timer', [])
258289

259290
$scope.addCDSeconds = function (extraSeconds) {
260291
$scope.countdown += extraSeconds;
261-
$scope.$digest();
262292
if (!$scope.isRunning) {
263293
$scope.start();
264294
}
265295
};
266296

267297
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
268-
$timeout(function () {
269-
$scope.addCDSeconds(extraSeconds);
270-
});
298+
$scope.addCDSeconds(extraSeconds);
271299
});
272300

273301
$scope.$on('timer-set-countdown-seconds', function (e, countdownSeconds) {
@@ -317,7 +345,14 @@ var timerModule = angular.module('timer', [])
317345
$scope.$digest();
318346
}, $scope.interval - adjustment);
319347

320-
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
348+
$scope.$emit('timer-tick', {
349+
timeoutId: $scope.timeoutId,
350+
millis: $scope.millis,
351+
seconds: $scope.seconds,
352+
minutes: $scope.minutes,
353+
hours: $scope.hours,
354+
days: $scope.days
355+
});
321356

322357
if ($scope.countdown > 0) {
323358
$scope.countdown--;
@@ -344,7 +379,50 @@ var timerModule = angular.module('timer', [])
344379
}
345380
}]
346381
};
347-
}]);
382+
}])
383+
.directive('timerControls', function() {
384+
return {
385+
restrict: 'EA',
386+
scope: true,
387+
controller: ['$scope', function($scope) {
388+
$scope.timerStatus = "reset";
389+
$scope.$on('timer-started', function() {
390+
$scope.timerStatus = "started";
391+
});
392+
$scope.$on('timer-stopped', function() {
393+
$scope.timerStatus = "stopped";
394+
});
395+
$scope.$on('timer-reset', function() {
396+
$scope.timerStatus = "reset";
397+
});
398+
$scope.start = function() {
399+
$scope.$broadcast('timer-start');
400+
};
401+
$scope.stop = function() {
402+
$scope.$broadcast('timer-stop');
403+
};
404+
$scope.resume = function() {
405+
$scope.$broadcast('timer-resume');
406+
};
407+
$scope.toggle = function() {
408+
switch ($scope.timerStatus) {
409+
case "started":
410+
$scope.stop();
411+
break;
412+
case "stopped":
413+
$scope.resume();
414+
break;
415+
case "reset":
416+
$scope.start();
417+
break;
418+
}
419+
};
420+
$scope.addCDSeconds = function(extraSeconds) {
421+
$scope.$broadcast('timer-add-cd-seconds', extraSeconds);
422+
};
423+
}]
424+
};
425+
});
348426

349427
/* commonjs package manager support (eg componentjs) */
350428
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){

0 commit comments

Comments
 (0)