Thiemo Mättig (WMDE) has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/312279

Change subject: Fix duplicate search suggestions appearing when focusing to fast
......................................................................

Fix duplicate search suggestions appearing when focusing to fast

The bug is quite obvious the moment you realize what's going on: The whole
code snippet was in a "focus" event handler. But when the user focused the
search field earlier, before this event handler was assigned, this event
was never fired.

My approach might have one issue: Let's assume my event handler is already
active, but the native search was not initialized yet. When the user
triggers a change event in this exact moment, the "one" event does nothing
and stops being executed. This is extremely unlikely, it may even be
impossible.

You may ask why I'm doing the null check then, and not assuming the native
search is initialized. Yes, but I want an obvious regression (the duplicate
suggestion will re-appear) instead of a script execution error.

Bug: T119492
Change-Id: I276a9c25ad7af6bd0a069d15a1ef0d7d00a8a80c
---
M repo/resources/wikibase.ui.entitysearch.js
1 file changed, 8 insertions(+), 24 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/79/312279/1

diff --git a/repo/resources/wikibase.ui.entitysearch.js 
b/repo/resources/wikibase.ui.entitysearch.js
index f4db77a..6495b8c 100644
--- a/repo/resources/wikibase.ui.entitysearch.js
+++ b/repo/resources/wikibase.ui.entitysearch.js
@@ -44,17 +44,6 @@
                        searchContaining.setLink( getHref( $form ) + 
'&fulltext=1' );
                }
 
-               /**
-                * Removes the native search box suggestion list.
-                *
-                * @param {HTMLElement} input Search box node
-                */
-               function removeSuggestionContext( input ) {
-                       // Native fetch() updates/re-sets the data attribute 
with the suggestion context.
-                       $.data( input, 'suggestionsContext' ).config.fetch = 
function() {};
-                       $.removeData( input, 'suggestionsContext' );
-               }
-
                var suggestionsPlaceholder = new $.ui.ooMenu.CustomItem(
                        $( '<div/>' ).append( $.createSpinner() )
                );
@@ -81,19 +70,14 @@
                $input.before( $hiddenInput );
 
                $input
-               .one( 'focus', function( event ) {
-                       if ( $.data( this, 'suggestionsContext' ) ) {
-                               removeSuggestionContext( this );
-                       } else {
-                               // Suggestion context might not be initialized 
when focusing the search box while
-                               // the page is still rendered.
-                               var $input = $( this );
-                               $input.on( 'keypress.entitysearch', function( 
event ) {
-                                       if ( $.data( this, 'suggestionsContext' 
) ) {
-                                               removeSuggestionContext( this );
-                                               $input.off( '.entitysearch' );
-                                       }
-                               } );
+               .one( 'eachchange.entitysearch', function() {
+                       // Removes the native search box suggestion list.
+                       var context = $.data( this, 'suggestionsContext' );
+
+                       if ( context ) {
+                               // Native fetch() updates/re-sets the data 
attribute with the suggestion context.
+                               context.config.fetch = function() {};
+                               $.removeData( this, 'suggestionsContext' );
                        }
                } )
                .entitysearch( {

-- 
To view, visit https://gerrit.wikimedia.org/r/312279
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I276a9c25ad7af6bd0a069d15a1ef0d7d00a8a80c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Thiemo Mättig (WMDE) <thiemo.maet...@wikimedia.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to