Tobias Gritschacher has submitted this change and it was merged.

Change subject: (bug 44674) Entity search: Prevent closing suggestion list
......................................................................


(bug 44674) Entity search: Prevent closing suggestion list

The change set prevents the suggestion list of the entity selector replacing 
the native
search box from closing if there are no actual suggestions. If there are no 
suggestions,
the link to the full-text search will remain visible.

- patch set 4: rebase
- patch set 5: minor fix regarding event handling

Change-Id: I0069a6b1ae1f6264c2a59c29bafffbb6b520a946
---
M lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
M repo/resources/wikibase.ui.entitysearch.js
2 files changed, 25 insertions(+), 10 deletions(-)

Approvals:
  Tobias Gritschacher: Verified; Looks good to me, approved



diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js 
b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
index 55cfd66..ae85f8b 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
@@ -51,7 +51,7 @@
  * @option messages['more'] {String} (optional) Label of the link to display 
more suggestions.
  *         Default value: 'more'
  *
- * @option emulateSearch {boolean} (optional) Allows emulating the behaviour 
of a search box by
+ * @option emulateSearchBox {boolean} (optional) Allows emulating the 
behaviour of a search box by
  *         linking the entities to their corresponding pages. Instead of 
selecting an entity, the
  *         whole page will be redirected to the entity page.
  *         Default value: false
@@ -117,7 +117,7 @@
                                'aliases-label': mwMsgOrString( 
'wikibase-aliases-label', 'also known as:' ),
                                'more': mwMsgOrString( 
'wikibase-entityselector-more', 'more' )
                        },
-                       emulateSearch: false // TODO: Allow setting a custom 
target to trigger the redirect on
+                       emulateSearchBox: false // TODO: Allow setting a custom 
target to trigger the redirect on
                },
 
                /**
@@ -193,7 +193,7 @@
                                // and select it
                                if ( event.keyCode === $.ui.keyCode.ENTER ) {
 
-                                       if ( self.options.emulateSearch && 
self.selectedEntity() ) {
+                                       if ( self.options.emulateSearchBox && 
self.selectedEntity() ) {
                                                // Prevent submitting search 
form since we want to redirect directly to the
                                                // entity.
                                                
event.stopImmediatePropagation();
@@ -231,7 +231,7 @@
                                                $( event.target ).closest( 
'.ui-menu-item' ).data( 'item.autocomplete' );
                                        self._setEntity( item );
 
-                                       if ( self.options.emulateSearch ) {
+                                       if ( self.options.emulateSearchBox ) {
                                                window.location.href = item.url;
                                        }
 
@@ -344,7 +344,22 @@
                 * @see ui.suggester.close
                 */
                close: function( event ) {
-                       if ( !event || !event.originalEvent || 
event.originalEvent.type !== 'programmatic' ) {
+                       var originalType = ( event && event.originalEvent && 
event.originalEvent.type )
+                               ? event.originalEvent.type
+                               : null;
+
+                       // When emulating a search box and there is a custom 
item, only close the list of
+                       // suggestions when blurring or when there is no text 
in the input box.
+                       if (
+                               this.options.emulateSearchBox && 
this.options.customListItem
+                               && originalType !== 'blur' && originalType !== 
'programmatic'
+                               && this.element.val() !== ''
+                       ) {
+                               // Reset list content leaving just the custom 
item.
+                               this.offset = 0;
+                               this.menu.element.children().remove();
+                               this.menu.refresh();
+                       } else {
                                // Do not close the list of suggestions when 
programmatically selecting an entity
                                // (e.g by typing an exact, unique entity 
label), allowing the user to check that
                                // the typed string actually matches a single 
entity.
@@ -486,13 +501,13 @@
                                        }
                                } else {
                                        this._setEntity( null );
-                                       if ( this.options.emulateSearch ) {
+                                       if ( this.options.emulateSearchBox ) {
                                                this._hiddenInput( 
this.element.val() );
                                        }
                                }
                                return true;
                        }
-                       if ( this.options.emulateSearch ) {
+                       if ( this.options.emulateSearchBox ) {
                                this._hiddenInput( this.element.val() );
                        }
                        return false;
@@ -523,7 +538,7 @@
                                ),
                                $section = $( '<li/>' ).data( 
'item.autocomplete', item ).append( $link );
 
-                       if ( this.options.emulateSearch ) {
+                       if ( this.options.emulateSearchBox ) {
                                $link.attr( 'href', item.url );
                        }
 
@@ -607,7 +622,7 @@
                                found = true;
                        }
 
-                       if ( !found && !alreadySelected && 
!this.options.emulateSearch ) {
+                       if ( !found && !alreadySelected && 
!this.options.emulateSearchBox ) {
                                this._setEntity( null );
                        }
                        return found;
diff --git a/repo/resources/wikibase.ui.entitysearch.js 
b/repo/resources/wikibase.ui.entitysearch.js
index 6ffb426..8be2044 100644
--- a/repo/resources/wikibase.ui.entitysearch.js
+++ b/repo/resources/wikibase.ui.entitysearch.js
@@ -53,7 +53,7 @@
                .entityselector( {
                        url: mw.config.get( 'wgServer' ) + mw.config.get( 
'wgScriptPath' ) + '/api.php',
                        language: mw.config.get( 'wgUserLanguage' ),
-                       emulateSearch: true,
+                       emulateSearchBox: true,
                        customListItem: {
                                content: $( '<div/>' ).addClass( 
'suggestions-special' )
                                        .append( $( '<div/>' ).addClass( 
'special-label ' ).text(

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I0069a6b1ae1f6264c2a59c29bafffbb6b520a946
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Tobias Gritschacher <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to