Skip to content
This repository has been archived by the owner on May 29, 2019. It is now read-only.

feat(timepicker): Added ability to handle empty model #4617

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
46 changes: 35 additions & 11 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ angular.module('ui.bootstrap.timepicker', [])
}

function pad(value) {
if (value === null) {
return '';
}
return (angular.isDefined(value) && value.toString().length < 2) ? '0' + value : value.toString();
}

Expand Down Expand Up @@ -223,6 +226,7 @@ angular.module('ui.bootstrap.timepicker', [])

if (angular.isDefined(hours) && angular.isDefined(minutes)) {
selected.setHours(hours);
selected.setMinutes(minutes);
if (selected < min || selected > max) {
invalidate(true);
} else {
Expand All @@ -234,7 +238,10 @@ angular.module('ui.bootstrap.timepicker', [])
};

hoursInputEl.bind('blur', function(e) {
if (!$scope.invalidHours && $scope.hours < 10) {
ngModelCtrl.$setTouched();
if ($scope.hours === null || $scope.hours === '') {
invalidate(true);
} else if (!$scope.invalidHours && $scope.hours < 10) {
$scope.$apply(function() {
$scope.hours = pad($scope.hours);
});
Expand All @@ -246,6 +253,7 @@ angular.module('ui.bootstrap.timepicker', [])
hours = getHoursFromTemplate();

if (angular.isDefined(minutes) && angular.isDefined(hours)) {
selected.setHours(hours);
selected.setMinutes(minutes);
if (selected < min || selected > max) {
invalidate(undefined, true);
Expand All @@ -258,7 +266,10 @@ angular.module('ui.bootstrap.timepicker', [])
};

minutesInputEl.bind('blur', function(e) {
if (!$scope.invalidMinutes && $scope.minutes < 10) {
ngModelCtrl.$setTouched();
if ($scope.minutes === null) {
invalidate(undefined, true);
} else if (!$scope.invalidMinutes && $scope.minutes < 10) {
$scope.$apply(function() {
$scope.minutes = pad($scope.minutes);
});
Expand Down Expand Up @@ -303,17 +314,23 @@ angular.module('ui.bootstrap.timepicker', [])
}

function updateTemplate(keyboardChange) {
var hours = selected.getHours(), minutes = selected.getMinutes();
if (ngModelCtrl.$modelValue == null) {
$scope.hours = null;
$scope.minutes = null;
$scope.meridian = meridians[0];
} else {
var hours = selected.getHours(), minutes = selected.getMinutes();

if ($scope.showMeridian) {
hours = (hours === 0 || hours === 12) ? 12 : hours % 12; // Convert 24 to 12 hour system
}
if ($scope.showMeridian) {
hours = (hours === 0 || hours === 12) ? 12 : hours % 12; // Convert 24 to 12 hour system
}

$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
if (keyboardChange !== 'm') {
$scope.minutes = pad(minutes);
$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
if (keyboardChange !== 'm') {
$scope.minutes = pad(minutes);
}
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
}
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
}

function addMinutes(date, minutes) {
Expand Down Expand Up @@ -356,8 +373,15 @@ angular.module('ui.bootstrap.timepicker', [])
};

$scope.toggleMeridian = function() {
var minutes = getMinutesFromTemplate(),
hours = getHoursFromTemplate();

if (!$scope.noToggleMeridian()) {
addMinutesToSelected(12 * 60 * (selected.getHours() < 12 ? 1 : -1));
if (angular.isDefined(minutes) && angular.isDefined(hours)) {
addMinutesToSelected(12 * 60 * (selected.getHours() < 12 ? 1 : -1));
} else {
$scope.meridian = $scope.meridian === meridians[0] ? meridians[1] : meridians[0];
}
}
};
}])
Expand Down
4 changes: 2 additions & 2 deletions template/timepicker/timepicker.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
</tr>
<tr>
<td class="form-group" ng-class="{'has-error': invalidHours}">
<input style="width:50px;" type="text" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
<input style="width:50px;" type="text" placeholder="HH" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
</td>
<td>:</td>
<td class="form-group" ng-class="{'has-error': invalidMinutes}">
<input style="width:50px;" type="text" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
<input style="width:50px;" type="text" placeholder="MM" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
</td>
<td ng-show="showMeridian"><button type="button" ng-class="{disabled: noToggleMeridian()}" class="btn btn-default text-center" ng-click="toggleMeridian()" ng-disabled="noToggleMeridian()" tabindex="{{::tabindex}}">{{meridian}}</button></td>
</tr>
Expand Down