mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/VisualEditor
synced 2024-11-25 14:56:20 +00:00
bed038b509
Updates: * grunt-contrib-csslint 0.3.1 -> 0.4.0 * grunt-contrib-jslint 0.10.0 -> 0.11.0 * grunt-jscs 0.8.1 -> 1.2.0 For jscs, leaving requireSpacesInsideArrayBrackets to avoid headaches for now. Change-Id: I62d34444edbba65c8bd22d2fa5e50e16cabb0042
138 lines
3.3 KiB
JavaScript
138 lines
3.3 KiB
JavaScript
/*!
|
|
* VisualEditor UserInterface MWTitleInputWidget class.
|
|
*
|
|
* @copyright 2011-2015 VisualEditor Team and others; see AUTHORS.txt
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
*/
|
|
|
|
/**
|
|
* Creates an ve.ui.MWTitleInputWidget object.
|
|
*
|
|
* @class
|
|
* @extends OO.ui.TextInputWidget
|
|
* @mixins OO.ui.LookupElement
|
|
*
|
|
* @constructor
|
|
* @param {Object} [config] Configuration options
|
|
* @cfg {number} [namespace] Namespace to prepend to queries
|
|
*/
|
|
ve.ui.MWTitleInputWidget = function VeUiMWTitleInputWidget( config ) {
|
|
// Config initialization
|
|
config = config || {};
|
|
|
|
// Parent constructor
|
|
OO.ui.TextInputWidget.call( this, config );
|
|
|
|
// Mixin constructors
|
|
OO.ui.LookupElement.call( this, config );
|
|
|
|
// Properties
|
|
this.namespace = config.namespace || null;
|
|
|
|
// Initialization
|
|
this.$element.addClass( 've-ui-mwTitleInputWidget' );
|
|
this.lookupMenu.$element.addClass( 've-ui-mwTitleInputWidget-menu' );
|
|
};
|
|
|
|
/* Inheritance */
|
|
|
|
OO.inheritClass( ve.ui.MWTitleInputWidget, OO.ui.TextInputWidget );
|
|
|
|
OO.mixinClass( ve.ui.MWTitleInputWidget, OO.ui.LookupElement );
|
|
|
|
/* Methods */
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.onLookupMenuItemChoose = function ( item ) {
|
|
this.closeLookupMenu();
|
|
if ( item ) {
|
|
this.setLookupsDisabled( true );
|
|
this.setValue( item.getData() );
|
|
this.setLookupsDisabled( false );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupRequest = function () {
|
|
var value = this.value;
|
|
|
|
// Prefix with default namespace name
|
|
if ( this.namespace !== null && mw.Title.newFromText( value, this.namespace ) ) {
|
|
value = mw.Title.newFromText( value, this.namespace ).getPrefixedText();
|
|
}
|
|
|
|
// Dont send leading ':' to open search
|
|
if ( value.charAt( 0 ) === ':' ) {
|
|
value = value.slice( 1 );
|
|
}
|
|
|
|
return ve.init.target.constructor.static.apiRequest( {
|
|
action: 'opensearch',
|
|
search: value,
|
|
suggest: ''
|
|
} );
|
|
};
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupCacheDataFromResponse = function ( data ) {
|
|
return data[1] || [];
|
|
};
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.getLookupMenuOptionsFromData = function ( data ) {
|
|
var i, len, title, value,
|
|
menu$ = this.lookupMenu.$,
|
|
items = [],
|
|
matchingPages = data,
|
|
linkCacheUpdate = {};
|
|
|
|
// Matching pages
|
|
if ( matchingPages && matchingPages.length ) {
|
|
for ( i = 0, len = matchingPages.length; i < len; i++ ) {
|
|
title = new mw.Title( matchingPages[i] );
|
|
linkCacheUpdate[matchingPages[i]] = { missing: false };
|
|
if ( this.namespace !== null ) {
|
|
value = title.getRelativeText( this.namespace );
|
|
} else {
|
|
value = title.getPrefixedText();
|
|
}
|
|
items.push( new OO.ui.MenuOptionWidget( {
|
|
$: menu$,
|
|
data: value,
|
|
label: value
|
|
} ) );
|
|
}
|
|
ve.init.platform.linkCache.set( linkCacheUpdate );
|
|
}
|
|
|
|
return items;
|
|
};
|
|
|
|
/**
|
|
* Get title object corresponding to #getValue
|
|
*
|
|
* @returns {mw.Title|null} Title object, or null if value is invalid
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.getTitle = function () {
|
|
var title = this.getValue(),
|
|
// mw.Title doesn't handle null well
|
|
titleObj = mw.Title.newFromText( title, this.namespace !== null ? this.namespace : undefined );
|
|
|
|
return titleObj;
|
|
};
|
|
|
|
/**
|
|
* @inheritdoc
|
|
*/
|
|
ve.ui.MWTitleInputWidget.prototype.isValid = function () {
|
|
return $.Deferred().resolve( !!this.getTitle() ).promise();
|
|
};
|