Skip to content

Commit 3411eb6

Browse files
authored
Merge pull request siddii#298 from webconnex/master
Add timerControls and fix tests
2 parents fdbf37f + 2b86810 commit 3411eb6

File tree

8 files changed

+237
-103
lines changed

8 files changed

+237
-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.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+
});
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:36 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.timerStart = function() {
399+
$scope.$broadcast('timer-start');
400+
};
401+
$scope.timerStop = function() {
402+
$scope.$broadcast('timer-stop');
403+
};
404+
$scope.timerResume = function() {
405+
$scope.$broadcast('timer-resume');
406+
};
407+
$scope.timerToggle = function() {
408+
switch ($scope.timerStatus) {
409+
case "started":
410+
$scope.timerStop();
411+
break;
412+
case "stopped":
413+
$scope.timerResume();
414+
break;
415+
case "reset":
416+
$scope.timerStart();
417+
break;
418+
}
419+
};
420+
$scope.timerAddCDSeconds = 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){

dist/angular-timer.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assets/js/angular-timer-all.min.js

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assets/js/angular-timer-bower.js

Lines changed: 13 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/docs.js

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,3 @@
1-
function startTimer(sectionId) {
2-
document.getElementById(sectionId).getElementsByTagName('timer')[0].start();
3-
}
4-
5-
function stopTimer(sectionId) {
6-
document.getElementById(sectionId).getElementsByTagName('timer')[0].stop();
7-
}
8-
9-
10-
function addCDSeconds(sectionId, extraTime) {
11-
document.getElementById(sectionId).getElementsByTagName('timer')[0].addCDSeconds(extraTime);
12-
}
13-
14-
function stopResumeTimer(sectionId, btn) {
15-
if (btn.innerHTML === 'Start') {
16-
document.getElementById(sectionId).getElementsByTagName('timer')[0].start();
17-
btn.innerHTML = 'Stop';
18-
}
19-
else if (btn.innerHTML === 'Stop') {
20-
document.getElementById(sectionId).getElementsByTagName('timer')[0].stop();
21-
btn.innerHTML = 'Resume';
22-
}
23-
else {
24-
document.getElementById(sectionId).getElementsByTagName('timer')[0].resume();
25-
btn.innerHTML = 'Stop';
26-
}
27-
}
281
angular.module('timer-demo',['timer']).controller('TimerDemoController',['$scope', function ($scope) {
292
$scope.linkAnchors = function () {
303
$('ul.nav a').click(function (){
@@ -34,10 +7,21 @@ angular.module('timer-demo',['timer']).controller('TimerDemoController',['$scope
347
}
358
});
369
};
37-
10+
11+
$scope.btnText = {
12+
reset: "Start",
13+
started: "Stop",
14+
stopped: "Resume"
15+
};
16+
17+
$scope.currentYear = (new Date).getFullYear();
18+
$scope.startTime = (new Date($scope.currentYear, 0, 1)).getTime();
19+
$scope.endYear = $scope.currentYear+1;
20+
$scope.endTime = (new Date($scope.endYear, 0, 1)).getTime();
21+
3822
$scope.callbackTimer={};
3923
$scope.callbackTimer.status='Running';
40-
$scope.callbackTimer.callbackCount=0;
24+
$scope.callbackTimer.callbackCount=0;
4125
$scope.callbackTimer.finished=function(){
4226
$scope.callbackTimer.status='COMPLETE!!';
4327
$scope.callbackTimer.callbackCount++;

0 commit comments

Comments
 (0)