2020-11-02 12:38:51 +00:00
|
|
|
( function () {
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an mw.widgets.MathWbEntitySelector object. This class was inspired by
|
|
|
|
* mw.widgets.TitleInputWidget.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.TextInputWidget
|
2021-10-05 16:04:47 +00:00
|
|
|
* @mixin mw.widgets.TitleWidget
|
|
|
|
* @mixin OO.ui.mixin.LookupElement
|
2020-11-02 12:38:51 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @cfg {mw.Api} [api] API object to use, creates a mw.ForeignApi instance if not specified
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector = function ( config ) {
|
|
|
|
config = config || {};
|
|
|
|
this.api = config.api;
|
|
|
|
if ( !this.api ) {
|
2022-07-17 09:59:09 +00:00
|
|
|
this.api = new mw.ForeignApi(
|
|
|
|
mw.config.get( 'wgMathEntitySelectorUrl' ),
|
|
|
|
{ anonymous: true } );
|
2020-11-02 12:38:51 +00:00
|
|
|
}
|
|
|
|
OO.ui.TextInputWidget.call( this, config );
|
|
|
|
OO.ui.mixin.LookupElement.call( this, config );
|
|
|
|
this.$element.addClass( 'mw-widget-MathWbEntitySelector' );
|
|
|
|
};
|
|
|
|
OO.inheritClass( mw.widgets.MathWbEntitySelector, OO.ui.TextInputWidget );
|
|
|
|
OO.mixinClass( mw.widgets.MathWbEntitySelector, OO.ui.mixin.LookupElement );
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* Get the API object for wikibase requests
|
|
|
|
*
|
|
|
|
* @return {mw.Api} MediaWiki API
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getApi = function () {
|
|
|
|
return this.api;
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* Get the current value of the search query
|
|
|
|
*
|
|
|
|
* @abstract
|
|
|
|
* @return {string} Search query
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getQueryValue = function () {
|
|
|
|
return this.getValue();
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.mixin.LookupElement
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getLookupCacheDataFromResponse = function ( response ) {
|
|
|
|
return response.search || {};
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc OO.ui.mixin.LookupElement
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getLookupMenuOptionsFromData = function ( response ) {
|
|
|
|
return response.map( function ( res ) {
|
|
|
|
return new OO.ui.MenuOptionWidget( { data: res.id, label: res.label, title: res.description } );
|
|
|
|
} );
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* Get API params for a given query
|
|
|
|
*
|
|
|
|
* @param {string} query User query
|
|
|
|
* @return {Object} API params
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getApiParams = function ( query ) {
|
|
|
|
|
|
|
|
return {
|
|
|
|
action: 'wbsearchentities',
|
|
|
|
search: query,
|
|
|
|
format: 'json',
|
|
|
|
errorformat: 'plaintext',
|
|
|
|
language: mw.config.get( 'wgContentLanguage' ),
|
|
|
|
uselang: mw.config.get( 'wgContentLanguage' )
|
|
|
|
};
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2020-11-02 12:38:51 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
mw.widgets.MathWbEntitySelector.prototype.getLookupRequest = function () {
|
2024-02-13 00:31:24 +00:00
|
|
|
const api = this.getApi(),
|
2020-11-02 12:38:51 +00:00
|
|
|
query = this.getQueryValue(),
|
|
|
|
widget = this,
|
|
|
|
promiseAbortObject = {
|
|
|
|
abort: function () {
|
|
|
|
// Do nothing. This is just so OOUI doesn't break due to abort being undefined.
|
|
|
|
// see also mw.widgets.TitleWidget.prototype.getSuggestionsPromise
|
|
|
|
}
|
2021-10-05 16:04:47 +00:00
|
|
|
},
|
|
|
|
req = api.get( widget.getApiParams( query ) );
|
2020-11-02 12:38:51 +00:00
|
|
|
promiseAbortObject.abort = req.abort.bind( req );
|
|
|
|
return req.promise( promiseAbortObject );
|
|
|
|
};
|
2021-10-05 16:04:47 +00:00
|
|
|
|
2021-04-16 05:19:30 +00:00
|
|
|
// eslint-disable-next-line no-jquery/no-global-selector
|
2024-02-13 00:31:24 +00:00
|
|
|
const $wbEntitySelector = $( '#wbEntitySelector' );
|
2020-11-02 12:38:51 +00:00
|
|
|
if ( $wbEntitySelector.length ) {
|
|
|
|
OO.ui.infuse( $wbEntitySelector );
|
|
|
|
}
|
|
|
|
}() );
|