From 9a02bb3b7b7daa55635b7f80943d8bee74d1af4b Mon Sep 17 00:00:00 2001 From: Patrick McKay Date: Sun, 1 Mar 2020 07:13:05 -0600 Subject: [PATCH 1/2] 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. --- tests/unit/datepicker/options.js | 20 ++++++++++++++++++-- ui/widgets/datepicker.js | 8 +++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/tests/unit/datepicker/options.js b/tests/unit/datepicker/options.js index 7711e741233..ec3630047f9 100644 --- a/tests/unit/datepicker/options.js +++ b/tests/unit/datepicker/options.js @@ -793,7 +793,9 @@ var beforeShowThis = null, beforeShowInput = null, beforeShowInst = null, beforeShowDayThis = null, - beforeShowDayOK = true; + beforeShowDayOK = true, + onUpdateDatepickerThis = null, + onUpdateDatepickerInst = null; function beforeAll( input, inst ) { beforeShowThis = this; @@ -810,8 +812,14 @@ function beforeDay( date ) { ( date.getDate() % 3 === 0 ? "Divisble by 3" : "" ) ]; } +function onUpdateDatepicker( inst ) { + onUpdateDatepickerThis = this; + onUpdateDatepickerInst = inst; + inst.dpDiv.append( $( "
" ).addClass( "on-update-datepicker-test" ) ); +} + QUnit.test( "callbacks", function( assert ) { - assert.expect( 13 ); + assert.expect( 16 ); // Before show var dp, day20, day21, @@ -840,6 +848,14 @@ QUnit.test( "callbacks", function( assert ) { assert.ok( !day20.attr( "title" ), "Before show day - title 20" ); assert.ok( day21.attr( "title" ) === "Divisble by 3", "Before show day - title 21" ); inp.datepicker( "hide" ).datepicker( "destroy" ); + + inp = testHelper.init( "#inp", { onUpdateDatepicker: onUpdateDatepicker } ); + inst = $.data( inp[ 0 ], "datepicker" ); + dp = $( "#ui-datepicker-div" ); + inp.val( "02/04/2008" ).datepicker( "show" ); + assert.ok( onUpdateDatepickerThis.id === inp[ 0 ].id, "On update datepicker - this OK" ); + assert.deepEqual( onUpdateDatepickerInst, inst, "On update datepicker - inst OK" ); + assert.ok( dp.find( "div.on-update-datepicker-test" ).length > 0, "On update datepicker - custom element" ); } ); QUnit.test( "beforeShowDay - tooltips with quotes", function( assert ) { diff --git a/ui/widgets/datepicker.js b/ui/widgets/datepicker.js index 5e6321e1d44..d5202b3e2b6 100644 --- a/ui/widgets/datepicker.js +++ b/ui/widgets/datepicker.js @@ -140,6 +140,7 @@ function Datepicker() { onSelect: null, // Define a callback function when a date is selected onChangeMonthYear: null, // Define a callback function when the month or year is changed onClose: null, // Define a callback function when the datepicker is closed + onUpdateDatepicker: null, // Define a callback function when the datepicker is updated numberOfMonths: 1, // Number of months to show at a time showCurrentAtPos: 0, // The position in multipe months at which to show the current month (starting at 0) stepMonths: 1, // Number of months to step back/forward @@ -832,7 +833,8 @@ $.extend( Datepicker.prototype, { numMonths = this._getNumberOfMonths( inst ), cols = numMonths[ 1 ], width = 17, - activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" ); + activeCell = inst.dpDiv.find( "." + this._dayOverClass + " a" ), + onUpdateDatepicker = $.datepicker._get( inst, "onUpdateDatepicker" ); if ( activeCell.length > 0 ) { datepicker_handleMouseover.apply( activeCell.get( 0 ) ); @@ -863,6 +865,10 @@ $.extend( Datepicker.prototype, { origyearshtml = inst.yearshtml = null; }, 0 ); } + + if ( onUpdateDatepicker ) { + onUpdateDatepicker.apply( ( inst.input ? inst.input[ 0 ] : null ), [ inst ] ); + } }, // #6694 - don't focus the input if it's already focused From d6885b8499daad337b01eed7afd80aee3614d0cb Mon Sep 17 00:00:00 2001 From: Patrick McKay Date: Sun, 1 Mar 2020 08:09:21 -0600 Subject: [PATCH 2/2] Datepicker: Improve callbacks test for onUpdateDatepicker Make sure the custom element added by the onUpdateDatepicker callback still exists and is not duplicated after calling refresh and setDate. --- tests/unit/datepicker/options.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/unit/datepicker/options.js b/tests/unit/datepicker/options.js index ec3630047f9..662594c7f4c 100644 --- a/tests/unit/datepicker/options.js +++ b/tests/unit/datepicker/options.js @@ -819,7 +819,7 @@ function onUpdateDatepicker( inst ) { } QUnit.test( "callbacks", function( assert ) { - assert.expect( 16 ); + assert.expect( 18 ); // Before show var dp, day20, day21, @@ -855,7 +855,12 @@ QUnit.test( "callbacks", function( assert ) { inp.val( "02/04/2008" ).datepicker( "show" ); assert.ok( onUpdateDatepickerThis.id === inp[ 0 ].id, "On update datepicker - this OK" ); assert.deepEqual( onUpdateDatepickerInst, inst, "On update datepicker - inst OK" ); - assert.ok( dp.find( "div.on-update-datepicker-test" ).length > 0, "On update datepicker - custom element" ); + assert.ok( dp.find( "div.on-update-datepicker-test" ).length === 1, "On update datepicker - custom element" ); + inp.datepicker( "setDate", "02/05/2008" ); + assert.ok( dp.find( "div.on-update-datepicker-test" ).length === 1, "On update datepicker - custom element after setDate" ); + inp.datepicker( "refresh" ); + assert.ok( dp.find( "div.on-update-datepicker-test" ).length === 1, "On update datepicker - custom element after refresh" ); + inp.datepicker( "hide" ).datepicker( "destroy" ); } ); QUnit.test( "beforeShowDay - tooltips with quotes", function( assert ) {