Skip to content

Commit 0d71cbf

Browse files
committed
Button: updates based on pr review
1 parent 35564dd commit 0d71cbf

File tree

2 files changed

+36
-32
lines changed

2 files changed

+36
-32
lines changed

tests/unit/button/button_options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ test( "disabled, explicit value", function() {
2828
test("disabled, null", function() {
2929
expect( 4 );
3030
$("#radio01").button({ disabled: null });
31-
deepEqual(false, $("#radio01").button("option", "disabled"),
31+
deepEqual( $("#radio01").button("option", "disabled"), false,
3232
"disabled option set to false");
3333
deepEqual(false, $("#radio01").prop("disabled"), "element is disabled");
3434

ui/button.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,19 @@ $.widget( "ui.button", {
6666
},
6767

6868
_create: function() {
69-
this.element.closest( "form" )
70-
.unbind( "reset" + this.eventNamespace )
71-
.bind( "reset" + this.eventNamespace, formResetHandler );
69+
this._off( this.element.closest( "form" ), "reset" );
70+
this._on( this.element.closest( "form" ), {
71+
"reset": formResetHandler
72+
});
7273

7374
if ( typeof this.options.disabled === "boolean" ) {
7475
this.element.prop( "disabled", this.options.disabled );
75-
} else {
76-
this.options.disabled = !!this.element.prop( "disabled" );
7776
}
7877
if ( this.options.disabled === true ){
7978
this._setOption( "disabled", true );
8079
}
8180

82-
this.element
83-
.addClass( baseClasses )
84-
.attr( "role", "button" );
81+
this.element.addClass( baseClasses ).attr( "role", "button" );
8582

8683
if ( this.options.label ){
8784
if ( this.isInput ) {
@@ -93,44 +90,43 @@ $.widget( "ui.button", {
9390

9491
if ( this.options.icon ) {
9592
this.icon = $( "<span>" );
96-
this.icon.addClass( " ui-icon " + this.options.icon );
93+
this.icon.addClass( "ui-icon " + this.options.icon );
9794
if ( this.options.iconPosition ) {
9895
this.element.addClass( "ui-icon-" + this.options.iconPosition );
9996
}
10097
if ( !this.options.showLabel ){
101-
this.element.addClass( " ui-button-icon-only" );
98+
this.element.addClass( "ui-button-icon-only" );
10299
}
103100
this.element.append( this.icon );
104-
this._setTitle();
101+
this._updateTooltip();
105102
}
106103

107104
if ( this.element.is("a") ) {
108-
this.element.keyup(function(event) {
109-
if ( event.keyCode === $.ui.keyCode.SPACE ) {
110-
// TODO pass through original event correctly (just as 2nd argument doesn't work)
111-
$( this ).click();
105+
this._on({
106+
"keyup": function(event) {
107+
if ( event.keyCode === $.ui.keyCode.SPACE ) {
108+
event.type = "click";
109+
this.element.trigger( event );
110+
}
112111
}
113112
});
114113
}
115114
},
116115

117-
_setTitle: function() {
116+
_updateTooltip: function() {
118117
this.title = this.element.attr( "title" );
119118
this.hasTitle = !!this.title;
120119

121-
if ( !this.options.showLabel ){
122-
if ( !this.hasTitle ) {
123-
this.element.attr( "title", this.title );
124-
}
120+
if ( !this.options.showLabel && !this.hasTitle ){
121+
this.element.attr( "title", this.title );
125122
}
126123
},
127124

128125
_destroy: function() {
129126
this.element
130-
.removeClass( "ui-helper-hidden-accessible " + baseClasses +
127+
.removeClass( baseClasses +
131128
" ui-state-active " + typeClasses )
132-
.removeAttr( "role" )
133-
.removeAttr( "aria-pressed" );
129+
.removeAttr( "role" );
134130

135131
if ( !this.hasTitle ) {
136132
this.element.removeAttr( "title" );
@@ -139,13 +135,20 @@ $.widget( "ui.button", {
139135

140136
_setOption: function( key, value ) {
141137
if ( key === "icon" ) {
142-
this.icon.addClass( " ui-icon " + value )
143-
.removeClass( this.options.icon );
138+
if ( value !== null ) {
139+
if ( this.icon === undefined ) {
140+
this.icon = $( "<span>" );
141+
}
142+
this.icon.addClass( "ui-icon " + value ).removeClass( this.options.icon );
143+
} else {
144+
this.icon.remove();
145+
this.element.removeClass( this.options.iconPosition );
146+
}
144147
}
145148
if ( key === "showLabel" ) {
146-
this.element.toggleClass( ".ui-button-icon-only", !( !!value ) )
149+
this.element.toggleClass( "ui-button-icon-only", !value )
147150
.toggleClass( this.options.iconPosition, !!value );
148-
this._setTitle();
151+
this._updateTooltip();
149152
}
150153
if ( key === "iconPosition" && this.options.showLabel ) {
151154
this.element.addClass( value )
@@ -155,13 +158,14 @@ $.widget( "ui.button", {
155158
if ( this.element.is( "input" ) ) {
156159
this.element.val( value );
157160
} else {
158-
this.element.html( value );
161+
console.log( this.icon );
162+
this.element.html( ( ( !!this.icon ) ? "" : this.icon ) + value );
159163
}
160164
}
161165
this._super( key, value );
162166
if ( key === "disabled" ) {
163-
this.element.toggleClass( " ui-state-disabled", !!value );
164-
this.element.prop( "disabled", !!value ).blur();
167+
this.element.toggleClass( "ui-state-disabled", value );
168+
this.element.prop( "disabled", value ).blur();
165169
return;
166170
}
167171
},
@@ -175,7 +179,7 @@ $.widget( "ui.button", {
175179
this._setOptions( { "disabled": isDisabled } );
176180
}
177181

178-
this._setTitle();
182+
this._updateTooltip();
179183
}
180184

181185
});

0 commit comments

Comments
 (0)