mediawiki-extensions-Cite/modules/ve-cite/ve.ui.MWReferenceContextItem.js
thiemowmde ce63522726 Fix wrong "this reference is used twice" in VisualEditor
This patch also updates a second place that does almost the same.

That other place also excludes placeholders. We intentionally don't
do this in this new place.

Bug: T241885
Change-Id: Ib003b8a7bbe247db6f7da0a4efcfd4e5967fd033
2023-04-12 15:19:11 +02:00

134 lines
3.7 KiB
JavaScript

/*!
* VisualEditor MWReferenceContextItem class.
*
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
* @license MIT
*/
/**
* Context item for a MWReference.
*
* @class
* @extends ve.ui.LinearContextItem
*
* @constructor
* @param {ve.ui.Context} context Context item is in
* @param {ve.dm.Model} model Model item is related to
* @param {Object} [config]
*/
ve.ui.MWReferenceContextItem = function VeUiMWReferenceContextItem() {
// Parent constructor
ve.ui.MWReferenceContextItem.super.apply( this, arguments );
this.view = null;
// Initialization
this.$element.addClass( 've-ui-mwReferenceContextItem' );
};
/* Inheritance */
OO.inheritClass( ve.ui.MWReferenceContextItem, ve.ui.LinearContextItem );
/* Static Properties */
ve.ui.MWReferenceContextItem.static.name = 'reference';
ve.ui.MWReferenceContextItem.static.icon = 'reference';
ve.ui.MWReferenceContextItem.static.label = OO.ui.deferMsg( 'cite-ve-dialogbutton-reference-title' );
ve.ui.MWReferenceContextItem.static.modelClasses = [ ve.dm.MWReferenceNode ];
ve.ui.MWReferenceContextItem.static.commandName = 'reference';
/* Methods */
/**
* Get a DOM rendering of the reference.
*
* @private
* @return {jQuery} DOM rendering of reference
*/
ve.ui.MWReferenceContextItem.prototype.getRendering = function () {
var refNode = this.getReferenceNode();
if ( refNode ) {
this.view = new ve.ui.MWPreviewElement( refNode );
// The $element property may be rendered into asynchronously, update the context's size when the
// rendering is complete if that's the case
this.view.once( 'render', this.context.updateDimensions.bind( this.context ) );
return this.view.$element;
} else {
return $( '<div>' )
.addClass( 've-ui-mwReferenceContextItem-muted' )
.text( ve.msg( 'cite-ve-referenceslist-missingref' ) );
}
};
/**
* Get a DOM rendering of a warning if this reference is reused.
*
* @private
* @return {jQuery|null}
*/
ve.ui.MWReferenceContextItem.prototype.getReuseWarning = function () {
var refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model ),
group = this.getFragment().getDocument().getInternalList()
.getNodeGroup( refModel.getListGroup() );
var nodes = ve.getProp( group, 'keyedNodes', refModel.getListKey() );
var usages = nodes && nodes.filter( function ( node ) {
return !node.findParent( ve.dm.MWReferencesListNode );
} ).length;
if ( usages > 1 ) {
return $( '<div>' )
.addClass( 've-ui-mwReferenceContextItem-muted' )
.text( mw.msg( 'cite-ve-dialog-reference-editing-reused', usages ) );
}
};
/**
* Get the reference node in the containing document (not the internal list document)
*
* @return {ve.dm.InternalItemNode|null} Reference item node
*/
ve.ui.MWReferenceContextItem.prototype.getReferenceNode = function () {
if ( !this.model.isEditable() ) {
return null;
}
if ( !this.referenceNode ) {
var refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
this.referenceNode = this.getFragment().getDocument().getInternalList().getItemNode( refModel.getListIndex() );
}
return this.referenceNode;
};
/**
* @inheritdoc
*/
ve.ui.MWReferenceContextItem.prototype.getDescription = function () {
return this.model.isEditable() ? this.getRendering().text() : ve.msg( 'cite-ve-referenceslist-missingref' );
};
/**
* @inheritdoc
*/
ve.ui.MWReferenceContextItem.prototype.renderBody = function () {
this.$body.empty().append( this.getRendering(), this.getReuseWarning() );
};
/**
* @inheritdoc
*/
ve.ui.MWReferenceContextItem.prototype.teardown = function () {
if ( this.view ) {
this.view.destroy();
}
// Call parent
ve.ui.MWReferenceContextItem.super.prototype.teardown.call( this );
};
/* Registration */
ve.ui.contextItemFactory.register( ve.ui.MWReferenceContextItem );