Skip to content

Commit dc89ba4

Browse files
committed
Checkboxradio: updates based on new classes api
1 parent 5bdef55 commit dc89ba4

File tree

3 files changed

+32
-69
lines changed

3 files changed

+32
-69
lines changed

tests/unit/checkboxradio/checkboxradio_common.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@ TestHelpers.commonWidgetTests( "checkboxradio", {
55
label: null,
66
icon: true,
77
classes: {
8-
"ui-checkboxradio": "",
98
"ui-checkboxradio-label": "ui-corner-all",
10-
"ui-checkboxradio-radio-label": "",
11-
"ui-checkboxradio-icon": "ui-corner-all",
12-
"ui-checkboxradio-checked": ""
9+
"ui-checkboxradio-icon": "ui-corner-all"
1310
},
1411

1512
// Callbacks

tests/unit/checkboxradio/checkboxradio_core.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ test("Checkbox", function() {
1313
ok( input.is( ":visible" ) );
1414
ok( !label.hasClass(".ui-button)") );
1515
input.checkboxradio();
16-
strictEqual( input.attr( "class" ), "ui-helper-hidden-accessible ui-checkboxradio" );
17-
strictEqual( label.attr( "class" ), "ui-icon-beginning ui-button ui-widget" +
18-
" ui-checkboxradio-label ui-corner-all" );
16+
equal( input.is( ".ui-helper-hidden-accessible.ui-checkboxradio" ), true,
17+
"Input has proper classes" );
18+
equal( label.is( ".ui-icon-beginning.ui-button.ui-widget.ui-checkboxradio-label.ui-corner-all" ),
19+
true, "Label has proper classes" );
1920
});
2021

2122
test("Radios", function() {

ui/checkboxradio.js

Lines changed: 27 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,7 @@
2424
}
2525
}(function( $ ) {
2626

27-
var baseClasses = "ui-button ui-widget",
28-
typeClasses = "ui-state-focus ui-radio-label ui-checkbox-label ui-state-active " +
29-
"ui-icon-beginning ui-icon-end ui-icon-top ui-icon-bottom ui-checkboxradio-radio-checked " +
30-
"ui-checkboxradio-checkbox-checked",
31-
formResetHandler = function() {
27+
var formResetHandler = function() {
3228
var form = $( this );
3329
setTimeout(function() {
3430
form.find( ".ui-checkboxradio" ).checkboxradio( "refresh" );
@@ -59,11 +55,8 @@ $.widget( "ui.checkboxradio", {
5955
label: null,
6056
icon: true,
6157
classes: {
62-
"ui-checkboxradio": "",
6358
"ui-checkboxradio-label": "ui-corner-all",
64-
"ui-checkboxradio-radio-label": "",
65-
"ui-checkboxradio-icon": "ui-corner-all",
66-
"ui-checkboxradio-checked": ""
59+
"ui-checkboxradio-icon": "ui-corner-all"
6760
}
6861
},
6962

@@ -110,10 +103,10 @@ $.widget( "ui.checkboxradio", {
110103
this._on({
111104
"change": "_toggleClasses",
112105
"focus": function() {
113-
this.label.addClass( "ui-state-focus ui-visual-focus" );
106+
this._addClass( this.label, null, "ui-state-focus ui-visual-focus" );
114107
},
115108
"blur": function() {
116-
this.label.removeClass( "ui-state-focus ui-visual-focus" );
109+
this._removeClass( this.label, null, "ui-state-focus ui-visual-focus" );
117110
}
118111
});
119112
},
@@ -171,18 +164,15 @@ $.widget( "ui.checkboxradio", {
171164

172165
this._setOption( "disabled", this.options.disabled );
173166
this._updateIcon( checked );
174-
this.element.addClass( "ui-helper-hidden-accessible " +
175-
this._classes( "ui-checkboxradio" ) );
167+
this._addClass( "ui-checkboxradio", "ui-helper-hidden-accessible " );
176168

177-
this.label.addClass( baseClasses + " " + this._classes( "ui-checkboxradio-label" ) );
169+
this._addClass( this.label, "ui-checkboxradio-label", "ui-button ui-widget" );
178170

179171
if ( this.type === "radio" ) {
180-
this.label.addClass( "ui-checkboxradio-radio-label" );
172+
this._addClass( this.label, "ui-checkboxradio-radio-label" );
181173
}
182-
183174
if ( checked ) {
184-
this.label.addClass( this._classes( "ui-checkboxradio-checked" ) +
185-
" ui-state-active" );
175+
this._addClass( this.label, "ui-checkboxradio-checked", "ui-state-active" );
186176
}
187177
if ( this.options.label && this.options.label !== this.originalLabel ) {
188178
this.label.html( this.icon ? this.icon : "" ).append( this.options.label );
@@ -197,53 +187,29 @@ $.widget( "ui.checkboxradio", {
197187

198188
_toggleClasses: function() {
199189
var checked = this.element[ 0 ].checked;
200-
this.label.toggleClass( this._classes( "ui-checkboxradio-checked" ) +
201-
" ui-state-active", checked );
190+
this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
202191
if ( this.options.icon && this.type === "checkbox" ) {
203-
this.icon
204-
.toggleClass( "ui-icon-check", checked )
205-
.toggleClass( "ui-icon-blank", !checked );
192+
this._toggleClass( this.icon, null, "ui-icon-check", checked )
193+
._toggleClass( this.icon, null, "ui-icon-blank", !checked );
206194
}
207195
if ( this.type === "radio" ) {
208196
radioGroup( this.element[0] )
209197
.not( this.element )
210-
.map(function() {
211-
return $( this ).checkboxradio( "widget" )[ 0 ];
212-
})
213-
.removeClass( "ui-state-active " + this._classes( "ui-checkboxradio-checked" ) );
198+
.each( function(){
199+
var instance = $( this ).checkboxradio( "instance" );
200+
201+
if ( instance ) {
202+
instance._removeClass( instance.label,
203+
"ui-checkboxradio-checked", "ui-state-active" );
204+
}
205+
});
214206
}
215207
},
216208

217209
_destroy: function() {
218-
this.label.removeClass( this._classes( "ui-checkboxradio-radio-label ui-checkboxradio-label" ) + " " +
219-
baseClasses + " " + typeClasses );
220210
if ( this.icon ) {
221211
this.icon.remove();
222212
}
223-
this.element.removeClass( this._classes( "ui-checkboxradio" ) +
224-
" ui-helper-hidden-accessible" );
225-
},
226-
227-
_elementsFromClassKey: function( classKey ) {
228-
var parts = classKey.split( "-" ),
229-
checkedType = parts[ 2 ] === this.type || parts[ 2 ] === undefined,
230-
checkedClass = parts[ 3 ] === "checked" || this.element[ 0 ].checked;
231-
switch ( classKey ) {
232-
case "ui-checkboxradio":
233-
case "ui-checkboxradio-radio-label":
234-
case "ui-checkboxradio-label":
235-
case "ui-checkboxradio-checked":
236-
if ( checkedType && checkedClass ) {
237-
return this.label;
238-
}
239-
return $();
240-
case "ui-checkboxradio-icon":
241-
if ( this.icon ) {
242-
return this.icon;
243-
}
244-
return $();
245-
}
246-
return this._superApply( arguments );
247213
},
248214

249215
_setOption: function( key, value ) {
@@ -253,8 +219,8 @@ $.widget( "ui.checkboxradio", {
253219
}
254220
this._super( key, value );
255221
if ( key === "disabled" ) {
256-
this.label.toggleClass( "ui-state-disabled", !!value );
257-
this.element[ 0 ].disabled = !!value;
222+
this._toggleClass( this.label, null, "ui-state-disabled", value );
223+
this.element[ 0 ].disabled = value;
258224
return;
259225
}
260226
if ( key === "label" && value === null ) {
@@ -264,11 +230,10 @@ $.widget( "ui.checkboxradio", {
264230
},
265231

266232
_updateIcon: function( checked ) {
267-
var toAdd = this._classes( "ui-checkboxradio-icon" ) +
268-
" ui-icon ui-icon-background ";
233+
var toAdd = "ui-icon ui-icon-background ";
269234

270235
if ( this.options.icon ) {
271-
this.label.addClass( "ui-icon-beginning" );
236+
this._addClass( this.label, null, "ui-icon-beginning" );
272237
if ( !this.icon ) {
273238
this.icon = $( "<span>" );
274239
}
@@ -278,9 +243,10 @@ $.widget( "ui.checkboxradio", {
278243
} else {
279244
toAdd += "ui-icon-blank";
280245
}
281-
this.icon.addClass( toAdd ).appendTo( this.label );
246+
this._addClass( this.icon, "ui-checkboxradio-icon", toAdd );
247+
this.icon.appendTo( this.label );
282248
} else if ( this.icon !== undefined ) {
283-
this.label.removeClass( "ui-icon-beginning" );
249+
this._removeClass( this.label, null, "ui-icon-beginning" );
284250
this.icon.remove();
285251
delete this.icon;
286252
}
@@ -290,8 +256,7 @@ $.widget( "ui.checkboxradio", {
290256
var checked = this.element[ 0 ].checked,
291257
isDisabled = this.element[ 0 ].disabled;
292258
this._updateIcon( checked );
293-
this.label.toggleClass( "ui-state-active " +
294-
this._classes( "ui-checkboxradio-checked" ), checked );
259+
this._toggleClass( this.label, "ui-checkboxradio-checked", "ui-state-active", checked );
295260
if ( this.options.label !== null ) {
296261
this.label.contents().not( this.element.add( this.icon ) ).remove();
297262
this.label.append( this.options.label );

0 commit comments

Comments
 (0)