mirror of
https://github.com/jquery/jquery-ui.git
synced 2025-01-07 20:34:24 +00:00
Selectmenu: cleanup, CGL and lint fix
This commit is contained in:
parent
7ce8e0515e
commit
12fe28fefd
82
ui/jquery.ui.selectmenu.js
vendored
82
ui/jquery.ui.selectmenu.js
vendored
@ -37,10 +37,10 @@ $.widget( "ui.selectmenu", {
|
|||||||
|
|
||||||
_create: function() {
|
_create: function() {
|
||||||
// make / set unique id
|
// make / set unique id
|
||||||
var selectmenuId = this.element.uniqueId().attr( 'id' );
|
var selectmenuId = this.element.uniqueId().attr( "id" );
|
||||||
|
|
||||||
// array of button and menu id's
|
// array of button and menu id's
|
||||||
this.ids = { id: selectmenuId, button: selectmenuId + '-button', menu: selectmenuId + '-menu' };
|
this.ids = { id: selectmenuId, button: selectmenuId + "-button", menu: selectmenuId + "-menu" };
|
||||||
|
|
||||||
this._drawButton();
|
this._drawButton();
|
||||||
this._on( this.button, this._buttonEvents );
|
this._on( this.button, this._buttonEvents );
|
||||||
@ -64,13 +64,13 @@ $.widget( "ui.selectmenu", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_drawButton: function() {
|
_drawButton: function() {
|
||||||
var tabindex = this.element.attr( 'tabindex' );
|
var tabindex = this.element.attr( "tabindex" );
|
||||||
|
|
||||||
// fix existing label
|
// fix existing label
|
||||||
this.label = $( "label[for='" + this.ids.id + "']" ).attr( "for", this.ids.button );
|
this.label = $( "label[for='" + this.ids.id + "']" ).attr( "for", this.ids.button );
|
||||||
// catch click event of the label
|
// catch click event of the label
|
||||||
this._on( this.label, {
|
this._on( this.label, {
|
||||||
'click': function( event ) {
|
"click": function( event ) {
|
||||||
this.button.focus();
|
this.button.focus();
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
}
|
}
|
||||||
@ -80,30 +80,32 @@ $.widget( "ui.selectmenu", {
|
|||||||
this.element.hide();
|
this.element.hide();
|
||||||
|
|
||||||
// create button
|
// create button
|
||||||
this.button = $( '<a />', {
|
this.button = $( "<a />", {
|
||||||
'class': 'ui-button ui-widget ui-state-default ui-corner-all',
|
"class": "ui-button ui-widget ui-state-default ui-corner-all",
|
||||||
href: '#' + this.ids.id,
|
href: "#" + this.ids.id,
|
||||||
tabindex: ( tabindex ? tabindex : this.options.disabled ? -1 : 0 ),
|
tabindex: ( tabindex ? tabindex : this.options.disabled ? -1 : 0 ),
|
||||||
id: this.ids.button,
|
id: this.ids.button,
|
||||||
width: this.element.outerWidth(),
|
width: this.element.outerWidth(),
|
||||||
role: 'combobox',
|
role: "combobox",
|
||||||
'aria-expanded': false,
|
"aria-expanded": false,
|
||||||
'aria-autocomplete': 'list',
|
"aria-autocomplete": "list",
|
||||||
'aria-owns': this.ids.menu,
|
"aria-owns": this.ids.menu,
|
||||||
'aria-haspopup': true
|
"aria-haspopup": true
|
||||||
});
|
});
|
||||||
|
|
||||||
this.button.prepend( $( '<span class="ui-icon ' + ( this.options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' ) + '"/>' ) );
|
this.button.prepend( $( "<span />", {
|
||||||
|
"class": "ui-icon " + ( ( this.options.dropdown ) ? "ui-icon-triangle-1-s" : "ui-icon-triangle-2-n-s" )
|
||||||
|
}));
|
||||||
|
|
||||||
this.buttonText = $( '<span />', {
|
this.buttonText = $( "<span />", {
|
||||||
'class': 'ui-selectmenu-text' ,
|
"class": "ui-selectmenu-text" ,
|
||||||
html: this.element.find( "option:selected" ).text() || ' '
|
html: this.element.find( "option:selected" ).text() || " "
|
||||||
})
|
})
|
||||||
.appendTo( this.button );
|
.appendTo( this.button );
|
||||||
|
|
||||||
// wrap and insert new button
|
// wrap and insert new button
|
||||||
this.buttonWrap = $( '<span />', {
|
this.buttonWrap = $( "<span />", {
|
||||||
'class': 'ui-selectmenu-button'
|
"class": "ui-selectmenu-button"
|
||||||
})
|
})
|
||||||
.append( this.button )
|
.append( this.button )
|
||||||
.insertAfter( this.element );
|
.insertAfter( this.element );
|
||||||
@ -114,16 +116,16 @@ $.widget( "ui.selectmenu", {
|
|||||||
that = this;
|
that = this;
|
||||||
|
|
||||||
// create menu portion, append to body
|
// create menu portion, append to body
|
||||||
this.menu = $( '<ul />', {
|
this.menu = $( "<ul />", {
|
||||||
'aria-hidden': true,
|
"aria-hidden": true,
|
||||||
'aria-labelledby': this.ids.button,
|
"aria-labelledby": this.ids.button,
|
||||||
id: this.ids.menu
|
id: this.ids.menu
|
||||||
});
|
});
|
||||||
|
|
||||||
// wrap menu
|
// wrap menu
|
||||||
this.menuWrap = $( '<div />', {
|
this.menuWrap = $( "<div />", {
|
||||||
'class': 'ui-selectmenu-menu',
|
"class": "ui-selectmenu-menu",
|
||||||
width: ( this.options.dropdown ) ? this.button.outerWidth() : this.buttonText.width() + parseFloat( this.buttonText.css( "padding-left" ) ) || 0 + parseFloat( this.buttonText.css( "margin-left" ) ) || 0
|
width: ( this.options.dropdown ) ? this.button.outerWidth() : this.buttonText.width() + parseFloat( this.buttonText.css( "padding-left" ) ) || 0 + parseFloat( this.buttonText.css( "margin-left") ) || 0
|
||||||
})
|
})
|
||||||
.append( this.menu )
|
.append( this.menu )
|
||||||
.appendTo( this.options.appendTo );
|
.appendTo( this.options.appendTo );
|
||||||
@ -157,13 +159,13 @@ $.widget( "ui.selectmenu", {
|
|||||||
that.button.attr( "aria-activedescendant", that.menuItems.eq( item.index ).find( "a" ).attr( "id" ) );
|
that.button.attr( "aria-activedescendant", that.menuItems.eq( item.index ).find( "a" ).attr( "id" ) );
|
||||||
},
|
},
|
||||||
// set ARIA role
|
// set ARIA role
|
||||||
role: 'listbox'
|
role: "listbox"
|
||||||
})
|
})
|
||||||
.data( "ui-menu" );
|
.data( "ui-menu" );
|
||||||
|
|
||||||
// change menu styles?
|
// change menu styles?
|
||||||
if ( this.options.dropdown ) {
|
if ( this.options.dropdown ) {
|
||||||
this.menu.addClass( 'ui-corner-bottom' ).removeClass( 'ui-corner-all' );
|
this.menu.addClass( "ui-corner-bottom" ).removeClass( "ui-corner-all" );
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure focus stays on selected item
|
// make sure focus stays on selected item
|
||||||
@ -176,13 +178,13 @@ $.widget( "ui.selectmenu", {
|
|||||||
this.menu.empty();
|
this.menu.empty();
|
||||||
|
|
||||||
var item,
|
var item,
|
||||||
options = this.element.find( 'option' );
|
options = this.element.find( "option" );
|
||||||
if ( options.length ) {
|
if ( options.length ) {
|
||||||
this._readOptions( options );
|
this._readOptions( options );
|
||||||
this._renderMenu( this.menu, this.items );
|
this._renderMenu( this.menu, this.items );
|
||||||
|
|
||||||
this.menu.menu( "refresh" );
|
this.menu.menu( "refresh" );
|
||||||
this.menuItems = this.menu.find( "li" ).not( '.ui-selectmenu-optgroup' );
|
this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" );
|
||||||
|
|
||||||
// select current item
|
// select current item
|
||||||
item = this._getSelectedItem();
|
item = this._getSelectedItem();
|
||||||
@ -256,8 +258,8 @@ $.widget( "ui.selectmenu", {
|
|||||||
|
|
||||||
$.each( items, function( index, item ) {
|
$.each( items, function( index, item ) {
|
||||||
if ( item.optgroup !== currentOptgroup ) {
|
if ( item.optgroup !== currentOptgroup ) {
|
||||||
$( '<li />', {
|
$( "<li />", {
|
||||||
'class': 'ui-selectmenu-optgroup' + ( item.element.parent( "optgroup" ).attr( "disabled" ) ? ' ui-state-disabled' : '' ),
|
"class": "ui-selectmenu-optgroup" + ( item.element.parent( "optgroup" ).attr( "disabled" ) ? " ui-state-disabled" : "" ),
|
||||||
html: item.optgroup
|
html: item.optgroup
|
||||||
}).appendTo( ul );
|
}).appendTo( ul );
|
||||||
currentOptgroup = item.optgroup;
|
currentOptgroup = item.optgroup;
|
||||||
@ -269,11 +271,11 @@ $.widget( "ui.selectmenu", {
|
|||||||
_renderItem: function( ul, item ) {
|
_renderItem: function( ul, item ) {
|
||||||
var li = $( "<li />" ).data( "ui-selectmenu-item", item );
|
var li = $( "<li />" ).data( "ui-selectmenu-item", item );
|
||||||
if ( item.disabled ) {
|
if ( item.disabled ) {
|
||||||
li.addClass( 'ui-state-disabled' );
|
li.addClass( "ui-state-disabled" );
|
||||||
}
|
}
|
||||||
li.append( $( "<a />", {
|
li.append( $( "<a />", {
|
||||||
html: item.label,
|
html: item.label,
|
||||||
href: '#'
|
href: "#"
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
@ -303,7 +305,7 @@ $.widget( "ui.selectmenu", {
|
|||||||
},
|
},
|
||||||
|
|
||||||
_buttonEvents: {
|
_buttonEvents: {
|
||||||
focus: function( event ) {
|
focus: function() {
|
||||||
// init Menu on first focus
|
// init Menu on first focus
|
||||||
this.refresh();
|
this.refresh();
|
||||||
// reset focus class as its removed by ui.widget._setOption
|
// reset focus class as its removed by ui.widget._setOption
|
||||||
@ -412,15 +414,15 @@ $.widget( "ui.selectmenu", {
|
|||||||
|
|
||||||
_toggleAttr: function(){
|
_toggleAttr: function(){
|
||||||
if ( this.options.dropdown ) {
|
if ( this.options.dropdown ) {
|
||||||
this.button.toggleClass( 'ui-corner-top', this.isOpen ).toggleClass( 'ui-corner-all', !this.isOpen );
|
this.button.toggleClass( "ui-corner-top", this.isOpen ).toggleClass( "ui-corner-all", !this.isOpen );
|
||||||
}
|
}
|
||||||
this.menuWrap.toggleClass( 'ui-selectmenu-open', this.isOpen );
|
this.menuWrap.toggleClass( "ui-selectmenu-open", this.isOpen );
|
||||||
this.menu.attr("aria-hidden", !this.isOpen);
|
this.menu.attr( "aria-hidden", !this.isOpen);
|
||||||
this.button.attr("aria-expanded", this.isOpen);
|
this.button.attr( "aria-expanded", this.isOpen);
|
||||||
},
|
},
|
||||||
|
|
||||||
_getCreateOptions: function() {
|
_getCreateOptions: function() {
|
||||||
return { disabled: !!this.element.attr( 'disabled' ) };
|
return { disabled: !!this.element.attr( "disabled" ) };
|
||||||
},
|
},
|
||||||
|
|
||||||
_readOptions: function( options ) {
|
_readOptions: function( options ) {
|
||||||
@ -431,8 +433,8 @@ $.widget( "ui.selectmenu", {
|
|||||||
data.push({
|
data.push({
|
||||||
element: option,
|
element: option,
|
||||||
index: index,
|
index: index,
|
||||||
value: option.attr( 'value' ),
|
value: option.attr( "value" ),
|
||||||
label: option.text() || ' ',
|
label: option.text() || " ",
|
||||||
optgroup: optgroup.attr( "label" ) || "",
|
optgroup: optgroup.attr( "label" ) || "",
|
||||||
disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" )
|
disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" )
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user