Skip to content

Controlgroup: Correctly handle non-empty child class key #1713

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tests/unit/controlgroup/controlgroup.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@
<div class="controlgroup-single-button">
<button class="single-button">button</button>
</div>
<div class="controlgroup-class-key-init">
<select id="class-key-init-child">
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
<div class="controlgroup-class-key-dupe">
<select id="class-key-dupe-first">
<option>Option 1</option>
<option>Option 2</option>
</select>
<select id="class-key-dupe-second">
<option>Option 1</option>
<option>Option 2</option>
</select>
</div>
</div>
</body>
</html>
37 changes: 37 additions & 0 deletions tests/unit/controlgroup/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,41 @@ QUnit.test( "Single controlgroup button - vertical", function( assert ) {
" ui-corner-tr ui-corner-tl ui-corner-bl ui corner-br" );
} );

QUnit.module( "Controlgroup: Non-empty class key", {
setup: function() {
this.classKey = $.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ];
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why dont we just hard code a value here to simplify it really does not matter what we set here just that we set something non default

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

"something-custom";
},
teardown: function() {
$.ui.selectmenu.prototype.options.classes[ "ui-selectmenu-button-closed" ] = this.classKey;
}
} );

QUnit.test( "Controlgroup instantiates child widgets with correct options", function( assert ) {
assert.expect( 1 );

$( ".controlgroup-class-key-init" ).controlgroup();

assert.hasClasses( $( "#class-key-init-child" ).next(), "something-custom" );
} );

QUnit.test( "Controlgroup correctly assigns child widget classes options key", function( assert ) {
assert.expect( 2 );

$( ".controlgroup-class-key-dupe" ).controlgroup();

assert.strictEqual(
( $( "#class-key-dupe-first" )
.selectmenu( "option", "classes.ui-selectmenu-button-closed" )
.match( /something-custom/g ) || [] ).length, 1,
"Class 'something-custom' appears exactly once in the first widget's class key value" );

assert.strictEqual(
( $( "#class-key-dupe-second" )
.selectmenu( "option", "classes.ui-selectmenu-button-closed" )
.match( /something-custom/g ) || [] ).length, 1,
"Class 'something-custom' appears exactly once in the second widget's class key value" );
} );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we shorten all the class and id's here and in the html kind of excessively long

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure can :)


} );
20 changes: 16 additions & 4 deletions ui/widgets/controlgroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,25 @@ return $.widget( "ui.controlgroup", {
var element = $( this );
var instance = element[ widget ]( "instance" );

// We need to clone the default options for this type of widget to avoid
// polluting the variable options which has a wider scope than a single widget.
var instanceOptions = $.widget.extend( {}, options );

// If the button is the child of a spinner ignore it
// TODO: Find a more generic solution
if ( widget === "button" && element.parent( ".ui-spinner" ).length ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know you're not touching this block of code, but since you're modifying a bunch of code around it, can you just add a comment here that says "TODO: Find a more generic solution"? This is something @arschmitz and I discussed before, but we don't have any ideas for how to implement it.

return;
}

// Create the widget if it doesn't exist
if ( !instance ) {
instance = element[ widget ]()[ widget ]( "instance" );
}
if ( instance ) {
options.classes = that._resolveClassesValues( options.classes, instance );
instanceOptions.classes =
that._resolveClassesValues( instanceOptions.classes, instance );
}
element[ widget ]( options );
element[ widget ]( instanceOptions );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't be sure what the problem you're trying to fix is since you didn't include any description but im guessing its that when there are multiple of the same widget the options variable is over-written and re-used. if so i think we should fix this within the _resolveClassesValues function.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but it's more than that. Even if I were to fix _resolveClassesValues() to not modify the value that is being passed in, you'd still need $.widget.extend(), because the new object returned by _resolveClassesValues() would be stored inside options, and options would be reused at the next iteration. So, it is options as well as options.classes that need to be cloned at every iteration.


// Store an instance of the controlgroup to be able to reference
// from the outermost element for changing options and refresh
Expand Down Expand Up @@ -218,12 +229,13 @@ return $.widget( "ui.controlgroup", {
},

_resolveClassesValues: function( classes, instance ) {
var result = {};
$.each( classes, function( key ) {
var current = instance.options.classes[ key ] || "";
current = current.replace( controlgroupCornerRegex, "" ).trim();
classes[ key ] = ( current + " " + classes[ key ] ).replace( /\s+/g, " " );
result[ key ] = ( current + " " + classes[ key ] ).replace( /\s+/g, " " );
} );
return classes;
return result;
},

_setOption: function( key, value ) {
Expand Down