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

409 lines
12 KiB
JavaScript
Raw Normal View History

/*!
* VisualEditor DataModel MWTemplateModel class.
*
* @copyright 2011-2020 VisualEditor Team and others; 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 add
* @param {ve.dm.MWParameterModel} param Added param
*/
/**
* Emitted when a parameter was removed from the template.
*
* @event 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 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 */
/**
* 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
* @param {Object.<string,{wt:string}>} data.params
* @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 ) {
var 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 ( var 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;
};
/**
* 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
* @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 ) {
var title,
templateNs = mw.config.get( 'wgNamespaceIds' ).template;
if ( name instanceof mw.Title ) {
title = name;
name = title.getRelativeText( templateNs );
} else {
title = mw.Title.newFromText( name, templateNs );
}
if ( title !== null ) {
var 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} Template title, if available
*/
ve.dm.MWTemplateModel.prototype.getTitle = function () {
return this.title;
};
/**
* @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
* @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 ) {
var params = this.params;
// Check for `name` (which may be an alias), its primary name, and all of its aliases
return name in params ||
this.spec.getPrimaryParameterName( name ) in params ||
this.spec.getParameterAliases( name ).some( function ( alias ) {
return alias in params;
} );
};
/**
* 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. Parameters that are deprecated and do not have a value are filtered out.
*
* @return {string[]}
*/
ve.dm.MWTemplateModel.prototype.getAllParametersOrdered = function () {
var spec = this.spec,
params = this.params,
parameters = spec.getCanonicalParameterOrder();
// Restore aliases originally used in the wikitext. The spec doesn't know which alias was used.
Object.keys( params ).forEach( function ( name ) {
if ( spec.isParameterAlias( name ) ) {
parameters.splice(
// This can never fail because only documented parameters can have aliases
parameters.indexOf( spec.getPrimaryParameterName( name ) ),
1,
name
);
}
} );
// Restore the placeholder, if present. The spec doesn't keep track of placeholders.
if ( '' in params ) {
parameters.push( '' );
}
// Remove deprecated parameters, if they do not have a value.
if ( mw.config.get( 'wgVisualEditorConfig' ).transclusionDialogNewSidebar ) {
parameters = parameters.filter( function ( name ) {
if ( spec.isParameterDeprecated( name ) ) {
if ( !params[ name ] || !this.originalData.params[ name ].wt ) {
return false;
}
}
return true;
}, this );
}
// 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 ) {
var params = this.params;
this.orderedParameterNames = this.getAllParametersOrdered().filter( function ( name ) {
return name in params;
} );
}
return this.orderedParameterNames;
};
/**
* Get parameter from its ID.
*
* @private
* @param {string} id Parameter ID
* @return {ve.dm.MWParameterModel|null} Parameter with matching ID, null if no parameters match
*/
ve.dm.MWTemplateModel.prototype.getParameterFromId = function ( id ) {
for ( var name in this.params ) {
if ( this.params[ name ].getId() === id ) {
return this.params[ name ];
}
}
return null;
};
/**
* Add a parameter to template.
*
* @param {ve.dm.MWParameterModel} param Parameter to add
* @fires add
* @fires change
*/
ve.dm.MWTemplateModel.prototype.addParameter = function ( param ) {
var name = param.getName(),
exists = name in this.params;
if ( !exists ) {
this.orderedParameterNames = null;
this.params[ name ] = param;
this.spec.fillFromTemplate();
// This forwards cange 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' ] } );
}
// FIXME: This should be skipped as well, but is currently needed for the hacks in
// {@see ve.ui.MWTransclusionOutlineTemplateWidget}
this.emit( 'add', param );
if ( !exists ) {
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 remove
* @fires 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
/**
* @inheritdoc
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 () {
var addedCount = 0,
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 ( var i = 0; i < names.length; i++ ) {
var name = names[ i ];
var foundAlias = spec.getParameterAliases( name ).some( function ( alias ) {
return alias in params;
} );
if (
!foundAlias &&
!params[ name ] &&
(
spec.isParameterRequired( name ) ||
spec.isParameterSuggested( name )
)
) {
this.addParameter( new ve.dm.MWParameterModel( this, names[ i ] ) );
addedCount++;
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
}
}
return addedCount;
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 () {
var 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 ( var 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;
}
var 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)
template = ve.extendObject( {}, origData, template );
return { template: 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.isEmpty = function () {
var params = this.params;
return Object.keys( params ).every( function ( name ) {
// There is always an unnamed placeholder at the start
if ( !name ) {
return true;
}
var param = params[ name ],
value = param.getValue();
// Check that the value has not been set, or is indistinguishable from
// the automatically-set value. See `MWParameterModel.getValue`
return value === '' || value === param.getAutoValue();
} );
};