Skip to content

Commit fc576ea

Browse files
committed
improved: disable / enable options
1 parent e68126e commit fc576ea

File tree

1 file changed

+24
-33
lines changed

1 file changed

+24
-33
lines changed

ui/jquery.ui.selectmenu.js

+24-33
Original file line numberDiff line numberDiff line change
@@ -721,9 +721,9 @@ $.widget("ui.selectmenu", {
721721
this._setOption( 'disabled', true );
722722
} else {
723723
if ( type == "optgroup" ) {
724-
this._disableOptgroup( index );
724+
this._toggleOptgroup( index, false );
725725
} else {
726-
this._disableOption( index );
726+
this._toggleOption( index, false );
727727
}
728728
}
729729
},
@@ -734,52 +734,43 @@ $.widget("ui.selectmenu", {
734734
this._setOption( 'disabled', false );
735735
} else {
736736
if ( type == "optgroup" ) {
737-
this._enableOptgroup( index );
737+
this._toggleOptgroup( index, true );
738738
} else {
739-
this._enableOption( index );
739+
this._toggleOption( index, true );
740740
}
741741
}
742742
},
743743

744744
_disabled: function( elem ) {
745745
return $( elem ).hasClass( this.namespace + '-state-disabled' );
746746
},
747-
748-
_disableOption: function( index ) {
749-
var optionElem = this._optionLis.eq( index );
750-
if (optionElem) {
751-
optionElem.addClass( this.namespace + '-state-disabled' )
752-
.find( "a" ).attr( "aria-disabled", true );
753-
this.element.find( "option" ).eq( index ).attr( "disabled", "disabled" );
754-
}
755-
},
756-
757-
_enableOption: function( index ) {
758-
var optionElem = this._optionLis.eq( index );
759-
if ( optionElem ) {
747+
748+
_toggleOption: function( index, flag ) {
749+
var optionElem = this._optionLis.eq( index );
750+
if ( optionElem ) {
760751
optionElem
761-
.removeClass( this.namespace + '-state-disabled' )
762-
.find( "a" ).attr( "aria-disabled", false);
752+
.toggleClass( this.namespace + '-state-disabled', flag )
753+
.find( "a" ).attr( "aria-disabled", !flag );
754+
if ( flag ) {
755+
this.element.find( "option" ).eq( index ).attr( "disabled", "disabled" );
756+
} else {
763757
this.element.find( "option" ).eq( index ).removeAttr( "disabled" );
764758
}
759+
}
765760
},
766761

767-
_disableOptgroup: function( index ) {
762+
// true = enabled, false = disabled
763+
_toggleOptgroup: function( index, flag ) {
768764
var optGroupElem = this.list.find( 'li.ui-selectmenu-group-' + index );
769-
if ( optGroupElem ) {
765+
if ( optGroupElem ) {
770766
optGroupElem
771-
.addClass( this.namespace + '-state-disabled' )
772-
.attr( "aria-disabled", true );
773-
this.element.find( "optgroup" ).eq( index ).attr( "disabled", "disabled" );
774-
}
775-
},
776-
777-
_enableOptgroup: function( index ) {
778-
var optGroupElem = this.list.find( 'li.ui-selectmenu-group-' + index );
779-
if ( optGroupElem ) {
780-
optGroupElem.removeClass( this.namespace + '-state-disabled' )
781-
.attr( "aria-disabled", false);
782-
this.element.find( "optgroup" ).eq( index ).removeAttr( "disabled" );
767+
.toggleClass( this.namespace + '-state-disabled', flag )
768+
.attr( "aria-disabled", !flag );
769+
if ( flag ) {
770+
this.element.find( "optgroup" ).eq( index ).attr( "disabled", "disabled" );
771+
} else {
772+
this.element.find( "optgroup" ).eq( index ).removeAttr( "disabled" );
773+
}
783774
}
784775
},
785776

0 commit comments

Comments
 (0)