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

Commit 0bcd30c

Browse files
Urigowesleycho
authored andcommitted
feat(datepicker): Add custom class to specific days via outside logic
1 parent b004443 commit 0bcd30c

File tree

6 files changed

+88
-5
lines changed

6 files changed

+88
-5
lines changed

src/datepicker/datepicker.js

+13-3
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,19 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
9393
label: dateFilter(date, format),
9494
selected: model && this.compare(date, model) === 0,
9595
disabled: this.isDisabled(date),
96-
current: this.compare(date, new Date()) === 0
96+
current: this.compare(date, new Date()) === 0,
97+
customClass: this.customClass(date)
9798
};
9899
};
99100

100101
this.isDisabled = function( date ) {
101102
return ((this.minDate && this.compare(date, this.minDate) < 0) || (this.maxDate && this.compare(date, this.maxDate) > 0) || ($attrs.dateDisabled && $scope.dateDisabled({date: date, mode: $scope.datepickerMode})));
102103
};
103104

105+
this.customClass = function( date ) {
106+
return $scope.customClass({date: date, mode: $scope.datepickerMode});
107+
};
108+
104109
// Split array into smaller arrays
105110
this.split = function(arr, size) {
106111
var arrays = [];
@@ -184,7 +189,8 @@ angular.module('ui.bootstrap.datepicker', ['ui.bootstrap.dateparser', 'ui.bootst
184189
templateUrl: 'template/datepicker/datepicker.html',
185190
scope: {
186191
datepickerMode: '=?',
187-
dateDisabled: '&'
192+
dateDisabled: '&',
193+
customClass: '&'
188194
},
189195
require: ['datepicker', '?^ngModel'],
190196
controller: 'DatepickerController',
@@ -440,7 +446,8 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
440446
currentText: '@',
441447
clearText: '@',
442448
closeText: '@',
443-
dateDisabled: '&'
449+
dateDisabled: '&',
450+
customClass: '&'
444451
},
445452
link: function(scope, element, attrs, ngModel) {
446453
var dateFormat,
@@ -500,6 +507,9 @@ function ($compile, $parse, $document, $position, dateFilter, dateParser, datepi
500507
if (attrs.dateDisabled) {
501508
datepickerEl.attr('date-disabled', 'dateDisabled({ date: date, mode: mode })');
502509
}
510+
if (attrs.customClass){
511+
datepickerEl.attr('custom-class', 'customClass({ date: date, mode: mode })');
512+
}
503513

504514
function parseDate(viewValue) {
505515
if (angular.isNumber(viewValue)) {

src/datepicker/docs/demo.html

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
1+
<style>
2+
.full button span {
3+
background-color: limegreen;
4+
border-radius: 32px;
5+
color: black;
6+
}
7+
.partially button span {
8+
background-color: orange;
9+
border-radius: 32px;
10+
color: black;
11+
}
12+
</style>
113
<div ng-controller="DatepickerDemoCtrl">
214
<pre>Selected date is: <em>{{dt | date:'fullDate' }}</em></pre>
315

416
<h4>Inline</h4>
517
<div style="display:inline-block; min-height:290px;">
6-
<datepicker ng-model="dt" min-date="minDate" show-weeks="true" class="well well-sm"></datepicker>
18+
<datepicker ng-model="dt" min-date="minDate" show-weeks="true" class="well well-sm" custom-class="getDayClass(date, mode)"></datepicker>
719
</div>
820

921
<h4>Popup</h4>

src/datepicker/docs/demo.js

+32
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,36 @@ angular.module('ui.bootstrap.demo').controller('DatepickerDemoCtrl', function ($
3232

3333
$scope.formats = ['dd-MMMM-yyyy', 'yyyy/MM/dd', 'dd.MM.yyyy', 'shortDate'];
3434
$scope.format = $scope.formats[0];
35+
36+
var tomorrow = new Date();
37+
tomorrow.setDate(tomorrow.getDate() + 1);
38+
var afterTomorrow = new Date();
39+
afterTomorrow.setDate(tomorrow.getDate() + 2);
40+
$scope.events =
41+
[
42+
{
43+
date: tomorrow,
44+
status: 'full'
45+
},
46+
{
47+
date: afterTomorrow,
48+
status: 'partially'
49+
}
50+
];
51+
52+
$scope.getDayClass = function(date, mode) {
53+
if (mode === 'day') {
54+
var dayToCheck = new Date(date).setHours(0,0,0,0);
55+
56+
for (var i=0;i<$scope.events.length;i++){
57+
var currentDay = new Date($scope.events[i].date).setHours(0,0,0,0);
58+
59+
if (dayToCheck === currentDay) {
60+
return $scope.events[i].status;
61+
}
62+
}
63+
}
64+
65+
return '';
66+
};
3567
});

src/datepicker/docs/readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ All settings can be provided as attributes in the `datepicker` or globally confi
2828
* `date-disabled (date, mode)`
2929
_(Default: null)_ :
3030
An optional expression to disable visible options based on passing date and current mode _(day|month|year)_.
31+
32+
* `custom-class (date, mode)`
33+
_(Default: null)_ :
34+
An optional expression to add classes based on passing date and current mode _(day|month|year)_.
3135

3236
* `show-weeks`
3337
_(Defaults: true)_ :

src/datepicker/test/datepicker.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,31 @@ describe('datepicker directive', function () {
955955
});
956956
});
957957

958+
describe('custom-class expression', function () {
959+
beforeEach(function() {
960+
$rootScope.customClassHandler = jasmine.createSpy('customClassHandler');
961+
element = $compile('<datepicker ng-model="date" custom-class="customClassHandler(date, mode)"></datepicker>')($rootScope);
962+
$rootScope.$digest();
963+
});
964+
965+
it('executes the customClass expression for each visible day plus one for validation', function() {
966+
expect($rootScope.customClassHandler.calls.length).toEqual(42);
967+
});
968+
969+
it('executes the customClass expression for each visible month plus one for validation', function() {
970+
$rootScope.customClassHandler.reset();
971+
clickTitleButton();
972+
expect($rootScope.customClassHandler.calls.length).toEqual(12);
973+
});
974+
975+
it('executes the customClass expression for each visible year plus one for validation', function() {
976+
clickTitleButton();
977+
$rootScope.customClassHandler.reset();
978+
clickTitleButton();
979+
expect($rootScope.customClassHandler.calls.length).toEqual(20);
980+
});
981+
});
982+
958983
describe('formatting', function () {
959984
beforeEach(function() {
960985
$rootScope.dayTitle = 'MMMM, yy';

template/datepicker/day.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
<tbody>
1414
<tr ng-repeat="row in rows track by $index">
1515
<td ng-show="showWeeks" class="text-center h6"><em>{{ weekNumbers[$index] }}</em></td>
16-
<td ng-repeat="dt in row track by dt.date" class="text-center" role="gridcell" id="{{dt.uid}}" aria-disabled="{{!!dt.disabled}}">
16+
<td ng-repeat="dt in row track by dt.date" class="text-center" role="gridcell" id="{{dt.uid}}" aria-disabled="{{!!dt.disabled}}" ng-class="dt.customClass">
1717
<button type="button" style="width:100%;" class="btn btn-default btn-sm" ng-class="{'btn-info': dt.selected, active: isActive(dt)}" ng-click="select(dt.date)" ng-disabled="dt.disabled" tabindex="-1"><span ng-class="{'text-muted': dt.secondary, 'text-info': dt.current}">{{dt.label}}</span></button>
1818
</td>
1919
</tr>

0 commit comments

Comments
 (0)