| 1 |
( function () { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 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 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
mw.widgets.MathWbEntitySelector.prototype.getApi = function () { |
| 38 |
return this.api; |
| 39 |
}; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
mw.widgets.MathWbEntitySelector.prototype.getQueryValue = function () { |
| 48 |
return this.getValue(); |
| 49 |
}; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
mw.widgets.MathWbEntitySelector.prototype.getLookupCacheDataFromResponse = function ( response ) { |
| 55 |
return response.search || {}; |
| 56 |
}; |
| 57 |
|
| 58 |
|
| 59 |
|
| 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 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 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 |
|
| 85 |
|
| 86 |
mw.widgets.MathWbEntitySelector.prototype.getLookupRequest = function () { |
| 87 |
const api = this.getApi(), |
| 88 |
query = this.getQueryValue(), |
| 89 |
promiseAbortObject = { |
| 90 |
abort: function () { |
| 91 |
|
| 92 |
|
| 93 |
} |
| 94 |
}, |
| 95 |
req = api.get( this.getApiParams( query ) ); |
| 96 |
promiseAbortObject.abort = req.abort.bind( req ); |
| 97 |
return req.promise( promiseAbortObject ); |
| 98 |
}; |
| 99 |
|
| 100 |
|
| 101 |
const $wbEntitySelector = $( '#wbEntitySelector' ); |
| 102 |
if ( $wbEntitySelector.length ) { |
| 103 |
OO.ui.infuse( $wbEntitySelector ); |
| 104 |
} |
| 105 |
}() ); |