@@ -67,7 +67,11 @@ return $.widget( "ui.selectmenu", {
67
67
} ,
68
68
69
69
_drawButton : function ( ) {
70
- var that = this ;
70
+ var that = this ,
71
+ item = this . _parseOption (
72
+ this . element . find ( "option:selected" ) ,
73
+ this . element [ 0 ] . selectedIndex
74
+ ) ;
71
75
72
76
// Associate existing label with the new button
73
77
this . label = $ ( "label[for='" + this . ids . element + "']" ) . attr ( "for" , this . ids . button ) ;
@@ -99,12 +103,9 @@ return $.widget( "ui.selectmenu", {
99
103
} )
100
104
. prependTo ( this . button ) ;
101
105
102
- this . buttonText = $ ( "<span>" , {
103
- "class" : "ui-selectmenu-text"
104
- } )
106
+ this . buttonItem = this . _renderButtonItem ( item )
105
107
. appendTo ( this . button ) ;
106
108
107
- this . _setText ( this . buttonText , this . element . find ( "option:selected" ) . text ( ) ) ;
108
109
this . _resizeButton ( ) ;
109
110
110
111
this . _on ( this . button , this . _buttonEvents ) ;
@@ -190,7 +191,11 @@ return $.widget( "ui.selectmenu", {
190
191
191
192
refresh : function ( ) {
192
193
this . _refreshMenu ( ) ;
193
- this . _setText ( this . buttonText , this . _getSelectedItem ( ) . text ( ) ) ;
194
+ this . buttonItem . replaceWith (
195
+ this . buttonItem = this . _renderButtonItem (
196
+ this . _getSelectedItem ( ) . data ( "ui-selectmenu-item" )
197
+ )
198
+ ) ;
194
199
if ( ! this . options . width ) {
195
200
this . _resizeButton ( ) ;
196
201
}
@@ -275,6 +280,15 @@ return $.widget( "ui.selectmenu", {
275
280
return this . menu ;
276
281
} ,
277
282
283
+ _renderButtonItem : function ( item ) {
284
+ var buttonItem = $ ( "<span>" , {
285
+ "class" : "ui-selectmenu-text"
286
+ } ) ;
287
+ this . _setText ( buttonItem , item . label ) ;
288
+
289
+ return buttonItem ;
290
+ } ,
291
+
278
292
_renderMenu : function ( ul , items ) {
279
293
var that = this ,
280
294
currentOptgroup = "" ;
@@ -480,7 +494,7 @@ return $.widget( "ui.selectmenu", {
480
494
481
495
// Change native select element
482
496
this . element [ 0 ] . selectedIndex = item . index ;
483
- this . _setText ( this . buttonText , item . label ) ;
497
+ this . buttonItem . replaceWith ( this . buttonItem = this . _renderButtonItem ( item ) ) ;
484
498
this . _setAria ( item ) ;
485
499
this . _trigger ( "select" , event , { item : item } ) ;
486
500
@@ -590,22 +604,27 @@ return $.widget( "ui.selectmenu", {
590
604
} ,
591
605
592
606
_parseOptions : function ( options ) {
593
- var data = [ ] ;
607
+ var that = this ,
608
+ data = [ ] ;
594
609
options . each ( function ( index , item ) {
595
- var option = $ ( item ) ,
596
- optgroup = option . parent ( "optgroup" ) ;
597
- data . push ( {
598
- element : option ,
599
- index : index ,
600
- value : option . attr ( "value" ) ,
601
- label : option . text ( ) ,
602
- optgroup : optgroup . attr ( "label" ) || "" ,
603
- disabled : optgroup . prop ( "disabled" ) || option . prop ( "disabled" )
604
- } ) ;
610
+ data . push ( that . _parseOption ( $ ( item ) , index ) ) ;
605
611
} ) ;
606
612
this . items = data ;
607
613
} ,
608
614
615
+ _parseOption : function ( option , index ) {
616
+ var optgroup = option . parent ( "optgroup" ) ;
617
+
618
+ return {
619
+ element : option ,
620
+ index : index ,
621
+ value : option . attr ( "value" ) ,
622
+ label : option . text ( ) ,
623
+ optgroup : optgroup . attr ( "label" ) || "" ,
624
+ disabled : optgroup . prop ( "disabled" ) || option . prop ( "disabled" )
625
+ } ;
626
+ } ,
627
+
609
628
_destroy : function ( ) {
610
629
this . menuWrap . remove ( ) ;
611
630
this . button . remove ( ) ;
0 commit comments