Henning Snater has uploaded a new change for review.
https://gerrit.wikimedia.org/r/51350
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.
Change-Id: If17dd8bd50af5fce096c07f3232eb5612c76167d
---
M lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
M repo/resources/wikibase.ui.entitysearch.js
2 files changed, 34 insertions(+), 7 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase
refs/changes/50/51350/1
diff --git a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
index 802cbc3..6f5c4a7 100644
--- a/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
+++ b/lib/resources/jquery.wikibase/jquery.wikibase.entityselector.js
@@ -111,7 +111,8 @@
messages: {
'aliases-label': mwMsgOrString(
'wikibase-aliases-label', 'also known as:' ),
'more': mwMsgOrString(
'wikibase-entityselector-more', 'more' )
- }
+ },
+ emulateSearch: false
},
/**
@@ -186,6 +187,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 +221,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;
+ }
+
}
} );
@@ -451,10 +467,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;
},
@@ -483,6 +504,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(
@@ -564,7 +589,7 @@
found = true;
}
- if ( !found && !alreadySelected ) {
+ if ( !found && !alreadySelected &&
!this.options.emulateSearch ) {
this._setEntity( null );
}
return found;
@@ -576,14 +601,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: newchange
Gerrit-Change-Id: If17dd8bd50af5fce096c07f3232eb5612c76167d
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Henning Snater <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits