Skip to content

Commit d49f57e

Browse files
committed
Controlgroup: updates based on new classes api
1 parent dc89ba4 commit d49f57e

File tree

2 files changed

+71
-82
lines changed

2 files changed

+71
-82
lines changed

tests/unit/controlgroup/controlgroup_common.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ TestHelpers.commonWidgetTests( "controlgroup", {
88
},
99
direction: "horizontal",
1010
excludeInvisible: true,
11-
classes: {
12-
"ui-controlgroup": "",
13-
"ui-controlgroup-horizontal": "",
14-
"ui-controlgroup-vertical": ""
15-
},
11+
classes: {},
1612

1713
// Callbacks
1814
create: null

ui/controlgroup.js

Lines changed: 70 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,7 @@ $.widget( "ui.controlgroup", {
3636
},
3737
direction: "horizontal",
3838
excludeInvisible: true,
39-
classes: {
40-
"ui-controlgroup": "",
41-
"ui-controlgroup-horizontal": "",
42-
"ui-controlgroup-vertical": ""
43-
}
39+
classes: {}
4440
},
4541

4642
_create: function() {
@@ -54,59 +50,87 @@ $.widget( "ui.controlgroup", {
5450
},
5551

5652
_destroy: function() {
53+
var that = this;
54+
$.each( this.options.items, function( widget, selector ) {
55+
that.element.children( selector ).map(function(){
56+
return $( this )[ widget ]( "widget" )[ 0 ];
57+
}).removeData( "ui-controlgroup-data" );
58+
});
5759
this._callChildMethod( "destroy" );
5860
this.element.removeAttr( "role" );
59-
this.element.removeClass(
60-
this._classes( "ui-controlgroup ui-controlgroup-vertical ui-controlgroup-horizontal" )
61-
).children().removeClass( "ui-corner-all ui-corner-top" +
62-
" ui-corner-bottom ui-corner-left ui-corner-tl ui-corner-tr" );
6361
},
6462

6563
_callChildMethod: function( method ) {
6664
var that = this;
6765
$.each( this.options.items, function( widget, selector ) {
6866
var options = {};
6967
if ( that[ "_" + widget + "_options" ] ) {
70-
options = that[ "_" + widget + "_options" ]();
68+
options = that[ "_" + widget + "_options" ]( "middle" );
7169
}
7270
if ( $.fn[ widget ] && selector ) {
73-
that.element.children( selector )[ widget ]( method ?
74-
method : options );
71+
that.element
72+
.children( selector )[ widget ]( method ? method: options )
73+
.each(function(){
74+
if ( method !== "destroy" ) {
75+
$( this )[ widget ]( "widget" ).data( "ui-controlgroup-data", {
76+
"type": widget,
77+
"element": $( this )
78+
});
79+
}
80+
});
7581
}
7682
});
7783
},
7884

79-
_button_options: function() {
80-
return {
81-
classes: {
82-
"ui-button": ""
83-
}
84-
};
85-
},
85+
_button_options: function( position ) {
86+
var cornerClasses = {
87+
"middle": null,
88+
"first": "ui-corner-" + ( ( this.options.direction === "vertical" )? "top" : "left" ),
89+
"last": "ui-corner-" + ( ( this.options.direction === "vertical" )? "bottom" : "right" )
90+
};
8691

87-
_checkboxradio_options: function() {
8892
return {
8993
classes: {
90-
"ui-checkbox-label": "",
91-
"ui-radio-label": ""
94+
"ui-button": cornerClasses[ position ]
9295
}
9396
};
9497
},
9598

96-
_selectmenu_options: function() {
99+
_checkboxradio_options: function( position ) {
100+
var cornerClasses = {
101+
"middle": null,
102+
"first": "ui-corner-" + ( ( this.options.direction === "vertical" )? "top" : "left" ),
103+
"last": "ui-corner-" + ( ( this.options.direction === "vertical" )? "bottom" : "right" )
104+
};
105+
97106
return {
98107
classes: {
99-
"ui-selectmenu-button-open": "",
100-
"ui-selectmenu-button-closed": ""
108+
"ui-checkboxradio-label": cornerClasses[ position ]
101109
}
102110
};
103111
},
104112

105-
_elementsFromClassKey: function( classKey ) {
106-
if ( this.options.direction !== classKey.split( "-" )[ 2 ] ) {
107-
return $();
108-
}
109-
return this._superApply( arguments );
113+
_selectmenu_options: function( position ) {
114+
var classes = {
115+
middle: {
116+
"ui-selectmenu-button-open": null,
117+
"ui-selectmenu-button-closed": null
118+
},
119+
first: {
120+
"ui-selectmenu-button-open":
121+
"ui-corner-" + ( ( this.options.direction === "vertical" )? "top": "tl" ),
122+
"ui-selectmenu-button-closed":
123+
"ui-corner-" + ( ( this.options.direction === "vertical" )? "top": "left" )
124+
},
125+
last: {
126+
"ui-selectmenu-button-open":
127+
( this.options.direction === "vertical" )? null: "ui-corner-tr",
128+
"ui-selectmenu-button-closed":
129+
"ui-corner-" + ( ( this.options.direction === "vertical" )? "bottom": "right" )
130+
}
131+
132+
};
133+
return { classes: classes[ position ] };
110134
},
111135

112136
_setOption: function( key, value ) {
@@ -124,55 +148,24 @@ $.widget( "ui.controlgroup", {
124148

125149
},
126150

127-
_refresh_selectmenu: function() {
128-
var firstClasses = {},
129-
lastClasses = {},
130-
vertical = ( this.options.direction === "vertical" );
131-
132-
if ( $.ui.selectmenu ) {
133-
if ( this.first.is( ".ui-selectmenu-button" ) && !vertical ) {
134-
firstClasses[ "ui-selectmenu-button-open" ] = "ui-corner-tl";
135-
firstClasses[ "ui-selectmenu-button-closed" ] = "ui-corner-left";
136-
$( "#" + this.first.attr( "id" ).replace( /-button/, "" ) )
137-
.selectmenu( "option", "classes", firstClasses );
138-
}
139-
if ( this.last.is( ".ui-selectmenu-button" ) ) {
140-
if ( vertical ) {
141-
lastClasses[ "ui-selectmenu-button-open" ] = null;
142-
lastClasses[ "ui-selectmenu-button-closed" ] = "ui-corner-bottom";
143-
} else {
144-
lastClasses[ "ui-selectmenu-button-open" ] = "ui-corner-tr";
145-
lastClasses[ "ui-selectmenu-button-closed" ] = "ui-corner-right";
146-
}
147-
$( "#" + this.last.attr( "id" ).replace( /-button/, "" ) )
148-
.selectmenu( "option", "classes", lastClasses );
149-
}
150-
this.element.find( this.options.items.selectmenu ).selectmenu( "refresh" );
151-
}
152-
},
153-
154151
refresh: function() {
155-
var that = this,
156-
vertical = ( this.options.direction === "vertical" );
157-
this.element.addClass( this._classes( "ui-controlgroup ui-controlgroup-" +
158-
this.options.direction ) );
159-
this._callChildMethod( undefined );
160-
this.visible = this.element.children( ".ui-button" ).removeClass( function(index, css) {
161-
return ( css.match( /ui-corner-[a-z]*/g ) || [] ).join( " " );
162-
}).filter( this.options.excludeInvisible ? ":visible" : "*" );
163-
164-
this.first = this.visible.eq( 0 )
165-
.addClass( "ui-corner-" + ( vertical ? "top" : "left" ) );
166-
this.last = this.visible.last()
167-
.addClass( "ui-corner-" + ( vertical ? "bottom" : "right" ) );
168-
169-
$.each( this.options.items, function( widget ) {
170-
if ( that[ "_refresh_" + widget ] ) {
171-
that[ "_refresh_" + widget ]();
172-
}
173-
});
174-
this._callChildMethod( "refresh" );
152+
var children,
153+
that = this;
154+
155+
this._addClass( "ui-controlgroup ui-controlgroup-" + this.options.direction );
156+
this._callChildMethod();
157+
children = this.element.children( ".ui-button" );
175158

159+
if ( this.options.excludeInvisible ) {
160+
children = children.filter( ":visible" );
161+
}
162+
if ( children.length ) {
163+
[ "first", "last" ].forEach( function( value ){
164+
var data = children[ value ]().data( "ui-controlgroup-data" );
165+
data.element[ data.type ]( that[ "_" + data.type + "_options" ]( value ) );
166+
});
167+
this._callChildMethod( "refresh" );
168+
}
176169
}
177170

178171
});

0 commit comments

Comments
 (0)