2021-05-17 15:43:49 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor user interface MWTemplateOutlineTemplateWidget class.
|
|
|
|
*
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for template, as rendered in the template dialog sidebar.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends OO.ui.Widget
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget = function VeUiMWTemplateOutlineTemplateWidget( config ) {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget.super.call( this, config );
|
|
|
|
|
|
|
|
// Initialization
|
2021-06-17 11:20:50 +00:00
|
|
|
this.templateModel = config.templateModel.connect( this, {
|
2021-06-18 15:23:35 +00:00
|
|
|
add: 'onAddParameter',
|
|
|
|
remove: 'onRemoveParameter'
|
2021-06-17 11:20:50 +00:00
|
|
|
} );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
2021-06-21 14:19:03 +00:00
|
|
|
var widget = this;
|
2021-06-29 19:15:08 +00:00
|
|
|
var checkboxes = this.templateModel
|
|
|
|
.getAllParametersOrdered()
|
|
|
|
.filter( function ( paramName ) {
|
|
|
|
// Don't create a checkbox for ve.ui.MWParameterPlaceholderPage
|
|
|
|
return paramName !== '';
|
|
|
|
} )
|
|
|
|
.map( function ( paramName ) {
|
|
|
|
return widget.createCheckbox( paramName );
|
|
|
|
} );
|
2021-06-21 14:19:03 +00:00
|
|
|
|
|
|
|
var addParameterButton = new OO.ui.ButtonWidget( {
|
|
|
|
framed: false,
|
|
|
|
icon: 'parameter',
|
|
|
|
label: ve.msg( 'visualeditor-dialog-transclusion-add-param' ),
|
|
|
|
classes: [ 've-ui-templateOutlineItem' ]
|
|
|
|
} );
|
|
|
|
|
2021-06-21 10:35:50 +00:00
|
|
|
var templateLabel = new OO.ui.Layout( {
|
|
|
|
classes: [ 've-ui-templateOutlineTemplateLabel' ],
|
|
|
|
content: [
|
|
|
|
new OO.ui.IconWidget( {
|
|
|
|
icon: 'puzzle'
|
|
|
|
} ),
|
|
|
|
new OO.ui.LabelWidget( {
|
|
|
|
label: config.templateModel.getSpec().getLabel()
|
|
|
|
} )
|
|
|
|
]
|
|
|
|
} );
|
|
|
|
|
2021-06-21 09:20:03 +00:00
|
|
|
this.parameters = new OO.ui.FieldsetLayout( {
|
2021-06-17 11:20:50 +00:00
|
|
|
items: checkboxes
|
2021-05-17 15:43:49 +00:00
|
|
|
} );
|
|
|
|
var layout = new OO.ui.Layout( {
|
|
|
|
// TODO: template title and icon
|
2021-06-18 15:23:35 +00:00
|
|
|
// items: [ this.parameters ]
|
2021-05-17 15:43:49 +00:00
|
|
|
} );
|
|
|
|
layout.$element
|
2021-06-21 10:35:50 +00:00
|
|
|
.append( templateLabel.$element, this.parameters.$element, addParameterButton.$element );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
|
|
|
this.$element
|
|
|
|
.append( layout.$element )
|
|
|
|
.addClass( 've-ui-mwTemplateDialogOutlineTemplate' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWTemplateOutlineTemplateWidget, OO.ui.Widget );
|
2021-06-17 11:20:50 +00:00
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* @param {string|ve.dm.MWParameterModel} parameter
|
|
|
|
* @returns {ve.ui.MWTemplateOutlineParameterCheckboxLayout}
|
|
|
|
*/
|
2021-06-18 15:23:35 +00:00
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget.prototype.createCheckbox = function ( parameter ) {
|
2021-06-29 18:44:17 +00:00
|
|
|
var templateSpec = this.templateModel.getSpec(),
|
|
|
|
parameterModel = ( parameter instanceof ve.dm.MWParameterModel ) ?
|
2021-06-18 15:23:35 +00:00
|
|
|
parameter : this.templateModel.getParameter( parameter ),
|
|
|
|
isPresent = !!parameterModel;
|
|
|
|
|
2021-06-17 11:20:50 +00:00
|
|
|
if ( !parameterModel ) {
|
|
|
|
// TODO: Streamline, don't create a temporary parameter model?
|
2021-06-18 15:23:35 +00:00
|
|
|
parameterModel = new ve.dm.MWParameterModel( this.templateModel, parameter );
|
2021-06-17 11:20:50 +00:00
|
|
|
}
|
|
|
|
return new ve.ui.MWTemplateOutlineParameterCheckboxLayout( {
|
|
|
|
required: parameterModel.isRequired(),
|
2021-06-29 18:44:17 +00:00
|
|
|
label: templateSpec.getParameterLabel( parameterModel.getName() ),
|
2021-06-18 15:23:35 +00:00
|
|
|
data: parameterModel.getName(),
|
2021-06-17 11:20:50 +00:00
|
|
|
selected: isPresent
|
2021-06-18 15:23:35 +00:00
|
|
|
} ).connect( this, {
|
|
|
|
change: 'onCheckboxChange'
|
2021-06-17 11:20:50 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a template model add event {@see ve.dm.MWTemplateModel}.
|
|
|
|
* Triggered when a parameter is added to the template model.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWParameterModel} parameter
|
|
|
|
*/
|
2021-06-18 15:23:35 +00:00
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget.prototype.onAddParameter = function ( parameter ) {
|
2021-06-29 19:15:08 +00:00
|
|
|
var paramName = parameter.getName(),
|
|
|
|
paramCheckbox = this.parameters.findItemFromData( paramName );
|
|
|
|
|
|
|
|
if ( paramName === '' ) {
|
|
|
|
// Don't create a checkbox for ve.ui.MWParameterPlaceholderPage
|
|
|
|
return;
|
|
|
|
}
|
2021-06-18 15:23:35 +00:00
|
|
|
|
|
|
|
if ( !paramCheckbox ) {
|
|
|
|
this.parameters.addItems(
|
|
|
|
this.createCheckbox( parameter ),
|
2021-06-29 19:15:08 +00:00
|
|
|
this.templateModel.getAllParametersOrdered().indexOf( paramName )
|
2021-06-18 15:23:35 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
paramCheckbox.setSelected( true, true );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a template model remove event {@see ve.dm.MWTemplateModel}.
|
|
|
|
* Triggered when a parameter is removed from the template model.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWParameterModel} parameter
|
|
|
|
*/
|
2021-06-18 15:23:35 +00:00
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget.prototype.onRemoveParameter = function ( parameter ) {
|
|
|
|
var paramCheckbox = this.parameters.findItemFromData( parameter.getName() );
|
|
|
|
|
|
|
|
if ( paramCheckbox ) {
|
2021-06-28 09:14:18 +00:00
|
|
|
if ( !this.templateModel.isParameterDocumented( parameter ) ) {
|
2021-06-18 15:23:35 +00:00
|
|
|
paramCheckbox.disconnect( this );
|
|
|
|
this.parameters.removeItems( [ paramCheckbox ] );
|
|
|
|
} else {
|
|
|
|
paramCheckbox.setSelected( false, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a parameter checkbox change event {@see ve.ui.MWTemplateOutlineParameterCheckboxLayout}
|
|
|
|
*
|
|
|
|
* @param {string} data Parameter name
|
|
|
|
* @param {boolean} checked New checkbox state
|
|
|
|
*/
|
2021-06-18 15:23:35 +00:00
|
|
|
ve.ui.MWTemplateOutlineTemplateWidget.prototype.onCheckboxChange = function ( data, checked ) {
|
|
|
|
var parameter = this.templateModel.getParameter( data );
|
|
|
|
|
|
|
|
if ( checked ) {
|
|
|
|
parameter = parameter || new ve.dm.MWParameterModel( this.templateModel, data );
|
|
|
|
this.templateModel.addParameter( parameter );
|
|
|
|
} else {
|
|
|
|
if ( parameter ) {
|
|
|
|
this.templateModel.removeParameter( parameter );
|
|
|
|
}
|
|
|
|
}
|
2021-06-17 11:20:50 +00:00
|
|
|
};
|