mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/Cite
synced 2024-12-18 09:40:49 +00:00
44afa99d49
I believe this was a mistake in the previous patch Idd57997. Using only the .text() of the parent reference is not sufficient. Think of a reference that contains a <math> formula or – even worse – is nothing but a <math> formula. The preview will be empty in this case. Click handlers in the preview need to be disabled. It's only a preview. Bug: T367030 Change-Id: I7e82e03ecfeb4e9cf132985684cff5e191506307
168 lines
4.7 KiB
JavaScript
168 lines
4.7 KiB
JavaScript
'use strict';
|
|
|
|
/*!
|
|
* VisualEditor MWReferenceContextItem class.
|
|
*
|
|
* @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
|
|
* @license MIT
|
|
*/
|
|
|
|
/**
|
|
* Context item for a MWReference.
|
|
*
|
|
* @constructor
|
|
* @extends ve.ui.LinearContextItem
|
|
* @param {ve.ui.LinearContext} context Context the item is in
|
|
* @param {ve.dm.Model} model Model the 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 () {
|
|
const 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|undefined}
|
|
*/
|
|
ve.ui.MWReferenceContextItem.prototype.getReuseWarning = function () {
|
|
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( ( node ) => !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 a DOM rendering of a warning if this reference is an extension.
|
|
*
|
|
* @private
|
|
* @return {jQuery|undefined}
|
|
*/
|
|
ve.ui.MWReferenceContextItem.prototype.getExtendsWarning = function () {
|
|
if ( this.model.getAttribute( 'extendsRef' ) ) {
|
|
return $( '<div>' )
|
|
.addClass( 've-ui-mwReferenceContextItem-muted' )
|
|
.text( mw.msg( 'cite-ve-dialog-reference-editing-extends' ) );
|
|
}
|
|
};
|
|
|
|
/**
|
|
* 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 ) {
|
|
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( this.model );
|
|
this.referenceNode = this.getFragment().getDocument().getInternalList()
|
|
.getItemNode( refModel.getListIndex() );
|
|
}
|
|
return this.referenceNode;
|
|
};
|
|
|
|
/**
|
|
* @override
|
|
*/
|
|
ve.ui.MWReferenceContextItem.prototype.getDescription = function () {
|
|
return this.model.isEditable() ? this.getRendering().text() : ve.msg( 'cite-ve-referenceslist-missingref' );
|
|
};
|
|
|
|
/**
|
|
* Get the text of the parent reference.
|
|
*
|
|
* @private
|
|
* @return {jQuery|null}
|
|
*/
|
|
ve.ui.MWReferenceContextItem.prototype.getParentRef = function () {
|
|
const extendsRef = this.model.getAttribute( 'extendsRef' );
|
|
if ( !extendsRef ) {
|
|
return null;
|
|
}
|
|
const list = this.getFragment().getDocument().getInternalList();
|
|
const itemNode = list.getItemNode( list.keys.indexOf( extendsRef ) );
|
|
return new ve.ui.MWPreviewElement( itemNode, { useView: true } ).$element;
|
|
};
|
|
|
|
/**
|
|
* @override
|
|
*/
|
|
ve.ui.MWReferenceContextItem.prototype.renderBody = function () {
|
|
this.$body.empty().append(
|
|
this.getParentRef(),
|
|
this.getRendering(),
|
|
this.getReuseWarning(),
|
|
this.getExtendsWarning()
|
|
);
|
|
};
|
|
|
|
/**
|
|
* @override
|
|
*/
|
|
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 );
|