Refactor onReplacePart in TemplateDialog

As preparation to introduce then new UI to add unknown parameters.
This is a few things:
- Merge the code paths when adding a MWTemplateModel
- Put code adding parameters to the dialog next to each other so
  that preventing reselection happens around that block
- Reduce duplicated code when re-focusing after addition
- Move adding the placeholder page to the end
- Add and clean up inline documentation

Bug: T272487
Change-Id: Ic700edd42027a928a236ed11f2c257fffe994257
This commit is contained in:
WMDE-Fisch 2021-08-18 14:52:11 +02:00
parent d41714f3a1
commit eeda931f59

View file

@ -98,7 +98,7 @@ ve.ui.MWTemplateDialog.prototype.onTransclusionModelChange = function () {
* @param {ve.dm.MWTransclusionPartModel|null} added Added part
*/
ve.ui.MWTemplateDialog.prototype.onReplacePart = function ( removed, added ) {
var names, reselect,
var reselect,
removePages = [];
if ( removed ) {
@ -127,32 +127,27 @@ ve.ui.MWTemplateDialog.prototype.onReplacePart = function ( removed, added ) {
// Use added page instead of closest page
this.transclusions.focusPart( added.getId() );
}
// Add existing params to templates (the template might be being moved)
if ( added instanceof ve.dm.MWTemplateModel ) {
names = added.getOrderedParameterNames();
// Prevent selection changes
// Prevent selection changes while parameters are added
this.preventReselection = true;
// Add existing params to templates (the template might be being moved)
var names = added.getOrderedParameterNames();
for ( var i = 0; i < names.length; i++ ) {
this.onAddParameter( added.getParameter( names[ i ] ) );
}
this.preventReselection = false;
added.connect( this, { add: 'onAddParameter', remove: 'onRemoveParameter' } );
if ( names.length ) {
this.transclusions.focusPart( added.getParameter( names[ 0 ] ).getId() );
}
}
// Add required and suggested params to user created templates
if ( added instanceof ve.dm.MWTemplateModel && this.loaded ) {
// Prevent selection changes
this.preventReselection = true;
var addedCount = added.addPromptedParameters();
var shouldAddPlaceholder = this.loaded && added.addPromptedParameters() === 0;
this.preventReselection = false;
names = added.getOrderedParameterNames();
if ( names.length ) {
// Focus the first element when parameters are present
this.transclusions.focusPart( added.getParameter( names[ 0 ] ).getId() );
} else if ( addedCount === 0 && !this.isNewSidebar ) {
// This adds a placeholder, i.e. the parameter search widget appears
} else if ( shouldAddPlaceholder && !this.isNewSidebar ) {
page.addPlaceholderParameter();
}
}