Skip to content

Commit

Permalink
Selectmenu: removed unneeded that, use this instead
Browse files Browse the repository at this point in the history
  • Loading branch information
fnagel committed Oct 11, 2011
1 parent ace8f3e commit 4694261
Showing 1 changed file with 75 additions and 87 deletions.
162 changes: 75 additions & 87 deletions ui/jquery.ui.selectmenu.js
Expand Up @@ -37,101 +37,96 @@ $.widget( "ui.selectmenu", {
}, },


_create: function() { _create: function() {
var that = this, // set a default id value, generate a new random one if not set by developer
options = this.options, var selectmenuId = this.element.attr( 'id' ) || 'ui-selectmenu-' + Math.random().toString( 16 ).slice( 2, 10 );
// set a default id value, generate a new random one if not set by developer
selectmenuId = that.element.attr( 'id' ) || 'ui-selectmenu-' + Math.random().toString( 16 ).slice( 2, 10 );


// quick array of button and menu id's // quick array of button and menu id's
that.ids = [ selectmenuId, selectmenuId + '-button', selectmenuId + '-menu' ]; this.ids = [ selectmenuId, selectmenuId + '-button', selectmenuId + '-menu' ];


// set current value // set current value
if ( options.value ) { if ( this.options.value ) {
that.element[0].value = options.value; this.element[0].value = this.options.value;
} else { } else {
options.value = that.element[0].value; this.options.value = this.element[0].value;
} }


// catch click event of the label // catch click event of the label
that._bind({ this._bind({
'click': function( event ) { 'click': function( event ) {
that.newelement.focus(); this.newelement.focus();
event.preventDefault(); event.preventDefault();
} }
}); });


that._addNewelement(); this._addNewelement();
that._bind( that.newelement, that._newelementEvents ); this._bind( this.newelement, this._newelementEvents );


that._addList(); this._addList();
that.refresh(); this.refresh();
}, },


