Debian Codegraph @

extensions/Cite/modules/ve-cite/ve.ce.MWReferenceNode.js

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor ContentEditable MWReferenceNode class.
5 *
6 * @copyright 2011-2018 VisualEditor Team's Cite sub-team and others; see AUTHORS.txt
7 * @license MIT
8 */
9
10 /**
11 * ContentEditable MediaWiki reference node.
12 *
13 * @constructor
14 * @extends ve.ce.LeafNode
15 * @mixes ve.ce.FocusableNode
16 * @param {ve.dm.MWReferenceNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWReferenceNode = function VeCeMWReferenceNode() {
20 // Parent constructor
21 ve.ce.MWReferenceNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.ce.FocusableNode.call( this );
25
26 // DOM changes
27 this.$link = $( '<a>' ).attr( 'href', '#' );
28 this.$element.addClass( 've-ce-mwReferenceNode mw-ref reference' ).append( this.$link );
29 this.$text = $( '<span>' ).addClass( 'mw-reflink-text' );
30 this.$link.append( this.$text );
31
32 this.index = '';
33 this.internalList = this.model.getDocument().internalList;
34
35 // Events
36 this.connect( this, {
37 setup: 'onSetup',
38 teardown: 'onTeardown'
39 } );
40 this.model.connect( this, { attributeChange: 'onAttributeChange' } );
41
42 // Initialization
43 this.update();
44 };
45
46 /* Inheritance */
47
48 OO.inheritClass( ve.ce.MWReferenceNode, ve.ce.LeafNode );
49
50 OO.mixinClass( ve.ce.MWReferenceNode, ve.ce.FocusableNode );
51
52 /* Static Properties */
53
54 ve.ce.MWReferenceNode.static.name = 'mwReference';
55
56 ve.ce.MWReferenceNode.static.tagName = 'sup';
57
58 ve.ce.MWReferenceNode.static.primaryCommandName = 'reference';
59
60 /* Methods */
61
62 /**
63 * Handle setup event.
64 */
65 ve.ce.MWReferenceNode.prototype.onSetup = function () {
66 ve.ce.MWReferenceNode.super.prototype.onSetup.call( this );
67 // FIXME: if this is about getIndex, just do it once at a higher level.
68 this.internalList.connect( this, { update: 'onInternalListUpdate' } );
69 };
70
71 /**
72 * Handle teardown event.
73 */
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 );
78
79 ve.ce.MWReferenceNode.super.prototype.onTeardown.call( this );
80 };
81
82 /**
83 * Handle the updating of the InternalList object.
84 *
85 * This will occur after a document transaction.
86 *
87 * @param {string[]} groupsChanged A list of groups which have changed in this transaction
88 */
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 ) {
92 this.update();
93 }
94 };
95
96 /**
97 * Handle attribute change events
98 *
99 * @param {string} key Attribute key
100 * @param {string} from Old value
101 * @param {string} to New value
102 */
103 ve.ce.MWReferenceNode.prototype.onAttributeChange = function ( key ) {
104 if ( key === 'placeholder' ) {
105 this.update();
106 }
107 };
108
109 /**
110 * @override
111 * @see ve.ce.FocusableNode
112 */
113 ve.ce.MWReferenceNode.prototype.executeCommand = function () {
114 const items = ve.ui.contextItemFactory.getRelatedItems( [ this.model ] );
115
116 if ( items.length ) {
117 const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
118 if ( contextItem ) {
119 const command = this.getRoot().getSurface().getSurface().commandRegistry
120 .lookup( contextItem.static.commandName );
121 if ( command ) {
122 command.execute( this.focusableSurface.getSurface() );
123 }
124 }
125 }
126 };
127
128 /**
129 * Update the rendering
130 */
131 ve.ce.MWReferenceNode.prototype.update = function () {
132 this.$text.html( this.model.getIndexLabel() );
133 this.$link
134 .attr( 'data-mw-group', this.model.getGroup() || null );
135 this.$element.toggleClass( 've-ce-mwReferenceNode-placeholder', !!this.model.getAttribute( 'placeholder' ) );
136 };
137
138 /* Registration */
139
140 ve.ce.nodeFactory.register( ve.ce.MWReferenceNode );