Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow Negative #116

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 27 additions & 10 deletions app/js/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var timerModule = angular.module('timer', [])
countdownattr: '=countdown',
finishCallback: '&finishCallback',
autoStart: '&autoStart',
maxTimeUnit: '='
maxTimeUnit: '=',
allowNegative: '=allowNegative'
},
controller: ['$scope', '$element', '$attrs', '$timeout', function ($scope, $element, $attrs, $timeout) {

Expand All @@ -21,7 +22,7 @@ var timerModule = angular.module('timer', [])
return this.replace(/^\s+|\s+$/g, '');
};
}

//angular 1.2 doesn't support attributes ending in "-start", so we're
//supporting both "autostart" and "auto-start" as a solution for
//backward and forward compatibility.
Expand All @@ -36,7 +37,7 @@ var timerModule = angular.module('timer', [])
$scope.startTime = null;
$scope.endTime = null;
$scope.timeoutId = null;
$scope.countdown = $scope.countdownattr && parseInt($scope.countdownattr, 10) >= 0 ? parseInt($scope.countdownattr, 10) : undefined;
$scope.countdown = $scope.countdownattr ? parseInt($scope.countdownattr, 10) : undefined;
$scope.isRunning = false;

$scope.$on('timer-start', function () {
Expand Down Expand Up @@ -109,6 +110,11 @@ var timerModule = angular.module('timer', [])
if ($attrs.startTime !== undefined){
$scope.millis = new Date() - new Date($scope.startTimeAttr);
}

if ($scope.allowNegative && $scope.millis < 0) {
$scope.millis = -$scope.millis;
}

// compute time values based on maxTimeUnit specification
if (!$scope.maxTimeUnit || $scope.maxTimeUnit === 'day') {
$scope.seconds = Math.floor(($scope.millis / 1000) % 60);
Expand Down Expand Up @@ -167,7 +173,6 @@ var timerModule = angular.module('timer', [])
$scope.ddays = $scope.days < 10 ? '0' + $scope.days : $scope.days;
$scope.mmonths = $scope.months < 10 ? '0' + $scope.months : $scope.months;
$scope.yyears = $scope.years < 10 ? '0' + $scope.years : $scope.years;

}

//determine initial values of time units and add AddSeconds functionality
Expand Down Expand Up @@ -203,7 +208,6 @@ var timerModule = angular.module('timer', [])
calculateTimeUnits();

var tick = function () {

$scope.millis = new Date() - $scope.startTime;
var adjustment = $scope.millis % 1000;

Expand All @@ -212,12 +216,11 @@ var timerModule = angular.module('timer', [])
adjustment = $scope.interval - $scope.millis % 1000;
}


if ($scope.countdownattr) {
$scope.millis = $scope.countdown * 1000;
}

if ($scope.millis < 0) {
if (!$scope.allowNegative && $scope.millis < 0) {
$scope.stop();
$scope.millis = 0;
calculateTimeUnits();
Expand All @@ -236,15 +239,29 @@ var timerModule = angular.module('timer', [])

$scope.$emit('timer-tick', {timeoutId: $scope.timeoutId, millis: $scope.millis});

if ($scope.countdown > 0) {
if ($scope.allowNegative || $scope.countdown > 0) {
$scope.countdown--;
}
else if ($scope.countdown <= 0) {
$scope.stop();
if (!$scope.allowNegative) {
$scope.stop();
}

if($scope.finishCallback) {
$scope.$eval($scope.finishCallback);
}
}

if ($scope.allowNegative && $scope.countdown < 0) {
if ($element.text().trim()[0] != "{" && $element.text().trim()[0] != "-") {
$element.prepend('-');
}
}

// Callback when set 0
if ($scope.allowNegative && $scope.millis == 0 && $scope.finishCallback) {
$scope.$eval($scope.finishCallback);
}
};

if ($scope.autoStart === undefined || $scope.autoStart === true) {
Expand All @@ -257,4 +274,4 @@ var timerModule = angular.module('timer', [])
/* commonjs package manager support (eg componentjs) */
if (typeof module !== "undefined" && typeof exports !== "undefined" && module.exports === exports){
module.exports = timerModule;
}
}