jenkins-bot has submitted this change and it was merged.
Change subject: Allow API endpoint customization for mw.widgets.TitleWidget
..
Allow API endpoint customization for mw.widgets.TitleWidget
Currently the API is not customizable and always points to
current wiki's api returned by `new mw.Api()`.
This patch allows users to specify their own API object for
querying titles e.g. searching for titles on a different
language Wikipedia when translating pages.
Change-Id: I81811cdd1a0750a8335432eee8f971ab9e0b8ee7
---
M resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
M resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
2 files changed, 38 insertions(+), 14 deletions(-)
Approvals:
Krinkle: Looks good to me, but someone else must approve
Esanders: Looks good to me, approved
jenkins-bot: Verified
diff --git a/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
b/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
index 39bee7c..2ac75c5 100755
--- a/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
+++ b/resources/src/mediawiki.widgets/mw.widgets.SearchInputWidget.js
@@ -78,7 +78,7 @@
* @inheritdoc mw.widgets.TitleWidget
*/
mw.widgets.SearchInputWidget.prototype.getSuggestionsPromise = function
() {
- var api = new mw.Api(),
+ var api = this.getApi(),
promise,
self = this;
diff --git a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
index e1e50ea..0e5e0c5 100644
--- a/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
+++ b/resources/src/mediawiki.widgets/mw.widgets.TitleWidget.js
@@ -6,16 +6,6 @@
*/
( function ( $, mw ) {
- var interwikiPrefixesPromise = new mw.Api().get( {
- action: 'query',
- meta: 'siteinfo',
- siprop: 'interwikimap'
- } ).then( function ( data ) {
- return $.map( data.query.interwikimap, function ( interwiki ) {
- return interwiki.prefix;
- } );
- } );
-
/**
* Mixin for title widgets
*
@@ -36,6 +26,7 @@
* @cfg {boolean} [validateTitle=true] Whether the input must be a
valid title (if set to true,
* the widget will marks itself red for invalid inputs, including an
empty query).
* @cfg {Object} [cache] Result cache which implements a 'set' method,
taking keyed values as an argument
+* @cfg {mw.Api} [api] API object to use, creates a default mw.Api
instance if not specified
*/
mw.widgets.TitleWidget = function MwWidgetsTitleWidget( config ) {
// Config initialization
@@ -56,6 +47,7 @@
this.excludeCurrentPage = !!config.excludeCurrentPage;
this.validateTitle = config.validateTitle !== undefined ?
config.validateTitle : true;
this.cache = config.cache;
+ this.api = config.api || new mw.Api();
// Initialization
this.$element.addClass( 'mw-widget-titleWidget' );
@@ -64,6 +56,10 @@
/* Setup */
OO.initClass( mw.widgets.TitleWidget );
+
+ /* Static properties */
+
+ mw.widgets.TitleWidget.static.interwikiPrefixesPromiseCache = {};
/* Methods */
@@ -93,6 +89,24 @@
this.namespace = namespace;
};
+ mw.widgets.TitleWidget.prototype.getInterwikiPrefixesPromise = function
() {
+ var api = this.getApi(),
+ cache =
this.constructor.static.interwikiPrefixesPromiseCache,
+ key = api.defaults.ajax.url;
+ if ( !cache.hasOwnProperty( key ) ) {
+ cache[ key ] = api.get( {
+ action: 'query',
+ meta: 'siteinfo',
+ siprop: 'interwikimap'
+ } ).then( function ( data ) {
+ return $.map( data.query.interwikimap, function
( interwiki ) {
+ return interwiki.prefix;
+ } );
+ } );
+ }
+ return cache[ key ];
+ };
+
/**
* Get a promise which resolves with an API repsonse for suggested
* links for the current query.
@@ -101,6 +115,7 @@
*/
mw.widgets.TitleWidget.prototype.getSuggestionsPromise = function () {
var req,
+ api = this.getApi(),
query = this.getQueryValue(),
widget = this,
promiseAbortObject = { abort: function () {
@@ -108,7 +123,7 @@
} };
if ( mw.Title.newFromText(