2013-06-11 19:16:04 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor DataModel MWTemplateModel class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2013-06-11 19:16:04 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-06-04 12:43:21 +00:00
|
|
|
* Represents a template invocation that's part of a (possibly unbalanced) sequence of template
|
2021-06-03 16:25:15 +00:00
|
|
|
* invocations and raw wikitext snippets. Meant to be an item in a {@see ve.dm.MWTransclusionModel}.
|
2021-06-18 08:44:32 +00:00
|
|
|
* Holds a back-reference to its parent.
|
2021-06-03 16:25:15 +00:00
|
|
|
*
|
|
|
|
* Holds a reference to the specification of the template, i.e. how the template is documented via
|
|
|
|
* TemplateData. The actual invocation might be entirely different, missing parameters as well as
|
|
|
|
* containing undocumented ones.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.dm.MWTransclusionPartModel
|
|
|
|
*
|
|
|
|
* @constructor
|
2021-06-04 11:58:18 +00:00
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion
|
2013-06-11 19:16:04 +00:00
|
|
|
* @param {Object} target Template target
|
2021-07-05 09:55:11 +00:00
|
|
|
* @param {string} target.wt Template name as originally used in the wikitext, including optional
|
|
|
|
* whitespace
|
|
|
|
* @param {string} [target.href] Hypertext reference to target, e.g. "./Template:Example"
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-12-02 20:10:55 +00:00
|
|
|
ve.dm.MWTemplateModel = function VeDmMWTemplateModel( transclusion, target ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.dm.MWTemplateModel.super.call( this, transclusion );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.target = target;
|
2013-10-09 21:20:51 +00:00
|
|
|
|
|
|
|
// TODO: Either here or in uses of this constructor we need to validate the title
|
2020-03-20 17:08:24 +00:00
|
|
|
this.title = target.href ? mw.libs.ve.normalizeParsoidResourceName( target.href ) : null;
|
2021-06-04 12:04:10 +00:00
|
|
|
this.orderedParameterNames = null;
|
2013-06-11 19:16:04 +00:00
|
|
|
this.params = {};
|
|
|
|
this.spec = new ve.dm.MWTemplateSpecModel( this );
|
2013-07-12 00:23:33 +00:00
|
|
|
this.originalData = null;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2013-10-11 21:44:09 +00:00
|
|
|
OO.inheritClass( ve.dm.MWTemplateModel, ve.dm.MWTransclusionPartModel );
|
2013-06-11 19:16:04 +00:00
|
|
|
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
/**
|
2021-07-15 15:07:21 +00:00
|
|
|
* Emitted when a new parameter was added to the template.
|
|
|
|
*
|
2013-06-11 19:16:04 +00:00
|
|
|
* @event add
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Added param
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-07-15 15:07:21 +00:00
|
|
|
* Emitted when a parameter was removed from the template.
|
|
|
|
*
|
2013-06-11 19:16:04 +00:00
|
|
|
* @event remove
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Removed param
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
|
2021-07-15 15:07:21 +00:00
|
|
|
/**
|
|
|
|
* Emitted when anything changed, e.g. a parameter was added or removed, or a parameter's value
|
|
|
|
* edited.
|
|
|
|
*
|
|
|
|
* @event change
|
|
|
|
*/
|
|
|
|
|
2013-07-12 00:23:33 +00:00
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create from data.
|
|
|
|
*
|
|
|
|
* Data is in the format provided by Parsoid.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion template is in
|
|
|
|
* @param {Object} data Template data
|
2021-07-05 09:55:11 +00:00
|
|
|
* @param {Object.<string,{wt:string}>} data.params
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateModel} New template model
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.newFromData = function ( transclusion, data ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var template = new ve.dm.MWTemplateModel( transclusion, data.target );
|
2013-07-12 00:23:33 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var key in data.params ) {
|
2013-07-12 00:23:33 +00:00
|
|
|
template.addParameter(
|
2015-08-19 17:33:02 +00:00
|
|
|
new ve.dm.MWParameterModel( template, key, data.params[ key ].wt )
|
2013-07-12 00:23:33 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
template.setOriginalData( data );
|
|
|
|
|
|
|
|
return template;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create from name.
|
|
|
|
*
|
|
|
|
* Name is equivalent to what would be entered between double brackets, defaulting to the Template
|
|
|
|
* namespace, using a leading colon to access other namespaces.
|
|
|
|
*
|
|
|
|
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion template is in
|
2014-10-30 16:24:04 +00:00
|
|
|
* @param {string|mw.Title} name Template name
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateModel|null} New template model
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.newFromName = function ( transclusion, name ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var title,
|
2014-11-10 20:03:10 +00:00
|
|
|
templateNs = mw.config.get( 'wgNamespaceIds' ).template;
|
2014-10-30 16:24:04 +00:00
|
|
|
if ( name instanceof mw.Title ) {
|
|
|
|
title = name;
|
2014-11-10 20:03:10 +00:00
|
|
|
name = title.getRelativeText( templateNs );
|
2014-10-30 16:24:04 +00:00
|
|
|
} else {
|
2014-11-10 20:03:10 +00:00
|
|
|
title = mw.Title.newFromText( name, templateNs );
|
2014-10-30 16:24:04 +00:00
|
|
|
}
|
|
|
|
if ( title !== null ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
var href = title.getPrefixedText();
|
2015-01-11 17:38:36 +00:00
|
|
|
return new ve.dm.MWTemplateModel( transclusion, { href: href, wt: name } );
|
2014-10-30 16:24:04 +00:00
|
|
|
}
|
2013-07-12 00:23:33 +00:00
|
|
|
|
2014-10-30 16:24:04 +00:00
|
|
|
return null;
|
2013-07-12 00:23:33 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {Object} Template target
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getTarget = function () {
|
|
|
|
return this.target;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-08-26 13:34:39 +00:00
|
|
|
* @return {string|null} Prefixed template title including the "Template:" namespace, if available.
|
|
|
|
* Use {@see ve.dm.MWTemplateSpecModel.getLabel} for a human-readable label without the namespace.
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getTitle = function () {
|
|
|
|
return this.title;
|
|
|
|
};
|
|
|
|
|
2021-09-17 14:13:19 +00:00
|
|
|
/**
|
|
|
|
* @return {string|null} Prefixed page name including the `Template:` namespace, but with syntax
|
|
|
|
* elements like `subst:` stripped.
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getTemplateDataQueryTitle = function () {
|
|
|
|
// FIXME: This currently doesn't strip localized versions of these magic words.
|
|
|
|
// Strip magic words {{subst:…}} and {{safesubst:…}}, see MagicWordFactory::$mSubstIDs
|
|
|
|
var name = this.target.wt.replace( /^\s*(?:safe)?subst:/i, '' ),
|
|
|
|
templateNs = mw.config.get( 'wgNamespaceIds' ).template,
|
|
|
|
title = mw.Title.newFromText( name, templateNs );
|
|
|
|
return title ? title.getPrefixedText() : this.getTitle();
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWTemplateSpecModel} Template specification
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getSpec = function () {
|
|
|
|
return this.spec;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-28 10:53:45 +00:00
|
|
|
* Get all parameters that are currently present in this template invocation in the order as they
|
|
|
|
* originally appear in the wikitext. This is critical for {@see serialize}. Might contain
|
|
|
|
* placeholders with the parameter name "".
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2021-06-28 10:53:45 +00:00
|
|
|
* @return {Object.<string,ve.dm.MWParameterModel>} Parameters keyed by name or alias
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameters = function () {
|
|
|
|
return this.params;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-09-03 14:26:43 +00:00
|
|
|
* @param {string} name Parameter name or alias as originally used in the wikitext
|
2021-06-18 08:44:32 +00:00
|
|
|
* @return {ve.dm.MWParameterModel|undefined}
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameter = function ( name ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
return this.params[ name ];
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2013-07-10 19:04:11 +00:00
|
|
|
/**
|
2021-07-05 09:55:11 +00:00
|
|
|
* Check if a parameter with this name or one of its aliases is currently part of this template.
|
2013-07-10 19:04:11 +00:00
|
|
|
*
|
2021-07-05 09:55:11 +00:00
|
|
|
* @param {string} name Parameter name or alias
|
|
|
|
* @return {boolean} Parameter is in the template
|
2013-07-10 19:04:11 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.hasParameter = function ( name ) {
|
2021-09-03 14:26:43 +00:00
|
|
|
return this.getOriginalParameterName( name ) in this.params;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} name Parameter name or alias
|
|
|
|
* @return {string} Parameter name or alias as originally used in the wikitext
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getOriginalParameterName = function ( name ) {
|
|
|
|
if ( name in this.params ) {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
var aliases = this.spec.getParameterAliases( name );
|
|
|
|
// FIXME: Should use .filter() when we dropped IE11 support
|
|
|
|
for ( var i = 0; i < aliases.length; i++ ) {
|
|
|
|
if ( aliases[ i ] in this.params ) {
|
|
|
|
return aliases[ i ];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return this.spec.getPrimaryParameterName( name );
|
2013-07-10 19:04:11 +00:00
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
2021-07-07 14:28:13 +00:00
|
|
|
* Get all current and potential parameter names in a canonical order that's always the same,
|
|
|
|
* unrelated to how the parameters appear in the wikitext. Parameter names and aliases documented
|
|
|
|
* via TemplateData are first, in their documented order. Undocumented parameters are sorted with
|
|
|
|
* numeric names first, followed by alphabetically sorted names. The unnamed placeholder parameter
|
2021-08-03 08:24:24 +00:00
|
|
|
* is last.
|
2021-06-18 14:09:52 +00:00
|
|
|
*
|
|
|
|
* @return {string[]}
|
2021-06-18 08:44:32 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getAllParametersOrdered = function () {
|
2021-06-28 10:37:01 +00:00
|
|
|
var spec = this.spec,
|
2021-07-07 14:28:13 +00:00
|
|
|
parameters = spec.getCanonicalParameterOrder();
|
2021-06-18 08:44:32 +00:00
|
|
|
|
2021-07-07 14:28:13 +00:00
|
|
|
// Restore aliases originally used in the wikitext. The spec doesn't know which alias was used.
|
2021-08-03 08:24:24 +00:00
|
|
|
for ( var name in this.params ) {
|
2021-06-28 10:37:01 +00:00
|
|
|
if ( spec.isParameterAlias( name ) ) {
|
2021-07-07 14:28:13 +00:00
|
|
|
parameters.splice(
|
2021-06-28 10:37:01 +00:00
|
|
|
// This can never fail because only documented parameters can have aliases
|
2021-07-07 14:28:13 +00:00
|
|
|
parameters.indexOf( spec.getPrimaryParameterName( name ) ),
|
2021-06-28 10:37:01 +00:00
|
|
|
1,
|
|
|
|
name
|
|
|
|
);
|
|
|
|
}
|
2021-08-03 08:24:24 +00:00
|
|
|
}
|
2021-07-07 14:28:13 +00:00
|
|
|
|
|
|
|
// Restore the placeholder, if present. The spec doesn't keep track of placeholders.
|
2021-08-03 08:24:24 +00:00
|
|
|
if ( '' in this.params ) {
|
2021-07-07 14:28:13 +00:00
|
|
|
parameters.push( '' );
|
|
|
|
}
|
2021-06-28 10:37:01 +00:00
|
|
|
|
2021-06-18 08:44:32 +00:00
|
|
|
// TODO: cache results
|
2021-07-07 14:28:13 +00:00
|
|
|
return parameters;
|
2021-06-18 08:44:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-28 10:53:45 +00:00
|
|
|
* Returns the same parameters as {@see getParameters}, i.e. parameters that are currently present
|
|
|
|
* in this template invocation, but sorted in a canonical order for presentational purposes.
|
|
|
|
*
|
|
|
|
* Don't use this if you need the parameters as they originally appear in the wikitext, or if you
|
|
|
|
* don't care about an order. Use {@see getParameters} together with `Object.keys()` instead.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2021-06-28 10:53:45 +00:00
|
|
|
* @return {string[]} Sorted list of parameter names
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2021-06-04 12:04:10 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.getOrderedParameterNames = function () {
|
|
|
|
if ( !this.orderedParameterNames ) {
|
2021-07-07 14:28:13 +00:00
|
|
|
var params = this.params;
|
2021-06-18 08:44:32 +00:00
|
|
|
this.orderedParameterNames = this.getAllParametersOrdered().filter( function ( name ) {
|
2021-07-07 14:28:13 +00:00
|
|
|
return name in params;
|
2013-06-11 19:16:04 +00:00
|
|
|
} );
|
|
|
|
}
|
2021-06-04 12:04:10 +00:00
|
|
|
return this.orderedParameterNames;
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
2014-03-01 00:39:02 +00:00
|
|
|
/**
|
|
|
|
* Get parameter from its ID.
|
|
|
|
*
|
2021-06-18 13:51:25 +00:00
|
|
|
* @private
|
2014-03-01 00:39:02 +00:00
|
|
|
* @param {string} id Parameter ID
|
2015-08-19 18:09:34 +00:00
|
|
|
* @return {ve.dm.MWParameterModel|null} Parameter with matching ID, null if no parameters match
|
2014-03-01 00:39:02 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.getParameterFromId = function ( id ) {
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var name in this.params ) {
|
2015-08-19 17:33:02 +00:00
|
|
|
if ( this.params[ name ].getId() === id ) {
|
|
|
|
return this.params[ name ];
|
2014-03-01 00:39:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
2013-06-11 19:16:04 +00:00
|
|
|
/**
|
|
|
|
* Add a parameter to template.
|
|
|
|
*
|
2014-02-24 21:49:48 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} param Parameter to add
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires add
|
2021-06-25 14:46:59 +00:00
|
|
|
* @fires change
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.addParameter = function ( param ) {
|
2021-08-13 14:41:48 +00:00
|
|
|
var name = param.getName();
|
|
|
|
if ( name in this.params ) {
|
|
|
|
return;
|
2021-07-13 19:01:30 +00:00
|
|
|
}
|
2021-08-13 14:41:48 +00:00
|
|
|
|
|
|
|
this.orderedParameterNames = null;
|
|
|
|
this.params[ name ] = param;
|
|
|
|
this.spec.fillFromTemplate();
|
2021-08-20 12:52:39 +00:00
|
|
|
// This forwards change events from the nested ve.dm.MWParameterModel upwards. The array
|
2021-08-13 14:41:48 +00:00
|
|
|
// syntax is a way to call `this.emit( 'change' )`.
|
|
|
|
param.connect( this, { change: [ 'emit', 'change' ] } );
|
2013-06-11 19:16:04 +00:00
|
|
|
this.emit( 'add', param );
|
2021-08-13 14:41:48 +00:00
|
|
|
this.emit( 'change' );
|
2013-06-11 19:16:04 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2021-06-17 16:32:26 +00:00
|
|
|
* Remove a parameter from this MWTemplateModel, and emit events which result in removing the
|
|
|
|
* parameter from the UI. Note this does *not* remove the parameter from the linked specification.
|
2013-06-11 19:16:04 +00:00
|
|
|
*
|
2021-06-04 11:46:16 +00:00
|
|
|
* @param {ve.dm.MWParameterModel} [param]
|
2013-10-22 17:54:59 +00:00
|
|
|
* @fires remove
|
2021-06-17 16:32:26 +00:00
|
|
|
* @fires change
|
2013-06-11 19:16:04 +00:00
|
|
|
*/
|
2013-06-29 02:37:42 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.removeParameter = function ( param ) {
|
2013-06-11 19:16:04 +00:00
|
|
|
if ( param ) {
|
2021-06-04 12:04:10 +00:00
|
|
|
this.orderedParameterNames = null;
|
2015-08-19 17:33:02 +00:00
|
|
|
delete this.params[ param.getName() ];
|
2013-12-09 19:05:57 +00:00
|
|
|
param.disconnect( this );
|
2013-06-11 19:16:04 +00:00
|
|
|
this.emit( 'remove', param );
|
2013-12-09 19:05:57 +00:00
|
|
|
this.emit( 'change' );
|
2013-06-11 19:16:04 +00:00
|
|
|
}
|
|
|
|
};
|
2013-07-12 00:23:33 +00:00
|
|
|
|
|
|
|
/**
|
2016-05-13 19:43:47 +00:00
|
|
|
* @inheritdoc
|
2013-12-02 20:10:55 +00:00
|
|
|
*/
|
2014-04-28 19:10:31 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.addPromptedParameters = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
var addedCount = 0,
|
2021-06-08 13:09:52 +00:00
|
|
|
params = this.params,
|
2021-07-02 15:30:54 +00:00
|
|
|
spec = this.spec,
|
2021-06-28 10:53:45 +00:00
|
|
|
names = spec.getKnownParameterNames();
|
2013-12-02 20:10:55 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var i = 0; i < names.length; i++ ) {
|
|
|
|
var name = names[ i ];
|
|
|
|
var foundAlias = spec.getParameterAliases( name ).some( function ( alias ) {
|
2021-06-08 13:09:52 +00:00
|
|
|
return alias in params;
|
|
|
|
} );
|
2014-04-28 19:10:31 +00:00
|
|
|
if (
|
2016-09-30 00:57:23 +00:00
|
|
|
!foundAlias &&
|
2021-06-08 13:09:52 +00:00
|
|
|
!params[ name ] &&
|
2016-06-30 15:46:38 +00:00
|
|
|
(
|
|
|
|
spec.isParameterRequired( name ) ||
|
|
|
|
spec.isParameterSuggested( name )
|
|
|
|
)
|
|
|
|
) {
|
2015-08-19 17:33:02 +00:00
|
|
|
this.addParameter( new ve.dm.MWParameterModel( this, names[ i ] ) );
|
2014-07-21 21:06:54 +00:00
|
|
|
addedCount++;
|
2013-12-02 20:10:55 +00:00
|
|
|
}
|
|
|
|
}
|
2014-07-21 21:06:54 +00:00
|
|
|
|
|
|
|
return addedCount;
|
2013-12-02 20:10:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set original data, to be used as a base for serialization.
|
2016-10-28 19:02:36 +00:00
|
|
|
*
|
2021-06-18 13:51:25 +00:00
|
|
|
* @private
|
2016-10-28 19:02:36 +00:00
|
|
|
* @param {Object} data Original data
|
2021-07-05 09:55:11 +00:00
|
|
|
* @param {Object.<string,Object>} [data.params]
|
2013-07-12 00:23:33 +00:00
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.setOriginalData = function ( data ) {
|
|
|
|
this.originalData = data;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.dm.MWTemplateModel.prototype.serialize = function () {
|
2021-06-21 08:10:27 +00:00
|
|
|
var origData = this.originalData || {},
|
2014-11-10 20:00:34 +00:00
|
|
|
origParams = origData.params || {},
|
2021-07-02 15:30:54 +00:00
|
|
|
template = { target: this.target, params: {} },
|
|
|
|
spec = this.spec,
|
|
|
|
params = this.params;
|
2013-07-12 00:23:33 +00:00
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
for ( var name in params ) {
|
2014-03-20 05:27:59 +00:00
|
|
|
if ( name === '' ) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-11-16 18:43:00 +00:00
|
|
|
|
2021-03-12 21:50:42 +00:00
|
|
|
if (
|
|
|
|
// Don't add empty parameters (T101075)
|
|
|
|
params[ name ].getValue() === '' &&
|
|
|
|
// …unless they were present before the edit
|
|
|
|
!Object.prototype.hasOwnProperty.call( origParams, name ) &&
|
|
|
|
// …unless they are required (T276989)
|
2021-07-07 08:18:41 +00:00
|
|
|
!spec.isParameterRequired( name )
|
2021-03-12 21:50:42 +00:00
|
|
|
) {
|
2020-11-16 18:43:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-06-21 08:10:27 +00:00
|
|
|
var origName = params[ name ].getOriginalName();
|
2015-08-19 17:33:02 +00:00
|
|
|
template.params[ origName ] = ve.extendObject(
|
2014-11-10 20:00:34 +00:00
|
|
|
{},
|
2015-08-19 17:33:02 +00:00
|
|
|
origParams[ origName ],
|
|
|
|
{ wt: params[ name ].getValue() }
|
2014-11-10 20:00:34 +00:00
|
|
|
);
|
|
|
|
|
2013-07-12 00:23:33 +00:00
|
|
|
}
|
|
|
|
|
2014-11-10 20:00:34 +00:00
|
|
|
// Performs a non-deep extend, so this won't reintroduce
|
2018-08-09 14:10:53 +00:00
|
|
|
// deleted parameters (T75134)
|
2014-11-10 20:00:34 +00:00
|
|
|
template = ve.extendObject( {}, origData, template );
|
2014-08-22 20:50:48 +00:00
|
|
|
return { template: template };
|
2013-07-12 00:23:33 +00:00
|
|
|
};
|
2013-12-09 19:05:57 +00:00
|
|
|
|
2021-05-17 11:26:11 +00:00
|
|
|
/**
|
2021-06-18 13:51:25 +00:00
|
|
|
* @inheritdoc
|
2021-05-17 11:26:11 +00:00
|
|
|
*/
|
2021-08-03 13:24:36 +00:00
|
|
|
ve.dm.MWTemplateModel.prototype.containsValuableData = function () {
|
2021-07-02 15:30:54 +00:00
|
|
|
var params = this.params;
|
2021-05-17 11:26:11 +00:00
|
|
|
|
2021-08-03 13:24:36 +00:00
|
|
|
return Object.keys( params ).some( function ( name ) {
|
|
|
|
// Skip unnamed placeholders
|
2021-05-17 11:26:11 +00:00
|
|
|
if ( !name ) {
|
2021-08-03 13:24:36 +00:00
|
|
|
return false;
|
2021-05-17 11:26:11 +00:00
|
|
|
}
|
|
|
|
|
2021-06-04 12:04:10 +00:00
|
|
|
var param = params[ name ],
|
|
|
|
value = param.getValue();
|
2021-09-08 13:46:36 +00:00
|
|
|
return value &&
|
2021-08-03 13:45:43 +00:00
|
|
|
// This will automatically be restored, see {@see ve.dm.MWParameterModel.getValue}
|
|
|
|
value !== param.getAutoValue() &&
|
|
|
|
// While this isn't always meaningless, it typically is, and it's easy to restore
|
|
|
|
value !== param.getDefaultValue();
|
2021-05-17 11:26:11 +00:00
|
|
|
} );
|
|
|
|
};
|