Skip to content

Commit a21dc55

Browse files
committed
Improve beforeShowDay option to be a bit more extensible in the future
1 parent 06c6809 commit a21dc55

3 files changed

Lines changed: 42 additions & 23 deletions

File tree

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,17 @@ Whether or not to force parsing of the input value when the picker is closed. T
208208

209209
### beforeShowDay
210210

211-
Function. Default: null
211+
Function(Date). Default: $.noop
212212

213-
A function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name or null for the default presentation.
213+
A function that takes a date as a parameter and returns one of the following values:
214+
215+
* undefined to have no effect
216+
* A Boolean, indicating whether or not this date is selectable
217+
* A String representing additional CSS classes to apply to the date's cell
218+
* An object with the following properties:
219+
* `enabled`: same as the Boolean value above
220+
* `classes`: same as the String value above
221+
* `tooltip`: a tooltip to apply to this date, via the `title` HTML attribute
214222

215223
## Markup
216224

js/bootstrap-datepicker.js

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@
113113
this.startDate = -Infinity;
114114
this.endDate = Infinity;
115115
this.daysOfWeekDisabled = [];
116-
this.beforeShowDay = options.beforeShowDay;
116+
this.beforeShowDay = options.beforeShowDay || $.noop;
117117
this.setStartDate(options.startDate||this.element.data('date-startdate'));
118118
this.setEndDate(options.endDate||this.element.data('date-enddate'));
119119
this.setDaysOfWeekDisabled(options.daysOfWeekDisabled||this.element.data('date-days-of-week-disabled'));
@@ -365,7 +365,8 @@
365365
endYear = this.endDate !== Infinity ? this.endDate.getUTCFullYear() : Infinity,
366366
endMonth = this.endDate !== Infinity ? this.endDate.getUTCMonth() : Infinity,
367367
currentDate = this.date && this.date.valueOf(),
368-
today = new Date();
368+
today = new Date(),
369+
tooltip;
369370
this.picker.find('.datepicker-days thead th:eq(1)')
370371
.text(dates[this.language].months[month]+' '+year);
371372
this.picker.find('tfoot th.today')
@@ -407,19 +408,21 @@
407408
clsName += ' disabled';
408409
}
409410

410-
if (!!this.beforeShowDay) {
411-
var beforeShowDay = this.beforeShowDay(prevMonth);
412-
if (!!beforeShowDay) {
413-
if (!beforeShowDay[0]) {
414-
clsName += ' disabled';
415-
}
416-
if (!!beforeShowDay[1] && beforeShowDay[1] != '') {
417-
clsName += ' ' + beforeShowDay[1];
418-
}
419-
}
420-
}
411+
var before = this.beforeShowDay(prevMonth);
412+
if (before === undefined)
413+
before = {};
414+
if (typeof(before) === 'boolean')
415+
before = {enabled: before};
416+
else if (typeof(before) === 'string')
417+
before = {classes: before};
418+
if (before.enabled === false)
419+
clsName += ' disabled';
420+
if (before.classes)
421+
clsName += ' ' + before.classes;
422+
if (before.tooltip)
423+
tooltip = before.tooltip;
421424

422-
html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');
425+
html.push('<td class="day'+clsName+'"' + (tooltip ? 'title="'+tooltip+'"' : '') + '>'+prevMonth.getUTCDate() + '</td>');
423426
if (prevMonth.getUTCDay() == this.weekEnd) {
424427
html.push('</tr>');
425428
}

tests/suites/options.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,23 @@ test('BeforeShowDay', function(){
288288

289289
var beforeShowDay = function(date) {
290290
var dateTime = UTCDate(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()).getTime();
291+
var dateTime25th = UTCDate(2012, 9, 25).getTime();
291292
var dateTime26th = UTCDate(2012, 9, 26).getTime();
292293
var dateTime27th = UTCDate(2012, 9, 27).getTime();
293294
var dateTime28th = UTCDate(2012, 9, 28).getTime();
294295

295-
if (dateTime == dateTime26th) {
296-
return [true, 'test26'];
297-
} else if (dateTime == dateTime27th) {
298-
return [false, 'test27'];
299-
} else if (dateTime == dateTime28th) {
300-
return [false, null];
296+
if (dateTime == dateTime25th) {
297+
return {tooltip: 'A tooltip'};
298+
}
299+
else if (dateTime == dateTime26th) {
300+
return 'test26';
301+
}
302+
else if (dateTime == dateTime27th) {
303+
return {enabled: false, classes:'test27'};
304+
}
305+
else if (dateTime == dateTime28th) {
306+
return false;
301307
}
302-
return [true, null];
303308
};
304309

305310
var input = $('<input />')
@@ -315,6 +320,9 @@ test('BeforeShowDay', function(){
315320

316321

317322
input.focus();
323+
target = picker.find('.datepicker-days tbody td:nth(25)');
324+
equal(target.attr('title'), 'A tooltip', '25th has tooltip');
325+
ok(!target.hasClass('disabled'), '25th is enabled');
318326
target = picker.find('.datepicker-days tbody td:nth(26)');
319327
ok(target.hasClass('test26'), '26th has test26 class');
320328
ok(!target.hasClass('disabled'), '26th is enabled');

0 commit comments

Comments
 (0)