VE include subref count in warning message

Include subref count in warning messages in VE editPanel dialog and contextItem

Bug: T373023
Change-Id: Ie7d162c4a2291db441846ec338159e8955c206c2
This commit is contained in:
mareikeheuer 2024-09-05 13:44:11 +02:00
parent 2890772a9f
commit bdcbcc08f8
5 changed files with 47 additions and 13 deletions

View file

@ -206,6 +206,24 @@ ve.dm.MWGroupReferences.prototype.getRefUsages = function ( key ) {
); );
}; };
/**
* Get the total number of usages for a reference, including sub-references.
*
* @param {string} listKey Full key of the reference
* @return {number} Total usage count of main refs and subrefs
*/
ve.dm.MWGroupReferences.prototype.getTotalUsageCount = function ( listKey ) {
const mainRefs = this.getRefUsages( listKey );
const mainRefsCount = mainRefs.length;
const subrefs = this.getSubrefs( listKey );
const subrefsCount = subrefs.length;
const totalUsageCount = mainRefsCount + subrefsCount;
return totalUsageCount;
};
/** /**
* @param {string} parentKey parent ref key * @param {string} parentKey parent ref key
* @return {ve.dm.MWReferenceNode[]} List of subrefs for this parent * @return {ve.dm.MWReferenceNode[]} List of subrefs for this parent

View file

@ -74,11 +74,13 @@ ve.ui.MWReferenceContextItem.prototype.getRendering = function () {
* @return {jQuery|undefined} * @return {jQuery|undefined}
*/ */
ve.ui.MWReferenceContextItem.prototype.getReuseWarning = function () { ve.ui.MWReferenceContextItem.prototype.getReuseWarning = function () {
const usageCount = this.groupRefs.getRefUsages( this.model.getAttribute( 'listKey' ) ).length; const listKey = this.model.getAttribute( 'listKey' );
if ( usageCount > 1 ) { const totalUsageCount = this.groupRefs.getTotalUsageCount( listKey );
if ( totalUsageCount > 1 ) {
return $( '<div>' ) return $( '<div>' )
.addClass( 've-ui-mwReferenceContextItem-muted' ) .addClass( 've-ui-mwReferenceContextItem-muted' )
.text( mw.msg( 'cite-ve-dialog-reference-editing-reused', usageCount ) ); .text( mw.msg( 'cite-ve-dialog-reference-editing-reused', totalUsageCount ) );
} }
}; };

View file

@ -253,13 +253,12 @@ ve.ui.MWReferenceEditPanel.prototype.setFormFieldsFromRef = function ( ref ) {
* @param {ve.dm.MWReferenceModel} ref * @param {ve.dm.MWReferenceModel} ref
*/ */
ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref ) { ve.ui.MWReferenceEditPanel.prototype.updateReuseWarningFromRef = function ( ref ) {
const usageCount = this.docRefs const totalUsageCount = this.docRefs
.getGroupRefs( ref.getListGroup() ) .getGroupRefs( ref.getListGroup() )
.getRefUsages( ref.getListKey() ) .getTotalUsageCount( ref.getListKey() );
.length;
this.reuseWarning this.reuseWarning
.toggle( usageCount > 1 ) .toggle( totalUsageCount > 1 )
.setLabel( mw.msg( 'cite-ve-dialog-reference-editing-reused-long', usageCount ) ); .setLabel( mw.msg( 'cite-ve-dialog-reference-editing-reused-long', totalUsageCount ) );
}; };
/** /**

View file

@ -7,7 +7,7 @@
const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( doc ); const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( doc );
const plainGroupRefs = docRefs.getGroupRefs( '' ); const plainGroupRefs = docRefs.getGroupRefs( '' );
const fooGroupRefs = docRefs.getGroupRefs( 'foo' ); const fooGroupRefs = docRefs.getGroupRefs( 'foo' );
const emptyGroupRefs = docRefs.getGroupRefs( 'doenotexist' ); const emptyGroupRefs = docRefs.getGroupRefs( 'doesnotexist' );
QUnit.test( 'isEmpty', ( assert ) => { QUnit.test( 'isEmpty', ( assert ) => {
assert.false( plainGroupRefs.isEmpty() ); assert.false( plainGroupRefs.isEmpty() );
@ -55,13 +55,13 @@
QUnit.test( 'getRefNode', ( assert ) => { QUnit.test( 'getRefNode', ( assert ) => {
assert.strictEqual( plainGroupRefs.getRefNode( 'auto/0' ).getAttribute( 'listKey' ), 'auto/0' ); assert.strictEqual( plainGroupRefs.getRefNode( 'auto/0' ).getAttribute( 'listKey' ), 'auto/0' );
assert.strictEqual( plainGroupRefs.getRefNode( 'doenotexist' ), undefined ); assert.strictEqual( plainGroupRefs.getRefNode( 'doesnotexist' ), undefined );
} ); } );
QUnit.test( 'getInternalModelNode', ( assert ) => { QUnit.test( 'getInternalModelNode', ( assert ) => {
// TODO: assert something that makes sense // TODO: assert something that makes sense
// assert.strictEqual( plainGroupRefs.getInternalModelNode( 'auto/0' ), undefined ); // assert.strictEqual( plainGroupRefs.getInternalModelNode( 'auto/0' ), undefined );
assert.strictEqual( plainGroupRefs.getInternalModelNode( 'doenotexist' ), undefined ); assert.strictEqual( plainGroupRefs.getInternalModelNode( 'doesnotexist' ), undefined );
} ); } );
QUnit.test( 'getRefUsages', ( assert ) => { QUnit.test( 'getRefUsages', ( assert ) => {
@ -72,7 +72,17 @@
'literal/bar' 'literal/bar'
] ]
); );
assert.deepEqual( plainGroupRefs.getRefUsages( 'doenotexist' ), [] ); assert.deepEqual( plainGroupRefs.getRefUsages( 'doesnotexist' ), [] );
} );
QUnit.test( 'getTotalUsageCount', ( assert ) => {
const mockListKey = 'literal/bar';
// The total usage count should be the sum of main refs and subrefs
assert.strictEqual(
plainGroupRefs.getTotalUsageCount( mockListKey ),
plainGroupRefs.getRefUsages( mockListKey ).length + plainGroupRefs.getSubrefs( mockListKey ).length
);
} ); } );
QUnit.test( 'sub-references', ( assert ) => { QUnit.test( 'sub-references', ( assert ) => {

View file

@ -18,7 +18,12 @@
function getDocRefsMock( node, reUse ) { function getDocRefsMock( node, reUse ) {
const groupRefs = { const groupRefs = {
getRefUsages: () => ( reUse ? [ node, node ] : [] ), getRefUsages: () => ( reUse ? [ node, node ] : [] ),
getInternalModelNode: () => ( node ) getInternalModelNode: () => ( node ),
getTotalUsageCount: () => {
const mainRefsCount = reUse ? 2 : 0;
const subRefsCount = reUse ? 1 : 0;
return mainRefsCount + subRefsCount;
}
}; };
return { return {
getAllGroupNames: () => ( [ 'mwReference/' ] ), getAllGroupNames: () => ( [ 'mwReference/' ] ),