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

Commit

Permalink
feat(timepicker): add pad-hours support
Browse files Browse the repository at this point in the history
- Add support for optionally padding hours
  • Loading branch information
wesleycho committed Mar 15, 2016
1 parent 8747b58 commit f6dabd2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/timepicker/docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ A lightweight & configurable timepicker directive.
<i class="glyphicon glyphicon-eye-open"></i> -
Date object that provides the time state.

* `pad-hours`
<small class="badge">$</small>
_(Default: true)_ -
Whether the hours column is padded with a 0.

* `readonly-input`
<small class="badge">$</small>
<small class="badge">C</small>
Expand Down
32 changes: 32 additions & 0 deletions src/timepicker/test/timepicker.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,38 @@ describe('timepicker directive', function() {
});
});

describe('`pad-hours` attribute', function() {
function triggerInput(elem, val) {
var event = $.Event('input');
elem.val(val);
elem.trigger(event);
}

it('should not pad the hours by default', function() {
element = $compile('<uib-timepicker ng-model="time"></uib-timepicker>')($rootScope);
$rootScope.$digest();

var inputs = element.find('input');
var hoursInput = inputs.eq(0);
triggerInput(hoursInput, 4);
hoursInput.blur();

expect(hoursInput.val()).toBe('04');
});

it('should not pad the hours', function() {
element = $compile('<uib-timepicker ng-model="time" pad-hours="false"></uib-timepicker>')($rootScope);
$rootScope.$digest();

var inputs = element.find('input');
var hoursInput = inputs.eq(0);
triggerInput(hoursInput, 4);
hoursInput.blur();

expect(hoursInput.val()).toBe('4');
});
});

describe('setting uibTimepickerConfig steps', function() {
var originalConfig = {};
beforeEach(inject(function(_$compile_, _$rootScope_, uibTimepickerConfig) {
Expand Down
11 changes: 6 additions & 5 deletions src/timepicker/timepicker.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ angular.module('ui.bootstrap.timepicker', [])
var selected = new Date(),
watchers = [],
ngModelCtrl = { $setViewValue: angular.noop }, // nullModelCtrl
meridians = angular.isDefined($attrs.meridians) ? $scope.$parent.$eval($attrs.meridians) : timepickerConfig.meridians || $locale.DATETIME_FORMATS.AMPMS;
meridians = angular.isDefined($attrs.meridians) ? $scope.$parent.$eval($attrs.meridians) : timepickerConfig.meridians || $locale.DATETIME_FORMATS.AMPMS,
padHours = angular.isDefined($attrs.padHours) ? $scope.$parent.$eval($attrs.padHours) : true;

$scope.tabindex = angular.isDefined($attrs.tabindex) ? $attrs.tabindex : 0;
$element.removeAttr('tabindex');
Expand Down Expand Up @@ -190,12 +191,12 @@ angular.module('ui.bootstrap.timepicker', [])
return seconds >= 0 && seconds < 60 ? seconds : undefined;
}

function pad(value) {
function pad(value, noPad) {
if (value === null) {
return '';
}

return angular.isDefined(value) && value.toString().length < 2 ?
return angular.isDefined(value) && value.toString().length < 2 && !noPad ?
'0' + value : value.toString();
}

Expand Down Expand Up @@ -326,7 +327,7 @@ angular.module('ui.bootstrap.timepicker', [])
invalidate(true);
} else if (!$scope.invalidHours && $scope.hours < 10) {
$scope.$apply(function() {
$scope.hours = pad($scope.hours);
$scope.hours = pad($scope.hours, !padHours);
});
}
});
Expand Down Expand Up @@ -435,7 +436,7 @@ angular.module('ui.bootstrap.timepicker', [])
hours = hours === 0 || hours === 12 ? 12 : hours % 12; // Convert 24 to 12 hour system
}

$scope.hours = keyboardChange === 'h' ? hours : pad(hours);
$scope.hours = keyboardChange === 'h' ? hours : pad(hours, !padHours);
if (keyboardChange !== 'm') {
$scope.minutes = pad(minutes);
}
Expand Down

0 comments on commit f6dabd2

Please sign in to comment.