'use strict'; /*! * VisualEditor ContentEditable MWReferencesListNode class. * * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt * @license MIT */ /** * ContentEditable MediaWiki references list node. * * @class * @extends ve.ce.LeafNode * @mixin ve.ce.FocusableNode * * @constructor * @param {ve.dm.MWReferencesListNode} model Model to observe * @param {Object} [config] Configuration options */ ve.ce.MWReferencesListNode = function VeCeMWReferencesListNode() { // Parent constructor ve.ce.MWReferencesListNode.super.apply( this, arguments ); // Mixin constructors ve.ce.FocusableNode.call( this ); // Properties this.internalList = null; this.listNode = null; this.modified = false; // DOM changes this.$element.addClass( 've-ce-mwReferencesListNode' ); this.$reflist = $( '
' ) .addClass( 've-ce-mwReferencesListNode-muted' ); // Events this.getModel().connect( this, { attributeChange: 'onAttributeChange' } ); this.updateDebounced = ve.debounce( this.update.bind( this ) ); // Initialization // 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 ) ); }; /* 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' ); }; /** * @inheritdoc ve.ce.FocusableNode */ ve.ce.MWReferencesListNode.prototype.getExtraHighlightClasses = function () { const extraClasses = ve.ce.FocusableNode.prototype.getExtraHighlightClasses.apply( this, arguments ); return extraClasses.concat( [ 've-ce-mwReferencesListNode-highlight' ] ); }; /* Methods */ /** * Handle setup events. * * @method */ ve.ce.MWReferencesListNode.prototype.onSetup = function () { this.internalList = this.getModel().getDocument().getInternalList(); this.listNode = this.internalList.getListNode(); this.internalList.connect( this, { update: 'onInternalListUpdate' } ); this.listNode.connect( this, { update: 'onListNodeUpdate' } ); // Parent method ve.ce.MWReferencesListNode.super.prototype.onSetup.call( this ); }; /** * Handle teardown events. * * @method */ ve.ce.MWReferencesListNode.prototype.onTeardown = function () { // Parent method ve.ce.MWReferencesListNode.super.prototype.onTeardown.call( this ); if ( !this.listNode ) { return; } 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 if ( groupsChanged.indexOf( this.getModel().getAttribute( 'listGroup' ) ) !== -1 ) { this.modified = true; this.updateDebounced(); } }; /** * 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 ) { switch ( key ) { case 'listGroup': this.updateDebounced(); this.modified = true; break; case 'isResponsive': this.updateClasses(); break; } }; /** * 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 this.updateDebounced(); }; /** * Update the references list. */ ve.ce.MWReferencesListNode.prototype.update = function () { const model = this.getModel(); // Check the node hasn't been destroyed, as this method is debounced. if ( !model ) { return; } 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 ); let emptyText; if ( refGroup !== '' ) { emptyText = ve.msg( 'cite-ve-referenceslist-isempty', refGroup ); } else { emptyText = ve.msg( 'cite-ve-referenceslist-isempty-default' ); } // 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 ) { // 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( model.getElement().originalDomElementsHash ), document ) ); if ( this.$originalRefList.find( 'li' ).length ) { this.$element.append( this.$originalRefList ); } else { this.$refmsg.text( emptyText ); this.$element.append( this.$refmsg ); } return; } if ( this.$originalRefList ) { this.$originalRefList.remove(); this.$originalRefList = null; } this.$reflist.detach().empty().attr( 'data-mw-group', refGroup || null ); this.$refmsg.detach(); if ( !hasModelReferences ) { this.$refmsg.text( emptyText ); this.$element.append( this.$refmsg ); } else { nodes.indexOrder.forEach( function ( index ) { const firstNode = nodes.firstNodes[ index ]; const key = internalList.keys[ index ]; let keyedNodes = nodes.keyedNodes[ key ]; keyedNodes = keyedNodes.filter( function ( node ) { // Exclude placeholders and references defined inside the references list node return !node.getAttribute( 'placeholder' ) && !node.findParent( ve.dm.MWReferencesListNode ); } ); if ( !keyedNodes.length ) { return; } const $li = $( '