From b5893906dfe62df4951d7bb55faf0f081390b633 Mon Sep 17 00:00:00 2001 From: TJ VanToll Date: Fri, 15 Nov 2013 08:42:20 -0500 Subject: [PATCH] Datepicker: Support changing the `appendTo` option after init --- tests/unit/datepicker/datepicker_options.js | 38 +++++++++++++++++++++ ui/datepicker.js | 7 ++++ 2 files changed, 45 insertions(+) diff --git a/tests/unit/datepicker/datepicker_options.js b/tests/unit/datepicker/datepicker_options.js index fedfe7dd5..72f459db0 100644 --- a/tests/unit/datepicker/datepicker_options.js +++ b/tests/unit/datepicker/datepicker_options.js @@ -2,6 +2,44 @@ module( "datepicker: options" ); +test( "appendTo", function() { + expect( 6 ); + var container, + detached = $( "
" ), + input = $( "#datepicker" ); + + input.datepicker(); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, document.body, "defaults to body" ); + input.datepicker( "destroy" ); + + input.datepicker({ appendTo: "#qunit-fixture" }); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, $( "#qunit-fixture" )[ 0 ], "child of specified element" ); + input.datepicker( "destroy" ); + + input.datepicker({ appendTo: "#does-not-exist" }); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, document.body, "set to body if element does not exist" ); + input.datepicker( "destroy" ); + + input.datepicker() + .datepicker( "option", "appendTo", "#qunit-fixture" ); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, $( "#qunit-fixture" )[ 0 ], "modified after init" ); + input.datepicker( "destroy" ); + + input.datepicker({ appendTo: detached }); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, detached[ 0 ], "detached jQuery object" ); + input.datepicker( "destroy" ); + + input.datepicker({ appendTo: detached[ 0 ] }); + container = input.datepicker( "widget" ).parent()[ 0 ]; + equal( container, detached[ 0 ], "detached DOM element" ); + input.datepicker( "destroy" ); +}); + test( "dateFormat", function() { expect( 0 ); }); diff --git a/ui/datepicker.js b/ui/datepicker.js index 55b6c0f74..28fd9191a 100644 --- a/ui/datepicker.js +++ b/ui/datepicker.js @@ -618,6 +618,13 @@ $.widget( "ui.datepicker", { }, widget: function() { return this.picker; + }, + _setOption: function( key, value ) { + this._super( key, value ); + + if ( key === "appendTo" ) { + this.picker.appendTo( this._appendTo() ); + } } }); -- 2.39.5