Skip to content

Commit 9a02bb3

Browse files
committed
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 0c860b0 commit 9a02bb3

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
@@ -793,7 +793,9 @@ var beforeShowThis = null,
793793
beforeShowInput = null,
794794
beforeShowInst = null,
795795
beforeShowDayThis = null,
796-
beforeShowDayOK = true;
796+
beforeShowDayOK = true,
797+
onUpdateDatepickerThis = null,
798+
onUpdateDatepickerInst = null;
797799

798800
function beforeAll( input, inst ) {
799801
beforeShowThis = this;
@@ -810,8 +812,14 @@ function beforeDay( date ) {
810812
( date.getDate() % 3 === 0 ? "Divisble by 3" : "" ) ];
811813
}
812814

815+
function onUpdateDatepicker( inst ) {
816+
onUpdateDatepickerThis = this;
817+
onUpdateDatepickerInst = inst;
818+
inst.dpDiv.append( $( "<div>" ).addClass( "on-update-datepicker-test" ) );
819+
}
820+
813821
QUnit.test( "callbacks", function( assert ) {
814-
assert.expect( 13 );
822+
assert.expect( 16 );
815823

816824
// Before show
817825
var dp, day20, day21,
@@ -840,6 +848,14 @@ QUnit.test( "callbacks", function( assert ) {
840848
assert.ok( !day20.attr( "title" ), "Before show day - title 20" );
841849
assert.ok( day21.attr( "title" ) === "Divisble by 3", "Before show day - title 21" );
842850
inp.datepicker( "hide" ).datepicker( "destroy" );
851+
852+
inp = testHelper.init( "#inp", { onUpdateDatepicker: onUpdateDatepicker } );
853+
inst = $.data( inp[ 0 ], "datepicker" );
854+
dp = $( "#ui-datepicker-div" );
855+
inp.val( "02/04/2008" ).datepicker( "show" );
856+
assert.ok( onUpdateDatepickerThis.id === inp[ 0 ].id, "On update datepicker - this OK" );
857+
assert.deepEqual( onUpdateDatepickerInst, inst, "On update datepicker - inst OK" );
858+
assert.ok( dp.find( "div.on-update-datepicker-test" ).length > 0, "On update datepicker - custom element" );
843859
} );
844860

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

ui/widgets/datepicker.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ function Datepicker() {
140140
onSelect: null, // Define a callback function when a date is selected
141141
onChangeMonthYear: null, // Define a callback function when the month or year is changed
142142
onClose: null, // Define a callback function when the datepicker is closed
143+
onUpdateDatepicker: null, // Define a callback function when the datepicker is updated
143144
numberOfMonths: 1, // Number of months to show at a time
144145
showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0)
145146
stepMonths: 1, // Number of months to step back/forward
@@ -832,7 +833,8 @@ $.extend( Datepicker.prototype, {
832833
numMonths = this._getNumberOfMonths( inst ),
833834
cols = numMonths[ 1 ],
834835
width = 17,
835-
activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" );
836+
activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" ),
837+
onUpdateDatepicker = $.datepicker._get( inst, "onUpdateDatepicker" );
836838

837839
if ( activeCell.length > 0 ) {
838840
datepicker_handleMouseover.apply( activeCell.get( 0 ) );
@@ -863,6 +865,10 @@ $.extend( Datepicker.prototype, {
863865
origyearshtml = inst.yearshtml = null;
864866
}, 0 );
865867
}
868+
869+
if ( onUpdateDatepicker ) {
870+
onUpdateDatepicker.apply( ( inst.input ? inst.input[ 0 ] : null ), [ inst ] );
871+
}
866872
},
867873

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

0 commit comments

Comments
 (0)