Skip to content

Commit 17d115b

Browse files
patrick-vandyfnagel
authored andcommitted
Datepicker: Add option for onUpdateDatepicker callback
Add a new option named onUpdateDatepicker that allows a custom callback to be provided. If provided, the callback is called at the end of $.datepicker._updateDatepicker.
1 parent b864cd1 commit 17d115b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

tests/unit/datepicker/options.js

+18-2
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ var beforeShowThis = null,
813813
beforeShowInput = null,
814814
beforeShowInst = null,
815815
beforeShowDayThis = null,
816-
beforeShowDayOK = true;
816+
beforeShowDayOK = true,
817+
onUpdateDatepickerThis = null,
818+
onUpdateDatepickerInst = null;
817819

818820
function beforeAll( input, inst ) {
819821
beforeShowThis = this;
@@ -830,8 +832,14 @@ function beforeDay( date ) {
830832
( date.getDate() % 3 === 0 ? "Divisble by 3" : "" ) ];
831833
}
832834

835+
function onUpdateDatepicker( inst ) {
836+
onUpdateDatepickerThis = this;
837+
onUpdateDatepickerInst = inst;
838+
inst.dpDiv.append( $( "<div>" ).addClass( "on-update-datepicker-test" ) );
839+
}
840+
833841
QUnit.test( "callbacks", function( assert ) {
834-
assert.expect( 13 );
842+
assert.expect( 16 );
835843

836844
// Before show
837845
var dp, day20, day21,
@@ -860,6 +868,14 @@ QUnit.test( "callbacks", function( assert ) {
860868
assert.ok( !day20.attr( "title" ), "Before show day - title 20" );
861869
assert.ok( day21.attr( "title" ) === "Divisble by 3", "Before show day - title 21" );
862870
inp.datepicker( "hide" ).datepicker( "destroy" );
871+
872+
inp = testHelper.init( "#inp", { onUpdateDatepicker: onUpdateDatepicker } );
873+
inst = $.data( inp[ 0 ], "datepicker" );
874+
dp = $( "#ui-datepicker-div" );
875+
inp.val( "02/04/2008" ).datepicker( "show" );
876+
assert.ok( onUpdateDatepickerThis.id === inp[ 0 ].id, "On update datepicker - this OK" );
877+
assert.deepEqual( onUpdateDatepickerInst, inst, "On update datepicker - inst OK" );
878+
assert.ok( dp.find( "div.on-update-datepicker-test" ).length > 0, "On update datepicker - custom element" );
863879
} );
864880

865881
QUnit.test( "beforeShowDay - tooltips with quotes", function( assert ) {

ui/widgets/datepicker.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ function Datepicker() {
142142
onSelect: null, // Define a callback function when a date is selected
143143
onChangeMonthYear: null, // Define a callback function when the month or year is changed
144144
onClose: null, // Define a callback function when the datepicker is closed
145+
onUpdateDatepicker: null, // Define a callback function when the datepicker is updated
145146
numberOfMonths: 1, // Number of months to show at a time
146147
showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0)
147148
stepMonths: 1, // Number of months to step back/forward
@@ -857,7 +858,8 @@ $.extend( Datepicker.prototype, {
857858
numMonths = this._getNumberOfMonths( inst ),
858859
cols = numMonths[ 1 ],
859860
width = 17,
860-
activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" );
861+
activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" ),
862+
onUpdateDatepicker = $.datepicker._get( inst, "onUpdateDatepicker" );
861863

862864
if ( activeCell.length > 0 ) {
863865
datepicker_handleMouseover.apply( activeCell.get( 0 ) );
@@ -888,6 +890,10 @@ $.extend( Datepicker.prototype, {
888890
origyearshtml = inst.yearshtml = null;
889891
}, 0 );
890892
}
893+
894+
if ( onUpdateDatepicker ) {
895+
onUpdateDatepicker.apply( ( inst.input ? inst.input[ 0 ] : null ), [ inst ] );
896+
}
891897
},
892898

893899
// #6694 - don't focus the input if it's already focused

0 commit comments

Comments
 (0)