From 8ffdaeb099b3f3858369177056977940107e3d9d Mon Sep 17 00:00:00 2001 From: Franz Torghele Date: Wed, 14 Oct 2015 11:37:12 +0200 Subject: [PATCH] feat(timepicker): added ability to handle empty model Closes #1114 Closes #4203 Closes #4617 --- src/timepicker/timepicker.js | 63 ++++++++++++++++++++--------- template/timepicker/timepicker.html | 4 +- 2 files changed, 47 insertions(+), 20 deletions(-) diff --git a/src/timepicker/timepicker.js b/src/timepicker/timepicker.js index 925d5ad911..460bfe846c 100644 --- a/src/timepicker/timepicker.js +++ b/src/timepicker/timepicker.js @@ -180,7 +180,11 @@ angular.module('ui.bootstrap.timepicker', []) return (seconds >= 0 && seconds < 60) ? seconds : undefined; } - function pad( value ) { + function pad(value) { + if (value === null) { + return ''; + } + return (angular.isDefined(value) && value.toString().length < 2) ? '0' + value : value.toString(); } @@ -209,7 +213,6 @@ angular.module('ui.bootstrap.timepicker', []) $scope.$apply((isScrollingUp(e)) ? $scope.incrementSeconds() : $scope.decrementSeconds()); e.preventDefault(); }); - }; // Respond on up/down arrowkeys @@ -281,6 +284,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 { @@ -292,7 +296,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); }); @@ -304,6 +311,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); @@ -316,7 +324,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); }); @@ -381,22 +392,31 @@ angular.module('ui.bootstrap.timepicker', []) } function updateTemplate(keyboardChange) { - var hours = selected.getHours(), - minutes = selected.getMinutes(), - seconds = selected.getSeconds(); + if (ngModelCtrl.$modelValue == null) { + $scope.hours = null; + $scope.minutes = null; + $scope.seconds = null; + $scope.meridian = meridians[0]; + } else { + var hours = selected.getHours(), + minutes = selected.getMinutes(), + seconds = selected.getSeconds(); - 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); - } - if (keyboardChange !== 's') { - $scope.seconds = pad(seconds); + $scope.hours = keyboardChange === 'h' ? hours : pad(hours); + if (keyboardChange !== 'm') { + $scope.minutes = pad(minutes); + } + $scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1]; + + if (keyboardChange !== 's') { + $scope.seconds = pad(seconds); + } + $scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1]; } - $scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1]; } function addSecondsToSelected(seconds) { @@ -455,8 +475,15 @@ angular.module('ui.bootstrap.timepicker', []) }; $scope.toggleMeridian = function() { + var minutes = getMinutesFromTemplate(), + hours = getHoursFromTemplate(); + if (!$scope.noToggleMeridian()) { - addSecondsToSelected(12 * 60 * (selected.getHours() < 12 ? 60 : -60)); + if (angular.isDefined(minutes) && angular.isDefined(hours)) { + addSecondsToSelected(12 * 60 * (selected.getHours() < 12 ? 60 : -60)); + } else { + $scope.meridian = $scope.meridian === meridians[0] ? meridians[1] : meridians[0]; + } } }; }]) diff --git a/template/timepicker/timepicker.html b/template/timepicker/timepicker.html index 802e5474ad..a4db776537 100644 --- a/template/timepicker/timepicker.html +++ b/template/timepicker/timepicker.html @@ -10,11 +10,11 @@ - + : - + :