diff options
author | Felix Nagel <info@felixnagel.com> | 2011-09-24 04:44:13 +0200 |
---|---|---|
committer | Felix Nagel <info@felixnagel.com> | 2011-09-24 04:44:13 +0200 |
commit | 3503c586c4b8384e3c6f1b140935cf4454c0853a (patch) | |
tree | a56b54827f596a92cd7884f83b021dc5dec47d5f | |
parent | 35289489c4e6272fb6396e89f08a948df506099e (diff) | |
download | jquery-ui-3503c586c4b8384e3c6f1b140935cf4454c0853a.tar.gz jquery-ui-3503c586c4b8384e3c6f1b140935cf4454c0853a.zip |
Selectmenu: added callback events
-rw-r--r-- | ui/jquery.ui.selectmenu.js | 105 |
1 files changed, 56 insertions, 49 deletions
diff --git a/ui/jquery.ui.selectmenu.js b/ui/jquery.ui.selectmenu.js index 22f26953e..b000f75c8 100644 --- a/ui/jquery.ui.selectmenu.js +++ b/ui/jquery.ui.selectmenu.js @@ -29,13 +29,14 @@ $.widget( "ui.selectmenu", { at: "left bottom", collision: "none" }, - source: null, + source: null, + value: null, // callbacks open: null, focus: null, select: null, close: null, - change: null, + // change: null, }, _create: function() { @@ -142,46 +143,6 @@ $.widget( "ui.selectmenu", { }, - open: function( event ) { - var self = this, - options = this.options, - currentItem = self._getSelectedItem(); - - // close all other selectmenus - $( '.' + self.widgetBaseClass + '-open' ).not( self.newelement ).each( function() { - $( this ).children( 'ul.ui-menu' ).data( 'element.selectelemenu' ).selectmenu( 'close' ); - }); - - if ( options.dropdown ) { - self.newelement - .addClass( 'ui-corner-top' ) - .removeClass( 'ui-corner-all' ); - } - - self.listWrap.addClass( self.widgetBaseClass + '-open' ); - self.list.focus().menu( "focus", null, currentItem ); - - if ( !options.dropdown ) { - // center current item - if ( self.list.css("overflow") == "auto" ) { - self.list.scrollTop( self.list.scrollTop() + currentItem.position().top - self.list.outerHeight()/2 + currentItem.outerHeight()/2 ); - } - // calculate offset - var _offset = (self.list.offset().top - currentItem.offset().top + (self.newelement.outerHeight() - currentItem.outerHeight()) / 2); - $.extend( options.position, { - my: "left top", - at: "left top", - offset: "0 " + _offset - }); - } - - self.listWrap.position( $.extend({ - of: this.newelementWrap - }, options.position )); - - this.opened = true; - }, - refresh: function() { var self = this, options = this.options; @@ -209,12 +170,16 @@ $.widget( "ui.selectmenu", { .menu({ select: function( event, ui ) { var item = ui.item.data( "item.selectmenu" ); - self._setSelected( item ); + self._setSelected( event, item ); + item.element = $ ( self.items[ item.index ] ); + self._trigger( "select", event, { item: item } ); self.close( event, true); }, - focus: function( event, ui ) { - if ( !self.opened ) self._setSelected( ui.item.data( "item.selectmenu" ); - } + focus: function( event, ui ) { + var item = ui.item.data( "item.selectmenu" ); + if ( !self.opened ) self._setSelected( event, item ); + self._trigger( "focus", event, { item: item } ); + } }) .bind( 'mouseenter.selectelemenu', function() { self.hover = true; @@ -230,6 +195,47 @@ $.widget( "ui.selectmenu", { } }, + open: function( event ) { + var self = this, + options = this.options, + currentItem = self._getSelectedItem(); + + // close all other selectmenus + $( '.' + self.widgetBaseClass + '-open' ).not( self.newelement ).each( function() { + $( this ).children( 'ul.ui-menu' ).data( 'element.selectelemenu' ).selectmenu( 'close' ); + }); + + if ( options.dropdown ) { + self.newelement + .addClass( 'ui-corner-top' ) + .removeClass( 'ui-corner-all' ); + } + + self.listWrap.addClass( self.widgetBaseClass + '-open' ); + self.list.focus().menu( "focus", null, currentItem ); + + if ( !options.dropdown ) { + // center current item + if ( self.list.css("overflow") == "auto" ) { + self.list.scrollTop( self.list.scrollTop() + currentItem.position().top - self.list.outerHeight()/2 + currentItem.outerHeight()/2 ); + } + // calculate offset + var _offset = (self.list.offset().top - currentItem.offset().top + (self.newelement.outerHeight() - currentItem.outerHeight()) / 2); + $.extend( options.position, { + my: "left top", + at: "left top", + offset: "0 " + _offset + }); + } + + self.listWrap.position( $.extend({ + of: this.newelementWrap + }, options.position )); + + this.opened = true; + self._trigger( "open", event ); + }, + close: function( event, focus ) { var self = this, options = this.options; @@ -244,6 +250,8 @@ $.widget( "ui.selectmenu", { this.opened = false; if (focus) self.newelement.focus(); + + self._trigger( "close", event ); }, _renderMenu: function( ul, items ) { @@ -289,7 +297,7 @@ $.widget( "ui.selectmenu", { return this.list.find( "li" ).not( '.ui-selectmenu-optgroup' ).eq( this.element[0].selectedIndex ); }, - _setSelected: function( item ) { + _setSelected: function( event, item ) { this.newelement.children( '.ui-button-text' ).text( item.label ); this.element[0].selectedIndex = item.index; this.options.value = item.value; @@ -326,8 +334,7 @@ $.widget( "ui.selectmenu", { optgroup: optgroup.attr("label") || false, disabled: optgroup.attr( "disabled" ) || option.attr( "disabled" ) }); - }); - console.log( data ); + }); this.options.source = data; } }, |