Debian Codegraph @

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

raw symbols SCIP
1 'use strict';
2
3 /*!
4 * VisualEditor ContentEditable MWReferencesListNode 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 references list node.
12 *
13 * @constructor
14 * @extends ve.ce.LeafNode
15 * @mixes ve.ce.FocusableNode
16 * @param {ve.dm.MWReferencesListNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWReferencesListNode = function VeCeMWReferencesListNode() {
20 // Parent constructor
21 ve.ce.MWReferencesListNode.super.apply( this, arguments );
22
23 // Mixin constructors
24 ve.ce.FocusableNode.call( this );
25
26 // Properties
27 this.modified = false;
28
29 // DOM changes
30 this.$element.addClass( 've-ce-mwReferencesListNode' );
31 this.$reflist = $( '<ol>' ).addClass( 'mw-references references' );
32 this.$originalRefList = null;
33 this.$refmsg = $( '<p>' )
34 .addClass( 've-ce-mwReferencesListNode-muted' );
35
36 // Events
37 this.getModel().connect( this, { attributeChange: 'onAttributeChange' } );
38
39 this.updateDebounced = ve.debounce( this.update.bind( this ) );
40
41 // Initialization
42 // Rendering the reference list can be slow, so do it in an idle callback
43 // (i.e. after the editor has finished loading). Previously we used the
44 // Parsoid DOM rendering for the first paint, and updated only when references
45 // were modified, but this means the reference list is out of sync with the
46 // model for features such as T54750.
47 mw.requestIdleCallback( this.update.bind( this ) );
48 };
49
50 /* Inheritance */
51
52 OO.inheritClass( ve.ce.MWReferencesListNode, ve.ce.LeafNode );
53
54 OO.mixinClass( ve.ce.MWReferencesListNode, ve.ce.FocusableNode );
55
56 /* Static Properties */
57
58 ve.ce.MWReferencesListNode.static.name = 'mwReferencesList';
59
60 ve.ce.MWReferencesListNode.static.tagName = 'div';
61
62 ve.ce.MWReferencesListNode.static.primaryCommandName = 'referencesList';
63
64 /* Static Methods */
65
66 /**
67 * @override
68 * @see ve.ce.LeafNode
69 */
70 ve.ce.MWReferencesListNode.static.getDescription = function ( model ) {
71 return model.getAttribute( 'refGroup' );
72 };
73
74 /**
75 * @override
76 * @see ve.ce.FocusableNode
77 */
78 ve.ce.MWReferencesListNode.prototype.getExtraHighlightClasses = function () {
79 return ve.ce.FocusableNode.prototype
80 .getExtraHighlightClasses.apply( this, arguments )
81 .concat( [ 've-ce-mwReferencesListNode-highlight' ] );
82 };
83
84 /* Methods */
85
86 /**
87 * Handle setup events.
88 */
89 ve.ce.MWReferencesListNode.prototype.onSetup = function () {
90 const internalList = this.getModel().getDocument().getInternalList();
91 const listNode = internalList.getListNode();
92
93 internalList.connect( this, { update: 'onInternalListUpdate' } );
94 listNode.connect( this, { update: 'onListNodeUpdate' } );
95
96 // Parent method
97 ve.ce.MWReferencesListNode.super.prototype.onSetup.call( this );
98 };
99
100 /**
101 * Handle teardown events.
102 */
103 ve.ce.MWReferencesListNode.prototype.onTeardown = function () {
104 // Parent method
105 ve.ce.MWReferencesListNode.super.prototype.onTeardown.call( this );
106
107 if ( !this.getModel() || !this.getModel().getDocument() ) {
108 return;
109 }
110 const internalList = this.getModel().getDocument().getInternalList();
111 const listNode = internalList.getListNode();
112
113 internalList.disconnect( this, { update: 'onInternalListUpdate' } );
114 listNode.disconnect( this, { update: 'onListNodeUpdate' } );
115 };
116
117 /**
118 * Handle the updating of the InternalList object.
119 *
120 * This will occur after a document transaction.
121 *
122 * @param {string[]} groupsChanged A list of groups which have changed in this transaction
123 */
124 ve.ce.MWReferencesListNode.prototype.onInternalListUpdate = function ( groupsChanged ) {
125 if ( !this.getModel() ) {
126 return;
127 }
128 // Only update if this group has been changed
129 if ( groupsChanged.indexOf( this.getModel().getAttribute( 'listGroup' ) ) !== -1 ) {
130 this.modified = true;
131 this.updateDebounced();
132 }
133 };
134
135 /**
136 * Rerender when the 'listGroup' attribute changes in the model.
137 *
138 * @param {string} key Attribute key
139 * @param {string} from Old value
140 * @param {string} to New value
141 */
142 ve.ce.MWReferencesListNode.prototype.onAttributeChange = function ( key ) {
143 switch ( key ) {
144 case 'listGroup':
145 this.updateDebounced();
146 this.modified = true;
147 break;
148 case 'isResponsive':
149 this.updateClasses();
150 break;
151 }
152 };
153
154 /**
155 * Handle the updating of the InternalListNode.
156 *
157 * This will occur after changes to any InternalItemNode.
158 */
159 ve.ce.MWReferencesListNode.prototype.onListNodeUpdate = function () {
160 // When the list node updates we're not sure which list group the item
161 // belonged to so we always update
162 // TODO: Only re-render the reference which has been edited
163 this.updateDebounced();
164 };
165
166 /**
167 * Update the references list.
168 */
169 ve.ce.MWReferencesListNode.prototype.update = function () {
170 const model = this.getModel();
171
172 // Check the node hasn't been destroyed, as this method is debounced.
173 if ( !model ) {
174 return;
175 }
176
177 const refGroup = model.getAttribute( 'refGroup' );
178
179 const docRefs = ve.dm.MWDocumentReferences.static.refsForDoc( model.getDocument() );
180 const groupRefs = docRefs.getGroupRefs( refGroup );
181 const hasModelReferences = !groupRefs.isEmpty();
182
183 let emptyText;
184 if ( refGroup !== '' ) {
185 emptyText = ve.msg( 'cite-ve-referenceslist-isempty', refGroup );
186 } else {
187 emptyText = ve.msg( 'cite-ve-referenceslist-isempty-default' );
188 }
189
190 let originalDomElements;
191 if ( model.getElement().originalDomElementsHash ) {
192 originalDomElements = model.getStore().value(
193 model.getElement().originalDomElementsHash
194 );
195 }
196 // Use the Parsoid-provided DOM if:
197 //
198 // * There are no references in the model
199 // * There have been no changes to the references in the model (!this.modified)
200 //
201 // In practice this is for he.wiki where references are template-generated (T187495)
202 if (
203 !hasModelReferences &&
204 !this.modified &&
205 originalDomElements
206 ) {
207 // Create a copy when importing to the main document, as extensions may
208 this.$originalRefList = $( ve.copyDomElements( originalDomElements, document ) );
209 // modify DOM nodes in the main doc.
210 if ( this.$originalRefList.find( 'li' ).length ) {
211 this.$element.append( this.$originalRefList );
212 } else {
213 this.$refmsg.text( emptyText );
214 this.$element.append( this.$refmsg );
215 }
216 return;
217 }
218
219 if ( this.$originalRefList ) {
220 this.$originalRefList.remove();
221 this.$originalRefList = null;
222 }
223 // Copy CSS to dynamic ref list
224 if ( originalDomElements ) {
225 // Get first container, e.g. skipping TemplateStyles
226 const divs = originalDomElements.filter( ( element ) => element.tagName === 'DIV' );
227 if ( divs.length ) {
228 // eslint-disable-next-line mediawiki/class-doc
229 this.$element.addClass( divs[ 0 ].getAttribute( 'class' ) );
230 this.$element.attr( 'style', divs[ 0 ].getAttribute( 'style' ) );
231 }
232 }
233
234 this.$reflist.detach().empty().attr( 'data-mw-group', refGroup || null );
235 this.$refmsg.detach();
236
237 if ( !hasModelReferences ) {
238 this.$refmsg.text( emptyText );
239 this.$element.append( this.$refmsg );
240 } else {
241 // Render all at once.
242 this.$reflist.append(
243 groupRefs.getTopLevelKeysInReflistOrder()
244 .map( ( listKey ) => this.renderListItem(
245 groupRefs, refGroup, listKey
246 ) )
247 );
248
249 this.updateClasses();
250 this.$element.append( this.$reflist );
251 }
252 };
253
254 /**
255 * Render a reference list item
256 *
257 * @private
258 * @param {ve.dm.MWGroupReferences} groupRefs object holding calculated information about all group refs
259 * @param {string} refGroup Reference group
260 * @param {string} key top-level reference key, doesn't necessarily exist
261 * @return {jQuery} Rendered list item
262 */
263 ve.ce.MWReferencesListNode.prototype.renderListItem = function ( groupRefs, refGroup, key ) {
264 const ref = groupRefs.getInternalModelNode( key );
265 const backlinkNodes = groupRefs.getRefUsages( key );
266 const subrefs = groupRefs.getSubrefs( key );
267
268 const $li = $( '<li>' )
269 .css( '--footnote-number', `"${ groupRefs.getIndexLabel( key ) }."` )
270 .append( this.renderBacklinks( backlinkNodes, refGroup ), ' ' );
271
272 if ( ref && ref.length ) {
273 const refPreview = new ve.ui.MWPreviewElement( ref, { useView: true } );
274 $li.append(
275 $( '<span>' )
276 .addClass( 'reference-text' )
277 .append( refPreview.$element )
278 );
279
280 if ( this.getRoot() ) {
281 const surface = this.getRoot().getSurface().getSurface();
282 // TODO: attach to the singleton click handler on the surface
283 $li.on( 'mousedown', ( e ) => {
284 if ( ve.isUnmodifiedLeftClick( e ) ) {
285 const node = groupRefs.getRefNode( key );
286 const items = ve.ui.contextItemFactory.getRelatedItems( [ node ] )
287 .filter( ( item ) => item.name !== 'mobileActions' );
288 if ( items.length ) {
289 const contextItem = ve.ui.contextItemFactory.lookup( items[ 0 ].name );
290 if ( contextItem ) {
291 const command = surface.commandRegistry
292 .lookup( contextItem.static.commandName );
293 if ( command ) {
294 const fragmentArgs = {
295 fragment: surface.getModel()
296 .getLinearFragment( node.getOuterRange(), true ),
297 selectFragmentOnClose: false
298 };
299 const newArgs = ve.copy( command.args );
300 if ( command.name === 'reference' ) {
301 newArgs[ 1 ] = fragmentArgs;
302 } else {
303 ve.extendObject( newArgs[ 0 ], fragmentArgs );
304 }
305 command.execute( surface, newArgs );
306 }
307 }
308 }
309 }
310 e.preventDefault();
311 } );
312 }
313 } else {
314 $li.append(
315 $( '<span>' )
316 .addClass( 've-ce-mwReferencesListNode-muted' )
317 .text( subrefs.length ? ve.msg( 'cite-ve-referenceslist-missing-parent' ) :
318 ve.msg( 'cite-ve-referenceslist-missingref-in-list' ) )
319 ).addClass( 've-ce-mwReferencesListNode-missingRef' );
320 }
321
322 if ( subrefs.length ) {
323 $li.append(
324 $( '<ol>' ).append(
325 subrefs.map( ( subNode ) => this.renderListItem(
326 groupRefs, refGroup, subNode.getAttribute( 'listKey' )
327 ) )
328 )
329 );
330 }
331
332 return $li;
333 };
334
335 /**
336 * Update ref list classes.
337 *
338 * Currently used to set responsive layout
339 */
340 ve.ce.MWReferencesListNode.prototype.updateClasses = function () {
341 const isResponsive = this.getModel().getAttribute( 'isResponsive' );
342
343 this.$element
344 .toggleClass( 'mw-references-wrap', isResponsive )
345 .toggleClass( 'mw-references-columns', isResponsive && this.$reflist.children().length > 10 );
346 };
347
348 /**
349 * Build markers for backlinks
350 *
351 * @param {ve.dm.Node[]} keyedNodes A list of ref nodes linked to a reference list item
352 * @param {string} refGroup Reference group name
353 * @return {jQuery} Element containing backlinks
354 */
355 ve.ce.MWReferencesListNode.prototype.renderBacklinks = function ( keyedNodes, refGroup ) {
356 if ( keyedNodes.length === 1 ) {
357 return $( '<a>' )
358 .attr( 'rel', 'mw:referencedBy' )
359 .attr( 'data-mw-group', refGroup || null )
360 .append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( '↑ ' ) );
361 }
362
363 // named reference with multiple usages
364 const $refSpan = $( '<span>' ).attr( 'rel', 'mw:referencedBy' );
365 for ( let i = 0; i < keyedNodes.length; i++ ) {
366 $( '<a>' )
367 .attr( 'data-mw-group', refGroup || null )
368 // FIXME: i18n backlink numbering
369 .append( $( '<span>' ).addClass( 'mw-linkback-text' ).text( ( i + 1 ) + ' ' ) )
370 .appendTo( $refSpan );
371 }
372 return $refSpan;
373 };
374
375 /* Registration */
376
377 ve.ce.nodeFactory.register( ve.ce.MWReferencesListNode );