_addNewelement: function() { _addNewelement: function() {
var that = this, var tabindex = this.element.attr( 'tabindex' );
options = this.options,
tabindex = this.element.attr( 'tabindex' );


// hide original select tag // hide original select tag
that.element.hide(); this.element.hide();


// create button // create button
that.newelement = $( '<a />', { this.newelement = $( '<a />', {
href: '#' + that.ids[ 0 ], href: '#' + this.ids[ 0 ],
tabindex: ( tabindex ? tabindex : that.element.attr( 'disabled' ) ? 1 : 0 ), tabindex: ( tabindex ? tabindex : this.element.attr( 'disabled' ) ? 1 : 0 ),
id: that.ids[ 1 ], id: this.ids[ 1 ],
css: { css: {
width: that.element.outerWidth() width: this.element.outerWidth()
}, },
'aria-disabled': options.disabled, 'aria-disabled': this.options.disabled,
'aria-owns': that.ids[ 2 ], 'aria-owns': this.ids[ 2 ],
'aria-haspopup': true 'aria-haspopup': true
}) })
.button({ .button({
label: this.element.find( "option:selected" ).text(), label: this.element.find( "option:selected" ).text(),
icons: { icons: {
primary: ( options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' ) primary: ( this.options.dropdown ? 'ui-icon-triangle-1-s' : 'ui-icon-triangle-2-n-s' )
} }
}); });


// wrap and insert new button // wrap and insert new button
that.newelementWrap = $( '<span />' ) this.newelementWrap = $( '<span />' )
.addClass( 'ui-selectmenu-button' ) .addClass( 'ui-selectmenu-button' )
.append( that.newelement ) .append( this.newelement )
.insertAfter( that.element ); .insertAfter( this.element );
}, },


_addList: function() { _addList: function() {
var that = this, var that = this;
options = this.options;


// create menu portion, append to body // create menu portion, append to body
that.list = $( '<ul />', { this.list = $( '<ul />', {
'class': 'ui-widget ui-widget-content', 'class': 'ui-widget ui-widget-content',
'aria-hidden': true, 'aria-hidden': true,
'aria-labelledby': that.ids[1], 'aria-labelledby': this.ids[1],
role: 'listbox', role: 'listbox',
id: that.ids[2] id: this.ids[2]
}); });


// set width // set width
if ( options.dropdown ) { if ( this.options.dropdown ) {
var setWidth = that.newelement.outerWidth(); var setWidth = this.newelement.outerWidth();
} else { } else {
var text = that.newelement.find( "span.ui-button-text"); var text = this.newelement.find( "span.ui-button-text");
var setWidth = text.width() + parseFloat( text.css( "padding-left" ) ) + parseFloat( text.css( "margin-left" ) ); var setWidth = text.width() + parseFloat( text.css( "padding-left" ) ) + parseFloat( text.css( "margin-left" ) );
} }


// wrap list // wrap list
that.listWrap = $( '<div />' ) this.listWrap = $( '<div />' )
.addClass( 'ui-selectmenu-menu' ) .addClass( 'ui-selectmenu-menu' )
.width( setWidth ) .width( setWidth )
.append( that.list ) .append( this.list )
.appendTo( options.appendTo ); .appendTo( this.options.appendTo );


// init menu widget // init menu widget
that.list this.list
.data( 'element.selectelemenu', that.element ) .data( 'element.selectelemenu', this.element )
.menu({ .menu({
select: function( event, ui ) { select: function( event, ui ) {
var flag = false, var flag = false,
Expand All @@ -157,106 +152,99 @@ $.widget( "ui.selectmenu", {
}); });


// document click closes menu // document click closes menu
that._bind( document, { this._bind( document, {
'mousedown': function( event ) { 'mousedown': function( event ) {
if ( that.opened && !$( event.target ).is( that.list ) ) { if ( this.opened && !$( event.target ).is( this.list ) ) {
window.setTimeout( function() { window.setTimeout( function() {
that.close( event ); this.close( event );
}, 200 ); }, 200 );
} }
} }
}); });
}, },


refresh: function() { refresh: function() {
var that = this, this.list.empty();
options = this.options;


that.list.empty(); this._initSource();
this._renderMenu( this.list, this.items );


that._initSource(); this.list.menu( "refresh" );
that._renderMenu( that.list, that.items );

that.list.menu( "refresh" );


// adjust ARIA // adjust ARIA
that.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).find( 'a' ).attr( 'role', 'option' ); this.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).find( 'a' ).attr( 'role', 'option' );


if ( options.dropdown ) { if ( this.options.dropdown ) {
that.list this.list
.addClass( 'ui-corner-bottom' ) .addClass( 'ui-corner-bottom' )
.removeClass( 'ui-corner-all' ); .removeClass( 'ui-corner-all' );
} }


// transfer disabled state // transfer disabled state
if ( that.element.attr( 'disabled' ) ) { if ( this.element.attr( 'disabled' ) ) {
that.disable(); this.disable();
} else { } else {
that.enable() this.enable()
} }
}, },


open: function( event ) { open: function( event ) {
var that = this, var currentItem = this._getSelectedItem();
options = this.options,
currentItem = that._getSelectedItem();


if ( !options.disabled ) { if ( !this.options.disabled ) {
// close all other selectmenus // close all other selectmenus
$( '.ui-selectmenu-open' ).not( that.newelement ).each( function() { $( '.ui-selectmenu-open' ).not( this.newelement ).each( function() {
$( this ).children( 'ul.ui-menu' ).data( 'element.selectelemenu' ).selectmenu( 'close' ); $( this ).children( 'ul.ui-menu' ).data( 'element.selectelemenu' ).selectmenu( 'close' );
}); });


if ( options.dropdown ) { if ( this.options.dropdown ) {
that.newelement this.newelement
.addClass( 'ui-corner-top' ) .addClass( 'ui-corner-top' )
.removeClass( 'ui-corner-all' ); .removeClass( 'ui-corner-all' );
} }


that.listWrap.addClass( 'ui-selectmenu-open' ); this.listWrap.addClass( 'ui-selectmenu-open' );
that.list.menu( "focus", null, currentItem ); this.list.menu( "focus", null, currentItem );


if ( !options.dropdown ) { if ( !this.options.dropdown ) {
// center current item // center current item
if ( that.list.css("overflow") == "auto" ) { if ( this.list.css("overflow") == "auto" ) {
that.list.scrollTop( that.list.scrollTop() + currentItem.position().top - that.list.outerHeight()/2 + currentItem.outerHeight()/2 ); this.list.scrollTop( this.list.scrollTop() + currentItem.position().top - this.list.outerHeight()/2 + currentItem.outerHeight()/2 );
} }
// calculate offset // calculate offset
var _offset = (that.list.offset().top - currentItem.offset().top + (that.newelement.outerHeight() - currentItem.outerHeight()) / 2); var _offset = ( this.list.offset().top - currentItem.offset().top + ( this.newelement.outerHeight() - currentItem.outerHeight() ) / 2);
$.extend( options.position, { $.extend( this.options.position, {
my: "left top", my: "left top",
at: "left top", at: "left top",
offset: "0 " + _offset offset: "0 " + _offset
}); });
} }


that.listWrap this.listWrap
.zIndex( that.element.zIndex() + 1 ) .zIndex( this.element.zIndex() + 1 )
.position( $.extend({ .position( $.extend({
of: that.newelement of: this.newelement
}, options.position )); }, this.options.position ));


that.opened = true; this.opened = true;
that._trigger( "open", event ); this._trigger( "open", event );
} }
}, },


close: function( event, focus ) { close: function( event, focus ) {
var that = this, if ( this.opened ) {
options = this.options; if ( this.options.dropdown ) {
if ( that.opened ) { this.newelement
if ( options.dropdown ) {
that.newelement
.addClass( 'ui-corner-all' ) .addClass( 'ui-corner-all' )
.removeClass( 'ui-corner-top' ); .removeClass( 'ui-corner-top' );
} }


that.listWrap.removeClass( 'ui-selectmenu-open' ); this.listWrap.removeClass( 'ui-selectmenu-open' );
this.opened = false; this.opened = false;


if (focus) that.newelement.focus(); if (focus) this.newelement.focus();


that._trigger( "close", event ); this._trigger( "close", event );
} }
}, },


Expand Down

0 comments on commit 4694261

Please sign in to comment.