2021-05-17 15:43:49 +00:00
|
|
|
/*!
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Container for template, as rendered in the template dialog sidebar.
|
|
|
|
*
|
|
|
|
* @class
|
2021-07-09 15:04:14 +00:00
|
|
|
* @extends ve.ui.MWTransclusionOutlinePartWidget
|
2021-05-17 15:43:49 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
2021-07-09 15:33:16 +00:00
|
|
|
* @param {ve.dm.MWTemplateModel} template
|
2021-05-17 15:43:49 +00:00
|
|
|
*/
|
2021-07-13 09:00:57 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget = function VeUiMWTransclusionOutlineTemplateWidget( template ) {
|
2021-08-04 14:34:54 +00:00
|
|
|
var spec = template.getSpec();
|
|
|
|
|
2021-07-13 09:00:57 +00:00
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.super.call( this, template, {
|
2021-07-09 14:47:01 +00:00
|
|
|
icon: 'puzzle',
|
2021-08-04 14:34:54 +00:00
|
|
|
label: spec.getLabel()
|
2021-07-13 09:00:57 +00:00
|
|
|
} );
|
2021-05-17 15:43:49 +00:00
|
|
|
|
|
|
|
// Initialization
|
2021-07-09 15:33:16 +00:00
|
|
|
this.templateModel = template.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 ) {
|
2021-08-04 14:34:54 +00:00
|
|
|
if ( spec.isParameterDeprecated( paramName ) && !template.hasParameter( paramName ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
2021-06-29 19:15:08 +00:00
|
|
|
// Don't create a checkbox for ve.ui.MWParameterPlaceholderPage
|
2021-07-13 19:01:30 +00:00
|
|
|
return paramName;
|
2021-06-29 19:15:08 +00:00
|
|
|
} )
|
|
|
|
.map( function ( paramName ) {
|
|
|
|
return widget.createCheckbox( paramName );
|
|
|
|
} );
|
2021-06-21 14:19:03 +00:00
|
|
|
|
2021-07-20 10:36:23 +00:00
|
|
|
this.searchWidget = new OO.ui.SearchInputWidget( {
|
|
|
|
placeholder: ve.msg( 'visualeditor-dialog-transclusion-filter-placeholder' ),
|
|
|
|
classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-searchWidget' ]
|
|
|
|
} ).connect( this, {
|
2021-08-06 13:05:54 +00:00
|
|
|
change: 'filterParameters'
|
2021-07-20 10:36:23 +00:00
|
|
|
} ).toggle( false );
|
|
|
|
this.infoWidget = new OO.ui.LabelWidget( {
|
|
|
|
label: new OO.ui.HtmlSnippet( ve.msg( 'visualeditor-dialog-transclusion-filter-no-match' ) ),
|
|
|
|
classes: [ 've-ui-mwTransclusionOutlineTemplateWidget-no-match' ]
|
|
|
|
} ).toggle( false );
|
2021-07-12 15:00:04 +00:00
|
|
|
|
2021-07-20 10:36:23 +00:00
|
|
|
this.parameters = new OO.ui.FieldsetLayout()
|
|
|
|
.connect( this, { change: 'onCheckboxListChange' } );
|
|
|
|
this.parameters.addItems( checkboxes );
|
|
|
|
|
|
|
|
this.$element.append(
|
|
|
|
this.searchWidget.$element,
|
|
|
|
this.infoWidget.$element,
|
2021-08-18 07:22:43 +00:00
|
|
|
this.parameters.$element
|
2021-07-20 10:36:23 +00:00
|
|
|
);
|
2021-05-17 15:43:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2021-07-09 15:04:14 +00:00
|
|
|
OO.inheritClass( ve.ui.MWTransclusionOutlineTemplateWidget, ve.ui.MWTransclusionOutlinePartWidget );
|
2021-06-17 11:20:50 +00:00
|
|
|
|
2021-08-05 12:36:10 +00:00
|
|
|
/* Events */
|
|
|
|
|
2021-08-13 14:41:48 +00:00
|
|
|
/**
|
|
|
|
* @event selectParameter
|
|
|
|
* @param {string} paramId Unique id of the parameter, e.g. something like "part_1/param1"
|
|
|
|
*/
|
|
|
|
|
2021-08-05 12:36:10 +00:00
|
|
|
/**
|
|
|
|
* Triggered when the user uses the search widget at the top to filter the list of parameters.
|
|
|
|
*
|
2021-08-11 06:06:22 +00:00
|
|
|
* @event filterParameters
|
|
|
|
* @param {Object.<string,boolean>} visibility Keyed by unique id of the parameter, e.g. something
|
|
|
|
* like "part_1/param1". Note this lists only parameters that are currently in use.
|
2021-08-05 12:36:10 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {string} paramName
|
2021-06-28 10:37:01 +00:00
|
|
|
* @return {ve.ui.MWTemplateOutlineParameterCheckboxLayout}
|
2021-06-29 19:45:09 +00:00
|
|
|
*/
|
2021-07-13 19:01:30 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.createCheckbox = function ( paramName ) {
|
|
|
|
var spec = this.templateModel.getSpec();
|
2021-06-18 15:23:35 +00:00
|
|
|
|
2021-06-17 11:20:50 +00:00
|
|
|
return new ve.ui.MWTemplateOutlineParameterCheckboxLayout( {
|
2021-07-13 19:01:30 +00:00
|
|
|
required: spec.isParameterRequired( paramName ),
|
|
|
|
label: spec.getParameterLabel( paramName ),
|
|
|
|
data: paramName,
|
|
|
|
selected: this.templateModel.hasParameter( paramName )
|
2021-06-18 15:23:35 +00:00
|
|
|
} ).connect( this, {
|
2021-08-17 06:35:13 +00:00
|
|
|
parameterSelectionChanged: 'onParameterSelectionChanged',
|
|
|
|
parameterFocused: 'onParameterFocused'
|
2021-06-17 11:20:50 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
2021-08-06 13:05:54 +00:00
|
|
|
/**
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-08-06 13:05:54 +00:00
|
|
|
* @param {ve.ui.MWTemplateOutlineParameterCheckboxLayout} checkbox
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.insertCheckboxAtCanonicalPosition = function ( checkbox ) {
|
|
|
|
var paramName = checkbox.getData(),
|
|
|
|
insertAt = 0,
|
|
|
|
// Note this might include parameters that don't have a checkbox, e.g. deprecated
|
|
|
|
allParamNames = this.templateModel.getAllParametersOrdered();
|
|
|
|
for ( var i = 0; i < allParamNames.length; i++ ) {
|
|
|
|
if ( allParamNames[ i ] === paramName || !this.parameters.items[ insertAt ] ) {
|
|
|
|
break;
|
|
|
|
} else if ( this.parameters.items[ insertAt ].getData() === allParamNames[ i ] ) {
|
|
|
|
insertAt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.parameters.addItems( [ checkbox ], insertAt );
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param
|
2021-06-29 19:45:09 +00:00
|
|
|
*/
|
2021-07-13 19:01:30 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onAddParameter = function ( param ) {
|
2021-08-05 10:11:40 +00:00
|
|
|
var paramName = param.getName();
|
|
|
|
// The placeholder (currently) doesn't get a corresponding item in the sidebar
|
|
|
|
if ( !paramName ) {
|
|
|
|
return;
|
|
|
|
}
|
2021-07-13 19:01:30 +00:00
|
|
|
|
|
|
|
// All parameters known via the spec already have a checkbox
|
2021-08-05 10:11:40 +00:00
|
|
|
var checkbox = this.parameters.findItemFromData( paramName );
|
|
|
|
if ( !checkbox ) {
|
|
|
|
checkbox = this.createCheckbox( paramName );
|
2021-08-06 13:05:54 +00:00
|
|
|
this.insertCheckboxAtCanonicalPosition( checkbox );
|
2021-08-05 10:11:40 +00:00
|
|
|
// Make sure an active filter is applied to the new checkbox as well
|
|
|
|
var filter = this.searchWidget.getValue();
|
|
|
|
if ( filter ) {
|
2021-08-06 13:05:54 +00:00
|
|
|
this.filterParameters( filter );
|
2021-08-05 10:11:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkbox.setSelected( true, true );
|
|
|
|
|
|
|
|
// Reset filter, but only if it hides the relevant checkbox
|
|
|
|
if ( !checkbox.isVisible() ) {
|
2021-07-20 10:36:23 +00:00
|
|
|
this.searchWidget.setValue( '' );
|
2021-06-18 15:23:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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.
|
|
|
|
*
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param
|
2021-06-29 19:45:09 +00:00
|
|
|
*/
|
2021-07-13 19:01:30 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onRemoveParameter = function ( param ) {
|
|
|
|
var checkbox = this.parameters.findItemFromData( param.getName() );
|
|
|
|
if ( checkbox ) {
|
|
|
|
checkbox.setSelected( false, true );
|
2021-06-18 15:23:35 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-29 19:45:09 +00:00
|
|
|
/**
|
|
|
|
* Handles a parameter checkbox change event {@see ve.ui.MWTemplateOutlineParameterCheckboxLayout}
|
|
|
|
*
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {string} paramName
|
2021-08-17 06:35:13 +00:00
|
|
|
* @param {boolean} selected
|
2021-06-29 19:45:09 +00:00
|
|
|
*/
|
2021-08-17 06:35:13 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterSelectionChanged = function ( paramName, selected ) {
|
2021-07-13 19:01:30 +00:00
|
|
|
var param = this.templateModel.getParameter( paramName );
|
2021-08-17 06:35:13 +00:00
|
|
|
if ( !selected ) {
|
2021-07-13 19:01:30 +00:00
|
|
|
this.templateModel.removeParameter( param );
|
|
|
|
} else if ( !param ) {
|
|
|
|
this.templateModel.addParameter( new ve.dm.MWParameterModel( this.templateModel, paramName ) );
|
|
|
|
}
|
|
|
|
};
|
2021-06-18 15:23:35 +00:00
|
|
|
|
2021-07-13 19:01:30 +00:00
|
|
|
/**
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-13 19:01:30 +00:00
|
|
|
* @param {string} paramName
|
2021-08-13 14:41:48 +00:00
|
|
|
* @fires selectParameter
|
2021-07-13 19:01:30 +00:00
|
|
|
*/
|
2021-08-17 06:35:13 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onParameterFocused = function ( paramName ) {
|
2021-07-13 19:01:30 +00:00
|
|
|
var param = this.templateModel.getParameter( paramName );
|
|
|
|
if ( param ) {
|
2021-08-13 14:41:48 +00:00
|
|
|
this.emit( 'selectParameter', param.getId() );
|
2021-06-18 15:23:35 +00:00
|
|
|
}
|
2021-06-17 11:20:50 +00:00
|
|
|
};
|
2021-07-13 10:33:52 +00:00
|
|
|
|
2021-07-20 10:36:23 +00:00
|
|
|
/**
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-20 10:36:23 +00:00
|
|
|
* @param {OO.ui.Element[]} items
|
|
|
|
*/
|
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.onCheckboxListChange = function ( items ) {
|
|
|
|
this.searchWidget.toggle( items.length >= 1 );
|
|
|
|
};
|
|
|
|
|
2021-07-12 15:00:04 +00:00
|
|
|
/**
|
2021-07-14 07:31:42 +00:00
|
|
|
* Narrows the list of checkboxes down to parameters that match the user's input. The search
|
|
|
|
* algorithm is modelled after {@see ve.ui.MWParameterSearchWidget.buildIndex}. We search the
|
|
|
|
* parameter's primary name, aliases, label, and description. But not e.g. the example value.
|
2021-07-12 15:00:04 +00:00
|
|
|
*
|
2021-08-13 14:41:48 +00:00
|
|
|
* @private
|
2021-07-14 07:31:42 +00:00
|
|
|
* @param {string} query user input
|
2021-08-11 06:06:22 +00:00
|
|
|
* @fires filterParameters
|
2021-07-12 15:00:04 +00:00
|
|
|
*/
|
2021-08-06 13:05:54 +00:00
|
|
|
ve.ui.MWTransclusionOutlineTemplateWidget.prototype.filterParameters = function ( query ) {
|
2021-08-05 12:36:10 +00:00
|
|
|
var self = this,
|
|
|
|
template = this.templateModel,
|
|
|
|
spec = this.templateModel.getSpec(),
|
2021-08-11 06:06:22 +00:00
|
|
|
visibility = {},
|
2021-07-14 07:31:42 +00:00
|
|
|
nothingFound = true;
|
|
|
|
|
|
|
|
query = query.trim().toLowerCase();
|
|
|
|
|
|
|
|
// Note: We can't really cache this because the list of know parameters can change any time
|
2021-08-05 10:11:40 +00:00
|
|
|
this.parameters.items.forEach( function ( checkbox ) {
|
|
|
|
var paramName = checkbox.getData(),
|
|
|
|
placesToSearch = [
|
|
|
|
spec.getPrimaryParameterName( paramName ),
|
|
|
|
spec.getParameterLabel( paramName ),
|
|
|
|
spec.getParameterDescription( paramName )
|
|
|
|
].concat( spec.getParameterAliases( paramName ) );
|
2021-07-14 07:31:42 +00:00
|
|
|
|
|
|
|
var foundSomeMatch = placesToSearch.some( function ( term ) {
|
|
|
|
return term && term.toLowerCase().indexOf( query ) !== -1;
|
|
|
|
} );
|
2021-07-12 15:00:04 +00:00
|
|
|
|
2021-07-14 07:31:42 +00:00
|
|
|
checkbox.toggle( foundSomeMatch );
|
2021-08-11 06:06:22 +00:00
|
|
|
|
2021-07-14 07:31:42 +00:00
|
|
|
nothingFound = nothingFound && !foundSomeMatch;
|
2021-08-05 12:36:10 +00:00
|
|
|
|
|
|
|
var param = template.getParameter( paramName );
|
|
|
|
if ( param ) {
|
2021-08-11 06:06:22 +00:00
|
|
|
visibility[ param.getId() ] = foundSomeMatch;
|
2021-08-05 12:36:10 +00:00
|
|
|
}
|
2021-07-12 15:00:04 +00:00
|
|
|
} );
|
|
|
|
|
2021-07-14 07:31:42 +00:00
|
|
|
this.infoWidget.toggle( nothingFound );
|
2021-08-11 06:06:22 +00:00
|
|
|
self.emit( 'filterParameters', visibility );
|
2021-07-12 15:00:04 +00:00
|
|
|
};
|