]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - extensions/Cite/modules/ve-cite/ve.ce.MWReferenceNode.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / extensions / Cite / modules / ve-cite / ve.ce.MWReferenceNode.js
1 /*!
2  * VisualEditor ContentEditable MWReferenceNode class.
3  *
4  * @copyright 2011-2017 Cite VisualEditor Team and others; see AUTHORS.txt
5  * @license The MIT License (MIT); see LICENSE.txt
6  */
7
8 /**
9  * ContentEditable MediaWiki reference node.
10  *
11  * @class
12  * @extends ve.ce.LeafNode
13  * @mixins ve.ce.FocusableNode
14  *
15  * @constructor
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' ).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 );
35
36         this.index = '';
37         this.internalList = this.model.getDocument().internalList;
38
39         // Events
40         this.connect( this, { setup: 'onSetup' } );
41         this.connect( this, { teardown: 'onTeardown' } );
42
43         // Initialization
44         this.update();
45 };
46
47 /* Inheritance */
48
49 OO.inheritClass( ve.ce.MWReferenceNode, ve.ce.LeafNode );
50
51 OO.mixinClass( ve.ce.MWReferenceNode, ve.ce.FocusableNode );
52
53 /* Static Properties */
54
55 ve.ce.MWReferenceNode.static.name = 'mwReference';
56
57 ve.ce.MWReferenceNode.static.tagName = 'span';
58
59 ve.ce.MWReferenceNode.static.primaryCommandName = 'reference';
60
61 /* Methods */
62
63 /**
64  * Handle setup event.
65  */
66 ve.ce.MWReferenceNode.prototype.onSetup = function () {
67         ve.ce.MWReferenceNode.super.prototype.onSetup.call( this );
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  * @inheritdoc ve.ce.FocusableNode
98  */
99 ve.ce.MWReferenceNode.prototype.executeCommand = function () {
100         var command, contextItem,
101                 items = ve.ui.contextItemFactory.getRelatedItems( [ this.model ] );
102
103         if ( items.length ) {
104                 contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
105                 if ( contextItem ) {
106                         command = this.getRoot().getSurface().getSurface().commandRegistry.lookup( contextItem.static.commandName );
107                         if ( command ) {
108                                 command.execute( this.focusableSurface.getSurface() );
109                         }
110                 }
111         }
112 };
113
114 /**
115  * Update the rendering
116  */
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() );
121         if ( group ) {
122                 this.$link.attr( 'data-mw-group', group );
123         } else {
124                 this.$link.removeAttr( 'data-mw-group' );
125         }
126 };
127
128 /* Registration */
129
130 ve.ce.nodeFactory.register( ve.ce.MWReferenceNode );