Tobias Gritschacher has submitted this change and it was merged.

Change subject: Entity selector: Option to emulate search box behavior
......................................................................


Entity selector: Option to emulate search box behavior

This change set adds a option named "emulateSearch" to the entity selector. 
Setting
this option to "true" links the entity selector's suggestions to the actual 
entity
pages. Another change set will enable entering random text in the input box.

- patch set 2: added documentation

Change-Id: If17dd8bd50af5fce096c07f3232eb5612c76167d
---
M lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
M repo/resources/wikibase.ui.entitysearch.js
2 files changed, 40 insertions(+), 8 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 5605044..b2b8739 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
@@ -51,6 +51,11 @@
  * @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
+ *         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
+ *
  * @event response Triggered after an API request has been received 
successfully.
  *        Parameters: (1) {jQuery.Event}
  *                    (2) {Array} Entity data
@@ -63,7 +68,7 @@
  * @dependency jquery.ui.suggester
  * @dependency jquery.ui.resizable
  */
-( function( $, undefined ) {
+( function( $ ) {
        'use strict';
 
        /**
@@ -111,7 +116,8 @@
                        messages: {
                                '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
                },
 
                /**
@@ -186,6 +192,16 @@
                                // when pressing enter, check if the current 
input matches any of the suggested item
                                // and select it
                                if ( event.keyCode === $.ui.keyCode.ENTER ) {
+
+                                       if ( self.options.emulateSearch && 
self.selectedEntity() ) {
+                                               // Prevent submitting search 
form since we want to redirect directly to the
+                                               // entity.
+                                               
event.stopImmediatePropagation();
+                                               event.preventDefault();
+                                               window.location.href = 
self.selectedEntity().url;
+                                               return;
+                                       }
+
                                        if ( self.validateInput() ) {
                                                self.menu.select( $.Event( 
'programmatic' ) );
                                        }
@@ -210,10 +226,15 @@
                        // value but "blur" event will not trigger a redundant 
"selected" event since the entity
                        // is set already.
                        this.menu.element.on( 'mousedown.' + this.widgetName, 
function( event ) {
-                               if ( $( event.target ).closest( '.ui-menu-item' 
).length ) {
+                               if ( $( event.target ).closest( 
'.ui-menu-item:not( .ui-suggester-custom )' ).length ) {
                                        var item =
                                                $( event.target ).closest( 
'.ui-menu-item' ).data( 'item.autocomplete' );
                                        self._setEntity( item );
+
+                                       if ( self.options.emulateSearch ) {
+                                               window.location.href = item.url;
+                                       }
+
                                }
                        } );
 
@@ -452,10 +473,15 @@
                                                this.menu.element.show();
                                        }
                                } else {
-                                       this._hiddenInput( '' );
                                        this._setEntity( null );
+                                       if ( this.options.emulateSearch ) {
+                                               this._hiddenInput( 
this.element.val() );
+                                       }
                                }
                                return true;
+                       }
+                       if ( this.options.emulateSearch ) {
+                               this._hiddenInput( this.element.val() );
                        }
                        return false;
                },
@@ -484,6 +510,10 @@
                                        )
                                ),
                                $section = $( '<li/>' ).data( 
'item.autocomplete', item ).append( $link );
+
+                       if ( this.options.emulateSearch ) {
+                               $link.attr( 'href', item.url );
+                       }
 
                        if ( item.description !== undefined ) {
                                $section.find( '.ui-entityselector-itemcontent' 
).append(
@@ -565,7 +595,7 @@
                                found = true;
                        }
 
-                       if ( !found && !alreadySelected ) {
+                       if ( !found && !alreadySelected && 
!this.options.emulateSearch ) {
                                this._setEntity( null );
                        }
                        return found;
@@ -577,14 +607,15 @@
                 * @param {Object|null} entity Entity data or null to reset the 
currently selected entity
                 */
                _setEntity: function( entity ) {
-                       if ( entity === null ) {
+                       if ( entity === null || entity.isCustom ) {
                                this._hiddenInput( '' );
                                this._selectedEntity = null;
                        } else {
                                this._hiddenInput( entity.id );
                                this._selectedEntity = {
                                        id: entity.id,
-                                       label: entity.label || null
+                                       label: entity.label || null,
+                                       url: entity.url
                                };
                        }
                },
diff --git a/repo/resources/wikibase.ui.entitysearch.js 
b/repo/resources/wikibase.ui.entitysearch.js
index 702f662..ca56e69 100644
--- a/repo/resources/wikibase.ui.entitysearch.js
+++ b/repo/resources/wikibase.ui.entitysearch.js
@@ -42,7 +42,8 @@
                } )
                .entityselector( {
                        url: mw.config.get( 'wgServer' ) + mw.config.get( 
'wgScriptPath' ) + '/api.php',
-                       language: mw.config.get( 'wgUserLanguage' )
+                       language: mw.config.get( 'wgUserLanguage' ),
+                       emulateSearch: true
                } );
 
        } );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: If17dd8bd50af5fce096c07f3232eb5612c76167d
Gerrit-PatchSet: 6
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
Gerrit-Reviewer: Daniel Werner <[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