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.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ce.LeafNode
|
|
|
|
* @mixins ve.ce.FocusableNode
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @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
|
2017-09-18 12:06:44 +00:00
|
|
|
this.updateDebounced();
|
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 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ce.MWReferencesListNode.static.getDescription = function ( model ) {
|
|
|
|
return model.getAttribute( 'refGroup' );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* 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 () {
|
2021-11-03 12:28:17 +00:00
|
|
|
var 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;
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
var internalList = model.getDocument().internalList;
|
|
|
|
var refGroup = model.getAttribute( 'refGroup' );
|
|
|
|
var listGroup = model.getAttribute( 'listGroup' );
|
|
|
|
var nodes = internalList.getNodeGroup( listGroup );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
var 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' );
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:20:16 +00:00
|
|
|
// Just use Parsoid-provided DOM for first rendering
|
|
|
|
// NB: Technically this.modified could be reset to false if this
|
|
|
|
// node is re-attached, but that is an unlikely edge case.
|
2018-03-06 13:00:35 +00:00
|
|
|
if ( !this.modified && model.getElement().originalDomElementsHash ) {
|
2018-02-28 17:35:53 +00:00
|
|
|
this.$originalRefList = $( model.getStore().value(
|
2018-03-06 13:00:35 +00:00
|
|
|
model.getElement().originalDomElementsHash
|
2018-02-15 21:20:16 +00:00
|
|
|
) );
|
2021-08-24 10:24:02 +00:00
|
|
|
if ( !nodes || !nodes.indexOrder.length ) {
|
|
|
|
this.$refmsg.text( emptyText );
|
|
|
|
this.$element.append( this.$refmsg );
|
|
|
|
} else {
|
|
|
|
this.$element.append( this.$originalRefList );
|
|
|
|
}
|
2018-02-15 21:20:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( this.$originalRefList ) {
|
|
|
|
this.$originalRefList.remove();
|
|
|
|
this.$originalRefList = null;
|
|
|
|
}
|
2016-02-03 21:03:41 +00:00
|
|
|
this.$reflist.detach().empty();
|
|
|
|
this.$refmsg.detach();
|
|
|
|
|
|
|
|
if ( refGroup !== '' ) {
|
|
|
|
this.$reflist.attr( 'data-mw-group', refGroup );
|
|
|
|
} else {
|
|
|
|
this.$reflist.removeAttr( 'data-mw-group' );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !nodes || !nodes.indexOrder.length ) {
|
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 {
|
2021-11-03 12:28:17 +00:00
|
|
|
for ( var i = 0, iLen = nodes.indexOrder.length; i < iLen; i++ ) {
|
|
|
|
var index = nodes.indexOrder[ i ];
|
|
|
|
var firstNode = nodes.firstNodes[ index ];
|
2016-02-03 21:03:41 +00:00
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
var key = internalList.keys[ index ];
|
|
|
|
var keyedNodes = nodes.keyedNodes[ key ];
|
2016-02-03 21:03:41 +00:00
|
|
|
keyedNodes = keyedNodes.filter( function ( node ) {
|
|
|
|
// Exclude placeholder references
|
|
|
|
if ( node.getAttribute( 'placeholder' ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Exclude references defined inside the references list node
|
2019-05-17 13:24:26 +00:00
|
|
|
do {
|
|
|
|
node = node.parent;
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( node instanceof ve.dm.MWReferencesListNode ) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-17 13:24:26 +00:00
|
|
|
} while ( node );
|
2016-02-03 21:03:41 +00:00
|
|
|
return true;
|
|
|
|
} );
|
|
|
|
|
|
|
|
if ( !keyedNodes.length ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:28:17 +00:00
|
|
|
var $li = $( '<li>' )
|
2020-01-23 13:08:08 +00:00
|
|
|
.append( this.renderBacklinks( keyedNodes, refGroup ) );
|
2016-02-03 21:03:41 +00:00
|
|
|
|
|
|
|
// Generate reference HTML from first item in key
|
2021-11-03 12:28:17 +00:00
|
|
|
var modelNode = internalList.getItemNode( firstNode.getAttribute( 'listIndex' ) );
|
2016-02-03 21:03:41 +00:00
|
|
|
if ( modelNode && modelNode.length ) {
|
2021-11-03 12:28:17 +00:00
|
|
|
var 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' ) )
|
2016-02-03 21:03:41 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
this.$reflist.append( $li );
|
|
|
|
}
|
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 () {
|
2018-02-28 17:35:53 +00:00
|
|
|
var 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 ) {
|
2021-11-03 12:28:17 +00:00
|
|
|
var $link;
|
2020-01-23 13:08:08 +00:00
|
|
|
if ( keyedNodes.length > 1 ) {
|
|
|
|
// named reference with multiple usages
|
2021-11-03 12:28:17 +00:00
|
|
|
var $refSpan = $( '<span>' ).attr( 'rel', 'mw:referencedBy' );
|
|
|
|
for ( var j = 0, jLen = keyedNodes.length; j < jLen; j++ ) {
|
2020-01-23 13:08:08 +00:00
|
|
|
$link = $( '<a>' ).append(
|
|
|
|
$( '<span>' ).addClass( 'mw-linkback-text' )
|
|
|
|
.text( ( j + 1 ) + ' ' )
|
|
|
|
);
|
|
|
|
if ( refGroup !== '' ) {
|
|
|
|
$link.attr( 'data-mw-group', refGroup );
|
|
|
|
}
|
|
|
|
$refSpan.append( $link );
|
|
|
|
}
|
|
|
|
return $refSpan;
|
|
|
|
} else {
|
|
|
|
// solo reference
|
|
|
|
$link = $( '<a>' ).attr( 'rel', 'mw:referencedBy' ).append(
|
|
|
|
$( '<span>' ).addClass( 'mw-linkback-text' )
|
|
|
|
.text( '↑ ' )
|
|
|
|
);
|
|
|
|
if ( refGroup !== '' ) {
|
|
|
|
$link.attr( 'data-mw-group', refGroup );
|
|
|
|
}
|
|
|
|
return $link;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-02-03 21:03:41 +00:00
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ce.nodeFactory.register( ve.ce.MWReferencesListNode );
|