2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWReferenceContextItem class.
|
|
|
|
*
|
2018-01-03 01:05:45 +00:00
|
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
2017-12-29 12:12:35 +00:00
|
|
|
* @license MIT
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Context item for a MWReference.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.LinearContextItem
|
|
|
|
*
|
|
|
|
* @constructor
|
2023-07-10 13:33:24 +00:00
|
|
|
* @param {ve.ui.LinearContext} context Context the item is in
|
|
|
|
* @param {ve.dm.Model} model Model the item is related to
|
2021-12-29 17:22:08 +00:00
|
|
|
* @param {Object} [config]
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
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 () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refNode = this.getReferenceNode();
|
2016-02-03 21:03:41 +00:00
|
|
|
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' ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-01-03 20:06:12 +00:00
|
|
|
/**
|
|
|
|
* Get a DOM rendering of a warning if this reference is reused.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {jQuery|null}
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceContextItem.prototype.getReuseWarning = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
|
|
|
|
const group = this.getFragment().getDocument().getInternalList()
|
|
|
|
.getNodeGroup( refModel.getListGroup() );
|
|
|
|
const nodes = ve.getProp( group, 'keyedNodes', refModel.getListKey() );
|
|
|
|
const usages = nodes && nodes.filter( function ( node ) {
|
2023-02-27 11:25:44 +00:00
|
|
|
return !node.findParent( ve.dm.MWReferencesListNode );
|
|
|
|
} ).length;
|
|
|
|
if ( usages > 1 ) {
|
2019-01-03 20:06:12 +00:00
|
|
|
return $( '<div>' )
|
|
|
|
.addClass( 've-ui-mwReferenceContextItem-muted' )
|
2023-02-27 11:25:44 +00:00
|
|
|
.text( mw.msg( 'cite-ve-dialog-reference-editing-reused', usages ) );
|
2019-01-03 20:06:12 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2023-05-04 16:56:41 +00:00
|
|
|
/**
|
|
|
|
* Get a DOM rendering of a warning if this reference is an extension.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {jQuery|null}
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceContextItem.prototype.getExtendsWarning = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
|
2023-05-04 16:56:41 +00:00
|
|
|
|
|
|
|
if ( refModel.extendsRef ) {
|
|
|
|
return $( '<div>' )
|
|
|
|
.addClass( 've-ui-mwReferenceContextItem-muted' )
|
|
|
|
.text( mw.msg( 'cite-ve-dialog-reference-editing-extends' ) );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/**
|
|
|
|
* Get the reference node in the containing document (not the internal list document)
|
|
|
|
*
|
2016-05-24 12:49:22 +00:00
|
|
|
* @return {ve.dm.InternalItemNode|null} Reference item node
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceContextItem.prototype.getReferenceNode = function () {
|
|
|
|
if ( !this.model.isEditable() ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ( !this.referenceNode ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
|
2016-02-03 21:03:41 +00:00
|
|
|
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' );
|
|
|
|
};
|
|
|
|
|
2023-06-09 15:55:00 +00:00
|
|
|
/**
|
|
|
|
* Get the text of the parent reference.
|
|
|
|
*
|
|
|
|
* @private
|
|
|
|
* @return {string|null}
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceContextItem.prototype.getParentRef = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
|
2023-06-09 15:55:00 +00:00
|
|
|
if ( !refModel.extendsRef ) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-08-21 08:08:23 +00:00
|
|
|
const list = this.getFragment().getDocument().getInternalList();
|
|
|
|
const index = list.keys.indexOf( 'literal/' + refModel.extendsRef );
|
2023-06-09 15:55:00 +00:00
|
|
|
return list.getItemNode( index ).element.attributes.originalHtml;
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWReferenceContextItem.prototype.renderBody = function () {
|
2023-06-09 15:55:00 +00:00
|
|
|
this.$body.empty().append( this.getParentRef(), this.getRendering(), this.getReuseWarning(), this.getExtendsWarning() );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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 );
|