form-reset-mixin.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*!
  2. * jQuery UI Form Reset Mixin 1.14.1
  3. * https://jqueryui.com
  4. *
  5. * Copyright OpenJS Foundation and other contributors
  6. * Released under the MIT license.
  7. * https://jquery.org/license
  8. */
  9. //>>label: Form Reset Mixin
  10. //>>group: Core
  11. //>>description: Refresh input widgets when their form is reset
  12. //>>docs: https://api.jqueryui.com/form-reset-mixin/
  13. ( function( factory ) {
  14. "use strict";
  15. if ( typeof define === "function" && define.amd ) {
  16. // AMD. Register as an anonymous module.
  17. define( [
  18. "jquery",
  19. "./version"
  20. ], factory );
  21. } else {
  22. // Browser globals
  23. factory( jQuery );
  24. }
  25. } )( function( $ ) {
  26. "use strict";
  27. return $.ui.formResetMixin = {
  28. _formResetHandler: function() {
  29. var form = $( this );
  30. // Wait for the form reset to actually happen before refreshing
  31. setTimeout( function() {
  32. var instances = form.data( "ui-form-reset-instances" );
  33. $.each( instances, function() {
  34. this.refresh();
  35. } );
  36. } );
  37. },
  38. _bindFormResetHandler: function() {
  39. this.form = $( this.element.prop( "form" ) );
  40. if ( !this.form.length ) {
  41. return;
  42. }
  43. var instances = this.form.data( "ui-form-reset-instances" ) || [];
  44. if ( !instances.length ) {
  45. // We don't use _on() here because we use a single event handler per form
  46. this.form.on( "reset.ui-form-reset", this._formResetHandler );
  47. }
  48. instances.push( this );
  49. this.form.data( "ui-form-reset-instances", instances );
  50. },
  51. _unbindFormResetHandler: function() {
  52. if ( !this.form.length ) {
  53. return;
  54. }
  55. var instances = this.form.data( "ui-form-reset-instances" );
  56. instances.splice( $.inArray( this, instances ), 1 );
  57. if ( instances.length ) {
  58. this.form.data( "ui-form-reset-instances", instances );
  59. } else {
  60. this.form
  61. .removeData( "ui-form-reset-instances" )
  62. .off( "reset.ui-form-reset" );
  63. }
  64. }
  65. };
  66. } );