2021-12-07 06:51:41 +00:00
|
|
|
var LinkTypeField = require( './LinkTypeField.js' );
|
2021-09-01 07:17:53 +00:00
|
|
|
var TitleInputWidget = require( './TitleInputWidget.js' );
|
|
|
|
var TitleOptionWidget = require( './TitleOptionWidget.js' );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A FieldLayout containing a custom TitleInputwidget and message-display system.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.FieldLayout
|
|
|
|
* @mixes OO.EventEmitter
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
function TitleInputField() {
|
2021-10-11 05:31:20 +00:00
|
|
|
// Mixin constructor
|
|
|
|
OO.EventEmitter.call( this );
|
|
|
|
|
2021-09-01 07:17:53 +00:00
|
|
|
var input = new TitleInputWidget();
|
|
|
|
input.connect( this, {
|
|
|
|
change: this.onChange,
|
|
|
|
select: this.onSelect
|
|
|
|
} );
|
|
|
|
|
2021-11-30 03:21:44 +00:00
|
|
|
// The URL mode is set by the user via radio buttons, or automatically for link targets that look like URLs.
|
2021-12-07 06:51:41 +00:00
|
|
|
this.urlMode = LinkTypeField.static.LINK_MODE_INTERNAL;
|
2021-11-30 03:21:44 +00:00
|
|
|
// The 'manual' URL mode flag is set when the user changes the mode, and doesn't change again.
|
|
|
|
this.urlModeManual = false;
|
2021-09-01 07:17:53 +00:00
|
|
|
|
|
|
|
var config = {
|
|
|
|
align: 'top',
|
|
|
|
label: mw.msg( 'wikieditor-toolbar-tool-link-int-target' ),
|
|
|
|
classes: [ 'ext-WikiEditor-InsertLink-TitleInputField' ]
|
|
|
|
};
|
|
|
|
TitleInputField.super.call( this, input, config );
|
|
|
|
}
|
|
|
|
|
|
|
|
OO.inheritClass( TitleInputField, OO.ui.FieldLayout );
|
|
|
|
OO.mixinClass( TitleInputField, OO.EventEmitter );
|
|
|
|
|
|
|
|
/**
|
2021-11-30 03:21:44 +00:00
|
|
|
* Reset the field to initial state.
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.reset = function () {
|
|
|
|
this.getField().setValue( '' );
|
|
|
|
this.urlModeManual = false;
|
2021-12-07 06:51:41 +00:00
|
|
|
this.urlMode = LinkTypeField.static.LINK_MODE_INTERNAL;
|
2021-11-30 03:21:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the URL mode and disable automatic detection of external URLs.
|
|
|
|
*
|
2021-09-01 07:17:53 +00:00
|
|
|
* @public
|
|
|
|
* @param {string} urlMode One of the `TitleInputField.urlModes.*` values.
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.setUrlMode = function ( urlMode ) {
|
2021-12-07 06:51:41 +00:00
|
|
|
this.urlMode = urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ?
|
|
|
|
LinkTypeField.static.LINK_MODE_EXTERNAL :
|
|
|
|
LinkTypeField.static.LINK_MODE_INTERNAL;
|
2021-11-30 03:21:44 +00:00
|
|
|
this.urlModeManual = true;
|
|
|
|
this.getField().selectFirstMatch();
|
|
|
|
this.validate( this.getField().getValue() );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @public
|
|
|
|
* @return {boolean}
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.isExternal = function () {
|
2021-12-07 06:51:41 +00:00
|
|
|
return this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL;
|
2021-09-01 07:17:53 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.makeMessage = function ( kind, text ) {
|
|
|
|
this.message = new OO.ui.MessageWidget( {
|
|
|
|
type: kind,
|
|
|
|
inline: true,
|
|
|
|
label: text
|
|
|
|
} );
|
|
|
|
return this.message.$element;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the displayed field message, replacing any that is already set.
|
|
|
|
*
|
|
|
|
* @param {string} icon
|
|
|
|
* @param {string} message
|
|
|
|
* @param {string} type
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.setMessage = function ( icon, message, type ) {
|
|
|
|
this.setNotices( [ message ] );
|
|
|
|
// Note that setNotices() must be called before this.message is available.
|
|
|
|
this.message.setType( type || 'notice' );
|
|
|
|
this.message.setIcon( icon );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.onChange = function ( value ) {
|
2021-11-30 03:21:44 +00:00
|
|
|
if ( !this.urlModeManual && this.getField().looksLikeExternalLink( value ) ) {
|
2021-12-07 06:51:41 +00:00
|
|
|
this.urlMode = LinkTypeField.static.LINK_MODE_EXTERNAL;
|
2021-11-30 03:21:44 +00:00
|
|
|
}
|
|
|
|
this.validate( value );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set message and emit event.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.validate = function ( value ) {
|
2021-12-07 06:51:41 +00:00
|
|
|
if ( this.urlMode === LinkTypeField.static.LINK_MODE_INTERNAL && value !== '' && !mw.Title.newFromText( value ) ) {
|
2021-09-01 07:17:53 +00:00
|
|
|
this.setMessage( 'error', mw.message( 'wikieditor-toolbar-tool-link-int-target-status-invalid' ).parse(), 'error' );
|
|
|
|
this.emit( 'invalid' );
|
|
|
|
} else {
|
|
|
|
// Remove message; it'll be re-added if required (after selection or blur).
|
|
|
|
this.setNotices( [] );
|
|
|
|
this.emit( 'change', value );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {TitleOptionWidget} item
|
|
|
|
*/
|
|
|
|
TitleInputField.prototype.onSelect = function ( item ) {
|
2021-12-07 06:51:41 +00:00
|
|
|
if ( this.urlMode === LinkTypeField.static.LINK_MODE_EXTERNAL ||
|
|
|
|
( !this.urlModeManual && this.urlMode === LinkTypeField.static.LINK_MODE_INTERNAL && item.isExternal() )
|
|
|
|
) {
|
2021-09-01 07:17:53 +00:00
|
|
|
this.setMessage( 'linkExternal', mw.message( 'wikieditor-toolbar-tool-link-int-target-status-external' ).parse() );
|
|
|
|
} else if ( item.isDisambiguation() ) {
|
|
|
|
this.setMessage( 'articleDisambiguation', mw.message( 'wikieditor-toolbar-tool-link-int-target-status-disambig' ).parse() );
|
2021-11-30 03:21:44 +00:00
|
|
|
} else if ( !item.isMissing() && !item.isExternal() ) {
|
2021-09-01 07:17:53 +00:00
|
|
|
this.setMessage( 'article', mw.message( 'wikieditor-toolbar-tool-link-int-target-status-exists' ).parse() );
|
2021-11-30 03:21:44 +00:00
|
|
|
} else {
|
2021-09-01 07:17:53 +00:00
|
|
|
this.setMessage( 'articleNotFound', mw.message( 'wikieditor-toolbar-tool-link-int-target-status-notexists' ).parse() );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = TitleInputField;
|