From: Bartosz DziewoƄski Date: Sat, 22 Feb 2014 16:07:01 +0000 (+0100) Subject: jquery.suggestions, mediawiki.searchSuggest: Fix form submission X-Git-Tag: 1.31.0-rc.0~16841 X-Git-Url: http://git.cyclocoop.org//%27http:/code.google.com/p/ie7-js//%27?a=commitdiff_plain;h=d97a7d33882dc3bb3f43c84b9719a33dae4d8224;p=lhc%2Fweb%2Fwiklou.git jquery.suggestions, mediawiki.searchSuggest: Fix form submission 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 --- diff --git a/resources/jquery/jquery.suggestions.js b/resources/jquery/jquery.suggestions.js index c4e95a48af..f967a1d478 100644 --- a/resources/jquery/jquery.suggestions.js +++ b/resources/jquery/jquery.suggestions.js @@ -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(); } } }; diff --git a/resources/mediawiki/mediawiki.searchSuggest.js b/resources/mediawiki/mediawiki.searchSuggest.js index 8d950a0280..e22a3d311a 100644 --- a/resources/mediawiki/mediawiki.searchSuggest.js +++ b/resources/mediawiki/mediawiki.searchSuggest.js @@ -153,8 +153,8 @@ }, result: { render: renderFunction, - select: function ( $input ) { - $input.closest( 'form' ).submit(); + select: function () { + return true; // allow the form to be submitted } }, delay: 120, @@ -178,16 +178,16 @@ $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( $( '' ) ) - .submit(); + .append( $( '' ) ); + return true; // allow the form to be submitted } }, $region: $searchRegion