Skip to content

Commit 8f0792a

Browse files
committed
merging / adding conflicted files
1 parent b395845 commit 8f0792a

File tree

2 files changed

+224
-0
lines changed

2 files changed

+224
-0
lines changed

dist/angular-timer.js

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/**
2+
* angular-timer - v1.1.0 - 2014-04-23 5:44 AM
3+
* https://github.com/siddii/angular-timer
4+
*
5+
* Copyright (c) 2014 Siddique Hameed
6+
* Licensed MIT <https://github.com/siddii/angular-timer/blob/master/LICENSE.txt>
7+
*/
8+
angular.module('timer', [])
9+
.directive('timer', ['$compile', function ($compile) {
10+
return {
11+
restrict: 'EAC',
12+
replace: false,
13+
scope: {
14+
interval: '=interval',
15+
startTimeAttr: '=startTime',
16+
endTimeAttr: '=endTime',
17+
countdownattr: '=countdown',
18+
autoStart: '&autoStart',
19+
maxTimeUnit: '='
20+
},
21+
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
22+
23+
// Checking for trim function since IE8 doesn't have it
24+
// If not a function, create tirm with RegEx to mimic native trim
25+
if(typeof String.prototype.trim !== 'function') {
26+
String.prototype.trim = function() {
27+
return this.replace(/^\s+|\s+$/g, '');
28+
};
29+
}
30+
31+
//angular 1.2 doesn't support attributes ending in "-start", so we're
32+
//supporting both "autostart" and "auto-start" as a solution for
33+
//backward and forward compatibility.
34+
$scope.autoStart = $attrs.autoStart || $attrs.autostart;
35+
36+
if ($element.html().trim().length === 0) {
37+
$element.append($compile('<span>{{millis}}</span>')($scope));
38+
} else {
39+
$element.append($compile($element.contents())($scope));
40+
}
41+
42+
$scope.startTime = null;
43+
$scope.endTime = null;
44+
$scope.timeoutId = null;
45+
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) >= 0 ? parseInt($scope.countdownattr, 10) : undefined;
46+
$scope.isRunning = false;
47+
48+
$scope.$on('timer-start', function () {
49+
$scope.start();
50+
});
51+
52+
$scope.$on('timer-resume', function () {
53+
$scope.resume();
54+
});
55+
56+
$scope.$on('timer-stop', function () {
57+
$scope.stop();
58+
});
59+
60+
$scope.$on('timer-clear', function () {
61+
$scope.clear();
62+
});
63+
64+
$scope.$on('timer-set-countdown', function (e, countdown) {
65+
$scope.countdown = countdown;
66+
});
67+
68+
function resetTimeout() {
69+
if ($scope.timeoutId) {
70+
clearTimeout($scope.timeoutId);
71+
}
72+
}
73+
74+
$scope.start = $element[0].start = function () {
75+
$scope.startTime = $scope.startTimeAttr ? new Date($scope.startTimeAttr) : new Date();
76+
$scope.endTime = $scope.endTimeAttr ? new Date($scope.endTimeAttr) : null;
77+
if (!$scope.countdown) {
78+
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) > 0 ? parseInt($scope.countdownattr, 10) : undefined;
79+
}
80+
resetTimeout();
81+
tick();
82+
$scope.isRunning = true;
83+
};
84+
85+
$scope.resume = $element[0].resume = function () {
86+
resetTimeout();
87+
if ($scope.countdownattr) {
88+
$scope.countdown += 1;
89+
}
90+
$scope.startTime = new Date() - ($scope.stoppedTime - $scope.startTime);
91+
tick();
92+
$scope.isRunning = true;
93+
};
94+
95+
$scope.stop = $scope.pause = $element[0].stop = $element[0].pause = function () {
96+
$scope.clear();
97+
$scope.$emit('timer-stopped', {millis: $scope.millis, seconds: $scope.seconds, minutes: $scope.minutes, hours: $scope.hours, days: $scope.days});
98+
};
99+
100+
$scope.clear = $element[0].clear = function () {
101+
// same as stop but without the event being triggered
102+
$scope.stoppedTime = new Date();
103+
resetTimeout();
104+
$scope.timeoutId = null;
105+
$scope.isRunning = false;
106+
};
107+
108+
$element.bind('$destroy', function () {
109+
resetTimeout();
110+
$scope.isRunning = false;
111+
});
112+
113+
function calculateTimeUnits() {
114+
115+
// compute time values based on maxTimeUnit specification
116+
if(!$scope.maxTimeUnit) {
117+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
118+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
119+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
120+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
121+
} else if($scope.maxTimeUnit === 'second') {
122+
$scope.seconds = Math.floor($scope.millis / 1000);
123+
$scope.minutes = 0;
124+
$scope.hours = 0;
125+
$scope.days = 0;
126+
} else if($scope.maxTimeUnit === 'minute') {
127+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
128+
$scope.minutes = Math.floor($scope.millis / 60000);
129+
$scope.hours = 0;
130+
$scope.days = 0;
131+
} else if($scope.maxTimeUnit === 'hour') {
132+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
133+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
134+
$scope.hours = Math.floor($scope.millis / 3600000);
135+
$scope.days = 0;
136+
}
137+
138+
// plural - singular unit decision
139+
$scope.secondsS = $scope.seconds==1 ? '' : 's';
140+
$scope.minutesS = $scope.minutes==1 ? '' : 's';
141+
$scope.hoursS = $scope.hours==1 ? '' : 's';
142+
$scope.daysS = $scope.days==1 ? '' : 's';
143+
//add leading zero if number is smaller than 10
144+
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
145+
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;
146+
$scope.hhours = $scope.hours < 10 ? '0' + $scope.hours : $scope.hours;
147+
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
148+
149+
}
150+
//determine initial values of time units and add AddSeconds functionality
151+
if ($scope.countdownattr) {
152+
$scope.millis = $scope.countdownattr * 1000;
153+
154+
$scope.addCDSeconds = $element[0].addCDSeconds = function(extraSeconds){
155+
$scope.countdown += extraSeconds;
156+
$scope.$digest();
157+
if (!$scope.isRunning) {
158+
$scope.start();
159+
}
160+
};
161+
162+
$scope.$on('timer-add-cd-seconds', function (e, extraSeconds) {
163+
$timeout(function (){
164+
$scope.addCDSeconds(extraSeconds);
165+
});
166+
});
167+
} else {
168+
$scope.millis = 0;
169+
}
170+
calculateTimeUnits();
171+
172+
var tick = function () {
173+
174+
$scope.millis = new Date() - $scope.startTime;
175+
var adjustment = $scope.millis % 1000;
176+
177+
if ($scope.endTimeAttr) {
178+
$scope.millis = $scope.endTime - new Date();
179+
adjustment = $scope.interval - $scope.millis % 1000;
180+
}
181+
182+
183+
if ($scope.countdownattr) {
184+
$scope.millis = $scope.countdown * 1000;
185+
}
186+
187+
if ($scope.millis < 0) {
188+
$scope.stop();
189+
$scope.millis = 0;
190+
calculateTimeUnits();
191+
return;
192+
}
193+
calculateTimeUnits();
194+
195+
//We are not using $timeout for a reason. Please read here - https://github.com/siddii/angular-timer/pull/5
196+
$scope.timeoutId = setTimeout(function () {
197+
tick();
198+
$scope.$digest();
199+
}, $scope.interval - adjustment);
200+
201+
$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});
202+
203+
if ($scope.countdown > 0) {
204+
$scope.countdown--;
205+
}
206+
else if ($scope.countdown <= 0) {
207+
$scope.stop();
208+
}
209+
};
210+
211+
if ($scope.autoStart === undefined || $scope.autoStart === true) {
212+
$scope.start();
213+
}
214+
}]
215+
};
216+
}]);

dist/angular-timer.min.js

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

0 commit comments

Comments
 (0)