Skip to content

Commit d262698

Browse files
author
Stephen Deken
committed
Allow the user to style individual calendar days
1 parent d497177 commit d262698

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
@@ -686,7 +686,7 @@
686686
currentDate,
687687
html = [],
688688
row,
689-
clsName,
689+
clsNames = [],
690690
i;
691691

692692
if (!hasDate()) {
@@ -717,26 +717,31 @@
717717
}
718718
html.push(row);
719719
}
720-
clsName = '';
720+
clsNames = ['day'];
721721
if (currentDate.isBefore(viewDate, 'M')) {
722-
clsName += ' old';
722+
clsNames.push('old');
723723
}
724724
if (currentDate.isAfter(viewDate, 'M')) {
725-
clsName += ' new';
725+
clsNames.push('new');
726726
}
727727
if (currentDate.isSame(date, 'd') && !unset) {
728-
clsName += ' active';
728+
clsNames.push('active');
729729
}
730730
if (!isValid(currentDate, 'd')) {
731-
clsName += ' disabled';
731+
clsNames.push('disabled');
732732
}
733733
if (currentDate.isSame(getMoment(), 'd')) {
734-
clsName += ' today';
734+
clsNames.push('today');
735735
}
736736
if (currentDate.day() === 0 || currentDate.day() === 6) {
737-
clsName += ' weekend';
737+
clsNames.push('weekend');
738738
}
739-
row.append('<td data-action="selectDay" data-day="' + currentDate.format('L') + '" class="day' + clsName + '">' + currentDate.date() + '</td>');
739+
notifyEvent({
740+
type: 'dp.classify',
741+
date: currentDate,
742+
classNames: clsNames
743+
});
744+
row.append('<td data-action="selectDay" data-day="' + currentDate.format('L') + '" class="' + clsNames.join(' ') + '">' + currentDate.date() + '</td>');
740745
currentDate.add(1, 'd');
741746
}
742747

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)