2015-04-10 20:28:38 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWTemplateTitleInputWidget class.
|
|
|
|
*
|
|
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an ve.ui.MWTemplateTitleInputWidget object.
|
|
|
|
*
|
|
|
|
* @class
|
2015-06-15 10:25:44 +00:00
|
|
|
* @extends mw.widgets.TitleInputWidget
|
2015-04-10 20:28:38 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @cfg {number} [namespace] Namespace to prepend to queries. Defaults to template namespace.
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateTitleInputWidget = function VeUiMWTemplateTitleInputWidget( config ) {
|
2015-06-15 10:25:44 +00:00
|
|
|
config = ve.extendObject( {}, {
|
|
|
|
namespace: mw.config.get( 'wgNamespaceIds' ).template
|
|
|
|
}, config );
|
|
|
|
|
2015-04-10 20:28:38 +00:00
|
|
|
// Parent constructor
|
2015-06-15 10:25:44 +00:00
|
|
|
ve.ui.MWTemplateTitleInputWidget.super.call( this, config );
|
|
|
|
|
|
|
|
this.showTemplateDescriptions = this.showDescriptions;
|
|
|
|
// Clear the showDescriptions flag for subsequent requests as we implement
|
|
|
|
// description fetch ourselves
|
|
|
|
this.showDescriptions = false;
|
2015-04-10 20:28:38 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.descriptions = {};
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$element.addClass( 've-ui-mwTemplateTitleInputWidget' );
|
|
|
|
};
|
2015-05-25 13:51:13 +00:00
|
|
|
|
2015-04-10 20:28:38 +00:00
|
|
|
/* Inheritance */
|
|
|
|
|
2015-06-15 10:25:44 +00:00
|
|
|
OO.inheritClass( ve.ui.MWTemplateTitleInputWidget, mw.widgets.TitleInputWidget );
|
2015-04-10 20:28:38 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2015-05-25 13:51:13 +00:00
|
|
|
* See the parent documentation at <https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.widgets.TitleInputWidget>
|
2015-04-10 20:28:38 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateTitleInputWidget.prototype.getLookupRequest = function () {
|
2015-06-15 10:25:44 +00:00
|
|
|
var widget = this,
|
|
|
|
originalResponse,
|
|
|
|
promise = ve.ui.MWTemplateTitleInputWidget.super.prototype.getLookupRequest.call( this );
|
|
|
|
|
|
|
|
if ( this.showTemplateDescriptions ) {
|
|
|
|
return promise
|
|
|
|
// Also get descriptions
|
|
|
|
.then( function ( response ) {
|
|
|
|
var xhr, index, params,
|
|
|
|
pages = ( response.query && response.query.pages ) || {},
|
|
|
|
titles = [];
|
|
|
|
|
|
|
|
for ( index in pages ) {
|
|
|
|
titles.push( pages[index].title );
|
|
|
|
}
|
2015-04-10 20:28:38 +00:00
|
|
|
|
2015-06-15 10:25:44 +00:00
|
|
|
originalResponse = response;
|
2015-04-10 20:28:38 +00:00
|
|
|
|
2015-06-15 10:25:44 +00:00
|
|
|
if ( titles.length > 0 ) {
|
|
|
|
params = {
|
|
|
|
action: 'templatedata',
|
|
|
|
titles: titles.join( '|' ),
|
|
|
|
lang: mw.config.get( 'wgUserLanguage' )
|
|
|
|
};
|
|
|
|
if ( widget.showRedirects ) {
|
|
|
|
params.redirects = '1';
|
2015-04-10 20:28:38 +00:00
|
|
|
}
|
2015-06-15 10:25:44 +00:00
|
|
|
xhr = new mw.Api().get( params );
|
|
|
|
return xhr.promise( { abort: xhr.abort } );
|
2015-04-10 20:28:38 +00:00
|
|
|
}
|
2015-06-15 10:25:44 +00:00
|
|
|
} )
|
|
|
|
.then( function ( templateDataResponse ) {
|
|
|
|
var index, page,
|
|
|
|
pages = ( templateDataResponse && templateDataResponse.pages ) || {};
|
|
|
|
// Look for descriptions and cache them
|
|
|
|
for ( index in pages ) {
|
|
|
|
page = pages[index];
|
|
|
|
// Cache descriptions
|
|
|
|
widget.descriptions[page.title] = page.description;
|
|
|
|
}
|
|
|
|
// Return the original response
|
|
|
|
return originalResponse;
|
|
|
|
} )
|
|
|
|
.promise( { abort: function () {} } );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise;
|
2015-04-10 20:28:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2015-06-15 10:25:44 +00:00
|
|
|
* See the parent documentation at <https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.widgets.TitleInputWidget>
|
2015-04-10 20:28:38 +00:00
|
|
|
*/
|
2015-06-15 10:25:44 +00:00
|
|
|
ve.ui.MWTemplateTitleInputWidget.prototype.getOptionWidgetData = function ( title ) {
|
|
|
|
return ve.extendObject(
|
|
|
|
ve.ui.MWTemplateTitleInputWidget.super.prototype.getOptionWidgetData.apply( this, arguments ),
|
|
|
|
{ description: this.descriptions[title] }
|
|
|
|
);
|
2015-04-10 20:28:38 +00:00
|
|
|
};
|