Skip to content

Commit decf9ce

Browse files
committed
Merge pull request uxsolutions#1777 from tpeter1985/master
Extend beforeShowMonth event functionality to work like beforeShowDay and beforeShowYear
2 parents 9a07a33 + 6511ffb commit decf9ce

File tree

3 files changed

+71
-7
lines changed

3 files changed

+71
-7
lines changed

docs/options.rst

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,16 @@ beforeShowMonth
3737

3838
Function(Date). Default: $.noop
3939

40-
A function that takes a date as a parameter and returns a boolean indicating whether or not this month is selectable
40+
A function that takes a date as a parameter and returns one of the following values:
41+
42+
* undefined to have no effect
43+
* A Boolean, indicating whether or not this month is selectable
44+
* A String representing additional CSS classes to apply to the month's cell
45+
* An object with the following properties:
46+
47+
* ``enabled``: same as the Boolean value above
48+
* ``classes``: same as the String value above
49+
* ``tooltip``: a tooltip to apply to this date, via the ``title`` HTML attribute
4150

4251

4352
beforeShowYear

js/bootstrap-datepicker.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,12 +1103,20 @@
11031103
if (this.o.beforeShowMonth !== $.noop){
11041104
var that = this;
11051105
$.each(months, function(i, month){
1106-
if (!$(month).hasClass('disabled')) {
1107-
var moDate = new Date(year, i, 1);
1108-
var before = that.o.beforeShowMonth(moDate);
1109-
if (before === false)
1110-
$(month).addClass('disabled');
1111-
}
1106+
var moDate = new Date(year, i, 1);
1107+
var before = that.o.beforeShowMonth(moDate);
1108+
if (before === undefined)
1109+
before = {};
1110+
else if (typeof(before) === 'boolean')
1111+
before = {enabled: before};
1112+
else if (typeof(before) === 'string')
1113+
before = {classes: before};
1114+
if (before.enabled === false && !$(month).hasClass('disabled'))
1115+
$(month).addClass('disabled');
1116+
if (before.classes)
1117+
$(month).addClass(before.classes);
1118+
if (before.tooltip)
1119+
$(month).prop('title', before.tooltip);
11121120
});
11131121
}
11141122

tests/suites/options.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -736,6 +736,53 @@ test('BeforeShowDay', function(){
736736
ok(!target.hasClass('disabled'), '29th is enabled');
737737
});
738738

739+
740+
test('BeforeShowMonth', function () {
741+
742+
var beforeShowMonth = function (date) {
743+
switch (date.getMonth()) {
744+
case 0:
745+
return {
746+
tooltip: 'Example tooltip',
747+
classes: 'active'
748+
};
749+
case 2:
750+
return "testMarch";
751+
case 4:
752+
return {enabled: false, classes: 'testMay'};
753+
case 5:
754+
return false;
755+
}
756+
};
757+
758+
var input = $('<input />')
759+
.appendTo('#qunit-fixture')
760+
.val('2012-10-26')
761+
.datepicker({
762+
format: 'yyyy-mm-dd',
763+
beforeShowMonth: beforeShowMonth
764+
}),
765+
dp = input.data('datepicker'),
766+
picker = dp.picker,
767+
target;
768+
769+
input.focus();
770+
target = picker.find('.datepicker-months tbody span:nth(0)');
771+
equal(target.attr('title'), 'Example tooltip', '1st has tooltip');
772+
ok(!target.hasClass('disabled'), 'January is enabled');
773+
target = picker.find('.datepicker-months tbody span:nth(2)');
774+
ok(target.hasClass('testMarch'), 'March has testMarch class');
775+
ok(!target.hasClass('disabled'), 'March enabled');
776+
target = picker.find('.datepicker-months tbody span:nth(4)');
777+
ok(target.hasClass('testMay'), 'May has testMay class');
778+
ok(target.hasClass('disabled'), 'May is disabled');
779+
target = picker.find('.datepicker-months tbody span:nth(5)');
780+
ok(target.hasClass('disabled'), 'June is disabled');
781+
target = picker.find('.datepicker-months tbody span:nth(6)');
782+
ok(!target.hasClass('disabled'), 'July is enabled');
783+
});
784+
785+
739786
test('BeforeShowYear', function () {
740787

741788
var beforeShowYear = function (date) {

0 commit comments

Comments
 (0)