Debian Codegraph @

extensions/Math/modules/mw.widgets.MathWbEntitySelector.js

raw symbols SCIP
1 ( function () {
2 'use strict';
3
4 /**
5 * Creates an mw.widgets.MathWbEntitySelector object. This class was inspired by
6 * mw.widgets.TitleInputWidget.
7 *
8 * @class
9 * @extends OO.ui.TextInputWidget
10 * @mixin mw.widgets.TitleWidget
11 * @mixin OO.ui.mixin.LookupElement
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @param {mw.Api} [config.api] API object to use, creates a mw.ForeignApi instance if not specified
16 */
17 mw.widgets.MathWbEntitySelector = function ( config ) {
18 config = config || {};
19 this.api = config.api;
20 if ( !this.api ) {
21 this.api = new mw.ForeignApi(
22 mw.config.get( 'wgMathEntitySelectorUrl' ),
23 { anonymous: true } );
24 }
25 OO.ui.TextInputWidget.call( this, config );
26 OO.ui.mixin.LookupElement.call( this, config );
27 this.$element.addClass( 'mw-widget-MathWbEntitySelector' );
28 };
29 OO.inheritClass( mw.widgets.MathWbEntitySelector, OO.ui.TextInputWidget );
30 OO.mixinClass( mw.widgets.MathWbEntitySelector, OO.ui.mixin.LookupElement );
31
32 /**
33 * Get the API object for wikibase requests
34 *
35 * @return {mw.Api} MediaWiki API
36 */
37 mw.widgets.MathWbEntitySelector.prototype.getApi = function () {
38 return this.api;
39 };
40
41 /**
42 * Get the current value of the search query
43 *
44 * @abstract
45 * @return {string} Search query
46 */
47 mw.widgets.MathWbEntitySelector.prototype.getQueryValue = function () {
48 return this.getValue();
49 };
50
51 /**
52 * @inheritdoc OO.ui.mixin.LookupElement
53 */
54 mw.widgets.MathWbEntitySelector.prototype.getLookupCacheDataFromResponse = function ( response ) {
55 return response.search || {};
56 };
57
58 /**
59 * @inheritdoc OO.ui.mixin.LookupElement
60 */
61 mw.widgets.MathWbEntitySelector.prototype.getLookupMenuOptionsFromData = function ( response ) {
62 return response.map( ( res ) => new OO.ui.MenuOptionWidget( { data: res.id, label: res.label, title: res.description } ) );
63 };
64
65 /**
66 * Get API params for a given query
67 *
68 * @param {string} query User query
69 * @return {Object} API params
70 */
71 mw.widgets.MathWbEntitySelector.prototype.getApiParams = function ( query ) {
72
73 return {
74 action: 'wbsearchentities',
75 search: query,
76 format: 'json',
77 errorformat: 'plaintext',
78 language: mw.config.get( 'wgContentLanguage' ),
79 uselang: mw.config.get( 'wgContentLanguage' )
80 };
81 };
82
83 /**
84 * @inheritdoc
85 */
86 mw.widgets.MathWbEntitySelector.prototype.getLookupRequest = function () {
87 const api = this.getApi(),
88 query = this.getQueryValue(),
89 promiseAbortObject = {
90 abort: function () {
91 // Do nothing. This is just so OOUI doesn't break due to abort being undefined.
92 // see also mw.widgets.TitleWidget.prototype.getSuggestionsPromise
93 }
94 },
95 req = api.get( this.getApiParams( query ) );
96 promiseAbortObject.abort = req.abort.bind( req );
97 return req.promise( promiseAbortObject );
98 };
99
100 // eslint-disable-next-line no-jquery/no-global-selector
101 const $wbEntitySelector = $( '#wbEntitySelector' );
102 if ( $wbEntitySelector.length ) {
103 OO.ui.infuse( $wbEntitySelector );
104 }
105 }() );