mediawiki-extensions-Visual.../modules/ve-mw/ui/widgets/ve.ui.MWTitleInputWidget.js
Roan Kattouw a0167c08c7 Clean up LookupInputWidget subclasses and use new functionality
MWCategoryInputWidget:
* Use @inheritdoc
* Don't modify data parameter in getLookupCacheItemFromData()

MWLinkTargetInputWidget:
* Remove this.choosing in favor of setLookupsDisabled()
* Explicitly close menu on choose
* Remove manual emission of change events
** This looks ridiculous, it doesn't seem to be necessary,
   and it causes infinite event loops. But I'm very curious
   why this was added in the first place.
* Remove onLookupInputChange override that is now unnecessary
* Use {} rather than [] for fake empty result
* Prevent programmatic focus from opening the menu

MWTitleInputWidget:
* On choose, close menu and disable lookups while changing value
* Use @inheritdoc
* Remove mentions of "template" from getTitle() documentation

Bug fixed:
* When choosing a suggestion in MWTitleInputWidget,
  new suggestions would be loaded and the menu would reopen

Depends on Iecae9b582 in oojs-ui.

Change-Id: I716f99bb464a5cebd4f17701197f768e4e0e02a9
2014-11-03 10:39:03 -08:00

141 lines
3.3 KiB
JavaScript

/*!
* VisualEditor UserInterface MWTitleInputWidget class.
*
* @copyright 2011-2014 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.LookupInputWidget
*
* @constructor
* @param {Object} [config] Configuration options
* @cfg {number} [namespace] Namespace to prepend to queries
*/
ve.ui.MWTitleInputWidget = function VeUiMWTitleInputWidget( config ) {
// Config intialization
config = config || {};
// Parent constructor
OO.ui.TextInputWidget.call( this, config );
// Mixin constructors
OO.ui.LookupInputWidget.call( this, this, config );
// Properties
this.namespace = config.namespace || null;
// Events
this.lookupMenu.connect( this, { choose: 'onLookupMenuItemChoose' } );
// 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.LookupInputWidget );
/* Methods */
/**
* Handle menu item select event.
*
* @method
* @param {OO.ui.MenuItemWidget} item Selected item
*/
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.substr( 1 );
}
return ve.init.target.constructor.static.apiRequest( {
action: 'opensearch',
search: value,
suggest: ''
} );
};
/**
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.getLookupCacheItemFromData = function ( data ) {
return data[1] || [];
};
/**
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.getLookupMenuItemsFromData = 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.MenuItemWidget(
value, { $: menu$, 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(),
titleObj = mw.Title.newFromText( title );
return titleObj;
};
/**
* @inheritdoc
*/
ve.ui.MWTitleInputWidget.prototype.isValid = function () {
return $.Deferred().resolve( !!this.getTitle() ).promise();
};