Skip to content

Commit 044aa9c

Browse files
authored
Merge pull request Eonasdan#1732 from sdeken/development
Allow the user to style individual calendar days
2 parents 2ddbd7a + d262698 commit 044aa9c

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

src/js/bootstrap-datetimepicker.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@
687687
currentDate,
688688
html = [],
689689
row,
690-
clsName,
690+
clsNames = [],
691691
i;
692692

693693
if (!hasDate()) {
@@ -718,26 +718,31 @@
718718
}
719719
html.push(row);
720720
}
721-
clsName = '';
721+
clsNames = ['day'];
722722
if (currentDate.isBefore(viewDate, 'M')) {
723-
clsName += ' old';
723+
clsNames.push('old');
724724
}
725725
if (currentDate.isAfter(viewDate, 'M')) {
726-
clsName += ' new';
726+
clsNames.push('new');
727727
}
728728
if (currentDate.isSame(date, 'd') && !unset) {
729-
clsName += ' active';
729+
clsNames.push('active');
730730
}
731731
if (!isValid(currentDate, 'd')) {
732-
clsName += ' disabled';
732+
clsNames.push('disabled');
733733
}
734734
if (currentDate.isSame(getMoment(), 'd')) {
735-
clsName += ' today';
735+
clsNames.push('today');
736736
}
737737
if (currentDate.day() === 0 || currentDate.day() === 6) {
738-
clsName += ' weekend';
738+
clsNames.push('weekend');
739739
}
740-
row.append('<td data-action="selectDay" data-day="' + currentDate.format('L') + '" class="day' + clsName + '">' + currentDate.date() + '</td>');
740+
notifyEvent({
741+
type: 'dp.classify',
742+
date: currentDate,
743+
classNames: clsNames
744+
});
745+
row.append('<td data-action="selectDay" data-day="' + currentDate.format('L') + '" class="' + clsNames.join(' ') + '">' + currentDate.date() + '</td>');
741746
currentDate.add(1, 'd');
742747
}
743748

test/publicApiSpec.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,23 @@ describe('Public API method tests', function () {
6363
dpChangeSpy,
6464
dpShowSpy,
6565
dpHideSpy,
66-
dpErrorSpy;
66+
dpErrorSpy,
67+
dpClassifySpy;
6768

6869
beforeEach(function () {
6970
dpChangeSpy = jasmine.createSpy('dp.change event Spy');
7071
dpShowSpy = jasmine.createSpy('dp.show event Spy');
7172
dpHideSpy = jasmine.createSpy('dp.hide event Spy');
7273
dpErrorSpy = jasmine.createSpy('dp.error event Spy');
74+
dpClassifySpy = jasmine.createSpy('dp.classify event Spy');
7375
dtpElement = $('<input>').attr('id', 'dtp');
7476

7577
$(document).find('body').append($('<div>').attr('class', 'row').append($('<div>').attr('class', 'col-md-12').append(dtpElement)));
7678
$(document).find('body').on('dp.change', dpChangeSpy);
7779
$(document).find('body').on('dp.show', dpShowSpy);
7880
$(document).find('body').on('dp.hide', dpHideSpy);
7981
$(document).find('body').on('dp.error', dpErrorSpy);
82+
$(document).find('body').on('dp.classify', dpClassifySpy);
8083

8184
dtpElement.datetimepicker();
8285
dtp = dtpElement.data('DateTimePicker');
@@ -290,10 +293,29 @@ describe('Public API method tests', function () {
290293
expect(dpShowSpy).not.toHaveBeenCalled();
291294
});
292295

296+
it('calls the classify event for each day that is shown', function () {
297+
dtp.show();
298+
expect(dpClassifySpy.calls.count()).toEqual(42);
299+
});
300+
293301
it('actually shows the widget', function () {
294302
dtp.show();
295303
expect($(document).find('body').find('.bootstrap-datetimepicker-widget').length).toEqual(1);
296304
});
305+
306+
it('applies the styles appended in the classify event handler', function () {
307+
var handler = function (event) {
308+
if (event.date.get('weekday') === 4) {
309+
event.classNames.push('humpday');
310+
}
311+
event.classNames.push('injected');
312+
};
313+
$(document).find('body').on('dp.classify', handler);
314+
dtp.show();
315+
$(document).find('body').off('dp.classify', handler);
316+
expect($(document).find('body').find('.bootstrap-datetimepicker-widget td.day.injected').length).toEqual(42);
317+
expect($(document).find('body').find('.bootstrap-datetimepicker-widget td.day.humpday').length).toEqual(6);
318+
});
297319
});
298320

299321
describe('access', function () {

0 commit comments

Comments
 (0)