jquery.suggestions, mediawiki.searchSuggest: Fix form submission
authorBartosz Dziewoński <matma.rex@gmail.com>
Sat, 22 Feb 2014 16:07:01 +0000 (17:07 +0100)
committerYuvipanda <yuvipanda@gmail.com>
Mon, 24 Feb 2014 15:25:30 +0000 (15:25 +0000)
Don't override form submission behavior (by capturing the Enter key on
search box, preventing default behavior, and then manually submitting
the form); just let it happen if it's wanted.

Then swap out a few unjustified stopImmediatePropagation() calls to
stopPropagation() so that the event can be actually fired. Add some
comments while we're at it.

This allows the user to use Ctrl+Enter (or Shift+Enter) to submit the
form into a new tab (or new window) on browsers that support this
(currently Opera and Chrome).

Bug: 34756
Bug: 35974
Change-Id: I49ef7cc89400032505bc444f21d522d5b5d47586

resources/jquery/jquery.suggestions.js
resources/mediawiki/mediawiki.searchSuggest.js

index c4e95a4..f967a1d 100644 (file)
@@ -421,20 +421,26 @@ $.suggestions = {
                                selected = context.data.$container.find( '.suggestions-result-current' );
                                $.suggestions.hide( context );
                                if ( selected.length === 0 || context.data.selectedWithMouse ) {
-                                       // if nothing is selected OR if something was selected with the mouse,
-                                       // cancel any current requests and submit the form
+                                       // If nothing is selected or if something was selected with the mouse
+                                       // cancel any current requests and allow the form to be submitted
+                                       // (simply don't prevent default behavior).
                                        $.suggestions.cancel( context );
-                                       context.config.$region.closest( 'form' ).submit();
+                                       preventDefault = false;
                                } else if ( selected.is( '.suggestions-special' ) ) {
                                        if ( typeof context.config.special.select === 'function' ) {
-                                               context.config.special.select.call( selected, context.data.$textbox );
+                                               // Allow the callback to decide whether to prevent default or not
+                                               if ( context.config.special.select.call( selected, context.data.$textbox ) === true ) {
+                                                       preventDefault = false;
+                                               }
                                        }
                                } else {
+                                       $.suggestions.highlight( context, selected, true );
+
                                        if ( typeof context.config.result.select === 'function' ) {
-                                               $.suggestions.highlight( context, selected, true );
-                                               context.config.result.select.call( selected, context.data.$textbox );
-                                       } else {
-                                               $.suggestions.highlight( context, selected, true );
+                                               // Allow the callback to decide whether to prevent default or not
+                                               if ( context.config.result.select.call( selected, context.data.$textbox ) === true ) {
+                                                       preventDefault = false;
+                                               }
                                        }
                                }
                                break;
@@ -444,7 +450,7 @@ $.suggestions = {
                }
                if ( preventDefault ) {
                        e.preventDefault();
-                       e.stopImmediatePropagation();
+                       e.stopPropagation();
                }
        }
 };
index 8d950a0..e22a3d3 100644 (file)
                                },
                                result: {
                                        render: renderFunction,
-                                       select: function ( $input ) {
-                                               $input.closest( 'form' ).submit();
+                                       select: function () {
+                                               return true; // allow the form to be submitted
                                        }
                                },
                                delay: 120,
                $searchInput.suggestions( {
                        result: {
                                render: renderFunction,
-                               select: function ( $input ) {
-                                       $input.closest( 'form' ).submit();
+                               select: function () {
+                                       return true; // allow the form to be submitted
                                }
                        },
                        special: {
                                render: specialRenderFunction,
                                select: function ( $input ) {
                                        $input.closest( 'form' )
-                                               .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) )
-                                               .submit();
+                                               .append( $( '<input type="hidden" name="fulltext" value="1"/>' ) );
+                                       return true; // allow the form to be submitted
                                }
                        },
                        $region: $searchRegion