jquery-ui/ui/jquery.ui.selectmenu.js

469 lines
11 KiB
JavaScript
Raw Normal View History

/*!
2011-09-01 22:21:09 +00:00
* jQuery UI Selectmenu @VERSION
2012-07-16 18:36:01 +00:00
* http://jqueryui.com
2011-09-01 22:21:09 +00:00
*
* Copyright 2013 jQuery Foundation and other contributors
2012-08-11 17:57:17 +00:00
* Released under the MIT license.
2011-09-01 22:21:09 +00:00
* http://jquery.org/license
*
2012-11-29 18:41:20 +00:00
* http://api.jqueryui.com/selectmenu
2011-09-01 22:21:09 +00:00
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.position.js
* jquery.ui.menu.js
*/
(function( $, undefined ) {
$.widget( "ui.selectmenu", {
version: "@VERSION",
defaultElement: "<select>",
options: {
appendTo: null,
icons: {
button: "ui-icon-triangle-1-s"
},
2011-09-01 22:21:09 +00:00
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
2011-09-01 22:21:09 +00:00
// callbacks
change: null,
close: null,
focus: null,
open: null,
select: null
2011-09-01 22:21:09 +00:00
},
_create: function() {
2012-11-12 22:06:05 +00:00
var selectmenuId = this.element.uniqueId().attr( "id" );
this.ids = { id: selectmenuId, button: selectmenuId + "-button", menu: selectmenuId + "-menu" };
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this._drawButton();
this._drawMenu();
2012-01-27 22:11:31 +00:00
this._on( document, this._documentClick );
if ( this.options.disabled ) {
this.disable();
}
},
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
_drawButton: function() {
2012-11-12 22:06:05 +00:00
var tabindex = this.element.attr( "tabindex" );
// fix existing label
this.label = $( "label[for='" + this.ids.id + "']" ).attr( "for", this.ids.button );
this._on( this.label, {
2012-11-12 22:06:05 +00:00
"click": function( event ) {
this.button.focus();
event.preventDefault();
}
});
2011-11-18 16:14:38 +00:00
// hide original select tag
this.element.hide();
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
// create button
2012-11-29 19:10:05 +00:00
this.button = $( "<a>", {
2012-11-12 22:06:05 +00:00
"class": "ui-button ui-widget ui-state-default ui-corner-all",
href: "#" + this.ids.id,
tabindex: ( tabindex ? tabindex : this.options.disabled ? -1 : 0 ),
id: this.ids.button,
width: this.element.outerWidth(),
2012-11-12 22:06:05 +00:00
role: "combobox",
"aria-expanded": false,
"aria-autocomplete": "list",
"aria-owns": this.ids.menu,
"aria-haspopup": true
});
2012-11-29 19:10:05 +00:00
this.button.prepend( $( "<span>", {
"class": "ui-icon " + this.options.icons.button
2012-11-12 22:06:05 +00:00
}));
2012-11-29 19:10:05 +00:00
this.buttonText = $( "<span>", {
"class": "ui-selectmenu-text"
2011-09-22 19:20:49 +00:00
})
.appendTo( this.button );
this._setText( this.buttonText, this.element.find( "option:selected" ).text() );
2011-11-18 16:14:38 +00:00
// wrap and insert new button
2012-11-29 19:10:05 +00:00
this.buttonWrap = $( "<span>", {
2012-11-12 22:06:05 +00:00
"class": "ui-selectmenu-button"
2012-01-22 19:25:27 +00:00
})
2011-10-11 23:38:30 +00:00
.append( this.button )
.insertAfter( this.element );
this._on( this.button, this._buttonEvents );
this._hoverable( this.button );
this._focusable( this.button );
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
_drawMenu: function() {
2012-07-17 19:27:26 +00:00
var menuInstance,
that = this;
2011-11-18 16:14:38 +00:00
// create menu portion, append to body
2012-11-29 19:10:05 +00:00
this.menu = $( "<ul>", {
2012-11-12 22:06:05 +00:00
"aria-hidden": true,
"aria-labelledby": this.ids.button,
id: this.ids.menu
});
2011-11-18 16:14:38 +00:00
2011-10-17 20:04:14 +00:00
// wrap menu
2012-11-29 19:10:05 +00:00
this.menuWrap = $( "<div>", {
2012-11-12 22:06:05 +00:00
"class": "ui-selectmenu-menu",
width: this.button.outerWidth()
2012-01-22 19:25:27 +00:00
})
2011-10-11 23:38:30 +00:00
.append( this.menu )
.appendTo( this._appendTo() );
2011-11-18 16:14:38 +00:00
// init menu widget
2012-07-17 19:27:26 +00:00
menuInstance = this.menu.menu({
select: function( event, ui ) {
var item = ui.item.data( "ui-selectmenu-item" );
2012-01-27 22:11:31 +00:00
that._select( item, event );
2012-01-27 22:11:31 +00:00
if ( that.isOpen ) {
event.preventDefault();
that.close( event );
}
},
focus: function( event, ui ) {
var item = ui.item.data( "ui-selectmenu-item" );
// prevent inital focus from firing and checks if its a newly focused item
if ( !that.isOpen && that.focus !== undefined && item.index !== that.focus ) {
that._trigger( "focus", event, { item: item } );
if ( !that.isOpen ) {
that._select( item, event );
}
}
that.focus = item.index;
2012-12-12 16:39:00 +00:00
// Set ARIA active descendant
that.button.attr( "aria-activedescendant", that.menuItems.eq( item.index ).find( "a" ).attr( "id" ) );
},
// set ARIA role
2012-11-12 22:06:05 +00:00
role: "listbox"
2012-07-17 19:27:26 +00:00
})
.data( "ui-menu" );
2012-01-27 22:11:31 +00:00
// adjust border radius
this.menu.addClass( "ui-corner-bottom" ).removeClass( "ui-corner-all" );
2011-11-18 16:14:38 +00:00
// make sure focus stays on selected item
menuInstance.delay = 999999999;
// unbind uneeded Menu events
2012-07-17 19:27:26 +00:00
menuInstance._off( this.menu, "mouseleave" );
},
2011-11-18 16:14:38 +00:00
refresh: function() {
2011-10-11 23:38:30 +00:00
this.menu.empty();
2011-11-18 16:14:38 +00:00
2012-05-24 16:30:39 +00:00
var item,
2012-11-12 22:06:05 +00:00
options = this.element.find( "option" );
if ( options.length ) {
this._readOptions( options );
this._renderMenu( this.menu, this.items );
2012-01-27 22:11:31 +00:00
this.menu.menu( "refresh" );
2012-11-12 22:06:05 +00:00
this.menuItems = this.menu.find( "li" ).not( ".ui-selectmenu-optgroup" );
2011-11-18 16:14:38 +00:00
2012-05-24 16:30:39 +00:00
item = this._getSelectedItem();
// make sure menu is selected item aware
this.menu.menu( "focus", null, item );
this._setAria( item.data( "ui-selectmenu-item" ) );
2012-01-27 22:11:31 +00:00
// set disabled state
2012-05-16 18:41:24 +00:00
this._setOption( "disabled", this._getCreateOptions().disabled );
}
2011-11-18 16:14:38 +00:00
},
open: function( event ) {
if ( this.options.disabled ) {
return;
}
this.isOpen = true;
this._toggleAttr();
this.menuWrap.position( $.extend( { of: this.button }, this.options.position ) );
this._trigger( "open", event );
2011-11-18 16:14:38 +00:00
},
close: function( event ) {
if ( !this.isOpen ) {
return;
2011-09-22 19:20:49 +00:00
}
this.isOpen = false;
this._toggleAttr();
// check if we have an item to select
if ( this.menuItems ) {
var item = this._getSelectedItem(),
id = item.find( "a" ).attr( "id" );
this.menu.menu( "focus", null, item );
this.button.attr( "aria-activedescendant", id );
this.menu.attr( "aria-activedescendant", id );
}
this._trigger( "close", event );
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-10-17 20:13:36 +00:00
widget: function() {
return this.button;
},
menuWidget: function() {
return this.menu;
2011-10-17 20:13:36 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
_renderMenu: function( ul, items ) {
2011-09-28 21:59:23 +00:00
var that = this,
2011-09-01 22:21:09 +00:00
currentOptgroup = "";
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
$.each( items, function( index, item ) {
2012-05-24 16:30:39 +00:00
if ( item.optgroup !== currentOptgroup ) {
2013-01-02 18:52:18 +00:00
$( "<li>", {
2012-11-12 22:06:05 +00:00
"class": "ui-selectmenu-optgroup" + ( item.element.parent( "optgroup" ).attr( "disabled" ) ? " ui-state-disabled" : "" ),
text: item.optgroup
}).appendTo( ul );
2011-09-01 22:21:09 +00:00
currentOptgroup = item.optgroup;
}
2011-09-28 21:59:23 +00:00
that._renderItem( ul, item );
2011-09-01 22:21:09 +00:00
});
},
2011-11-18 16:14:38 +00:00
_renderItem: function( ul, item ) {
2013-01-02 18:52:18 +00:00
var li = $( "<li>" ).data( "ui-selectmenu-item", item ),
a = $( "<a>", { href: "#" });
if ( item.disabled ) {
2012-11-12 22:06:05 +00:00
li.addClass( "ui-state-disabled" );
2011-11-18 16:14:38 +00:00
}
this._setText( a, item.label );
return li.append( a ).appendTo( ul );
},
2011-11-18 16:14:38 +00:00
_setText: function( element, value ) {
if ( value ) {
element.text( value );
} else {
element.html( "&#160;" );
}
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2012-01-27 22:11:31 +00:00
_move: function( direction, event ) {
2012-05-16 18:41:24 +00:00
if ( direction === "first" || direction === "last" ) {
// set focus manually for first or last item
2012-01-22 19:25:27 +00:00
this.menu.menu( "focus", event, this.menuItems[ direction ]() );
} else {
// move to and focus next or prev item
2012-01-27 22:11:31 +00:00
this.menu.menu( direction, event );
}
2011-09-24 00:04:06 +00:00
},
2011-11-18 16:14:38 +00:00
_getSelectedItem: function() {
return this.menuItems.eq( this.element[ 0 ].selectedIndex );
},
2011-11-18 16:14:38 +00:00
2011-09-24 00:04:06 +00:00
_toggle: function( event ) {
2011-11-18 16:14:38 +00:00
if ( this.isOpen ) {
2011-09-24 00:04:06 +00:00
this.close( event );
2011-11-18 16:14:38 +00:00
} else {
2011-09-24 00:04:06 +00:00
this.open( event );
}
},
2011-11-18 16:14:38 +00:00
_documentClick: {
click: function( event ) {
if ( this.isOpen && !$( event.target ).closest( "li.ui-state-disabled, li.ui-selectmenu-optgroup, #" + this.ids.button ).length ) {
this.close( event );
}
}
},
_buttonEvents: {
2012-11-12 22:06:05 +00:00
focus: function() {
// init Menu on first focus
this.refresh();
// reset focus class as its removed by ui.widget._setOption
this.button.addClass( "ui-state-focus" );
2012-07-17 19:27:26 +00:00
this._off( this.button, "focus" );
},
click: function( event ) {
this._toggle( event );
event.preventDefault();
},
keydown: function( event ) {
var prevDef = true;
switch ( event.keyCode ) {
case $.ui.keyCode.TAB:
case $.ui.keyCode.ESCAPE:
if ( this.isOpen ) {
this.close( event );
}
prevDef = false;
break;
case $.ui.keyCode.ENTER:
if ( this.isOpen ) {
this.menu.menu( "select", event );
2011-11-18 16:14:38 +00:00
}
break;
case $.ui.keyCode.UP:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "previous", event );
}
break;
case $.ui.keyCode.DOWN:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "next", event );
}
break;
case $.ui.keyCode.SPACE:
if ( this.isOpen ) {
this.menu.menu( "select", event );
} else {
this._toggle( event );
}
break;
case $.ui.keyCode.LEFT:
this._move( "previous", event );
break;
case $.ui.keyCode.RIGHT:
this._move( "next", event );
2012-01-27 22:11:31 +00:00
break;
case $.ui.keyCode.HOME:
case $.ui.keyCode.PAGE_UP:
this._move( "first", event );
break;
case $.ui.keyCode.END:
case $.ui.keyCode.PAGE_DOWN:
this._move( "last", event );
break;
default:
2011-10-11 23:38:30 +00:00
this.menu.trigger( event );
prevDef = false;
}
if ( prevDef ) {
event.preventDefault();
}
}
2011-11-18 16:14:38 +00:00
},
2012-01-27 22:11:31 +00:00
_select: function( item, event ) {
var oldIndex = this.element[ 0 ].selectedIndex;
// change native select element
this.element[ 0 ].selectedIndex = item.index;
this._setText( this.buttonText, item.label );
this._setAria( item );
this._trigger( "select", event, { item: item } );
2012-05-16 18:41:24 +00:00
if ( item.index !== oldIndex ) {
this._trigger( "change", event, { item: item } );
}
},
2012-01-27 22:11:31 +00:00
_setAria: function( item ) {
2012-01-22 18:19:37 +00:00
// change ARIA attr
this.menuItems.find( "a" ).attr( "aria-selected", false );
this.menuItems.eq( item.index ).find( "a" ).attr( "aria-selected", true );
this.button.attr( "aria-labelledby", this.menuItems.eq( item.index ).find( "a" ).attr( "id" ) );
2012-01-12 21:01:49 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
_setOption: function( key, value ) {
if ( key === "icons" ) {
this.button.find( "span.ui-icon" )
.removeClass( this.options.icons.button )
.addClass( value.button );
}
this._super( key, value );
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
if ( key === "appendTo" ) {
this.menuWrap.appendTo( this._appendTo() );
2011-09-01 22:21:09 +00:00
}
if ( key === "disabled" ) {
this.menu.menu( "option", "disabled", value );
if ( value ) {
this.element.attr( "disabled", "disabled" );
2011-10-11 23:38:30 +00:00
this.button.attr( "tabindex", -1 );
2011-10-17 20:04:54 +00:00
this.close();
} else {
this.element.removeAttr( "disabled" );
this.button.attr( "tabindex", 0 );
}
}
2011-09-01 22:21:09 +00:00
},
2012-01-27 22:11:31 +00:00
_appendTo: function() {
var element = this.options.appendTo;
if ( element ) {
element = element.jquery || element.nodeType ?
$( element ) :
this.document.find( element ).eq( 0 );
}
if ( !element ) {
element = this.element.closest( ".ui-front" );
}
if ( !element.length ) {
element = this.document[0].body;
}
return element;
},
_toggleAttr: function(){
this.button.toggleClass( "ui-corner-top", this.isOpen ).toggleClass( "ui-corner-all", !this.isOpen );
2012-11-12 22:06:05 +00:00
this.menuWrap.toggleClass( "ui-selectmenu-open", this.isOpen );
this.menu.attr( "aria-hidden", !this.isOpen);
this.button.attr( "aria-expanded", this.isOpen);
},
_getCreateOptions: function() {
2012-11-12 22:06:05 +00:00
return { disabled: !!this.element.attr( "disabled" ) };
},
_readOptions: function( options ) {
2011-11-18 16:14:38 +00:00
var data = [];
options.each( function( index, item ) {
var option = $( item ),
optgroup = option.parent( "optgroup" );
data.push({
element: option,
index: index,
2012-11-12 22:06:05 +00:00
value: option.attr( "value" ),
label: option.text(),
2012-05-24 16:30:39 +00:00
optgroup: optgroup.attr( "label" ) || "",
disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" )
2011-09-24 02:44:13 +00:00
});
});
this.items = data;
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
2011-09-24 00:04:06 +00:00
_destroy: function() {
2011-10-11 23:38:30 +00:00
this.menuWrap.remove();
this.buttonWrap.remove();
this.element.show();
this.element.removeUniqueId();
this.label.attr( "for", this.ids.id );
}
2011-09-01 22:21:09 +00:00
});
}( jQuery ));