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

418 lines
10 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() {
2011-09-28 21:59:23 +00:00
var that = this,
2011-09-01 22:21:09 +00:00
options = this.options,
// set a default id value, generate a new random one if not set by developer
2011-09-28 21:59:23 +00:00
selectmenuId = that.element.attr( 'id' ) || 'ui-selectmenu-' + Math.random().toString( 16 ).slice( 2, 10 );
2011-09-01 22:21:09 +00:00
// quick array of button and menu id's
that.ids = [ selectmenuId, selectmenuId + '-button', selectmenuId + '-menu' ];
// set current value
if ( options.value ) {
2011-09-28 21:59:23 +00:00
that.element[0].value = options.value;
} else {
2011-09-28 21:59:23 +00:00
options.value = that.element[0].value;
}
// catch click event of the label
that._bind({
'click': function( event ) {
that.newelement.focus();
event.preventDefault();
}
});
that._addNewelement();
that._bind( that.newelement, that._newelementEvents );
that._addList();
that.refresh();
},
_addNewelement: function() {
var that = this,
options = this.options,
tabindex = this.element.attr( 'tabindex' );
// hide original select tag
that.element.hide();
2011-09-01 22:21:09 +00:00
// create button
2011-09-28 21:59:23 +00:00
that.newelement = $( '<a />', {
href: '#' + that.ids[ 0 ],
2011-09-28 21:59:23 +00:00
tabindex: ( tabindex ? tabindex : that.element.attr( 'disabled' ) ? 1 : 0 ),
id: that.ids[ 1 ],
2011-09-01 22:21:09 +00:00
css: {
2011-09-28 21:59:23 +00:00
width: that.element.outerWidth()
},
'aria-disabled': options.disabled,
'aria-owns': that.ids[ 2 ],
'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: {
2011-09-22 19:20:49 +00:00
primary: ( options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' )
2011-09-01 22:21:09 +00:00
}
});
// wrap and insert new button
that.newelementWrap = $( '<span />' )
.addClass( 'ui-selectmenu-button' )
2011-09-28 21:59:23 +00:00
.append( that.newelement )
.insertAfter( that.element );
2011-09-01 22:21:09 +00:00
},
_addList: function() {
2011-09-28 21:59:23 +00:00
var that = this,
options = this.options;
// create menu portion, append to body
2011-09-28 21:59:23 +00:00
that.list = $( '<ul />', {
'class': 'ui-widget ui-widget-content',
'aria-hidden': true,
'aria-labelledby': that.ids[1],
role: 'listbox',
id: that.ids[2]
});
// set width
2011-09-28 20:22:36 +00:00
if ( options.dropdown ) {
2011-09-28 21:59:23 +00:00
var setWidth = that.newelement.outerWidth();
2011-09-28 20:22:36 +00:00
} else {
2011-09-28 21:59:23 +00:00
var text = that.newelement.find( "span.ui-button-text");
2011-09-28 20:22:36 +00:00
var setWidth = text.width() + parseFloat( text.css( "padding-left" ) ) + parseFloat( text.css( "margin-left" ) );
}
// wrap list
that.listWrap = $( '<div />' )
.addClass( 'ui-selectmenu-menu' )
2011-09-28 20:22:36 +00:00
.width( setWidth )
2011-09-28 21:59:23 +00:00
.append( that.list )
.appendTo( options.appendTo );
// init menu widget
2011-09-28 21:59:23 +00:00
that.list
.data( 'element.selectelemenu', that.element )
.menu({
select: function( event, ui ) {
var flag = false,
item = ui.item.data( "item.selectmenu" );
2011-09-28 21:59:23 +00:00
if ( item.index != that.element[0].selectedIndex ) flag = true;
2011-09-28 21:59:23 +00:00
that._setOption( "value", item.value );
that._trigger( "select", event, { item: item } );
2011-09-28 21:59:23 +00:00
if ( flag ) that._trigger( "change", event, { item: item } );
if ( that.opened ) {
event.preventDefault();
that.close( event, true);
}
},
focus: function( event, ui ) {
var item = ui.item.data( "item.selectmenu" );
if ( that.focus !== undefined && item.index != that.focus ) that._trigger( "focus", event, { item: item } );
that.focus = item.index;
2011-09-24 02:44:13 +00:00
}
});
// document click closes menu
that._bind( document, {
'mousedown': function( event ) {
if ( that.opened && !$( event.target ).is( that.list ) ) {
window.setTimeout( function() {
that.close( event );
}, 200 );
}
}
});
},
refresh: function() {
var that = this,
options = this.options;
that.list.empty();
that._initSource();
that._renderMenu( that.list, that.items );
that.list.menu( "refresh" );
// adjust ARIA
2011-09-28 21:59:23 +00:00
that.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).find( 'a' ).attr( 'role', 'option' );
if ( options.dropdown ) {
2011-09-28 21:59:23 +00:00
that.list
.addClass( 'ui-corner-bottom' )
.removeClass( 'ui-corner-all' );
}
// transfer disabled state
2011-09-28 21:59:23 +00:00
if ( that.element.attr( 'disabled' ) ) {
that.disable();
} else {
2011-09-28 21:59:23 +00:00
that.enable()
}
},
2011-09-24 02:44:13 +00:00
open: function( event ) {
2011-09-28 21:59:23 +00:00
var that = this,
2011-09-24 02:44:13 +00:00
options = this.options,
2011-09-28 21:59:23 +00:00
currentItem = that._getSelectedItem();
2011-09-24 02:44:13 +00:00
if ( !options.disabled ) {
// close all other selectmenus
$( '.ui-selectmenu-open' ).not( that.newelement ).each( function() {
$( this ).children( 'ul.ui-menu' ).data( 'element.selectelemenu' ).selectmenu( 'close' );
});
if ( options.dropdown ) {
2011-09-28 21:59:23 +00:00
that.newelement
.addClass( 'ui-corner-top' )
.removeClass( 'ui-corner-all' );
}
that.listWrap.addClass( 'ui-selectmenu-open' );
2011-09-28 21:59:23 +00:00
that.list.menu( "focus", null, currentItem );
2011-09-24 02:44:13 +00:00
if ( !options.dropdown ) {
// center current item
2011-09-28 21:59:23 +00:00
if ( that.list.css("overflow") == "auto" ) {
that.list.scrollTop( that.list.scrollTop() + currentItem.position().top - that.list.outerHeight()/2 + currentItem.outerHeight()/2 );
}
// calculate offset
2011-09-28 21:59:23 +00:00
var _offset = (that.list.offset().top - currentItem.offset().top + (that.newelement.outerHeight() - currentItem.outerHeight()) / 2);
$.extend( options.position, {
my: "left top",
at: "left top",
offset: "0 " + _offset
});
}
2011-09-28 21:59:23 +00:00
that.listWrap
.zIndex( that.element.zIndex() + 1 )
.position( $.extend({
of: that.newelement
}, options.position ));
2011-09-28 21:59:23 +00:00
that.opened = true;
that._trigger( "open", event );
}
2011-09-24 02:44:13 +00:00
},
2011-09-01 22:21:09 +00:00
close: function( event, focus ) {
2011-09-28 21:59:23 +00:00
var that = this,
2011-09-01 22:21:09 +00:00
options = this.options;
2011-09-28 21:59:23 +00:00
if ( that.opened ) {
if ( options.dropdown ) {
2011-09-28 21:59:23 +00:00
that.newelement
.addClass( 'ui-corner-all' )
.removeClass( 'ui-corner-top' );
}
that.listWrap.removeClass( 'ui-selectmenu-open' );
this.opened = false;
2011-09-28 21:59:23 +00:00
if (focus) that.newelement.focus();
2011-09-28 21:59:23 +00:00
that._trigger( "close", event );
2011-09-22 19:20:49 +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-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
});
},
_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: '#'
})
);
}
return li.appendTo( ul );
2011-09-01 22:21:09 +00:00
},
_move: function( key, event ) {
if ( !this.opened ) this.list.menu( "focus", event, this._getSelectedItem() );
this.list.menu( key, event );
if ( !this.opened ) this.list.menu( "select", event );
2011-09-24 00:04:06 +00:00
},
_getSelectedItem: function() {
return this.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).eq( this.element[0].selectedIndex );
},
2011-09-24 00:04:06 +00:00
_toggle: function( event ) {
if ( this.opened ) {
this.close( event );
} else {
this.open( event );
}
},
_newelementEvents: {
mousedown: function( event ) {
this._toggle( event );
event.stopImmediatePropagation();
},
click: function( event ) {
// return false needed to prevent browser from following the anchor
return false;
},
keydown: function( event ) {
switch (event.keyCode) {
case $.ui.keyCode.TAB:
if ( this.opened ) this.close( event );
break;
case $.ui.keyCode.ENTER:
if ( this.opened ) {
this.list.menu( "select", this._getSelectedItem() );
event.preventDefault();
}
break;
case $.ui.keyCode.SPACE:
this._toggle(event);
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:
this.list.trigger( event );
}
}
},
2011-09-01 22:21:09 +00:00
_setOption: function( key, value ) {
this._super( "_setOption", key, value );
2011-09-01 22:21:09 +00:00
if ( key === "appendTo" ) {
this.listWrap.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;
this.newelement.children( '.ui-button-text' ).text( this.items[ this.element[0].selectedIndex ].label );
}
if ( key === "disabled" ) {
this.newelement.button( "option", "disabled", value );
if ( value ) {
this.element.attr( "disabled", "disabled" );
this.newelement.attr( "tabindex", -1 );
} else {
this.element.removeAttr( "disabled" );
this.newelement.attr( "tabindex", 1 );
}
this.list.attr( "aria-disabled", value );
this.close();
}
2011-09-01 22:21:09 +00:00
},
widget: function() {
return this.newelementWrap.add( this.listWrap );
},
2011-09-01 22:21:09 +00:00
_initSource: function() {
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-09-24 00:04:06 +00:00
_destroy: function() {
2011-09-24 02:47:11 +00:00
this.listWrap.remove();
this.newelementWrap.remove();
this.element.show();
}
2011-09-01 22:21:09 +00:00
});
}( jQuery ));