mediawiki-extensions-Visual.../modules/ve-mw/dm/models/ve.dm.MWTemplateModel.js

400 lines
12 KiB
JavaScript
Raw Normal View History

/*!
* VisualEditor DataModel MWTemplateModel class.
*
* @copyright See AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* Represents a template invocation that's part of a (possibly unbalanced) sequence of template
* invocations and raw wikitext snippets. Meant to be an item in a {@see ve.dm.MWTransclusionModel}.
* Holds a back-reference to its parent.
*
* 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.
*
* @class
* @extends ve.dm.MWTransclusionPartModel
*
* @constructor
* @param {ve.dm.MWTransclusionModel} transclusion
* @param {Object} target Template target
* @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"
*/
Refactor Transclusion and Meta dialogs to use BookletLayout Use OOJS-UI's newly-extended paged dialogs (in e08eb2a03b) to refactor how the Transclusion and Meta dialogs work, splitting out the code for each of the panels into its own file and simplifying extensibility. The Meta dialog (ve.ui.MWMetaDialog) now has two self-managing panels: * ve.ui.MWCategoriesPage for categories and the default sort key * ve.ui.MWLanguagesPage for language links The Transclusion dialog (ve.ui.MWTransclusionDialog) now has four: * ve.ui.MWTemplatePage for a template's primary panel * ve.ui.MWTemplateParameterPage for each parameter of a template * ve.ui.MWTemplatePlaceholderPage for a placeholder to insert a template * ve.ui.MWTransclusionContentPage for non-template transclusion Additionally, the Transclusion dialog has been slightly cleaned up: * Replace add/remove events with replace events in transclusion model * Actually return and resolve a promise (as documented) * Get rid of "origin" info in template models * Add method for adding required parts TODO: * Decide how and when we will choose between advanced transclusion and template dialogs * Work out design issues with how template descriptions will be visible and how adding parameters will work if only showing parameters in outline * Add preview to template dialog * Consider ways to further improve pages for use in continuous mode WARNING: * Right now the template dialog gets overridden by the advanced transclusion dialog because they have the same symbolic name and the latter is registered later than the former. To test the template dialog, just change the symbolic name of the advanced transclusion dialog. Change-Id: I51e74b322aec9a4c3918e6f792bdb3d318060979
2013-12-02 20:10:55 +00:00
ve.dm.MWTemplateModel = function VeDmMWTemplateModel( transclusion, target ) {
// Parent constructor
ve.dm.MWTemplateModel.super.call( this, transclusion );
// Properties
this.target = target;
// TODO: Either here or in uses of this constructor we need to validate the title
this.title = target.href ? mw.libs.ve.normalizeParsoidResourceName( target.href ) : null;
this.orderedParameterNames = null;
this.params = {};
this.spec = new ve.dm.MWTemplateSpecModel( this );
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
this.originalData = null;
};
/* Inheritance */
OO.inheritClass( ve.dm.MWTemplateModel, ve.dm.MWTransclusionPartModel );
/* Events */
/**
* Emitted when a new parameter was added to the template.
*
* @event ve.dm.MWTemplateModel#add
* @param {ve.dm.MWParameterModel} param Added param
*/
/**
* Emitted when a parameter was removed from the template.
*
* @event ve.dm.MWTemplateModel#remove
* @param {ve.dm.MWParameterModel} param Removed param
*/
/**
* Emitted when anything changed, e.g. a parameter was added or removed, or a parameter's value
* edited.
*
* @event ve.dm.MWTemplateModel#change
*/
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
/* Static Methods */
/**
* Data is in the format provided by Parsoid.
*
* @param {ve.dm.MWTransclusionModel} transclusion Transclusion template is in
* @param {Object} data Template data
* @param {Object} data.params
* @param {string} data.params.wt Wikitext
* @return {ve.dm.MWTemplateModel} New template model
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
*/
ve.dm.MWTemplateModel.newFromData = function ( transclusion, data ) {
const template = new ve.dm.MWTemplateModel( transclusion, data.target );
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
for ( const key in data.params ) {
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
template.addParameter(
new ve.dm.MWParameterModel( template, key, data.params[ key ].wt )
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
);
}
template.setOriginalData( data );
return template;
};
/**
* 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
* @param {string|mw.Title} name Template name
* @return {ve.dm.MWTemplateModel|null} New template model
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
*/
ve.dm.MWTemplateModel.newFromName = function ( transclusion, name ) {
const templateNs = mw.config.get( 'wgNamespaceIds' ).template;
let title;
if ( name instanceof mw.Title ) {
title = name;
name = title.getRelativeText( templateNs );
} else {
title = mw.Title.newFromText( name, templateNs );
}
if ( title !== null ) {
const href = title.getPrefixedText();
return new ve.dm.MWTemplateModel( transclusion, { href: href, wt: name } );
}
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
return null;
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
};
/* Methods */
/**
* @return {Object} Template target
*/
ve.dm.MWTemplateModel.prototype.getTarget = function () {
return this.target;
};
/**
* @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.
*/
ve.dm.MWTemplateModel.prototype.getTitle = function () {
return this.title;
};
/**
* @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:…}}
const name = this.target.wt.trim().replace( /^(?:safe)?subst:/i, '' ),
templateNs = mw.config.get( 'wgNamespaceIds' ).template,
title = mw.Title.newFromText( name, templateNs );
return title ? title.getPrefixedText() : this.getTitle();
};
/**
* @return {ve.dm.MWTemplateSpecModel} Template specification
*/
ve.dm.MWTemplateModel.prototype.getSpec = function () {
return this.spec;
};
/**
* 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 "".
*
* @return {Object.<string,ve.dm.MWParameterModel>} Parameters keyed by name or alias
*/
ve.dm.MWTemplateModel.prototype.getParameters = function () {
return this.params;
};
/**
* @param {string} name Parameter name or alias as originally used in the wikitext
* @return {ve.dm.MWParameterModel|undefined}
*/
ve.dm.MWTemplateModel.prototype.getParameter = function ( name ) {
return this.params[ name ];
};
/**
* Check if a parameter with this name or one of its aliases is currently part of this template.
*
* @param {string} name Parameter name or alias
* @return {boolean} Parameter is in the template
*/
ve.dm.MWTemplateModel.prototype.hasParameter = function ( name ) {
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;
}
const aliases = this.spec.getParameterAliases( name );
// FIXME: Should use .filter() when we dropped IE11 support
for ( let i = 0; i < aliases.length; i++ ) {
if ( aliases[ i ] in this.params ) {
return aliases[ i ];
}
}
return this.spec.getPrimaryParameterName( name );
};
/**
* 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
* is last.
*
* @return {string[]}
*/
ve.dm.MWTemplateModel.prototype.getAllParametersOrdered = function () {
const spec = this.spec,
usedAliases = {};
let primaryName;
for ( const alias in this.params ) {
if ( spec.isParameterAlias( alias ) ) {
primaryName = spec.getPrimaryParameterName( alias );
if ( !usedAliases[ primaryName ] ) {
// Skip primary name when it's not used, otherwise move it to the front
usedAliases[ primaryName ] = primaryName in this.params ? [ primaryName ] : [];
}
// Append aliases in their original order (not documented order)
usedAliases[ primaryName ].push( alias );
}
}
let parameters = spec.getCanonicalParameterOrder();
// Restore aliases originally used in the wikitext. The spec doesn't know which alias was used.
for ( primaryName in usedAliases ) {
const i = parameters.indexOf( primaryName );
// TODO: parameters.splice( i, 1, ...usedAliases[ primaryName ] ) when we can use ES6
parameters = parameters.slice( 0, i ).concat( usedAliases[ primaryName ],
parameters.slice( i + 1 ) );
}
// Restore the placeholder, if present. The spec doesn't keep track of placeholders.
if ( '' in this.params ) {
parameters.push( '' );
}
// TODO: cache results
return parameters;
};
/**
* 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.
*
* @return {string[]} Sorted list of parameter names
*/
ve.dm.MWTemplateModel.prototype.getOrderedParameterNames = function () {
if ( !this.orderedParameterNames ) {
const params = this.params;
this.orderedParameterNames = this.getAllParametersOrdered().filter( ( name ) => name in params );
}
return this.orderedParameterNames;
};
/**
* @param {ve.dm.MWParameterModel} param Parameter to add
* @fires ve.dm.MWTemplateModel#add
* @fires ve.dm.MWTemplateModel#change
*/
ve.dm.MWTemplateModel.prototype.addParameter = function ( param ) {
const name = param.getName();
if ( name in this.params ) {
return;
}
this.orderedParameterNames = null;
this.params[ name ] = param;
this.spec.fillFromTemplate();
// This forwards change events from the nested ve.dm.MWParameterModel upwards. The array
// syntax is a way to call `this.emit( 'change' )`.
param.connect( this, { change: [ 'emit', 'change' ] } );
this.emit( 'add', param );
this.emit( 'change' );
};
/**
* 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.
*
* @param {ve.dm.MWParameterModel} [param]
* @fires ve.dm.MWTemplateModel#remove
* @fires ve.dm.MWTemplateModel#change
*/
ve.dm.MWTemplateModel.prototype.removeParameter = function ( param ) {
if ( param ) {
this.orderedParameterNames = null;
delete this.params[ param.getName() ];
param.disconnect( this );
this.emit( 'remove', param );
this.emit( 'change' );
}
};
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
/**
* Add all non-existing required and suggested parameters, if any.
Refactor Transclusion and Meta dialogs to use BookletLayout Use OOJS-UI's newly-extended paged dialogs (in e08eb2a03b) to refactor how the Transclusion and Meta dialogs work, splitting out the code for each of the panels into its own file and simplifying extensibility. The Meta dialog (ve.ui.MWMetaDialog) now has two self-managing panels: * ve.ui.MWCategoriesPage for categories and the default sort key * ve.ui.MWLanguagesPage for language links The Transclusion dialog (ve.ui.MWTransclusionDialog) now has four: * ve.ui.MWTemplatePage for a template's primary panel * ve.ui.MWTemplateParameterPage for each parameter of a template * ve.ui.MWTemplatePlaceholderPage for a placeholder to insert a template * ve.ui.MWTransclusionContentPage for non-template transclusion Additionally, the Transclusion dialog has been slightly cleaned up: * Replace add/remove events with replace events in transclusion model * Actually return and resolve a promise (as documented) * Get rid of "origin" info in template models * Add method for adding required parts TODO: * Decide how and when we will choose between advanced transclusion and template dialogs * Work out design issues with how template descriptions will be visible and how adding parameters will work if only showing parameters in outline * Add preview to template dialog * Consider ways to further improve pages for use in continuous mode WARNING: * Right now the template dialog gets overridden by the advanced transclusion dialog because they have the same symbolic name and the latter is registered later than the former. To test the template dialog, just change the symbolic name of the advanced transclusion dialog. Change-Id: I51e74b322aec9a4c3918e6f792bdb3d318060979
2013-12-02 20:10:55 +00:00
*/
ve.dm.MWTemplateModel.prototype.addPromptedParameters = function () {
const params = this.params,
spec = this.spec,
names = spec.getKnownParameterNames();
Refactor Transclusion and Meta dialogs to use BookletLayout Use OOJS-UI's newly-extended paged dialogs (in e08eb2a03b) to refactor how the Transclusion and Meta dialogs work, splitting out the code for each of the panels into its own file and simplifying extensibility. The Meta dialog (ve.ui.MWMetaDialog) now has two self-managing panels: * ve.ui.MWCategoriesPage for categories and the default sort key * ve.ui.MWLanguagesPage for language links The Transclusion dialog (ve.ui.MWTransclusionDialog) now has four: * ve.ui.MWTemplatePage for a template's primary panel * ve.ui.MWTemplateParameterPage for each parameter of a template * ve.ui.MWTemplatePlaceholderPage for a placeholder to insert a template * ve.ui.MWTransclusionContentPage for non-template transclusion Additionally, the Transclusion dialog has been slightly cleaned up: * Replace add/remove events with replace events in transclusion model * Actually return and resolve a promise (as documented) * Get rid of "origin" info in template models * Add method for adding required parts TODO: * Decide how and when we will choose between advanced transclusion and template dialogs * Work out design issues with how template descriptions will be visible and how adding parameters will work if only showing parameters in outline * Add preview to template dialog * Consider ways to further improve pages for use in continuous mode WARNING: * Right now the template dialog gets overridden by the advanced transclusion dialog because they have the same symbolic name and the latter is registered later than the former. To test the template dialog, just change the symbolic name of the advanced transclusion dialog. Change-Id: I51e74b322aec9a4c3918e6f792bdb3d318060979
2013-12-02 20:10:55 +00:00
for ( let i = 0; i < names.length; i++ ) {
const name = names[ i ];
const foundAlias = spec.getParameterAliases( name ).some( ( alias ) => alias in params );
if (
!foundAlias &&
!params[ name ] &&
(
spec.isParameterRequired( name ) ||
spec.isParameterSuggested( name )
)
) {
this.addParameter( new ve.dm.MWParameterModel( this, names[ i ] ) );
Refactor Transclusion and Meta dialogs to use BookletLayout Use OOJS-UI's newly-extended paged dialogs (in e08eb2a03b) to refactor how the Transclusion and Meta dialogs work, splitting out the code for each of the panels into its own file and simplifying extensibility. The Meta dialog (ve.ui.MWMetaDialog) now has two self-managing panels: * ve.ui.MWCategoriesPage for categories and the default sort key * ve.ui.MWLanguagesPage for language links The Transclusion dialog (ve.ui.MWTransclusionDialog) now has four: * ve.ui.MWTemplatePage for a template's primary panel * ve.ui.MWTemplateParameterPage for each parameter of a template * ve.ui.MWTemplatePlaceholderPage for a placeholder to insert a template * ve.ui.MWTransclusionContentPage for non-template transclusion Additionally, the Transclusion dialog has been slightly cleaned up: * Replace add/remove events with replace events in transclusion model * Actually return and resolve a promise (as documented) * Get rid of "origin" info in template models * Add method for adding required parts TODO: * Decide how and when we will choose between advanced transclusion and template dialogs * Work out design issues with how template descriptions will be visible and how adding parameters will work if only showing parameters in outline * Add preview to template dialog * Consider ways to further improve pages for use in continuous mode WARNING: * Right now the template dialog gets overridden by the advanced transclusion dialog because they have the same symbolic name and the latter is registered later than the former. To test the template dialog, just change the symbolic name of the advanced transclusion dialog. Change-Id: I51e74b322aec9a4c3918e6f792bdb3d318060979
2013-12-02 20:10:55 +00:00
}
}
};
/**
* Set original data, to be used as a base for serialization.
*
* @private
* @param {Object} data Original data
* @param {Object.<string,Object>} [data.params]
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
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 () {
const origData = this.originalData || {},
origParams = origData.params || {},
template = { target: this.target, params: {} },
spec = this.spec,
params = this.params;
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
for ( const name in params ) {
if ( name === '' ) {
continue;
}
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)
!spec.isParameterRequired( name )
) {
continue;
}
const origName = params[ name ].getOriginalName();
template.params[ origName ] = ve.extendObject(
{},
origParams[ origName ],
{ wt: params[ name ].getValue() }
);
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
}
// Performs a non-deep extend, so this won't reintroduce
// deleted parameters (T75134)
return { template: ve.extendObject( {}, origData, template ) };
Preserve unused Parsoid template properties Problem: Parsoid has a property called "i" which we don't use, but they need for round-tripping purposes. Since we were generating a structure from Parsoid data and then generating data from the structure without preserving properties we didn't use, it was getting lost. Solution: Abstract creating a template from data vs. creating it from name. Make only templates have an origin argument in their constructors, so and set it within a set of static constructors that create a template for either data or a template name. Store the original data in the former case, and use it as a base when serializing. Changes: ve.ui.MWTranslcusionDialog.js * Remove no-longer-needed mw global declaration * Move most of the addTemplate function to a static constructor in the template model class ve.dm.MWTransclusionPartModel.js, ve.dm.MWTransclusionContentModel.js, ve.dm.MWTemplatePlaceholder * Remove unused origin argument/property/getter * Add serialize method (if needed) ve.dm.MWTranclusionModel.js * Move template/parameter generation from data into static constructor of template model * Move serialization to part classes ve.dm.MWTemplateModel.js * Add mw global declaration * Stop passing origin to parent constructor, store it locally instead * Add original data property/setter for preserving unused properties when round tripping * Add static constructors for generating a template from data or by name * Add serialize method Bug: 51150 Change-Id: Ide596a0ca0ae8f93ffce6e79b7234a1db7e0586c
2013-07-12 00:23:33 +00:00
};
/**
* @inheritdoc
*/
ve.dm.MWTemplateModel.prototype.containsValuableData = function () {
const params = this.params;
return Object.keys( params ).some( ( name ) => {
// Skip unnamed placeholders
if ( !name ) {
return false;
}
const param = params[ name ],
value = param.getValue();
return value &&
// 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();
} );
};