2023-08-21 08:08:23 +00:00
|
|
|
'use strict';
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor ContentEditable MWReferencesListNode 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 references list node.
|
|
|
|
*
|
|
|
|
* @constructor
|
2024-02-28 08:57:24 +00:00
|
|
|
* @extends ve.ce.LeafNode
|
|
|
|
* @mixes ve.ce.FocusableNode
|
2016-02-03 21:03:41 +00:00
|
|
|
* @param {ve.dm.MWReferencesListNode} model Model to observe
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.ce.MWReferencesListNode = function VeCeMWReferencesListNode() {
|
2016-02-03 21:03:41 +00:00
|
|
|
// Parent constructor
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.ce.MWReferencesListNode.super.apply( this, arguments );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Mixin constructors
|
|
|
|
ve.ce.FocusableNode.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.internalList = null;
|
|
|
|
this.listNode = null;
|
2018-02-15 21:20:16 +00:00
|
|
|
this.modified = false;
|
2024-06-28 09:54:20 +00:00
|
|
|
this.docRefs = null;
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// DOM changes
|
|
|
|
this.$element.addClass( 've-ce-mwReferencesListNode' );
|
2017-08-28 16:07:52 +00:00
|
|
|
this.$reflist = $( '<ol>' ).addClass( 'mw-references references' );
|
2018-02-15 21:20:16 +00:00
|
|
|
this.$originalRefList = null;
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$refmsg = $( '<p>' )
|
|
|
|
.addClass( 've-ce-mwReferencesListNode-muted' );
|
|
|
|
|
|
|
|
// Events
|
2018-02-28 17:35:53 +00:00
|
|
|
this.getModel().connect( this, { attributeChange: 'onAttributeChange' } );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2017-09-18 12:06:44 +00:00
|
|
|
this.updateDebounced = ve.debounce( this.update.bind( this ) );
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
// Initialization
|
2023-05-03 15:49:16 +00:00
|
|
|
// Rendering the reference list can be slow, so do it in an idle callback
|
|
|
|
// (i.e. after the editor has finished loading). Previously we used the
|
|
|
|
// Parsoid DOM rendering for the first paint, and updated only when references
|
|
|
|
// were modified, but this means the reference list is out of sync with the
|
|
|
|
// model for features such as T54750.
|
|
|
|
mw.requestIdleCallback( this.update.bind( this ) );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ce.MWReferencesListNode, ve.ce.LeafNode );
|
|
|
|
|
|
|
|
OO.mixinClass( ve.ce.MWReferencesListNode, ve.ce.FocusableNode );
|
|
|
|
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.MWReferencesListNode.static.name = 'mwReferencesList';
|
|
|
|
|
|
|
|
ve.ce.MWReferencesListNode.static.tagName = 'div';
|
|
|
|
|
|
|
|
ve.ce.MWReferencesListNode.static.primaryCommandName = 'referencesList';
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.ce.LeafNode
|
2016-02-03 21:03:41 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.static.getDescription = function ( model ) {
|
|
|
|
return model.getAttribute( 'refGroup' );
|
|
|
|
};
|
|
|
|
|
2023-03-27 17:05:21 +00:00
|
|
|
/**
|
2024-02-28 08:57:24 +00:00
|
|
|
* @override
|
|
|
|
* @see ve.ce.FocusableNode
|
2023-03-27 17:05:21 +00:00
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.getExtraHighlightClasses = function () {
|
2024-01-30 11:18:52 +00:00
|
|
|
return ve.ce.FocusableNode.prototype
|
|
|
|
.getExtraHighlightClasses.apply( this, arguments )
|
|
|
|
.concat( [ 've-ce-mwReferencesListNode-highlight' ] );
|
2023-03-27 17:05:21 +00:00
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle setup events.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.onSetup = function () {
|
2018-02-28 17:35:53 +00:00
|
|
|
this.internalList = this.getModel().getDocument().getInternalList();
|
2016-02-03 21:03:41 +00:00
|
|
|
this.listNode = this.internalList.getListNode();
|
|
|
|
|
|
|
|
this.internalList.connect( this, { update: 'onInternalListUpdate' } );
|
|
|
|
this.listNode.connect( this, { update: 'onListNodeUpdate' } );
|
|
|
|
|
|
|
|
// Parent method
|
2016-11-02 12:43:14 +00:00
|
|
|
ve.ce.MWReferencesListNode.super.prototype.onSetup.call( this );
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle teardown events.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.onTeardown = function () {
|
2021-06-03 21:50:44 +00:00
|
|
|
// Parent method
|
|
|
|
ve.ce.MWReferencesListNode.super.prototype.onTeardown.call( this );
|
|
|
|
|
|
|
|
if ( !this.listNode ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
this.internalList.disconnect( this, { update: 'onInternalListUpdate' } );
|
|
|
|
this.listNode.disconnect( this, { update: 'onListNodeUpdate' } );
|
|
|
|
|
|
|
|
this.internalList = null;
|
|
|
|
this.listNode = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.MWReferencesListNode.prototype.onInternalListUpdate = function ( groupsChanged ) {
|
|
|
|
// Only update if this group has been changed
|
2018-02-28 17:35:53 +00:00
|
|
|
if ( groupsChanged.indexOf( this.getModel().getAttribute( 'listGroup' ) ) !== -1 ) {
|
2018-02-15 21:20:16 +00:00
|
|
|
this.modified = true;
|
2017-09-18 12:06:44 +00:00
|
|
|
this.updateDebounced();
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Rerender when the 'listGroup' attribute changes in the model.
|
|
|
|
*
|
|
|
|
* @param {string} key Attribute key
|
|
|
|
* @param {string} from Old value
|
|
|
|
* @param {string} to New value
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.onAttributeChange = function ( key ) {
|
2017-09-19 15:35:27 +00:00
|
|
|
switch ( key ) {
|
|
|
|
case 'listGroup':
|
|
|
|
this.updateDebounced();
|
2018-02-15 21:20:16 +00:00
|
|
|
this.modified = true;
|
2017-09-19 15:35:27 +00:00
|
|
|
break;
|
|
|
|
case 'isResponsive':
|
|
|
|
this.updateClasses();
|
|
|
|
break;
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the updating of the InternalListNode.
|
|
|
|
*
|
|
|
|
* This will occur after changes to any InternalItemNode.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.onListNodeUpdate = function () {
|
|
|
|
// When the list node updates we're not sure which list group the item
|
|
|
|
// belonged to so we always update
|
|
|
|
// TODO: Only re-render the reference which has been edited
|
2017-09-18 12:06:44 +00:00
|
|
|
this.updateDebounced();
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the references list.
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.update = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const model = this.getModel();
|
2018-02-28 17:35:53 +00:00
|
|
|
|
|
|
|
// Check the node hasn't been destroyed, as this method is debounced.
|
|
|
|
if ( !model ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-06-28 09:54:20 +00:00
|
|
|
this.docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( model.getDocument() );
|
2023-08-21 08:08:23 +00:00
|
|
|
const internalList = model.getDocument().internalList;
|
|
|
|
const refGroup = model.getAttribute( 'refGroup' );
|
|
|
|
const listGroup = model.getAttribute( 'listGroup' );
|
|
|
|
const nodes = internalList.getNodeGroup( listGroup );
|
|
|
|
const hasModelReferences = !!( nodes && nodes.indexOrder.length );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
let emptyText;
|
2021-08-24 10:24:02 +00:00
|
|
|
if ( refGroup !== '' ) {
|
|
|
|
emptyText = ve.msg( 'cite-ve-referenceslist-isempty', refGroup );
|
|
|
|
} else {
|
|
|
|
emptyText = ve.msg( 'cite-ve-referenceslist-isempty-default' );
|
|
|
|
}
|
|
|
|
|
2024-03-29 13:04:40 +00:00
|
|
|
let originalDomElements;
|
|
|
|
if ( model.getElement().originalDomElementsHash ) {
|
|
|
|
originalDomElements = model.getStore().value(
|
|
|
|
model.getElement().originalDomElementsHash
|
|
|
|
);
|
|
|
|
}
|
2023-05-03 15:49:16 +00:00
|
|
|
// Use the Parsoid-provided DOM if:
|
|
|
|
//
|
|
|
|
// * There are no references in the model
|
|
|
|
// * There have been no changes to the references in the model (!this.modified)
|
|
|
|
//
|
|
|
|
// In practice this is for he.wiki where references are template-generated (T187495)
|
|
|
|
if (
|
|
|
|
!hasModelReferences &&
|
|
|
|
!this.modified &&
|
2024-03-29 13:04:40 +00:00
|
|
|
originalDomElements
|
2023-05-03 15:49:16 +00:00
|
|
|
) {
|
2021-12-21 00:15:58 +00:00
|
|
|
// Create a copy when importing to the main document, as extensions may
|
2024-03-29 13:04:40 +00:00
|
|
|
this.$originalRefList = $( ve.copyDomElements( originalDomElements, document ) );
|
2021-12-21 00:15:58 +00:00
|
|
|
// modify DOM nodes in the main doc.
|
2023-05-03 15:49:16 +00:00
|
|
|
if ( this.$originalRefList.find( 'li' ).length ) {
|
2023-02-22 17:19:22 +00:00
|
|
|
this.$element.append( this.$originalRefList );
|
|
|
|
} else {
|
2021-08-24 10:24:02 +00:00
|
|
|
this.$refmsg.text( emptyText );
|
|
|
|
this.$element.append( this.$refmsg );
|
|
|
|
}
|
2018-02-15 21:20:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.$originalRefList ) {
|
|
|
|
this.$originalRefList.remove();
|
|
|
|
this.$originalRefList = null;
|
|
|
|
}
|
2024-03-29 13:04:40 +00:00
|
|
|
// Copy CSS to dynamic ref list
|
|
|
|
if ( originalDomElements ) {
|
|
|
|
// Get first container, e.g. skipping TemplateStyles
|
|
|
|
const divs = originalDomElements.filter( ( element ) => element.tagName === 'DIV' );
|
|
|
|
if ( divs.length ) {
|
|
|
|
// eslint-disable-next-line mediawiki/class-doc
|
|
|
|
this.$element.addClass( divs[ 0 ].getAttribute( 'class' ) );
|
|
|
|
this.$element.attr( 'style', divs[ 0 ].getAttribute( 'style' ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-17 09:18:33 +00:00
|
|
|
this.$reflist.detach().empty().attr( 'data-mw-group', refGroup || null );
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$refmsg.detach();
|
|
|
|
|
2023-05-03 15:49:16 +00:00
|
|
|
if ( !hasModelReferences ) {
|
2021-08-24 10:24:02 +00:00
|
|
|
this.$refmsg.text( emptyText );
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$element.append( this.$refmsg );
|
|
|
|
} else {
|
2024-06-28 09:54:20 +00:00
|
|
|
const groupedByParent = this.docRefs.getGroupRefsByParents( listGroup );
|
|
|
|
const topLevelNodes = groupedByParent[ '' ] || [];
|
2024-06-19 10:03:28 +00:00
|
|
|
this.$reflist.append(
|
2024-06-28 09:54:20 +00:00
|
|
|
topLevelNodes.map( ( node ) => this.renderListItem(
|
2024-06-28 09:58:03 +00:00
|
|
|
nodes, internalList, groupedByParent, refGroup, node
|
2024-06-19 10:03:28 +00:00
|
|
|
) )
|
|
|
|
);
|
|
|
|
|
|
|
|
this.updateClasses();
|
|
|
|
this.$element.append( this.$reflist );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2024-06-21 15:18:36 +00:00
|
|
|
* Render a reference list item
|
|
|
|
*
|
2024-06-19 10:03:28 +00:00
|
|
|
* @private
|
2024-06-21 15:18:36 +00:00
|
|
|
* @param {Object} nodes Node group object, containing nodes and key order array
|
|
|
|
* @param {ve.dm.InternalList} internalList Internal list
|
2024-06-28 09:58:03 +00:00
|
|
|
* @param {Object.<string, ve.dm.MWReferenceNode[]>} groupedByParent Mapping
|
|
|
|
* from parent ref name (or '' for top-level) to refs
|
2024-06-21 15:18:36 +00:00
|
|
|
* @param {string} refGroup Reference group
|
2024-06-28 09:54:20 +00:00
|
|
|
* @param {ve.dm.MWReferenceNode} node Reference node to render as a footnote body
|
|
|
|
* @return {jQuery} Rendered list item
|
2024-06-19 10:03:28 +00:00
|
|
|
*/
|
2024-06-28 09:58:03 +00:00
|
|
|
ve.ce.MWReferencesListNode.prototype.renderListItem = function ( nodes, internalList, groupedByParent, refGroup, node ) {
|
2024-06-28 09:54:20 +00:00
|
|
|
const listIndex = node.getAttribute( 'listIndex' );
|
|
|
|
const key = internalList.keys[ listIndex ];
|
2024-06-19 10:03:28 +00:00
|
|
|
const keyedNodes = ( nodes.keyedNodes[ key ] || [] )
|
|
|
|
.filter(
|
|
|
|
// Exclude placeholders and references defined inside the references list node
|
|
|
|
( backRefNode ) => !backRefNode.getAttribute( 'placeholder' ) && !backRefNode.findParent( ve.dm.MWReferencesListNode )
|
|
|
|
);
|
|
|
|
|
|
|
|
const $li = $( '<li>' )
|
2024-06-28 10:12:37 +00:00
|
|
|
.css( '--footnote-number', `"${ this.docRefs.getIndexNumber( refGroup, key ) }."` )
|
2024-06-19 10:03:28 +00:00
|
|
|
.append( this.renderBacklinks( keyedNodes, refGroup ), ' ' );
|
|
|
|
|
|
|
|
// Generate reference HTML from first item in key
|
2024-06-28 09:54:20 +00:00
|
|
|
const modelNode = internalList.getItemNode( listIndex );
|
2024-06-19 10:03:28 +00:00
|
|
|
if ( modelNode && modelNode.length ) {
|
|
|
|
const refPreview = new ve.ui.MWPreviewElement( modelNode, { useView: true } );
|
|
|
|
$li.append(
|
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 'reference-text' )
|
|
|
|
.append( refPreview.$element )
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( this.getRoot() ) {
|
|
|
|
const surface = this.getRoot().getSurface().getSurface();
|
|
|
|
// TODO: attach to the singleton click handler on the surface
|
|
|
|
$li.on( 'mousedown', ( e ) => {
|
|
|
|
if ( ve.isUnmodifiedLeftClick( e ) && modelNode && modelNode.length ) {
|
2024-06-28 09:54:20 +00:00
|
|
|
const items = ve.ui.contextItemFactory.getRelatedItems( [ node ] )
|
2024-06-19 10:03:28 +00:00
|
|
|
.filter( ( item ) => item.name !== 'mobileActions' );
|
|
|
|
if ( items.length ) {
|
|
|
|
const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
|
|
|
|
if ( contextItem ) {
|
|
|
|
const command = surface.commandRegistry
|
|
|
|
.lookup( contextItem.static.commandName );
|
|
|
|
if ( command ) {
|
|
|
|
const fragmentArgs = {
|
|
|
|
fragment: surface.getModel()
|
2024-06-28 09:54:20 +00:00
|
|
|
.getLinearFragment( node.getOuterRange(), true ),
|
2024-06-19 10:03:28 +00:00
|
|
|
selectFragmentOnClose: false
|
|
|
|
};
|
|
|
|
const newArgs = ve.copy( command.args );
|
|
|
|
if ( command.name === 'reference' ) {
|
|
|
|
newArgs[ 1 ] = fragmentArgs;
|
|
|
|
} else {
|
|
|
|
ve.extendObject( newArgs[ 0 ], fragmentArgs );
|
2023-03-27 17:05:21 +00:00
|
|
|
}
|
2024-06-19 10:03:28 +00:00
|
|
|
command.execute( surface, newArgs );
|
2023-03-27 17:05:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-06-19 10:03:28 +00:00
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} );
|
|
|
|
}
|
2024-06-28 09:58:03 +00:00
|
|
|
const listKey = node.getAttribute( 'listKey' );
|
|
|
|
const subrefs = groupedByParent[ listKey ] || [];
|
|
|
|
if ( subrefs.length ) {
|
|
|
|
$li.append(
|
|
|
|
$( '<ol>' ).append(
|
|
|
|
subrefs.map( ( subNode ) => this.renderListItem(
|
|
|
|
nodes, internalList, groupedByParent, refGroup, subNode
|
|
|
|
) )
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-06-19 10:03:28 +00:00
|
|
|
} else {
|
|
|
|
$li.append(
|
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 've-ce-mwReferencesListNode-muted' )
|
|
|
|
.text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) )
|
|
|
|
).addClass( 've-ce-mwReferencesListNode-missingRef' );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
2024-06-19 10:03:28 +00:00
|
|
|
|
|
|
|
return $li;
|
2016-02-03 21:03:41 +00:00
|
|
|
};
|
|
|
|
|
2017-09-19 15:35:27 +00:00
|
|
|
/**
|
|
|
|
* Update ref list classes.
|
|
|
|
*
|
|
|
|
* Currently used to set responsive layout
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.updateClasses = function () {
|
2023-08-21 08:08:23 +00:00
|
|
|
const isResponsive = this.getModel().getAttribute( 'isResponsive' );
|
2017-09-19 15:35:27 +00:00
|
|
|
|
|
|
|
this.$element
|
|
|
|
.toggleClass( 'mw-references-wrap', isResponsive )
|
|
|
|
.toggleClass( 'mw-references-columns', isResponsive && this.$reflist.children().length > 10 );
|
|
|
|
};
|
|
|
|
|
2020-01-23 13:08:08 +00:00
|
|
|
/**
|
|
|
|
* Build markers for backlinks
|
|
|
|
*
|
|
|
|
* @param {ve.dm.Node[]} keyedNodes A list of ref nodes linked to a reference list item
|
|
|
|
* @param {string} refGroup Reference group name
|
|
|
|
* @return {jQuery} Element containing backlinks
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.prototype.renderBacklinks = function ( keyedNodes, refGroup ) {
|
2023-05-17 09:18:33 +00:00
|
|
|
if ( keyedNodes.length === 1 ) {
|
|
|
|
return $( '<a>' )
|
|
|
|
.attr( 'rel', 'mw:referencedBy' )
|
|
|
|
.attr( 'data-mw-group', refGroup || null )
|
|
|
|
.append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( '↑ ' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// named reference with multiple usages
|
2023-08-21 08:08:23 +00:00
|
|
|
const $refSpan = $( '<span>' ).attr( 'rel', 'mw:referencedBy' );
|
|
|
|
for ( let i = 0; i < keyedNodes.length; i++ ) {
|
2023-05-17 09:18:33 +00:00
|
|
|
$( '<a>' )
|
|
|
|
.attr( 'data-mw-group', refGroup || null )
|
2024-06-19 10:03:28 +00:00
|
|
|
// FIXME: i18n backlink numbering
|
2023-05-17 09:18:33 +00:00
|
|
|
.append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( ( i + 1 ) + ' ' ) )
|
|
|
|
.appendTo( $refSpan );
|
2020-01-23 13:08:08 +00:00
|
|
|
}
|
2023-05-17 09:18:33 +00:00
|
|
|
return $refSpan;
|
2020-01-23 13:08:08 +00:00
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ce.nodeFactory.register( ve.ce.MWReferencesListNode );
|