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;
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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' );
|
|
|
|
}
|
|
|
|
|
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 &&
|
|
|
|
model.getElement().originalDomElementsHash
|
|
|
|
) {
|
2021-12-21 00:15:58 +00:00
|
|
|
// Create a copy when importing to the main document, as extensions may
|
|
|
|
// modify DOM nodes in the main doc.
|
|
|
|
this.$originalRefList = $( ve.copyDomElements( model.getStore().value(
|
2018-03-06 13:00:35 +00:00
|
|
|
model.getElement().originalDomElementsHash
|
2021-12-21 00:15:58 +00:00
|
|
|
), document ) );
|
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;
|
|
|
|
}
|
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 {
|
2023-03-27 17:05:21 +00:00
|
|
|
nodes.indexOrder.forEach( function ( index ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const firstNode = nodes.firstNodes[ index ];
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const key = internalList.keys[ index ];
|
|
|
|
let keyedNodes = nodes.keyedNodes[ key ];
|
2016-02-03 21:03:41 +00:00
|
|
|
keyedNodes = keyedNodes.filter( function ( node ) {
|
2023-02-27 11:25:44 +00:00
|
|
|
// Exclude placeholders and references defined inside the references list node
|
|
|
|
return !node.getAttribute( 'placeholder' ) &&
|
|
|
|
!node.findParent( ve.dm.MWReferencesListNode );
|
2016-02-03 21:03:41 +00:00
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !keyedNodes.length ) {
|
2023-03-27 17:05:21 +00:00
|
|
|
return;
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
2023-08-21 08:08:23 +00:00
|
|
|
const $li = $( '<li>' )
|
2023-05-15 10:12:44 +00:00
|
|
|
.append( this.renderBacklinks( keyedNodes, refGroup ), ' ' );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Generate reference HTML from first item in key
|
2023-08-21 08:08:23 +00:00
|
|
|
const modelNode = internalList.getItemNode( firstNode.getAttribute( 'listIndex' ) );
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( modelNode && modelNode.length ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const refPreview = new ve.ui.MWPreviewElement( modelNode, { useView: true } );
|
2018-05-11 13:53:03 +00:00
|
|
|
$li.append(
|
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 'reference-text' )
|
|
|
|
.append( refPreview.$element )
|
|
|
|
);
|
2016-02-03 21:03:41 +00:00
|
|
|
} else {
|
|
|
|
$li.append(
|
|
|
|
$( '<span>' )
|
|
|
|
.addClass( 've-ce-mwReferencesListNode-muted' )
|
2018-03-24 17:32:55 +00:00
|
|
|
.text( ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) )
|
2023-03-27 17:05:21 +00:00
|
|
|
).addClass( 've-ce-mwReferencesListNode-missingRef' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.getRoot() ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const surface = this.getRoot().getSurface().getSurface();
|
2023-03-27 17:05:21 +00:00
|
|
|
$li.on( 'mousedown', function ( e ) {
|
|
|
|
if ( modelNode && modelNode.length ) {
|
2024-01-30 11:18:52 +00:00
|
|
|
const items = ve.ui.contextItemFactory.getRelatedItems( [ firstNode ] )
|
|
|
|
.filter( ( item ) => item.name !== 'mobileActions' );
|
2023-03-27 17:05:21 +00:00
|
|
|
if ( items.length ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
|
2023-03-27 17:05:21 +00:00
|
|
|
if ( contextItem ) {
|
2024-01-30 11:18:52 +00:00
|
|
|
const command = surface.commandRegistry
|
|
|
|
.lookup( contextItem.static.commandName );
|
2023-03-27 17:05:21 +00:00
|
|
|
if ( command ) {
|
2023-08-21 08:08:23 +00:00
|
|
|
const fragmentArgs = {
|
2024-01-30 11:18:52 +00:00
|
|
|
fragment: surface.getModel()
|
|
|
|
.getLinearFragment( firstNode.getOuterRange(), true ),
|
2023-03-27 17:05:21 +00:00
|
|
|
selectFragmentOnClose: false
|
|
|
|
};
|
2023-08-21 08:08:23 +00:00
|
|
|
const newArgs = ve.copy( command.args );
|
2023-03-27 17:05:21 +00:00
|
|
|
if ( command.name === 'reference' ) {
|
|
|
|
newArgs[ 1 ] = fragmentArgs;
|
|
|
|
} else {
|
|
|
|
ve.extendObject( newArgs[ 0 ], fragmentArgs );
|
|
|
|
}
|
|
|
|
command.execute( surface, newArgs );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
e.preventDefault();
|
|
|
|
} );
|
2016-02-03 21:03:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
this.$reflist.append( $li );
|
2023-03-27 17:05:21 +00:00
|
|
|
}.bind( this ) );
|
|
|
|
|
2017-09-19 15:35:27 +00:00
|
|
|
this.updateClasses();
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$element.append( this.$reflist );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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 )
|
|
|
|
.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 );
|