2021-08-06 11:56:40 +00:00
|
|
|
/**
|
|
|
|
* Placeholder page for a currently unnamed parameter. Represented as a unnamed
|
|
|
|
* {@see ve.dm.MWParameterModel} in the corresponding {@see ve.dm.MWTemplateModel}.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.PageLayout
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWParameterModel} parameter Reference to a placeholder parameter with an empty
|
|
|
|
* name, as well as to the template the parameter belongs to
|
|
|
|
* @param {string} name Unique symbolic name of page
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
* @cfg {jQuery} [$overlay] Overlay to render dropdowns in
|
|
|
|
*/
|
|
|
|
ve.ui.MWAddParameterPage = function VeUiMWAddParameterPage( parameter, name, config ) {
|
|
|
|
// Parent constructor
|
2021-08-23 13:41:07 +00:00
|
|
|
ve.ui.MWAddParameterPage.super.call( this, name, ve.extendObject( {
|
|
|
|
scrollable: false
|
|
|
|
}, config ) );
|
2021-08-06 11:56:40 +00:00
|
|
|
|
2021-12-06 17:00:27 +00:00
|
|
|
this.template = parameter.getTemplate();
|
2021-08-06 11:56:40 +00:00
|
|
|
this.isExpanded = false;
|
|
|
|
|
|
|
|
// Header button to expand
|
|
|
|
this.addParameterInputHeader = new OO.ui.ButtonWidget( {
|
|
|
|
label: ve.msg( 'visualeditor-dialog-transclusion-add-undocumented-param' ),
|
|
|
|
icon: 'add',
|
|
|
|
framed: false,
|
|
|
|
inline: true,
|
|
|
|
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset-header' ]
|
|
|
|
} )
|
|
|
|
.connect( this, { click: 'togglePlaceholder' } );
|
|
|
|
|
2021-12-06 17:00:27 +00:00
|
|
|
this.addParameterFieldset = new OO.ui.FieldsetLayout( {
|
|
|
|
label: this.addParameterInputHeader.$element,
|
|
|
|
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset' ]
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Init visibility
|
|
|
|
this.togglePlaceholder( false );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.$element
|
|
|
|
.addClass( 've-ui-mwParameterPlaceholderPage' )
|
|
|
|
.append( this.addParameterFieldset.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWAddParameterPage, OO.ui.PageLayout );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.ui.MWAddParameterPage.prototype.initialize = function () {
|
|
|
|
this.template
|
|
|
|
.connect( this, {
|
|
|
|
// There is a "change" event, but it triggers way to often even for content changes
|
|
|
|
add: 'onTemplateParametersChanged',
|
|
|
|
remove: 'onTemplateParametersChanged'
|
|
|
|
} );
|
|
|
|
|
2021-08-31 10:18:19 +00:00
|
|
|
this.paramInputField = new OO.ui.TextInputWidget( {
|
|
|
|
placeholder: ve.msg( 'visualeditor-dialog-transclusion-add-param-placeholder' )
|
|
|
|
} )
|
2021-09-03 13:20:13 +00:00
|
|
|
.connect( this, {
|
2021-09-27 16:24:20 +00:00
|
|
|
change: 'updateParameterNameValidation',
|
2021-09-07 06:56:45 +00:00
|
|
|
enter: 'onParameterNameSubmitted'
|
2021-09-03 13:20:13 +00:00
|
|
|
} );
|
|
|
|
this.saveButton = new OO.ui.ButtonWidget( {
|
2021-08-06 11:56:40 +00:00
|
|
|
label: ve.msg( 'visualeditor-dialog-transclusion-add-param-save' ),
|
2021-08-31 16:01:11 +00:00
|
|
|
flags: [ 'primary', 'progressive' ],
|
|
|
|
disabled: true
|
2021-08-06 11:56:40 +00:00
|
|
|
} )
|
2021-08-31 16:01:11 +00:00
|
|
|
.connect( this, { click: 'onParameterNameSubmitted' } );
|
2021-08-06 11:56:40 +00:00
|
|
|
|
2021-09-14 15:15:55 +00:00
|
|
|
this.actionFieldLayout = new OO.ui.ActionFieldLayout(
|
2021-08-06 11:56:40 +00:00
|
|
|
this.paramInputField,
|
2021-09-03 13:20:13 +00:00
|
|
|
this.saveButton,
|
2022-01-13 14:18:40 +00:00
|
|
|
{
|
|
|
|
classes: [ 've-ui-mwTransclusionDialog-addParameterFieldset-input' ],
|
|
|
|
align: 'top'
|
|
|
|
}
|
2021-08-06 11:56:40 +00:00
|
|
|
);
|
|
|
|
|
2021-09-01 13:46:25 +00:00
|
|
|
var link = this.template.getTitle() || this.template.getTarget().wt;
|
2021-08-31 10:18:19 +00:00
|
|
|
var $helpText = mw.message(
|
|
|
|
'visualeditor-dialog-transclusion-add-param-help',
|
2021-09-01 13:46:25 +00:00
|
|
|
link
|
2021-08-31 10:18:19 +00:00
|
|
|
).parseDom();
|
2021-12-07 10:03:49 +00:00
|
|
|
ve.init.platform.linkCache.styleElement( link, $helpText.filter( 'a:not(.external)' ) );
|
2021-12-06 17:00:27 +00:00
|
|
|
|
|
|
|
// Copied from {@see OO.ui.FieldsetLayout} because there is no method to do this later
|
|
|
|
var helpWidget = new OO.ui.LabelWidget( {
|
|
|
|
label: $helpText,
|
|
|
|
classes: [ 'oo-ui-inline-help' ]
|
2021-08-06 11:56:40 +00:00
|
|
|
} );
|
|
|
|
|
2021-12-06 17:00:27 +00:00
|
|
|
ve.targetLinksToNewWindow( helpWidget.$element[ 0 ] );
|
2021-08-31 10:18:19 +00:00
|
|
|
|
2021-12-06 17:00:27 +00:00
|
|
|
this.addParameterFieldset.$element.append(
|
|
|
|
helpWidget.$element,
|
|
|
|
this.addParameterFieldset.$group,
|
|
|
|
this.actionFieldLayout.$element
|
|
|
|
);
|
2021-08-06 11:56:40 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 19:16:42 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.PanelLayout
|
|
|
|
*/
|
|
|
|
ve.ui.MWAddParameterPage.prototype.focus = function () {
|
|
|
|
if ( this.isExpanded ) {
|
|
|
|
this.paramInputField.focus();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWAddParameterPage.super.prototype.focus.apply( this, arguments );
|
|
|
|
};
|
2021-09-14 15:10:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
ve.ui.MWAddParameterPage.prototype.onTemplateParametersChanged = function () {
|
2021-09-27 16:24:20 +00:00
|
|
|
this.updateParameterNameValidation( this.paramInputField.getValue() );
|
2021-09-14 15:10:41 +00:00
|
|
|
};
|
2021-09-10 19:16:42 +00:00
|
|
|
|
2021-09-03 13:20:13 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {string} value
|
|
|
|
*/
|
2021-09-27 16:24:20 +00:00
|
|
|
ve.ui.MWAddParameterPage.prototype.updateParameterNameValidation = function ( value ) {
|
2021-09-03 13:20:13 +00:00
|
|
|
var paramName = value.trim(),
|
2021-08-31 16:01:11 +00:00
|
|
|
errors = this.getValidationErrors( paramName );
|
|
|
|
|
2021-09-14 15:15:55 +00:00
|
|
|
this.actionFieldLayout.setErrors( errors );
|
2021-09-03 15:37:30 +00:00
|
|
|
this.saveButton.setDisabled( !paramName || errors.length );
|
2021-09-03 13:20:13 +00:00
|
|
|
};
|
|
|
|
|
2021-09-10 19:16:42 +00:00
|
|
|
/**
|
|
|
|
* @private
|
2021-09-30 11:04:30 +00:00
|
|
|
* @fires focusTemplateParameterById
|
2021-09-10 19:16:42 +00:00
|
|
|
*/
|
2021-09-07 06:56:45 +00:00
|
|
|
ve.ui.MWAddParameterPage.prototype.onParameterNameSubmitted = function () {
|
2021-08-06 11:56:40 +00:00
|
|
|
var name = this.paramInputField.getValue().trim();
|
2021-09-03 13:20:13 +00:00
|
|
|
if ( !name || this.saveButton.isDisabled() ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-06 11:56:40 +00:00
|
|
|
this.paramInputField.setValue( '' );
|
|
|
|
|
2021-09-03 13:20:13 +00:00
|
|
|
if ( this.template.hasParameter( name ) ) {
|
2021-08-06 11:56:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-10 14:20:51 +00:00
|
|
|
var param = new ve.dm.MWParameterModel( this.template, name );
|
|
|
|
this.template.addParameter( param );
|
2021-09-07 15:53:37 +00:00
|
|
|
this.emit( 'focusTemplateParameterById', param.getId() );
|
2021-08-06 11:56:40 +00:00
|
|
|
|
|
|
|
ve.track( 'activity.transclusion', {
|
|
|
|
action: 'add-unknown-parameter'
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-09-10 19:16:42 +00:00
|
|
|
/**
|
|
|
|
* @private
|
2021-09-03 14:26:43 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
2021-09-10 19:16:42 +00:00
|
|
|
* @return {jQuery[]} An array as accepted by {@see OO.ui.FieldLayout.setErrors}
|
|
|
|
*/
|
2021-08-31 16:01:11 +00:00
|
|
|
ve.ui.MWAddParameterPage.prototype.getValidationErrors = function ( name ) {
|
|
|
|
if ( !name ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-09-03 15:37:30 +00:00
|
|
|
var forbiddenCharacter = name.match( /[={|}]/ );
|
|
|
|
if ( forbiddenCharacter ) {
|
|
|
|
return [ mw.message( 'visualeditor-dialog-transclusion-add-param-error-forbidden-char',
|
|
|
|
forbiddenCharacter[ 0 ] ).parseDom() ];
|
|
|
|
}
|
|
|
|
|
2021-08-31 16:01:11 +00:00
|
|
|
var key,
|
|
|
|
spec = this.template.getSpec();
|
|
|
|
|
|
|
|
if ( spec.getParameterAliases( name ).indexOf( name ) !== -1 ) {
|
|
|
|
key = 'visualeditor-dialog-transclusion-add-param-error-alias';
|
|
|
|
} else if ( this.template.hasParameter( name ) ) {
|
|
|
|
key = 'visualeditor-dialog-transclusion-add-param-error-exists-selected';
|
|
|
|
} else if ( spec.isParameterDeprecated( name ) ) {
|
|
|
|
key = 'visualeditor-dialog-transclusion-add-param-error-deprecated';
|
|
|
|
} else if ( spec.isKnownParameterOrAlias( name ) ) {
|
|
|
|
key = 'visualeditor-dialog-transclusion-add-param-error-exists-unselected';
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !key ) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-09-03 14:26:43 +00:00
|
|
|
var label = spec.getParameterLabel( this.template.getOriginalParameterName( name ) ),
|
2021-08-31 16:01:11 +00:00
|
|
|
// eslint-disable-next-line mediawiki/msg-doc
|
|
|
|
$msg = mw.message( key, name, label ).parseDom();
|
|
|
|
ve.targetLinksToNewWindow( $( '<div>' ).append( $msg )[ 0 ] );
|
|
|
|
return [ $msg ];
|
|
|
|
};
|
|
|
|
|
2021-08-06 11:56:40 +00:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
* @param {boolean} [expand]
|
|
|
|
*/
|
|
|
|
ve.ui.MWAddParameterPage.prototype.togglePlaceholder = function ( expand ) {
|
|
|
|
this.isExpanded = expand === undefined ? !this.isExpanded : !!expand;
|
|
|
|
|
|
|
|
this.addParameterInputHeader.setIcon( this.isExpanded ? 'subtract' : 'add' );
|
|
|
|
this.addParameterFieldset.$element.toggleClass(
|
|
|
|
've-ui-mwTransclusionDialog-addParameterFieldset-collapsed',
|
|
|
|
!this.isExpanded
|
|
|
|
);
|
2021-08-30 15:03:30 +00:00
|
|
|
if ( this.isExpanded ) {
|
2021-12-06 17:00:27 +00:00
|
|
|
if ( !this.paramInputField ) {
|
|
|
|
this.initialize();
|
|
|
|
}
|
2021-08-30 15:03:30 +00:00
|
|
|
this.paramInputField.focus();
|
|
|
|
}
|
2021-08-06 11:56:40 +00:00
|
|
|
};
|
2021-08-27 16:22:37 +00:00
|
|
|
|
2021-09-10 19:16:42 +00:00
|
|
|
/**
|
|
|
|
* @inheritDoc OO.ui.PageLayout
|
|
|
|
*/
|
2021-09-15 10:10:56 +00:00
|
|
|
ve.ui.MWAddParameterPage.prototype.setupOutlineItem = function () {
|
2021-09-13 15:09:42 +00:00
|
|
|
this.outlineItem
|
|
|
|
// Basic properties to make the OO.ui.OutlineControlsWidget buttons behave sane
|
|
|
|
.setMovable( false )
|
|
|
|
.setRemovable( false )
|
|
|
|
.setLevel( 1 )
|
|
|
|
// This page should not be shown in the (BookletLayout-based) sidebar
|
|
|
|
.$element.empty().removeAttr( 'class' );
|
2021-08-27 16:22:37 +00:00
|
|
|
};
|