Skip to content

Commit f4f6fce

Browse files
committed
Merge pull request siddii#68 from rajexcited/master
providing Max time Unit feature by attribute
2 parents b370051 + 8f0792a commit f4f6fce

File tree

5 files changed

+138
-15
lines changed

5 files changed

+138
-15
lines changed

app/js/timer.js

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ angular.module('timer', [])
88
startTimeAttr: '=startTime',
99
endTimeAttr: '=endTime',
1010
countdownattr: '=countdown',
11-
autoStart: '&autoStart'
11+
autoStart: '&autoStart',
12+
maxTimeUnit: '='
1213
},
1314
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
1415

@@ -104,15 +105,34 @@ angular.module('timer', [])
104105

105106
function calculateTimeUnits() {
106107

107-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
108+
// compute time values based on maxTimeUnit specification
109+
if(!$scope.maxTimeUnit) {
110+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
111+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
112+
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
113+
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
114+
} else if($scope.maxTimeUnit === 'second') {
115+
$scope.seconds = Math.floor($scope.millis / 1000);
116+
$scope.minutes = 0;
117+
$scope.hours = 0;
118+
$scope.days = 0;
119+
} else if($scope.maxTimeUnit === 'minute') {
120+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
121+
$scope.minutes = Math.floor($scope.millis / 60000);
122+
$scope.hours = 0;
123+
$scope.days = 0;
124+
} else if($scope.maxTimeUnit === 'hour') {
125+
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
126+
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
127+
$scope.hours = Math.floor($scope.millis / 3600000);
128+
$scope.days = 0;
129+
}
130+
131+
// plural - singular unit decision
108132
$scope.secondsS = $scope.seconds==1 ? '' : 's';
109-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
110133
$scope.minutesS = $scope.minutes==1 ? '' : 's';
111-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
112134
$scope.hoursS = $scope.hours==1 ? '' : 's';
113-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
114135
$scope.daysS = $scope.days==1 ? '' : 's';
115-
116136
//add leading zero if number is smaller than 10
117137
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
118138
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;

dist/angular-timer.js

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* angular-timer - v1.1.0 - 2014-04-14 9:36 AM
2+
* angular-timer - v1.1.0 - 2014-04-23 5:44 AM
33
* https://github.com/siddii/angular-timer
44
*
55
* Copyright (c) 2014 Siddique Hameed
@@ -15,7 +15,8 @@ angular.module('timer', [])
1515
startTimeAttr: '=startTime',
1616
endTimeAttr: '=endTime',
1717
countdownattr: '=countdown',
18-
autoStart: '&autoStart'
18+
autoStart: '&autoStart',
19+
maxTimeUnit: '='
1920
},
2021
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {
2122

@@ -111,15 +112,34 @@ angular.module('timer', [])
111112

112113
function calculateTimeUnits() {
113114

114-
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
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
115139
$scope.secondsS = $scope.seconds==1 ? '' : 's';
116-
$scope.minutes = Math.floor((($scope.millis / (60000)) % 60));
117140
$scope.minutesS = $scope.minutes==1 ? '' : 's';
118-
$scope.hours = Math.floor((($scope.millis / (3600000)) % 24));
119141
$scope.hoursS = $scope.hours==1 ? '' : 's';
120-
$scope.days = Math.floor((($scope.millis / (3600000)) / 24));
121142
$scope.daysS = $scope.days==1 ? '' : 's';
122-
123143
//add leading zero if number is smaller than 10
124144
$scope.sseconds = $scope.seconds < 10 ? '0' + $scope.seconds : $scope.seconds;
125145
$scope.mminutes = $scope.minutes < 10 ? '0' + $scope.minutes : $scope.minutes;

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.

index.html

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,34 @@ <h3 class="plural-counter">
183183
</div>
184184
</section>
185185

186+
<section id="max-time-unit-countdown-timer">
187+
<h3>countdown Time Display according to specified max Time Unit</h3>
188+
189+
<div class="bs-docs-example">
190+
<p>
191+
This markup will display countdown time in minute and seconds only. This attribute can be applied to regular clock timer as well.
192+
<code ng-non-bindable="">
193+
&lt;timer countdown="10041" max-time-unit="'minute'" interval="1000"&gt;{{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}&lt;/timer&gt;
194+
</code>
195+
</p>
196+
197+
<p class="muted">countdown Time with max time unit option - minute</p>
198+
<h3 class="WithMaxTimeUnitAsMinute">
199+
<timer countdown="10041" max-time-unit="'minute'" interval="1000"> {{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}</timer>
200+
</h3>
201+
202+
<p class="muted">countdown Time with max time unit option - second</p>
203+
<h3 class="WithMaxTimeUnitAsSecond">
204+
<timer countdown="10041" max-time-unit="'second'" interval="1000"> {{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}</timer>
205+
</h3>
206+
207+
<p class="muted">countdown Time without max time unit option - minute</p>
208+
<h3 class="WithoutMaxTimeUnit">
209+
<timer countdown="10041" interval="1000"> {{ddays}} day{{daysS}}, {{hhours}} hour{{hoursS}}, {{mminutes}} minute{{minutesS}}, {{sseconds}} second{{secondsS}}</timer>
210+
</h3>
211+
</div>
212+
</section>
213+
186214
<section id="markup">
187215
<h3>
188216
Markup</h3>
@@ -270,6 +298,16 @@ <h4>
270298
<td>Sets the countdown based on predefined end time (in milliseconds).
271299
</td>
272300
</tr>
301+
<tr>
302+
<td>
303+
max-time-unit
304+
</td>
305+
<td>
306+
false
307+
</td>
308+
<td> no default value. But you can give value, 'minute', 'second', or 'hour'.
309+
</td>
310+
</tr>
273311
</tbody>
274312
</table>
275313
<h4>

test/e2e/scenarios.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,39 @@ describe('Angular Timer E2E Tests', function () {
2323
return totalSeconds(actualValue) > totalSeconds(futureValue);
2424
});
2525

26+
angular.scenario.matcher('toCompareWith', function(future) {
27+
function getUnitValue (text, unitName) {
28+
var arr = text.toLowerCase().match(/\w+/g),
29+
returnVal,
30+
numInd= -1;
31+
arr.every(function (item,index,list) {
32+
if(isNaN(item)) {
33+
if(index===0) {
34+
numInd=1;
35+
}
36+
if(item === unitName) {
37+
returnVal = list[index+numInd];
38+
return false;
39+
}
40+
}
41+
return true;
42+
});
43+
return returnVal;
44+
}
45+
46+
var unitVal = getUnitValue(this.future.timerText.value,this.future.unit),
47+
compareResultFlag=false;
48+
if(this.future.compareTo === 'GreaterThan') {
49+
compareResultFlag = Number(unitVal) > Number(future);
50+
} else if(this.future.compareTo === 'LessThan') {
51+
compareResultFlag = Number(unitVal) < Number(future);
52+
} else if(this.future.compareTo === 'EqualTo') {
53+
compareResultFlag = Number(unitVal) == Number(future);
54+
}
55+
56+
return compareResultFlag;
57+
});
58+
2659
beforeEach(function () {
2760
if (window.location.host.indexOf("github.io") > -1) {
2861
browser().navigateTo('/angular-timer/index.html');
@@ -110,4 +143,16 @@ describe('Angular Timer E2E Tests', function () {
110143
expect(element('#clock-timer-leading-zero timer').html()).toMatch(/11 seconds./);
111144
});
112145

146+
it('Countdown timer with maxTimeUnit- should display time value from lower to specified maxTimeUnit', function() {
147+
var timer1Val = element('#max-time-unit-countdown-timer .WithMaxTimeUnitAsMinute timer').text();
148+
149+
expect({'timerText': timer1Val, 'unit': 'minutes', 'compareTo': 'GreaterThan'}).toCompareWith(59);
150+
expect({'timerText': timer1Val, 'unit': 'seconds', 'compareTo': 'LessThan'}).toCompareWith(60);
151+
152+
var timer2Val = element('#max-time-unit-countdown-timer .WithMaxTimeUnitAsSecond timer').text();
153+
expect({'timerText': timer2Val, 'unit': 'minutes', 'compareTo': 'EqualTo'}).toCompareWith(0);
154+
expect({'timerText': timer2Val, 'unit': 'seconds', 'compareTo': 'GreaterThan'}).toCompareWith(59);
155+
156+
});
157+
113158
});

0 commit comments

Comments
 (0)