From eeda931f599b9ee1474abab56fcaa829a1386015 Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Wed, 18 Aug 2021 14:52:11 +0200 Subject: [PATCH] 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 --- .../ui/dialogs/ve.ui.MWTemplateDialog.js | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWTemplateDialog.js b/modules/ve-mw/ui/dialogs/ve.ui.MWTemplateDialog.js index c57b06d9bc..6b8b977bc0 100644 --- a/modules/ve-mw/ui/dialogs/ve.ui.MWTemplateDialog.js +++ b/modules/ve-mw/ui/dialogs/ve.ui.MWTemplateDialog.js @@ -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(); + // Add required and suggested params to user created templates + 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(); } }