diff --git a/demos/selectmenu/default.html b/demos/selectmenu/default.html index 5fa1af28b9d..82a3ac6da36 100644 --- a/demos/selectmenu/default.html +++ b/demos/selectmenu/default.html @@ -10,6 +10,7 @@ + diff --git a/ui/form-reset-extension.js b/ui/form-reset-extension.js new file mode 100644 index 00000000000..0188d028966 --- /dev/null +++ b/ui/form-reset-extension.js @@ -0,0 +1,73 @@ +( function( factory ) { + if ( typeof define === "function" && define.amd ) { + + // AMD. Register as an anonymous module. + define( [ + "jquery" + ], factory ); + } else { + + // Browser globals + factory( jQuery ); + } +}( function( $ ) { + +return $.ui.formResetExtension = { + _create: function() { + this._super( "_create" ); + this._bindFormResetHandler(); + }, + + _formResetHandler: function( dataKey ) { + return function() { + var form = $( this ); + + // Wait for the form reset to actually happen before refreshing + setTimeout( function() { + + var instances = form.data( dataKey ); + $.each( instances, function( instance ) { + this.refresh(); + } ); + } ); + }; + }, + + _bindFormResetHandler: function() { + var dataKey = this.widgetFullName + "-instances"; + + this.form = this.element.form(); + if ( !this.form.length ) { + return; + } + + var instances = this.form.data( dataKey ) || []; + if ( !instances.length ) { + + // We don't use _on() here because we use a single event handler per form + this.form.on( "reset." + this.widgetFullName, this._formResetHandler( dataKey ) ); + } + instances.push( this ); + this.form.data( dataKey, instances ); + }, + + _destroy: function() { + var dataKey = this.widgetFullName + "-instances"; + + this._super( "_destroy" ); + + if ( this.form.length ) { + var instances = this.form.data( dataKey ); + instances.splice( $.inArray( this, instances ), 1 ); + if ( instances.length ) { + this.form.data( dataKey, instances ); + } else { + this.form + .removeData( dataKey ) + .off( "reset." + this.widgetFullName ); + } + } + } +}; + +} ) ); diff --git a/ui/form-reset-mixin.js b/ui/form-reset-mixin.js new file mode 100644 index 00000000000..d9d25a788f1 --- /dev/null +++ b/ui/form-reset-mixin.js @@ -0,0 +1,66 @@ +( function( factory ) { + if ( typeof define === "function" && define.amd ) { + + // AMD. Register as an anonymous module. + define( [ + "jquery" + ], factory ); + } else { + + // Browser globals + factory( jQuery ); + } +}( function( $ ) { + +return $.ui.formResetMixin = { + _formResetHandler: function( dataKey ) { + return function() { + var form = $( this ); + + // Wait for the form reset to actually happen before refreshing + setTimeout( function() { + + var instances = form.data( dataKey ); + $.each( instances, function( instance ) { + this.refresh(); + } ); + } ); + }; + }, + + _bindFormResetHandler: function() { + var dataKey = this.widgetFullName + "-instances"; + + this.form = this.element.form(); + if ( !this.form.length ) { + return; + } + + var instances = this.form.data( dataKey ) || []; + if ( !instances.length ) { + + // We don't use _on() here because we use a single event handler per form + this.form.on( "reset." + this.widgetFullName, this._formResetHandler( dataKey ) ); + } + instances.push( this ); + this.form.data( dataKey, instances ); + }, + + _unbindFormResetHandler: function() { + var dataKey = this.widgetFullName + "-instances"; + + if ( this.form.length ) { + var instances = this.form.data( dataKey ); + instances.splice( $.inArray( this, instances ), 1 ); + if ( instances.length ) { + this.form.data( dataKey, instances ); + } else { + this.form + .removeData( dataKey ) + .off( "reset." + this.widgetFullName ); + } + } + } +}; + +} ) ); diff --git a/ui/selectmenu.js b/ui/selectmenu.js index db2cea712aa..8d5c93078b8 100644 --- a/ui/selectmenu.js +++ b/ui/selectmenu.js @@ -25,7 +25,8 @@ "./core", "./widget", "./position", - "./menu" + "./menu", + "./form-reset-mixin" ], factory ); } else { @@ -34,7 +35,7 @@ } }( function( $ ) { -return $.widget( "ui.selectmenu", { +return $.widget( "ui.selectmenu", [ $.ui.formResetMixin, { version: "@VERSION", defaultElement: "