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

Commit 8ffdaeb

Browse files
ftorghele-efwesleycho
authored andcommitted
feat(timepicker): added ability to handle empty model
Closes #1114 Closes #4203 Closes #4617
1 parent c7fa845 commit 8ffdaeb

File tree

2 files changed

+47
-20
lines changed

2 files changed

+47
-20
lines changed

src/timepicker/timepicker.js

+45-18
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ angular.module('ui.bootstrap.timepicker', [])
180180
return (seconds >= 0 && seconds < 60) ? seconds : undefined;
181181
}
182182

183-
function pad( value ) {
183+
function pad(value) {
184+
if (value === null) {
185+
return '';
186+
}
187+
184188
return (angular.isDefined(value) && value.toString().length < 2) ? '0' + value : value.toString();
185189
}
186190

@@ -209,7 +213,6 @@ angular.module('ui.bootstrap.timepicker', [])
209213
$scope.$apply((isScrollingUp(e)) ? $scope.incrementSeconds() : $scope.decrementSeconds());
210214
e.preventDefault();
211215
});
212-
213216
};
214217

215218
// Respond on up/down arrowkeys
@@ -281,6 +284,7 @@ angular.module('ui.bootstrap.timepicker', [])
281284

282285
if (angular.isDefined(hours) && angular.isDefined(minutes)) {
283286
selected.setHours(hours);
287+
selected.setMinutes(minutes);
284288
if (selected < min || selected > max) {
285289
invalidate(true);
286290
} else {
@@ -292,7 +296,10 @@ angular.module('ui.bootstrap.timepicker', [])
292296
};
293297

294298
hoursInputEl.bind('blur', function(e) {
295-
if (!$scope.invalidHours && $scope.hours < 10) {
299+
ngModelCtrl.$setTouched();
300+
if ($scope.hours === null || $scope.hours === '') {
301+
invalidate(true);
302+
} else if (!$scope.invalidHours && $scope.hours < 10) {
296303
$scope.$apply(function() {
297304
$scope.hours = pad($scope.hours);
298305
});
@@ -304,6 +311,7 @@ angular.module('ui.bootstrap.timepicker', [])
304311
hours = getHoursFromTemplate();
305312

306313
if (angular.isDefined(minutes) && angular.isDefined(hours)) {
314+
selected.setHours(hours);
307315
selected.setMinutes(minutes);
308316
if (selected < min || selected > max) {
309317
invalidate(undefined, true);
@@ -316,7 +324,10 @@ angular.module('ui.bootstrap.timepicker', [])
316324
};
317325

318326
minutesInputEl.bind('blur', function(e) {
319-
if (!$scope.invalidMinutes && $scope.minutes < 10) {
327+
ngModelCtrl.$setTouched();
328+
if ($scope.minutes === null) {
329+
invalidate(undefined, true);
330+
} else if (!$scope.invalidMinutes && $scope.minutes < 10) {
320331
$scope.$apply(function() {
321332
$scope.minutes = pad($scope.minutes);
322333
});
@@ -381,22 +392,31 @@ angular.module('ui.bootstrap.timepicker', [])
381392
}
382393

383394
function updateTemplate(keyboardChange) {
384-
var hours = selected.getHours(),
385-
minutes = selected.getMinutes(),
386-
seconds = selected.getSeconds();
395+
if (ngModelCtrl.$modelValue == null) {
396+
$scope.hours = null;
397+
$scope.minutes = null;
398+
$scope.seconds = null;
399+
$scope.meridian = meridians[0];
400+
} else {
401+
var hours = selected.getHours(),
402+
minutes = selected.getMinutes(),
403+
seconds = selected.getSeconds();
387404

388-
if ($scope.showMeridian) {
389-
hours = (hours === 0 || hours === 12) ? 12 : hours % 12; // Convert 24 to 12 hour system
390-
}
405+
if ($scope.showMeridian) {
406+
hours = (hours === 0 || hours === 12) ? 12 : hours % 12; // Convert 24 to 12 hour system
407+
}
391408

392-
$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
393-
if (keyboardChange !== 'm') {
394-
$scope.minutes = pad(minutes);
395-
}
396-
if (keyboardChange !== 's') {
397-
$scope.seconds = pad(seconds);
409+
$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
410+
if (keyboardChange !== 'm') {
411+
$scope.minutes = pad(minutes);
412+
}
413+
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
414+
415+
if (keyboardChange !== 's') {
416+
$scope.seconds = pad(seconds);
417+
}
418+
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
398419
}
399-
$scope.meridian = selected.getHours() < 12 ? meridians[0] : meridians[1];
400420
}
401421

402422
function addSecondsToSelected(seconds) {
@@ -455,8 +475,15 @@ angular.module('ui.bootstrap.timepicker', [])
455475
};
456476

457477
$scope.toggleMeridian = function() {
478+
var minutes = getMinutesFromTemplate(),
479+
hours = getHoursFromTemplate();
480+
458481
if (!$scope.noToggleMeridian()) {
459-
addSecondsToSelected(12 * 60 * (selected.getHours() < 12 ? 60 : -60));
482+
if (angular.isDefined(minutes) && angular.isDefined(hours)) {
483+
addSecondsToSelected(12 * 60 * (selected.getHours() < 12 ? 60 : -60));
484+
} else {
485+
$scope.meridian = $scope.meridian === meridians[0] ? meridians[1] : meridians[0];
486+
}
460487
}
461488
};
462489
}])

template/timepicker/timepicker.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
</tr>
1111
<tr>
1212
<td class="form-group" ng-class="{'has-error': invalidHours}">
13-
<input style="width:50px;" type="text" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
13+
<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}}">
1414
</td>
1515
<td>:</td>
1616
<td class="form-group" ng-class="{'has-error': invalidMinutes}">
17-
<input style="width:50px;" type="text" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="::readonlyInput" maxlength="2" tabindex="{{::tabindex}}">
17+
<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}}">
1818
</td>
1919
<td ng-show="showSeconds">:</td>
2020
<td class="form-group" ng-class="{'has-error': invalidSeconds}" ng-show="showSeconds">

0 commit comments

Comments
 (0)