2 * VisualEditor ContentEditable MWReferenceNode class.
4 * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
9 * ContentEditable MediaWiki reference node.
12 * @extends ve.ce.LeafNode
13 * @mixins ve.ce.FocusableNode
16 * @param {ve.dm.MWReferenceNode} model Model to observe
17 * @param {Object} [config] Configuration options
19 ve.ce.MWReferenceNode = function VeCeMWReferenceNode() {
21 ve.ce.MWReferenceNode.super.apply( this, arguments );
24 ve.ce.FocusableNode.call( this );
27 this.$link = $( '<a>' ).attr( 'href', '#' );
28 this.$element.addClass( 've-ce-mwReferenceNode mw-ref' ).append( this.$link )
29 // In case we have received a version with old-style Cite HTML, remove the
30 // old reference class
31 .removeClass( 'reference' );
32 // Add a backwards-compatible text for browsers that don't support counters
33 this.$text = $( '<span>' ).addClass( 'mw-reflink-text' );
34 this.$link.append( this.$text );
37 this.internalList = this.model.getDocument().internalList;
40 this.connect( this, { setup: 'onSetup' } );
41 this.connect( this, { teardown: 'onTeardown' } );
49 OO.inheritClass( ve.ce.MWReferenceNode, ve.ce.LeafNode );
51 OO.mixinClass( ve.ce.MWReferenceNode, ve.ce.FocusableNode );
53 /* Static Properties */
55 ve.ce.MWReferenceNode.static.name = 'mwReference';
57 ve.ce.MWReferenceNode.static.tagName = 'span';
59 ve.ce.MWReferenceNode.static.primaryCommandName = 'reference';
66 ve.ce.MWReferenceNode.prototype.onSetup = function () {
67 ve.ce.MWReferenceNode.super.prototype.onSetup.call( this );
68 this.internalList.connect( this, { update: 'onInternalListUpdate' } );
72 * Handle teardown event.
74 ve.ce.MWReferenceNode.prototype.onTeardown = function () {
75 // As we are listening to the internal list, we need to make sure
76 // we remove the listeners when this object is removed from the document
77 this.internalList.disconnect( this );
79 ve.ce.MWReferenceNode.super.prototype.onTeardown.call( this );
83 * Handle the updating of the InternalList object.
85 * This will occur after a document transaction.
87 * @param {string[]} groupsChanged A list of groups which have changed in this transaction
89 ve.ce.MWReferenceNode.prototype.onInternalListUpdate = function ( groupsChanged ) {
90 // Only update if this group has been changed
91 if ( groupsChanged.indexOf( this.model.getAttribute( 'listGroup' ) ) !== -1 ) {
97 * @inheritdoc ve.ce.FocusableNode
99 ve.ce.MWReferenceNode.prototype.executeCommand = function () {
100 var command, contextItem,
101 items = ve.ui.contextItemFactory.getRelatedItems( [ this.model ] );
103 if ( items.length ) {
104 contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
106 command = this.getRoot().getSurface().getSurface().commandRegistry.lookup( contextItem.static.commandName );
108 command.execute( this.focusableSurface.getSurface() );
115 * Update the rendering
117 ve.ce.MWReferenceNode.prototype.update = function () {
118 var group = this.model.getGroup();
119 this.$text.text( this.model.getIndexLabel() );
120 this.$link.css( 'counterReset', 'mw-Ref ' + this.model.getIndex() );
122 this.$link.attr( 'data-mw-group', group );
124 this.$link.removeAttr( 'data-mw-group' );
130 ve.ce.nodeFactory.register( ve.ce.MWReferenceNode );