2013-12-02 20:10:55 +00:00
|
|
|
/*!
|
2014-02-24 21:49:48 +00:00
|
|
|
* VisualEditor user interface MWParameterPage class.
|
2013-12-02 20:10:55 +00:00
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-12-02 20:10:55 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-07-12 09:55:54 +00:00
|
|
|
* Container for editing the value of a parameter in the template dialog
|
|
|
|
* content pane. Includes a dynamic value input depending on the parameter's
|
|
|
|
* type documented in TemplateData.
|
2013-12-02 20:10:55 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.PageLayout
|
|
|
|
*
|
|
|
|
* @constructor
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} parameter Template parameter
|
2013-12-02 20:10:55 +00:00
|
|
|
* @param {Object} [config] Configuration options
|
2018-04-04 20:37:32 +00:00
|
|
|
* @cfg {jQuery} [$overlay] Overlay to render dropdowns in
|
2019-02-08 19:18:22 +00:00
|
|
|
* @cfg {boolean} [readOnly] Parameter is read-only
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2022-06-23 10:20:19 +00:00
|
|
|
ve.ui.MWParameterPage = function VeUiMWParameterPage( parameter, config ) {
|
2022-06-17 09:54:19 +00:00
|
|
|
var paramName = parameter.getName();
|
2014-02-12 21:45:37 +00:00
|
|
|
|
2014-04-25 23:50:21 +00:00
|
|
|
// Configuration initialization
|
|
|
|
config = ve.extendObject( {
|
2014-08-22 20:50:48 +00:00
|
|
|
scrollable: false
|
2014-04-25 23:50:21 +00:00
|
|
|
}, config );
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
// Parent constructor
|
2022-06-23 10:20:19 +00:00
|
|
|
ve.ui.MWParameterPage.super.call( this, parameter.getId(), config );
|
2013-12-02 20:10:55 +00:00
|
|
|
|
|
|
|
// Properties
|
2014-10-29 21:50:14 +00:00
|
|
|
this.edited = false;
|
2013-12-02 20:10:55 +00:00
|
|
|
this.parameter = parameter;
|
2014-02-26 00:29:13 +00:00
|
|
|
this.spec = parameter.getTemplate().getSpec();
|
2014-10-26 15:22:09 +00:00
|
|
|
this.defaultValue = parameter.getDefaultValue();
|
2015-03-04 23:31:05 +00:00
|
|
|
this.exampleValue = parameter.getExampleValue();
|
2022-08-05 08:07:14 +00:00
|
|
|
this.hasValue = null;
|
2014-10-22 22:46:20 +00:00
|
|
|
|
2015-04-09 23:47:15 +00:00
|
|
|
this.$info = $( '<div>' );
|
|
|
|
this.$field = $( '<div>' );
|
2014-10-29 21:50:14 +00:00
|
|
|
|
2021-10-21 14:24:02 +00:00
|
|
|
// Construct the field docs for the template description
|
|
|
|
var $doc = $( '<div>' )
|
|
|
|
.attr( 'id', OO.ui.generateElementId() )
|
2021-11-23 16:54:10 +00:00
|
|
|
.addClass( 've-ui-mwParameterPage-doc' );
|
|
|
|
var description = this.spec.getParameterDescription( paramName );
|
|
|
|
if ( description ) {
|
|
|
|
$( '<p>' ).text( description ).appendTo( $doc );
|
|
|
|
}
|
2021-10-21 14:24:02 +00:00
|
|
|
|
2016-12-20 13:21:07 +00:00
|
|
|
// Note: Calling createValueInput() sets some properties we rely on later in this function
|
2014-10-29 21:50:14 +00:00
|
|
|
this.valueInput = this.createValueInput()
|
2014-10-22 22:46:20 +00:00
|
|
|
.setValue( this.parameter.getValue() )
|
2014-08-22 20:50:48 +00:00
|
|
|
.connect( this, { change: 'onValueInputChange' } );
|
2014-06-26 10:41:18 +00:00
|
|
|
|
2021-10-21 14:24:02 +00:00
|
|
|
this.valueInput.$input.attr( 'aria-describedby', $doc.attr( 'id' ) );
|
|
|
|
|
2019-02-08 19:18:22 +00:00
|
|
|
if ( config.readOnly && this.valueInput.setReadOnly ) {
|
|
|
|
this.valueInput.setReadOnly( true );
|
|
|
|
}
|
|
|
|
|
2021-10-21 14:24:02 +00:00
|
|
|
var labelElement = new OO.ui.LabelWidget( {
|
|
|
|
input: this.valueInput,
|
|
|
|
label: this.spec.getParameterLabel( paramName ),
|
|
|
|
classes: [ 've-ui-mwParameterPage-label' ]
|
|
|
|
} );
|
2014-03-06 22:00:43 +00:00
|
|
|
|
2021-09-14 13:28:49 +00:00
|
|
|
var statusIndicator;
|
2014-03-06 22:00:43 +00:00
|
|
|
if ( this.parameter.isRequired() ) {
|
2021-11-30 13:07:32 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.addClass( 've-ui-mwParameterPage-doc-required' )
|
|
|
|
.text( ve.msg( 'visualeditor-dialog-transclusion-required-parameter-description' ) )
|
|
|
|
.appendTo( $doc );
|
2014-04-25 23:50:21 +00:00
|
|
|
} else if ( this.parameter.isDeprecated() ) {
|
2021-09-14 13:28:49 +00:00
|
|
|
statusIndicator = new OO.ui.IndicatorWidget( {
|
|
|
|
classes: [ 've-ui-mwParameterPage-statusIndicator' ],
|
|
|
|
indicator: 'alert',
|
|
|
|
title: ve.msg( 'visualeditor-dialog-transclusion-deprecated-parameter' )
|
|
|
|
} );
|
2021-11-30 13:07:32 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.addClass( 've-ui-mwParameterPage-doc-deprecated' )
|
|
|
|
.text( ve.msg(
|
|
|
|
'visualeditor-dialog-transclusion-deprecated-parameter-description',
|
|
|
|
this.spec.getParameterDeprecationDescription( paramName )
|
|
|
|
) )
|
|
|
|
.appendTo( $doc );
|
2014-04-25 23:50:21 +00:00
|
|
|
}
|
2015-03-04 23:31:05 +00:00
|
|
|
|
|
|
|
if ( this.defaultValue ) {
|
2021-11-30 13:07:32 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.addClass( 've-ui-mwParameterPage-doc-default' )
|
|
|
|
.text( ve.msg( 'visualeditor-dialog-transclusion-param-default', this.defaultValue ) )
|
|
|
|
.appendTo( $doc );
|
2015-03-04 23:31:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.exampleValue ) {
|
2021-11-30 13:07:32 +00:00
|
|
|
$( '<p>' )
|
|
|
|
.addClass( 've-ui-mwParameterPage-doc-example' )
|
|
|
|
.text( ve.msg(
|
2022-06-10 15:15:18 +00:00
|
|
|
'visualeditor-dialog-transclusion-param-example-long',
|
2021-11-30 13:07:32 +00:00
|
|
|
this.exampleValue
|
|
|
|
) )
|
|
|
|
.appendTo( $doc );
|
2015-03-04 23:31:05 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:21:07 +00:00
|
|
|
// Initialization
|
|
|
|
this.$info
|
|
|
|
.addClass( 've-ui-mwParameterPage-info' )
|
2021-10-21 14:24:02 +00:00
|
|
|
.append( labelElement.$element );
|
2021-09-14 13:28:49 +00:00
|
|
|
if ( statusIndicator ) {
|
|
|
|
this.$info.append( ' ', statusIndicator.$element );
|
|
|
|
}
|
2016-12-20 13:21:07 +00:00
|
|
|
this.$field
|
|
|
|
.addClass( 've-ui-mwParameterPage-field' )
|
|
|
|
.append(
|
|
|
|
this.valueInput.$element
|
|
|
|
);
|
2021-07-15 12:47:21 +00:00
|
|
|
|
2022-06-17 09:54:19 +00:00
|
|
|
if ( !this.parameter.isDocumented() ) {
|
2021-10-21 13:47:53 +00:00
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 've-ui-mwParameterPage-undocumentedLabel' )
|
|
|
|
.text( ve.msg( 'visualeditor-dialog-transclusion-param-undocumented' ) )
|
2022-08-29 15:00:23 +00:00
|
|
|
.appendTo( labelElement.$element );
|
2021-07-15 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 13:21:07 +00:00
|
|
|
this.$element
|
|
|
|
.addClass( 've-ui-mwParameterPage' )
|
2021-11-23 17:05:18 +00:00
|
|
|
.append( this.$info, this.$field );
|
2021-04-15 10:55:09 +00:00
|
|
|
|
2022-06-10 15:15:18 +00:00
|
|
|
if ( $doc.children().length ) {
|
2021-04-20 16:27:03 +00:00
|
|
|
this.$field.addClass( 've-ui-mwParameterPage-inlineDescription' );
|
2021-04-22 16:59:30 +00:00
|
|
|
this.collapsibleDoc = new ve.ui.MWExpandableContentElement( {
|
|
|
|
classes: [ 've-ui-mwParameterPage-inlineDescription' ],
|
|
|
|
$content: $doc
|
|
|
|
} );
|
|
|
|
this.$info.after( this.collapsibleDoc.$element );
|
2021-04-15 10:55:09 +00:00
|
|
|
}
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2014-02-24 21:49:48 +00:00
|
|
|
OO.inheritClass( ve.ui.MWParameterPage, OO.ui.PageLayout );
|
2013-12-02 20:10:55 +00:00
|
|
|
|
2022-06-15 14:47:54 +00:00
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2022-08-05 08:00:54 +00:00
|
|
|
* Triggered when the parameter value changes between empty and not empty.
|
2022-06-15 14:47:54 +00:00
|
|
|
*
|
|
|
|
* @event hasValueChange
|
|
|
|
* @param string parameterId Keyed by unique id of the parameter, e.g. something
|
|
|
|
* like "part_1/param1".
|
|
|
|
* @param boolean hasValue
|
|
|
|
*/
|
|
|
|
|
2013-12-02 20:10:55 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2014-10-29 21:50:14 +00:00
|
|
|
/**
|
|
|
|
* Get default configuration for an input widget.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
|
|
|
ve.ui.MWParameterPage.prototype.getDefaultInputConfig = function () {
|
2022-06-20 07:49:00 +00:00
|
|
|
var valueInputConfig = {
|
|
|
|
autosize: true,
|
|
|
|
required: this.parameter.isRequired()
|
|
|
|
};
|
2014-10-29 21:50:14 +00:00
|
|
|
|
|
|
|
if ( this.defaultValue ) {
|
|
|
|
valueInputConfig.placeholder = ve.msg(
|
|
|
|
'visualeditor-dialog-transclusion-param-default',
|
|
|
|
this.defaultValue
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return valueInputConfig;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a value input widget based on the parameter type and whether it is
|
|
|
|
* required or not.
|
|
|
|
*
|
2021-09-17 09:29:50 +00:00
|
|
|
* @private
|
2014-10-29 21:50:14 +00:00
|
|
|
* @return {OO.ui.InputWidget}
|
|
|
|
*/
|
|
|
|
ve.ui.MWParameterPage.prototype.createValueInput = function () {
|
|
|
|
var type = this.parameter.getType(),
|
|
|
|
value = this.parameter.getValue(),
|
2021-08-26 15:25:09 +00:00
|
|
|
valueInputConfig = this.getDefaultInputConfig();
|
2014-10-29 21:50:14 +00:00
|
|
|
|
|
|
|
// TODO:
|
|
|
|
// * date - T100206
|
|
|
|
// * number - T124850
|
|
|
|
// * unbalanced-wikitext/content - T106242
|
|
|
|
// * string? - T124917
|
|
|
|
if (
|
|
|
|
type === 'wiki-page-name' &&
|
|
|
|
( value === '' || mw.Title.newFromText( value ) )
|
|
|
|
) {
|
2021-11-15 17:25:51 +00:00
|
|
|
return new mw.widgets.TitleInputWidget( ve.extendObject( {
|
2020-08-31 15:19:36 +00:00
|
|
|
api: ve.init.target.getContentApi()
|
|
|
|
}, valueInputConfig ) );
|
2020-08-27 16:21:23 +00:00
|
|
|
} else if (
|
|
|
|
type === 'wiki-file-name' &&
|
|
|
|
( value === '' || mw.Title.newFromText( value ) )
|
|
|
|
) {
|
2021-11-15 17:25:51 +00:00
|
|
|
return new mw.widgets.TitleInputWidget( ve.extendObject( {}, valueInputConfig, {
|
2020-08-27 16:21:23 +00:00
|
|
|
api: ve.init.target.getContentApi(),
|
|
|
|
namespace: 6,
|
|
|
|
showImages: true
|
|
|
|
} ) );
|
2014-10-29 21:50:14 +00:00
|
|
|
} else if (
|
|
|
|
type === 'wiki-user-name' &&
|
|
|
|
( value === '' || mw.Title.newFromText( value ) )
|
|
|
|
) {
|
2020-08-18 12:16:49 +00:00
|
|
|
valueInputConfig.validate = function ( val ) {
|
2014-10-29 21:50:14 +00:00
|
|
|
// TODO: Check against wgMaxNameChars
|
2020-06-10 15:19:50 +00:00
|
|
|
// TODO: Check against unicode validation regex from MW core's User::isValidUserName
|
2020-08-18 12:16:49 +00:00
|
|
|
return !!mw.Title.newFromText( val );
|
2014-10-29 21:50:14 +00:00
|
|
|
};
|
2021-11-15 17:25:51 +00:00
|
|
|
return new mw.widgets.UserInputWidget( ve.extendObject( {
|
2020-08-31 15:19:36 +00:00
|
|
|
api: ve.init.target.getContentApi()
|
|
|
|
}, valueInputConfig ) );
|
2014-10-29 21:50:14 +00:00
|
|
|
} else if (
|
|
|
|
type === 'wiki-template-name' &&
|
|
|
|
( value === '' || mw.Title.newFromText( value ) )
|
|
|
|
) {
|
2021-11-15 17:25:51 +00:00
|
|
|
return new mw.widgets.TitleInputWidget( ve.extendObject( {
|
2018-05-04 13:30:10 +00:00
|
|
|
api: ve.init.target.getContentApi()
|
2020-08-31 15:19:36 +00:00
|
|
|
}, valueInputConfig, {
|
|
|
|
namespace: mw.config.get( 'wgNamespaceIds' ).template
|
2014-10-29 21:50:14 +00:00
|
|
|
} ) );
|
|
|
|
} else if ( type === 'boolean' && ( value === '1' || value === '0' ) ) {
|
|
|
|
return new ve.ui.MWParameterCheckboxInputWidget( valueInputConfig );
|
|
|
|
} else if (
|
|
|
|
type === 'url' &&
|
|
|
|
(
|
|
|
|
value === '' ||
|
|
|
|
ve.init.platform.getExternalLinkUrlProtocolsRegExp().exec( value.trim() )
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
return ve.ui.MWExternalLinkAnnotationWidget.static.createExternalLinkInputWidget( valueInputConfig );
|
2021-03-11 16:12:45 +00:00
|
|
|
} else if (
|
|
|
|
this.parameter.getSuggestedValues().length &&
|
|
|
|
this.isSuggestedValueType( type )
|
|
|
|
) {
|
2021-04-14 11:34:01 +00:00
|
|
|
valueInputConfig.menu = { filterFromInput: true, highlightOnFilter: true };
|
2021-03-11 16:12:45 +00:00
|
|
|
valueInputConfig.options =
|
2021-12-09 13:16:02 +00:00
|
|
|
this.parameter.getSuggestedValues().filter( function ( suggestedValue ) {
|
|
|
|
// This wasn't validated for a while, existing templates can do anything here
|
|
|
|
return typeof suggestedValue === 'string';
|
|
|
|
} ).map( function ( suggestedValue ) {
|
2022-02-28 07:47:27 +00:00
|
|
|
return { data: suggestedValue, label: suggestedValue || '\xA0' };
|
2021-03-11 16:12:45 +00:00
|
|
|
} );
|
|
|
|
return new OO.ui.ComboBoxInputWidget( valueInputConfig );
|
2018-09-12 17:01:03 +00:00
|
|
|
} else if ( type !== 'line' || value.indexOf( '\n' ) !== -1 ) {
|
|
|
|
// If the type is line, but there are already newlines in the provided
|
|
|
|
// value, don't break the existing content by only providing a single-
|
|
|
|
// line field. (This implies that the TemplateData for the field isn't
|
|
|
|
// complying with its use in practice...)
|
2018-08-07 11:39:14 +00:00
|
|
|
return new ve.ui.MWLazyMultilineTextInputWidget( valueInputConfig );
|
2014-10-29 21:50:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new OO.ui.TextInputWidget( valueInputConfig );
|
|
|
|
};
|
|
|
|
|
2021-03-11 16:12:45 +00:00
|
|
|
/**
|
|
|
|
* Whether or not to show suggested values for a given parameter type
|
|
|
|
*
|
2021-09-17 09:29:50 +00:00
|
|
|
* @private
|
2021-03-11 16:12:45 +00:00
|
|
|
* @param {string} type Parameter type
|
2021-03-01 17:02:38 +00:00
|
|
|
* @return {boolean} True if suggested values should be shown
|
2021-03-11 16:12:45 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWParameterPage.prototype.isSuggestedValueType = function ( type ) {
|
|
|
|
return [ 'unknown', 'content', 'line', 'string', 'number', 'unbalanced-wikitext' ].indexOf( type ) > -1;
|
|
|
|
};
|
|
|
|
|
2015-10-03 13:16:16 +00:00
|
|
|
/**
|
2021-08-03 12:44:48 +00:00
|
|
|
* @private
|
|
|
|
* @return {boolean} True if there is either user-provided input or a default value
|
2015-10-03 13:16:16 +00:00
|
|
|
*/
|
2021-08-03 12:44:48 +00:00
|
|
|
ve.ui.MWParameterPage.prototype.containsSomeValue = function () {
|
|
|
|
// Note: For templates that allow overriding a default value with nothing, the empty string is
|
|
|
|
// meaningful user input. For templates that don't, the parameter can never be truly empty.
|
2021-09-11 12:15:32 +00:00
|
|
|
return !!( this.valueInput.getValue() || this.defaultValue );
|
2014-02-26 00:29:13 +00:00
|
|
|
};
|
|
|
|
|
2015-10-03 13:16:16 +00:00
|
|
|
/**
|
|
|
|
* Handle change events from the value input
|
|
|
|
*
|
2021-09-17 09:29:50 +00:00
|
|
|
* @private
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {string} value
|
2015-10-03 13:16:16 +00:00
|
|
|
*/
|
2014-02-26 00:29:13 +00:00
|
|
|
ve.ui.MWParameterPage.prototype.onValueInputChange = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
var value = this.valueInput.getValue();
|
2014-02-26 00:29:13 +00:00
|
|
|
|
2020-11-25 13:51:30 +00:00
|
|
|
if ( !this.edited ) {
|
|
|
|
ve.track( 'activity.transclusion', { action: 'edit-parameter-value' } );
|
|
|
|
}
|
2014-10-29 21:50:14 +00:00
|
|
|
this.edited = true;
|
2014-02-26 00:29:13 +00:00
|
|
|
this.parameter.setValue( value );
|
2022-06-15 14:47:54 +00:00
|
|
|
|
2022-08-05 08:07:14 +00:00
|
|
|
if ( !!value !== this.hasValue ) {
|
|
|
|
this.hasValue = !!value;
|
|
|
|
this.emit( 'hasValueChange', this.parameter.getId(), this.hasValue );
|
2022-06-15 14:47:54 +00:00
|
|
|
}
|
2014-10-29 21:50:14 +00:00
|
|
|
};
|
|
|
|
|
2015-10-03 13:16:16 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWParameterPage.prototype.focus = function () {
|
|
|
|
this.valueInput.focus();
|
|
|
|
};
|
2021-04-22 16:59:30 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Refresh collapsible children.
|
|
|
|
*/
|
|
|
|
ve.ui.MWParameterPage.prototype.updateSize = function () {
|
|
|
|
if ( this.collapsibleDoc ) {
|
|
|
|
this.collapsibleDoc.updateSize();
|
|
|
|
}
|
|
|
|
};
|