*/
( 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
*
* @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
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' );
OO.initClass( mw.widgets.TitleWidget );
+ /* Static properties */
+
+ mw.widgets.TitleWidget.static.interwikiPrefixesPromiseCache = {};
+
/* Methods */
/**
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.
*/
mw.widgets.TitleWidget.prototype.getSuggestionsPromise = function () {
var req,
+ api = this.getApi(),
query = this.getQueryValue(),
widget = this,
promiseAbortObject = { abort: function () {
} };
if ( mw.Title.newFromText( query ) ) {
- return interwikiPrefixesPromise.then( function ( interwikiPrefixes ) {
+ return this.getInterwikiPrefixesPromise().then( function ( interwikiPrefixes ) {
var params,
interwiki = query.substring( 0, query.indexOf( ':' ) );
if (
params.prop.push( 'pageterms' );
params.wbptterms = 'description';
}
- req = new mw.Api().get( params );
+ req = api.get( params );
promiseAbortObject.abort = req.abort.bind( req ); // TODO ew
return req.then( function ( ret ) {
if ( ret.query === undefined ) {
- ret = new mw.Api().get( { action: 'query', titles: query } );
+ ret = api.get( { action: 'query', titles: query } );
promiseAbortObject.abort = ret.abort.bind( ret );
}
return ret;
}
};
+ /**
+ * Get the API object for title requests
+ *
+ * @return {mw.Api} MediaWiki API
+ */
+ mw.widgets.TitleWidget.prototype.getApi = function () {
+ return this.api;
+ };
+
/**
* Get option widgets from the server response
*