From b483c829abce0b2c581bf44fdae5d8dfabfffcdd Mon Sep 17 00:00:00 2001 From: WMDE-Fisch Date: Thu, 6 Jun 2024 14:09:33 +0200 Subject: [PATCH] Extract some methods around the reference insert/update workflow Includes renaming the method so it's more clear what it's doing. As preparation for adding the extends warning to the edit pane and to allow easier identification of parts belonging to the edit workflow. Change-Id: If84c5dbdee19c0ebc0a28b50dda93fef3f558c6e --- modules/ve-cite/ve.ui.MWReferenceDialog.js | 56 +++++++++++-------- .../ve-cite/ve.ui.MWReferenceDialog.test.js | 4 +- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/modules/ve-cite/ve.ui.MWReferenceDialog.js b/modules/ve-cite/ve.ui.MWReferenceDialog.js index 83d1f9ec8..3f6cf3e84 100644 --- a/modules/ve-cite/ve.ui.MWReferenceDialog.js +++ b/modules/ve-cite/ve.ui.MWReferenceDialog.js @@ -201,7 +201,7 @@ ve.ui.MWReferenceDialog.prototype.onSearchResultsChoose = function ( item ) { this.getFragment().removeContent(); this.selectedNode = null; } - this.useReference( ref ); + this.setReferenceForEditing( ref ); this.executeAction( 'insert' ); ve.track( 'activity.' + this.constructor.static.name, { action: 'reuse-choose' } ); @@ -238,38 +238,48 @@ ve.ui.MWReferenceDialog.prototype.getBodyHeight = function () { /** * Work on a specific reference. * - * @param {ve.dm.MWReferenceModel} [ref] Reference model, omit to work on a new reference + * @param {ve.dm.MWReferenceModel} ref * @return {ve.ui.MWReferenceDialog} * @chainable */ -ve.ui.MWReferenceDialog.prototype.useReference = function ( ref ) { - // Properties - if ( ref instanceof ve.dm.MWReferenceModel ) { - // Use an existing reference - this.referenceModel = ref; - } else { - // Create a new reference - this.referenceModel = new ve.dm.MWReferenceModel( this.getFragment().getDocument() ); - } +ve.ui.MWReferenceDialog.prototype.setReferenceForEditing = function ( ref ) { + this.referenceModel = ref; - this.referenceTarget.setDocument( this.referenceModel.getDocument() ); + this.setFormFieldsFromRef( this.referenceModel ); + this.updateReuseWarningFromRef( this.referenceModel ); - // Initialization - this.originalGroup = this.referenceModel.getGroup(); + return this; +}; + +/** + * @private + * @param {ve.dm.MWReferenceModel} ref + */ +ve.ui.MWReferenceDialog.prototype.setFormFieldsFromRef = function ( ref ) { + this.referenceTarget.setDocument( ref.getDocument() ); + + this.originalGroup = ref.getGroup(); // Set the group input while it's disabled, so this doesn't pop up the group-picker menu this.referenceGroupInput.setDisabled( true ); this.referenceGroupInput.setValue( this.originalGroup ); this.referenceGroupInput.setDisabled( false ); +}; +/** + * @private + * @param {ve.dm.MWReferenceModel} ref + */ +ve.ui.MWReferenceDialog.prototype.updateReuseWarningFromRef = function ( ref ) { const group = this.getFragment().getDocument().getInternalList() - .getNodeGroup( this.referenceModel.getListGroup() ); - const nodes = ve.getProp( group, 'keyedNodes', this.referenceModel.getListKey() ); - const usages = nodes ? nodes.filter( ( node ) => !node.findParent( ve.dm.MWReferencesListNode ) ).length : 0; + .getNodeGroup( ref.getListGroup() ); + const nodes = ve.getProp( group, 'keyedNodes', ref.getListKey() ); + const usages = nodes ? nodes.filter( + ( node ) => !node.findParent( ve.dm.MWReferencesListNode ) + ).length : 0; - this.reuseWarning.toggle( usages > 1 ) + this.reuseWarning + .toggle( usages > 1 ) .setLabel( mw.msg( 'cite-ve-dialog-reference-editing-reused-long', usages ) ); - - return this; }; /** @@ -390,11 +400,9 @@ ve.ui.MWReferenceDialog.prototype.getSetupProcess = function ( data ) { .next( () => { this.panels.setItem( this.editPanel ); if ( this.selectedNode instanceof ve.dm.MWReferenceNode ) { - this.useReference( - ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode ) - ); + this.setReferenceForEditing( ve.dm.MWReferenceModel.static.newFromReferenceNode( this.selectedNode ) ); } else { - this.useReference( null ); + this.setReferenceForEditing( new ve.dm.MWReferenceModel( this.getFragment().getDocument() ) ); this.actions.setAbilities( { done: false, insert: false } ); } diff --git a/tests/qunit/ve-cite/ve.ui.MWReferenceDialog.test.js b/tests/qunit/ve-cite/ve.ui.MWReferenceDialog.test.js index dbec4e780..c357b044b 100644 --- a/tests/qunit/ve-cite/ve.ui.MWReferenceDialog.test.js +++ b/tests/qunit/ve-cite/ve.ui.MWReferenceDialog.test.js @@ -2,7 +2,7 @@ QUnit.module( 've.ui.MWReferenceDialog (Cite)', ve.test.utils.newMwEnvironment() ); -QUnit.test( 'useReference', ( assert ) => { +QUnit.test( 'setReferenceForEditing', ( assert ) => { const dialog = new ve.ui.MWReferenceDialog(); dialog.referenceGroupInput = new ve.ui.MWReferenceGroupInputWidget( {} ); @@ -26,7 +26,7 @@ QUnit.test( 'useReference', ( assert ) => { }; const ref = new ve.dm.MWReferenceModel( parentDoc ); ref.setGroup( 'g' ); - dialog.useReference( ref ); + dialog.setReferenceForEditing( ref ); assert.strictEqual( dialog.referenceModel, ref ); assert.strictEqual( dialog.originalGroup, 'g' );