Skip to content

Commit f416a66

Browse files
committed
Selectmenu: Code review.
1 parent 134fcaf commit f416a66

File tree

1 file changed

+61
-56
lines changed

1 file changed

+61
-56
lines changed

ui/jquery.ui.selectmenu.js

Lines changed: 61 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ $.widget( "ui.selectmenu", {
5757
_drawButton: function() {
5858
var tabindex = this.element.attr( "tabindex" );
5959

60-
// Fix existing label
60+
// Associate existing label with the new button
6161
this.label = $( "label[for='" + this.ids.element + "']" ).attr( "for", this.ids.button );
6262
this._on( this.label, {
6363
click: function( event ) {
@@ -66,7 +66,7 @@ $.widget( "ui.selectmenu", {
6666
}
6767
});
6868

69-
// Hide original select tag
69+
// Hide original select element
7070
this.element.hide();
7171

7272
// Create button
@@ -101,7 +101,7 @@ $.widget( "ui.selectmenu", {
101101
_drawMenu: function() {
102102
var that = this;
103103

104-
// Create menu portion, append to body
104+
// Create menu
105105
this.menu = $( "<ul>", {
106106
"aria-hidden": "true",
107107
"aria-labelledby": this.ids.button,
@@ -110,28 +110,24 @@ $.widget( "ui.selectmenu", {
110110

111111
// Wrap menu
112112
this.menuWrap = $( "<div>", {
113-
"class": "ui-selectmenu-menu ui-front",
114-
outerWidth: this.button.outerWidth()
115-
})
116-
.append( this.menu )
117-
.appendTo( this._appendTo() );
113+
"class": "ui-selectmenu-menu ui-front",
114+
outerWidth: this.button.outerWidth()
115+
})
116+
.append( this.menu )
117+
.appendTo( this._appendTo() );
118118

119-
// Init menu widget
119+
// Initialize menu widget
120120
this.menuInstance = this.menu.menu({
121+
role: "listbox",
121122
select: function( event, ui ) {
122123
var item = ui.item.data( "ui-selectmenu-item" );
123124

124125
that._select( item, event );
125-
126-
if ( that.isOpen ) {
127-
event.preventDefault();
128-
that.close( event );
129-
}
130126
},
131127
focus: function( event, ui ) {
132128
var item = ui.item.data( "ui-selectmenu-item" );
133129

134-
// prevent inital focus from firing and checks if its a newly focused item
130+
// Prevent inital focus from firing and checks if its a newly focused item
135131
if ( that.focusIndex != null && item.index !== that.focusIndex ) {
136132
that._trigger( "focus", event, { item: item } );
137133
if ( !that.isOpen ) {
@@ -140,16 +136,18 @@ $.widget( "ui.selectmenu", {
140136
}
141137
that.focusIndex = item.index;
142138

143-
that.button.attr( "aria-activedescendant", that.menuItems.eq( item.index ).attr( "id" ) );
144-
},
145-
role: "listbox"
139+
that.button.attr( "aria-activedescendant",
140+
that.menuItems.eq( item.index ).attr( "id" ) );
141+
}
146142
})
147143
.menu( "instance" );
148144

149-
// adjust menu styles to dropdown
145+
// Adjust menu styles to dropdown
150146
this.menu.addClass( "ui-corner-bottom" ).removeClass( "ui-corner-all" );
151147

152-
// Unbind uneeded Menu events
148+
// TODO: Can we make this cleaner?
149+
// If not, at least update the comment to say what we're removing
150+
// Unbind uneeded menu events
153151
this.menuInstance._off( this.menu, "mouseleave" );
154152

155153
// Cancel the menu's collapseAll on document click
@@ -171,32 +169,34 @@ $.widget( "ui.selectmenu", {
171169
this._readOptions( options );
172170
this._renderMenu( this.menu, this.items );
173171

174-
this.menu.menu( "refresh" );
172+
this.menuInstance.refresh();
175173
this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" ).find( "a" );
176174

177175
item = this._getSelectedItem();
178176

179-
// Make sure menu is selected item aware
180-
this.menu.menu( "focus", null, item );
177+
// Update the menu to have the correct item focused
178+
this.menuInstance.focus( null, item );
181179
this._setAria( item.data( "ui-selectmenu-item" ) );
182180

183181
this._setText( this.buttonText, item.text() );
184182

185183
// Set disabled state
186-
this._setOption( "disabled", !!this.element.prop( "disabled" ) );
184+
this._setOption( "disabled", this.element.prop( "disabled" ) );
187185
},
188186

189187
open: function( event ) {
190188
if ( this.options.disabled ) {
191189
return;
192190
}
193191

194-
// Support: IE6-IE9 click doesn't trigger focus on the button
192+
// If this is the first time the menu is being opened, render the items
195193
if ( !this.menuItems ) {
196194
this.refresh();
197195
} else {
196+
// TODO: Why is this necessary?
197+
// Shouldn't the underlying menu always have accurate state?
198198
this.menu.find( ".ui-state-focus" ).removeClass( "ui-state-focus" );
199-
this.menu.menu( "focus", null, this._getSelectedItem() );
199+
this.menuInstance.focus( null, this._getSelectedItem() );
200200
}
201201

202202
this.isOpen = true;
@@ -245,9 +245,13 @@ $.widget( "ui.selectmenu", {
245245
$.each( items, function( index, item ) {
246246
if ( item.optgroup !== currentOptgroup ) {
247247
$( "<li>", {
248-
"class": "ui-selectmenu-optgroup" + ( item.element.parent( "optgroup" ).attr( "disabled" ) ? " ui-state-disabled" : "" ),
248+
"class": "ui-selectmenu-optgroup" +
249+
( item.element.parent( "optgroup" ).attr( "disabled" ) ?
250+
" ui-state-disabled" :
251+
"" ),
249252
text: item.optgroup
250-
}).appendTo( ul );
253+
})
254+
.appendTo( ul );
251255
currentOptgroup = item.optgroup;
252256
}
253257
that._renderItemData( ul, item );
@@ -283,7 +287,8 @@ $.widget( "ui.selectmenu", {
283287
// Set focus manually for first or last item
284288
this.menu.menu( "focus", event, this.menuItems[ direction ]() );
285289
} else {
286-
if ( direction === "previous" && this.menu.menu( "isFirstItem" ) || direction === "next" && this.menu.menu( "isLastItem" ) ) {
290+
if ( direction === "previous" && this.menu.menu( "isFirstItem" ) ||
291+
direction === "next" && this.menu.menu( "isLastItem" ) ) {
287292
return;
288293
}
289294

@@ -313,30 +318,25 @@ $.widget( "ui.selectmenu", {
313318
},
314319

315320
_buttonEvents: {
316-
focus: function() {
317-
// Init Menu on first focus
318-
this.refresh();
319-
// Reset focus class as its removed by ui.widget._setOption
320-
this.button.addClass( "ui-state-focus" );
321-
this._off( this.button, "focus" );
322-
},
323-
click: function( event ) {
324-
this._toggle( event );
325-
event.preventDefault();
321+
focusin: function() {
322+
// Delay rendering the menu items until the button receives focus
323+
if ( !this.menuItems ) {
324+
this.refresh();
325+
}
326+
this._off( this.button, "focusin" );
326327
},
328+
click: "_toggle",
327329
keydown: function( event ) {
328-
var prevDef = true;
330+
var preventDefault = true;
329331
switch ( event.keyCode ) {
330332
case $.ui.keyCode.TAB:
331333
case $.ui.keyCode.ESCAPE:
332-
if ( this.isOpen ) {
333-
this.close( event );
334-
}
335-
prevDef = false;
334+
this.close( event );
335+
preventDefault = false;
336336
break;
337337
case $.ui.keyCode.ENTER:
338338
if ( this.isOpen ) {
339-
this.menu.menu( "select", event );
339+
this.menuInstance.select( event );
340340
}
341341
break;
342342
case $.ui.keyCode.UP:
@@ -355,7 +355,7 @@ $.widget( "ui.selectmenu", {
355355
break;
356356
case $.ui.keyCode.SPACE:
357357
if ( this.isOpen ) {
358-
this.menu.menu( "select", event );
358+
this.menuInstance.select( event );
359359
} else {
360360
this._toggle( event );
361361
}
@@ -376,16 +376,18 @@ $.widget( "ui.selectmenu", {
376376
break;
377377
default:
378378
this.menu.trigger( event );
379-
prevDef = false;
379+
preventDefault = false;
380380
}
381-
if ( prevDef ) {
381+
382+
if ( preventDefault ) {
382383
event.preventDefault();
383384
}
384385
}
385386
},
386387

387388
_select: function( item, event ) {
388389
var oldIndex = this.element[ 0 ].selectedIndex;
390+
389391
// Change native select element
390392
this.element[ 0 ].selectedIndex = item.index;
391393
this._setText( this.buttonText, item.label );
@@ -395,6 +397,8 @@ $.widget( "ui.selectmenu", {
395397
if ( item.index !== oldIndex ) {
396398
this._trigger( "change", event, { item: item } );
397399
}
400+
401+
this.close( event );
398402
},
399403

400404
_setAria: function( item ) {
@@ -421,17 +425,16 @@ $.widget( "ui.selectmenu", {
421425
this.menuWrap.appendTo( this._appendTo() );
422426
}
423427
if ( key === "disabled" ) {
424-
this.menu.menu( "option", "disabled", value );
428+
this.menuInstance.option( "disabled", value );
425429
this.button
426-
.toggleClass( "ui-state-disabled", !!value )
430+
.toggleClass( "ui-state-disabled", value )
427431
.attr( "aria-disabled", value );
428432

433+
this.element.prop( "disabled", value );
429434
if ( value ) {
430-
this.element.attr( "disabled", "disabled" );
431435
this.button.attr( "tabindex", -1 );
432436
this.close();
433437
} else {
434-
this.element.removeAttr( "disabled" );
435438
this.button.attr( "tabindex", 0 );
436439
}
437440
}
@@ -458,14 +461,16 @@ $.widget( "ui.selectmenu", {
458461
},
459462

460463
_toggleAttr: function(){
461-
this.button.toggleClass( "ui-corner-top", this.isOpen ).toggleClass( "ui-corner-all", !this.isOpen );
464+
this.button
465+
.toggleClass( "ui-corner-top", this.isOpen )
466+
.toggleClass( "ui-corner-all", !this.isOpen );
462467
this.menuWrap.toggleClass( "ui-selectmenu-open", this.isOpen );
463-
this.menu.attr( "aria-hidden", !this.isOpen);
464-
this.button.attr( "aria-expanded", this.isOpen);
468+
this.menu.attr( "aria-hidden", !this.isOpen );
469+
this.button.attr( "aria-expanded", this.isOpen );
465470
},
466471

467472
_getCreateOptions: function() {
468-
return { disabled: !!this.element.prop( "disabled" ) };
473+
return { disabled: this.element.prop( "disabled" ) };
469474
},
470475

471476
_readOptions: function( options ) {

0 commit comments

Comments
 (0)