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

Commit 8cf810b

Browse files
ggcatwesleycho
authored andcommitted
feat(timepicker): implement ng-disabled support timepicker
Add support for ng-disabled for timepicker
1 parent 5cb2f78 commit 8cf810b

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

src/timepicker/docs/readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ All settings can be provided as attributes in the `<timepicker>` or globally con
88
:
99
The Date object that provides the time state.
1010

11+
* `ng-disabled` <i class="glyphicon glyphicon-eye-open"></i>
12+
:
13+
Will disable time picker for user interaction.
14+
1115
* `hour-step` <i class="glyphicon glyphicon-eye-open"></i>
1216
_(Defaults: 1)_ :
1317
Number of hours to increase or decrease when using a button.

src/timepicker/test/timepicker.spec.js

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('timepicker directive', function () {
3737
}
3838

3939
function getArrow(isUp, tdIndex) {
40-
return element.find('tr').eq( (isUp) ? 0 : 2 ).find('td').eq( tdIndex ).find('a').eq(0);
40+
return element.find('tr').eq( (isUp) ? 0 : 2 ).find('td').eq( tdIndex ).find('button').eq(0);
4141
}
4242

4343
function getHoursButton(isUp) {
@@ -49,7 +49,7 @@ describe('timepicker directive', function () {
4949
}
5050

5151
function getMeridianButton() {
52-
return element.find('button').eq(0);
52+
return element.find('tr:nth-child(2) button').eq(0);
5353
}
5454

5555
function doClick(button, n) {
@@ -93,7 +93,7 @@ describe('timepicker directive', function () {
9393
it('contains three row & three input elements', function() {
9494
expect(element.find('tr').length).toBe(3);
9595
expect(element.find('input').length).toBe(2);
96-
expect(element.find('button').length).toBe(1);
96+
expect(element.find('button').length).toBe(5);
9797
});
9898

9999
it('has initially the correct time & meridian', function() {
@@ -156,9 +156,12 @@ describe('timepicker directive', function () {
156156
expect(getModelState()).toEqual([14, 39]);
157157
});
158158

159-
it('meridian button has correct type', function() {
160-
var button = getMeridianButton();
161-
expect(button.attr('type')).toBe('button');
159+
it('all buttons has correct type', function() {
160+
var btns = element.find('button[type=button]');
161+
162+
for (var a=0; a<btns.length; a++) {
163+
expect(btns.eq(a).attr('type')).toBe('button');
164+
}
162165
});
163166

164167
it('toggles meridian when button is clicked', function() {
@@ -621,6 +624,30 @@ describe('timepicker directive', function () {
621624
expect(getModelState()).toEqual([18, 20]);
622625
});
623626

627+
describe('`ng-disabled` attribute', function() {
628+
var initialTime;
629+
630+
beforeEach(function() {
631+
$rootScope.disabled = true;
632+
$rootScope.time = newTime(14, 0);
633+
initialTime = angular.copy($rootScope.time);
634+
element = $compile('<timepicker ng-model="time" hour-step="hstep" minute-step="mstep" ng-disabled="disabled"></timepicker>')($rootScope);
635+
$rootScope.$digest();
636+
});
637+
638+
it('should not allow interact when disabled', function() {
639+
expect(element.find('input[disabled]').length).toBe(2);
640+
expect(element.find('button[disabled]').length).toBe(4);
641+
});
642+
643+
it('should allow interact after enabling', function() {
644+
$rootScope.disabled = false;
645+
$rootScope.$digest();
646+
expect(element.find('input[disabled]').length).toBe(0);
647+
expect(element.find('button[disabled]').length).toBe(0);
648+
});
649+
});
650+
624651
});
625652

626653
describe('12 / 24 hour mode', function () {

src/timepicker/timepicker.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ angular.module('ui.bootstrap.timepicker', [])
66
showMeridian: true,
77
meridians: null,
88
readonlyInput: false,
9+
disabled: false,
910
mousewheel: true,
1011
arrowkeys: true
1112
})
@@ -33,6 +34,14 @@ angular.module('ui.bootstrap.timepicker', [])
3334
}
3435

3536
$scope.readonlyInput = angular.isDefined($attrs.readonlyInput) ? $scope.$parent.$eval($attrs.readonlyInput) : timepickerConfig.readonlyInput;
37+
38+
$scope.disabled = timepickerConfig.disabled;
39+
if (angular.isDefined($attrs.ngDisabled)) {
40+
$scope.$parent.$watch($parse($attrs.ngDisabled), function(value) {
41+
$scope.disabled = $scope.$parent.$eval($attrs.ngDisabled);
42+
});
43+
}
44+
3645
this.setupInputEvents( hoursInputEl, minutesInputEl );
3746
};
3847

template/timepicker/timepicker.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
<table>
22
<tbody>
33
<tr class="text-center">
4-
<td><a ng-click="incrementHours()" class="btn btn-link"><span class="glyphicon glyphicon-chevron-up"></span></a></td>
4+
<td><button type="button" ng-click="incrementHours()" class="btn btn-link" ng-disabled="disabled" tabindex="-1"><span class="glyphicon glyphicon-chevron-up"></span></button></td>
55
<td>&nbsp;</td>
6-
<td><a ng-click="incrementMinutes()" class="btn btn-link"><span class="glyphicon glyphicon-chevron-up"></span></a></td>
6+
<td><button type="button" ng-click="incrementMinutes()" class="btn btn-link" ng-disabled="disabled" tabindex="-1"><span class="glyphicon glyphicon-chevron-up"></span></button></td>
77
<td ng-show="showMeridian"></td>
88
</tr>
99
<tr>
1010
<td class="form-group" ng-class="{'has-error': invalidHours}">
11-
<input style="width:50px;" type="text" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-mousewheel="incrementHours()" ng-readonly="readonlyInput" maxlength="2">
11+
<input style="width:50px;" type="text" ng-model="hours" ng-change="updateHours()" class="form-control text-center" ng-mousewheel="incrementHours()" ng-readonly="readonlyInput" ng-disabled="disabled" maxlength="2">
1212
</td>
1313
<td>:</td>
1414
<td class="form-group" ng-class="{'has-error': invalidMinutes}">
15-
<input style="width:50px;" type="text" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="readonlyInput" maxlength="2">
15+
<input style="width:50px;" type="text" ng-model="minutes" ng-change="updateMinutes()" class="form-control text-center" ng-readonly="readonlyInput" ng-disabled="disabled" maxlength="2">
1616
</td>
1717
<td ng-show="showMeridian"><button type="button" class="btn btn-default text-center" ng-click="toggleMeridian()">{{meridian}}</button></td>
1818
</tr>
1919
<tr class="text-center">
20-
<td><a ng-click="decrementHours()" class="btn btn-link"><span class="glyphicon glyphicon-chevron-down"></span></a></td>
20+
<td><button type="button" ng-click="decrementHours()" class="btn btn-link" ng-disabled="disabled" tabindex="-1"><span class="glyphicon glyphicon-chevron-down"></span></button></td>
2121
<td>&nbsp;</td>
22-
<td><a ng-click="decrementMinutes()" class="btn btn-link"><span class="glyphicon glyphicon-chevron-down"></span></a></td>
22+
<td><button type="button" ng-click="decrementMinutes()" class="btn btn-link" ng-disabled="disabled" tabindex="-1"><span class="glyphicon glyphicon-chevron-down"></span></button></td>
2323
<td ng-show="showMeridian"></td>
2424
</tr>
2525
</tbody>

0 commit comments

Comments
 (0)