-
Notifications
You must be signed in to change notification settings - Fork 5.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" ] = | ||
"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" ); | ||
} ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure can :) |
||
|
||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
// Store an instance of the controlgroup to be able to reference | ||
// from the outermost element for changing options and refresh | ||
|
@@ -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 ) { | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.