]> source.dussan.org Git - jquery-ui.git/commitdiff
Selectmenu: Preserve text selection and button focus on click
authorScott González <scott.gonzalez@gmail.com>
Thu, 9 Oct 2014 14:25:26 +0000 (10:25 -0400)
committerScott González <scott.gonzalez@gmail.com>
Thu, 16 Oct 2014 12:13:53 +0000 (08:13 -0400)
Fixes #10639
Closes gh-1358

ui/selectmenu.js

index a3d4281a6f32bc161ffa77939839484b403af44a..26456b0dc1b93812253653b75f42f5ab17e580fd 100644 (file)
@@ -144,6 +144,12 @@ return $.widget( "ui.selectmenu", {
                                role: "listbox",
                                select: function( event, ui ) {
                                        event.preventDefault();
+
+                                       // support: IE8
+                                       // If the item was selected via a click, the text selection
+                                       // will be destroyed in IE
+                                       that._setSelection();
+
                                        that._select( ui.item.data( "ui-selectmenu-item" ), event );
                                },
                                focus: function( event, ui ) {
@@ -254,6 +260,7 @@ return $.widget( "ui.selectmenu", {
                this.isOpen = false;
                this._toggleAttr();
 
+               this.range = null;
                this._off( this.document );
 
                this._trigger( "close", event );
@@ -342,6 +349,29 @@ return $.widget( "ui.selectmenu", {
                this[ this.isOpen ? "close" : "open" ]( event );
        },
 
+       _setSelection: function() {
+               var selection;
+
+               if ( !this.range ) {
+                       return;
+               }
+
+               if ( window.getSelection ) {
+                       selection = window.getSelection();
+                       selection.removeAllRanges();
+                       selection.addRange( this.range );
+
+               // support: IE8
+               } else {
+                       this.range.select();
+               }
+
+               // support: IE
+               // Setting the text selection kills the button focus in IE, but
+               // restoring the focus doesn't kill the selection.
+               this.button.focus();
+       },
+
        _documentClick: {
                mousedown: function( event ) {
                        if ( !this.isOpen ) {
@@ -357,11 +387,25 @@ return $.widget( "ui.selectmenu", {
        _buttonEvents: {
 
                // Prevent text selection from being reset when interacting with the selectmenu (#10144)
-               mousedown: function( event ) {
-                       event.preventDefault();
+               mousedown: function() {
+                       var selection;
+
+                       if ( window.getSelection ) {
+                               selection = window.getSelection();
+                               if ( selection.rangeCount ) {
+                                       this.range = selection.getRangeAt( 0 );
+                               }
+
+                       // support: IE8
+                       } else {
+                               this.range = document.selection.createRange();
+                       }
                },
 
-               click: "_toggle",
+               click: function( event ) {
+                       this._setSelection();
+                       this._toggle( event );
+               },
 
                keydown: function( event ) {
                        var preventDefault = true;