2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWReferenceNode 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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ContentEditable MediaWiki reference node.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ce.LeafNode
|
2021-11-03 12:33:54 +00:00
|
|
|
* @mixin ve.ce.FocusableNode
|
2016-02-03 21:03:41 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {ve.dm.MWReferenceNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.ce.MWReferenceNode = function VeCeMWReferenceNode() {
|
2016-02-03 21:03:41 +00:00
|
|
|
// Parent constructor
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.ce.MWReferenceNode.super.apply( this, arguments );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.FocusableNode.call( this );
|
|
|
|
|
|
|
|
// DOM changes
|
|
|
|
this.$link = $( '<a>' ).attr( 'href', '#' );
|
2020-11-05 00:49:54 +00:00
|
|
|
this.$element.addClass( 've-ce-mwReferenceNode mw-ref reference' ).append( this.$link );
|
2016-02-03 21:03:41 +00:00
|
|
|
// Add a backwards-compatible text for browsers that don't support counters
|
|
|
|
this.$text = $( '<span>' ).addClass( 'mw-reflink-text' );
|
|
|
|
this.$link.append( this.$text );
|
|
|
|
|
|
|
|
this.index = '';
|
|
|
|
this.internalList = this.model.getDocument().internalList;
|
|
|
|
|
|
|
|
// Events
|
2019-03-27 12:54:18 +00:00
|
|
|
this.connect( this, {
|
|
|
|
setup: 'onSetup',
|
|
|
|
teardown: 'onTeardown'
|
|
|
|
} );
|
|
|
|
this.model.connect( this, { attributeChange: 'onAttributeChange' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.update();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWReferenceNode, ve.ce.LeafNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ce.MWReferenceNode, ve.ce.FocusableNode );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.MWReferenceNode.static.name = 'mwReference';
|
|
|
|
|
2022-11-18 12:11:23 +00:00
|
|
|
ve.ce.MWReferenceNode.static.tagName = 'sup';
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
ve.ce.MWReferenceNode.static.primaryCommandName = 'reference';
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle setup event.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.onSetup = function () {
|
|
|
|
ve.ce.MWReferenceNode.super.prototype.onSetup.call( this );
|
|
|
|
this.internalList.connect( this, { update: 'onInternalListUpdate' } );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle teardown event.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.onTeardown = function () {
|
|
|
|
// As we are listening to the internal list, we need to make sure
|
|
|
|
// we remove the listeners when this object is removed from the document
|
|
|
|
this.internalList.disconnect( this );
|
|
|
|
|
|
|
|
ve.ce.MWReferenceNode.super.prototype.onTeardown.call( this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the updating of the InternalList object.
|
|
|
|
*
|
|
|
|
* This will occur after a document transaction.
|
|
|
|
*
|
|
|
|
* @param {string[]} groupsChanged A list of groups which have changed in this transaction
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.onInternalListUpdate = function ( groupsChanged ) {
|
|
|
|
// Only update if this group has been changed
|
|
|
|
if ( groupsChanged.indexOf( this.model.getAttribute( 'listGroup' ) ) !== -1 ) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-03-27 12:54:18 +00:00
|
|
|
/**
|
|
|
|
* Handle attribute change events
|
|
|
|
*
|
|
|
|
* @param {string} key Attribute key
|
|
|
|
* @param {string} from Old value
|
|
|
|
* @param {string} to New value
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.onAttributeChange = function ( key ) {
|
|
|
|
if ( key === 'placeholder' ) {
|
|
|
|
this.update();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/**
|
2016-02-27 17:29:37 +00:00
|
|
|
* @inheritdoc ve.ce.FocusableNode
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.executeCommand = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const items = ve.ui.contextItemFactory.getRelatedItems( [ this.model ] );
|
2016-02-27 17:29:37 +00:00
|
|
|
|
|
|
|
if ( items.length ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
|
2016-02-27 17:29:37 +00:00
|
|
|
if ( contextItem ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const command = this.getRoot().getSurface().getSurface().commandRegistry.lookup( contextItem.static.commandName );
|
2016-02-27 17:29:37 +00:00
|
|
|
if ( command ) {
|
|
|
|
command.execute( this.focusableSurface.getSurface() );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the rendering
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWReferenceNode.prototype.update = function () {
|
|
|
|
this.$text.text( this.model.getIndexLabel() );
|
2023-05-17 09:18:33 +00:00
|
|
|
this.$link
|
|
|
|
.css( 'counterReset', 'mw-Ref ' + this.model.getIndex() )
|
|
|
|
.attr( 'data-mw-group', this.model.getGroup() || null );
|
2019-03-27 12:54:18 +00:00
|
|
|
this.$element.toggleClass( 've-ce-mwReferenceNode-placeholder', !!this.model.getAttribute( 'placeholder' ) );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ce.nodeFactory.register( ve.ce.MWReferenceNode );
|