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

414 lines
9.8 KiB
JavaScript
Raw Normal View History

2011-09-01 22:21:09 +00:00
/*
* jQuery UI Selectmenu @VERSION
*
* Copyright 2011, AUTHORS.txt (http://jqueryui.com/about)
* Dual licensed under the MIT or GPL Version 2 licenses.
* http://jquery.org/license
*
* http://docs.jquery.com/UI/Selectmenu
*
* Depends:
* jquery.ui.core.js
* jquery.ui.widget.js
* jquery.ui.position.js
* jquery.ui.menu.js
2011-09-21 23:35:54 +00:00
* jquery.ui.button.js
2011-09-01 22:21:09 +00:00
*/
(function( $, undefined ) {
$.widget( "ui.selectmenu", {
version: "@VERSION",
defaultElement: "<select>",
options: {
2011-09-22 19:20:49 +00:00
dropdown: true,
2011-09-01 22:21:09 +00:00
appendTo: "body",
position: {
my: "left top",
at: "left bottom",
collision: "none"
},
2011-09-24 02:44:13 +00:00
value: null,
2011-09-01 22:21:09 +00:00
// callbacks
open: null,
focus: null,
select: null,
close: null,
change: null
2011-09-01 22:21:09 +00:00
},
_create: function() {
// set a default id value, generate a new random one if not set by developer
var selectmenuId = this.element.attr( 'id' ) || 'ui-selectmenu-' + Math.random().toString( 16 ).slice( 2, 10 );
2011-11-18 16:14:38 +00:00
// array of button and menu id's
this.ids = { id: selectmenuId, button: selectmenuId + '-button', menu: selectmenuId + '-menu' };
2011-11-18 16:14:38 +00:00
// set current value
if ( this.options.value ) {
this.element[0].value = this.options.value;
} else {
this.options.value = this.element[0].value;
}
2011-11-18 16:14:38 +00:00
// catch click event of the label
this._bind({
'click': function( event ) {
2011-10-11 23:38:30 +00:00
this.button.focus();
event.preventDefault();
}
});
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this._drawButton();
this._bind( this.button, this._buttonEvents );
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this._drawMenu();
2011-11-18 16:14:38 +00:00
this.refresh();
},
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
_drawButton: function() {
2011-11-18 16:14:38 +00:00
var tabindex = this.element.attr( 'tabindex' );
// 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
2011-10-11 23:38:30 +00:00
this.button = $( '<a />', {
href: '#' + this.ids.id,
tabindex: ( tabindex ? tabindex : this.element.attr( 'disabled' ) ? -1 : 0 ),
id: this.ids.button,
2011-09-01 22:21:09 +00:00
css: {
width: this.element.outerWidth()
},
'aria-disabled': this.options.disabled,
'aria-owns': this.ids.menu,
2011-11-18 16:14:38 +00:00
'aria-haspopup': true
2011-09-22 19:20:49 +00:00
})
.button({
label: this.element.find( "option:selected" ).text(),
2011-09-01 22:21:09 +00:00
icons: {
primary: ( this.options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' )
2011-09-01 22:21:09 +00:00
}
});
2011-11-18 16:14:38 +00:00
// wrap and insert new button
2011-10-11 23:38:30 +00:00
this.buttonWrap = $( '<span />' )
.addClass( 'ui-selectmenu-button' )
2011-10-11 23:38:30 +00:00
.append( this.button )
2011-11-18 16:14:38 +00:00
.insertAfter( this.element );
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() {
var that = this;
2011-11-18 16:14:38 +00:00
// create menu portion, append to body
2011-10-11 23:38:30 +00:00
this.menu = $( '<ul />', {
'class': 'ui-widget ui-widget-content',
'aria-hidden': true,
'aria-labelledby': this.ids.button,
2011-10-11 23:38:30 +00:00
role: 'menubox',
id: this.ids.menu
});
2011-11-18 16:14:38 +00:00
// set width
if ( this.options.dropdown ) {
2011-10-11 23:38:30 +00:00
var setWidth = this.button.outerWidth();
2011-09-28 20:22:36 +00:00
} else {
2011-10-11 23:38:30 +00:00
var text = this.button.find( "span.ui-button-text");
2011-10-17 20:04:14 +00:00
var setWidth = text.width() + parseFloat( text.css( "padding-left" ) ) || 0 + parseFloat( text.css( "margin-left" ) || 0 );
}
2011-10-17 20:04:14 +00:00
// wrap menu
2011-10-11 23:38:30 +00:00
this.menuWrap = $( '<div />' )
.addClass( 'ui-selectmenu-menu' )
2011-09-28 20:22:36 +00:00
.width( setWidth )
2011-10-11 23:38:30 +00:00
.append( this.menu )
.appendTo( this.options.appendTo );
2011-11-18 16:14:38 +00:00
// init menu widget
this.menu.menu({
select: function( event, ui ) {
var flag = false,
item = ui.item.data( "item.selectmenu" ),
oldIndex = that.element[0].selectedIndex;
2011-11-18 16:14:38 +00:00
that._setOption( "value", item.value );
that._trigger( "select", event, { item: item } );
2011-11-18 16:14:38 +00:00
if ( item.index != oldIndex ) {
that._trigger( "change", event, { item: item } );
2011-09-24 02:44:13 +00:00
}
2011-11-18 16:14:38 +00:00
if ( that.isOpen ) {
event.preventDefault();
that.close( event, true);
}
},
focus: function( event, ui ) {
var item = ui.item.data( "item.selectmenu" );
2011-11-18 16:14:38 +00:00
if ( that.focus !== undefined && item.index != that.focus ) {
that._trigger( "focus", event, { item: item } );
}
2011-11-18 16:14:38 +00:00
that.focus = item.index;
}
});
2011-11-18 16:14:38 +00:00
// document click closes menu
this._bind( document, {
'click': function( event ) {
if ( this.isOpen && !$( event.target ).closest( "#" + this.ids.button).length ) {
this.close( event );
}
}
2011-11-18 16:14:38 +00:00
});
},
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
2011-10-11 23:38:30 +00:00
this._readOptions();
this._renderMenu( this.menu, this.items );
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this.menu.menu( "refresh" );
2011-11-18 16:14:38 +00:00
// adjust ARIA
2011-10-11 23:38:30 +00:00
this.menu.find( "li" ).not( '.ui-selectmenu-optgroup' ).find( 'a' ).attr( 'role', 'option' );
2011-11-18 16:14:38 +00:00
if ( this.options.dropdown ) {
2011-10-11 23:38:30 +00:00
this.menu
.addClass( 'ui-corner-bottom' )
.removeClass( 'ui-corner-all' );
}
2011-11-18 16:14:38 +00:00
// transfer disabled state
if ( this.element.attr( 'disabled' ) ) {
this.disable();
} else {
this.enable()
}
2011-11-18 16:14:38 +00:00
},
open: function( event ) {
var currentItem = this._getSelectedItem();
2011-11-18 16:14:38 +00:00
if ( !this.options.disabled ) {
if ( this.options.dropdown ) {
2011-10-11 23:38:30 +00:00
this.button
.addClass( 'ui-corner-top' )
.removeClass( 'ui-corner-all' );
2011-11-18 16:14:38 +00:00
}
this.menuWrap.addClass( 'ui-selectmenu-open' );
this.menu.attr("aria-hidden", false);
// needs to be fired after the document click event has closed all other Selectmenus
// otherwise the current item is not indicated
// TODO check if this should be handled by Menu
this._delay( function(){
this.menu.menu( "focus", event, currentItem );
}, 1);
2011-11-18 16:14:38 +00:00
if ( !this.options.dropdown ) {
// center current item
2011-10-11 23:38:30 +00:00
if ( this.menu.css("overflow") == "auto" ) {
this.menu.scrollTop( this.menu.scrollTop() + currentItem.position().top - this.menu.outerHeight()/2 + currentItem.outerHeight()/2 );
2011-11-18 16:14:38 +00:00
}
// calculate offset
2011-11-18 16:14:38 +00:00
var _offset = ( this.menu.offset().top - currentItem.offset().top + ( this.button.outerHeight() - currentItem.outerHeight() ) / 2);
$.extend( this.options.position, {
my: "left top",
at: "left top",
offset: "0 " + _offset
});
}
2011-11-18 16:14:38 +00:00
this.menuWrap
.zIndex( this.element.zIndex() + 1 )
.position( $.extend({
2011-10-11 23:38:30 +00:00
of: this.button
}, this.options.position ));
2011-11-18 16:14:38 +00:00
this.isOpen = true;
this._trigger( "open", event );
}
2011-11-18 16:14:38 +00:00
},
close: function( event, focus ) {
if ( this.isOpen ) {
if ( this.options.dropdown ) {
2011-10-11 23:38:30 +00:00
this.button
.addClass( 'ui-corner-all' )
.removeClass( 'ui-corner-top' );
}
2011-11-18 16:14:38 +00:00
2011-10-11 23:38:30 +00:00
this.menuWrap.removeClass( 'ui-selectmenu-open' );
this.menu.attr("aria-hidden", true);
this.isOpen = false;
2011-11-18 16:14:38 +00:00
if ( focus ) {
this.button.focus();
}
2011-11-18 16:14:38 +00:00
this._trigger( "close", event );
2011-09-22 19:20:49 +00:00
}
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.buttonWrap.add( this.menuWrap );
},
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 ) {
if ( item.optgroup != currentOptgroup ) {
var optgroup = $( '<li class="ui-selectmenu-optgroup">' + item.optgroup + '</li>' );
if ( item.element.parent( "optgroup" ).attr( "disabled" ) ) optgroup.addClass( 'ui-state-disabled' );
ul.append( optgroup );
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
2011-09-01 22:21:09 +00:00
_renderItem: function( ul, item) {
var li = $( "<li />" ).data( "item.selectmenu", item );
if ( item.disabled ) {
li.addClass( 'ui-state-disabled' ).text( item.label );
} else {
li.append( $( "<a />", {
2011-09-01 22:21:09 +00:00
text: item.label,
href: '#'
})
);
2011-11-18 16:14:38 +00:00
}
return li.appendTo( ul );
2011-09-01 22:21:09 +00:00
},
2011-11-18 16:14:38 +00:00
_move: function( key, event ) {
if ( !this.isOpen ) {
this.menu.menu( "focus", event, this._getSelectedItem() );
}
2011-11-18 16:14:38 +00:00
this.menu.menu( key, event );
if ( !this.isOpen ) {
this.menu.menu( "select", event );
}
2011-09-24 00:04:06 +00:00
},
2011-11-18 16:14:38 +00:00
_getSelectedItem: function() {
2011-11-18 16:14:38 +00:00
return this.menu.find( "li" ).not( '.ui-selectmenu-optgroup' ).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
_buttonEvents: {
click: function( event ) {
this._toggle( event );
event.preventDefault();
},
keydown: function( event ) {
switch (event.keyCode) {
case $.ui.keyCode.TAB:
if ( this.isOpen ) {
this.close( event );
}
break;
case $.ui.keyCode.ENTER:
if ( this.isOpen ) {
this.menu.menu( "select", this._getSelectedItem() );
2011-11-18 16:14:38 +00:00
}
event.preventDefault();
break;
case $.ui.keyCode.SPACE:
event.preventDefault();
break;
case $.ui.keyCode.UP:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "previous", event );
}
event.preventDefault();
break;
case $.ui.keyCode.DOWN:
if ( event.altKey ) {
this._toggle( event );
} else {
this._move( "next", event );
}
event.preventDefault();
break;
case $.ui.keyCode.LEFT:
this._move( "previous", event );
event.preventDefault();
break;
case $.ui.keyCode.RIGHT:
this._move( "next", event );
event.preventDefault();
break;
default:
2011-10-11 23:38:30 +00:00
this.menu.trigger( event );
}
}
2011-11-18 16:14:38 +00:00
},
2011-09-01 22:21:09 +00:00
_setOption: function( key, value ) {
this._super( key, value );
2011-11-18 16:14:38 +00:00
2011-09-01 22:21:09 +00:00
if ( key === "appendTo" ) {
2011-10-11 23:38:30 +00:00
this.menuWrap.appendTo( $( value || "body", this.element[0].ownerDocument )[0] );
2011-09-01 22:21:09 +00:00
}
if ( key === "value" && value !== undefined ) {
this.element[0].value = value;
2011-10-11 23:38:30 +00:00
this.button.children( '.ui-button-text' ).text( this.items[ this.element[0].selectedIndex ].label );
}
if ( key === "disabled" ) {
2011-10-11 23:38:30 +00:00
this.button.button( "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-10-11 23:38:30 +00:00
this.menu.attr( "aria-disabled", value );
}
2011-09-01 22:21:09 +00:00
},
2011-10-11 23:38:30 +00:00
_readOptions: function() {
2011-11-18 16:14:38 +00:00
var data = [];
$.each( this.element.find( 'option' ), function( index, item ) {
var option = $( item ),
optgroup = option.parent( "optgroup" );
data.push({
element: option,
index: index,
value: option.attr( 'value' ),
label: option.text(),
optgroup: optgroup.attr( "label" ) || false,
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();
}
2011-09-01 22:21:09 +00:00
});
}( jQuery ));