]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - extensions/Cite/modules/ve-cite/ve.dm.MWReferenceNode.js
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / extensions / Cite / modules / ve-cite / ve.dm.MWReferenceNode.js
1 /*!
2  * VisualEditor DataModel 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  * DataModel MediaWiki reference node.
10  *
11  * @class
12  * @extends ve.dm.LeafNode
13  * @mixins ve.dm.FocusableNode
14  *
15  * @constructor
16  * @param {Object} [element] Reference to element in linear model
17  */
18 ve.dm.MWReferenceNode = function VeDmMWReferenceNode() {
19         // Parent constructor
20         ve.dm.MWReferenceNode.super.apply( this, arguments );
21
22         // Mixin constructors
23         ve.dm.FocusableNode.call( this );
24
25         // Event handlers
26         this.connect( this, {
27                 root: 'onRoot',
28                 unroot: 'onUnroot',
29                 attributeChange: 'onAttributeChange'
30         } );
31 };
32
33 /* Inheritance */
34
35 OO.inheritClass( ve.dm.MWReferenceNode, ve.dm.LeafNode );
36
37 OO.mixinClass( ve.dm.MWReferenceNode, ve.dm.FocusableNode );
38
39 /* Static members */
40
41 ve.dm.MWReferenceNode.static.name = 'mwReference';
42
43 ve.dm.MWReferenceNode.static.matchTagNames = null;
44
45 ve.dm.MWReferenceNode.static.matchRdfaTypes = [ 'mw:Extension/ref' ];
46
47 ve.dm.MWReferenceNode.static.allowedRdfaTypes = [ 'dc:references' ];
48
49 ve.dm.MWReferenceNode.static.isContent = true;
50
51 ve.dm.MWReferenceNode.static.blacklistedAnnotationTypes = [ 'link' ];
52
53 /**
54  * Regular expression for parsing the listKey attribute
55  * @static
56  * @property {RegExp}
57  * @inheritable
58  */
59 ve.dm.MWReferenceNode.static.listKeyRegex = /^(auto|literal)\/(.*)$/;
60
61 ve.dm.MWReferenceNode.static.toDataElement = function ( domElements, converter ) {
62         var dataElement, mwDataJSON, mwData, reflistItemId, body, refGroup, listGroup, autoKeyed, listKey, queueResult, listIndex, contentsUsed;
63
64         function getReflistItemHtml( id ) {
65                 var elem = converter.getHtmlDocument().getElementById( id );
66                 return elem && elem.innerHTML || '';
67         }
68
69         mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' );
70         mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
71         reflistItemId = mwData.body && mwData.body.id;
72         body = ( mwData.body && mwData.body.html ) ||
73                 ( reflistItemId && getReflistItemHtml( reflistItemId ) ) ||
74                 '';
75         refGroup = mwData.attrs && mwData.attrs.group || '';
76         listGroup = this.name + '/' + refGroup;
77         autoKeyed = !mwData.attrs || mwData.attrs.name === undefined;
78         listKey = autoKeyed ?
79                 'auto/' + converter.internalList.getNextUniqueNumber() :
80                 'literal/' + mwData.attrs.name;
81         queueResult = converter.internalList.queueItemHtml( listGroup, listKey, body );
82         listIndex = queueResult.index;
83         contentsUsed = ( body !== '' && queueResult.isNew );
84
85         dataElement = {
86                 type: this.name,
87                 attributes: {
88                         mw: mwData,
89                         originalMw: mwDataJSON,
90                         listIndex: listIndex,
91                         listGroup: listGroup,
92                         listKey: listKey,
93                         refGroup: refGroup,
94                         contentsUsed: contentsUsed
95                 }
96         };
97         if ( reflistItemId ) {
98                 dataElement.attributes.refListItemId = reflistItemId;
99         }
100         return dataElement;
101 };
102
103 ve.dm.MWReferenceNode.static.toDomElements = function ( dataElement, doc, converter ) {
104         var itemNodeHtml, originalHtml, mwData, i, iLen, keyedNodes, setContents, contentsAlreadySet,
105                 originalMw, listKeyParts, name, group, $link,
106                 isForClipboard = converter.isForClipboard(),
107                 el = doc.createElement( 'span' ),
108                 itemNodeWrapper = doc.createElement( 'div' ),
109                 originalHtmlWrapper = doc.createElement( 'div' ),
110                 itemNode = converter.internalList.getItemNode( dataElement.attributes.listIndex ),
111                 itemNodeRange = itemNode.getRange();
112
113         el.setAttribute( 'typeof', 'mw:Extension/ref' );
114
115         mwData = dataElement.attributes.mw ? ve.copy( dataElement.attributes.mw ) : {};
116         mwData.name = 'ref';
117
118         setContents = dataElement.attributes.contentsUsed;
119
120         keyedNodes = converter.internalList
121                 .getNodeGroup( dataElement.attributes.listGroup )
122                 .keyedNodes[ dataElement.attributes.listKey ];
123
124         if ( setContents ) {
125                 // Check if a previous node has already set the content. If so, we don't overwrite this
126                 // node's contents.
127                 contentsAlreadySet = false;
128                 if ( keyedNodes ) {
129                         for ( i = 0, iLen = keyedNodes.length; i < iLen; i++ ) {
130                                 if ( keyedNodes[ i ].element === dataElement ) {
131                                         break;
132                                 }
133                                 if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
134                                         contentsAlreadySet = true;
135                                         break;
136                                 }
137                         }
138                 }
139         } else {
140                 // Check if any other nodes with this key provided content. If not
141                 // then we attach the contents to the first reference with this key
142
143                 // Check that this is the first reference with its key
144                 if ( keyedNodes && dataElement === keyedNodes[ 0 ].element ) {
145                         setContents = true;
146                         // Check no other reference originally defined the contents
147                         // As this is keyedNodes[0] we can start at 1
148                         for ( i = 1, iLen = keyedNodes.length; i < iLen; i++ ) {
149                                 if ( keyedNodes[ i ].element.attributes.contentsUsed ) {
150                                         setContents = false;
151                                         break;
152                                 }
153                         }
154                 }
155         }
156
157         if ( setContents && !contentsAlreadySet ) {
158                 converter.getDomSubtreeFromData(
159                         itemNode.getDocument().getFullData( itemNodeRange, true ),
160                         itemNodeWrapper
161                 );
162                 itemNodeHtml = itemNodeWrapper.innerHTML; // Returns '' if itemNodeWrapper is empty
163                 originalHtml = ve.getProp( mwData, 'body', 'html' ) ||
164                         ( ve.getProp( mwData, 'body', 'id' ) !== undefined && itemNode.getAttribute( 'originalHtml' ) ) ||
165                         '';
166                 originalHtmlWrapper.innerHTML = originalHtml;
167                 // Only set body.html if itemNodeHtml and originalHtml are actually different,
168                 // or we are writing the clipboard for use in another VE instance
169                 if ( isForClipboard || !originalHtmlWrapper.isEqualNode( itemNodeWrapper ) ) {
170                         ve.setProp( mwData, 'body', 'html', itemNodeHtml );
171                 }
172         }
173
174         // If we have no internal item data for this reference, don't let it get pasted into
175         // another VE document. T110479
176         if ( isForClipboard && itemNodeRange.isCollapsed() ) {
177                 el.setAttribute( 'data-ve-ignore', 'true' );
178         }
179
180         // Generate name
181         listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
182         if ( listKeyParts[ 1 ] === 'auto' ) {
183                 // Only render a name if this key was reused
184                 if ( keyedNodes.length > 1 ) {
185                         // Allocate a unique list key, then strip the 'literal/'' prefix
186                         name = converter.internalList.getUniqueListKey(
187                                 dataElement.attributes.listGroup,
188                                 dataElement.attributes.listKey,
189                                 // Generate a name starting with ':' to distinguish it from normal names
190                                 'literal/:'
191                         ).slice( 'literal/'.length );
192                 } else {
193                         name = undefined;
194                 }
195         } else {
196                 // Use literal name
197                 name = listKeyParts[ 2 ];
198         }
199         // Set name
200         if ( name !== undefined ) {
201                 ve.setProp( mwData, 'attrs', 'name', name );
202         }
203
204         // Set or clear group
205         if ( dataElement.attributes.refGroup !== '' ) {
206                 ve.setProp( mwData, 'attrs', 'group', dataElement.attributes.refGroup );
207         } else if ( mwData.attrs ) {
208                 delete mwData.attrs.refGroup;
209         }
210
211         // If mwAttr and originalMw are the same, use originalMw to prevent reserialization,
212         // unless we are writing the clipboard for use in another VE instance
213         // Reserialization has the potential to reorder keys and so change the DOM unnecessarily
214         originalMw = dataElement.attributes.originalMw;
215         if ( !isForClipboard && originalMw && ve.compare( mwData, JSON.parse( originalMw ) ) ) {
216                 el.setAttribute( 'data-mw', originalMw );
217
218                 // Return the original DOM elements if possible
219                 if ( dataElement.originalDomElementsIndex !== undefined ) {
220                         return ve.copyDomElements( converter.getStore().value( dataElement.originalDomElementsIndex ), doc );
221                 }
222         } else {
223                 el.setAttribute( 'data-mw', JSON.stringify( mwData ) );
224
225                 // HTML for the external clipboard, it will be ignored by the converter
226                 group = this.getGroup( dataElement );
227                 $link = $( '<a>', doc ).css(
228                         'counterReset', 'mw-Ref ' + this.getIndex( dataElement, converter.internalList )
229                 );
230                 if ( group ) {
231                         $link.attr( 'data-mw-group', this.getGroup( dataElement ) );
232                 }
233                 $( el ).addClass( 'mw-ref' ).append(
234                         $link.append(
235                                 $( '<span>', doc ).addClass( 'mw-reflink-text' ).text( this.getIndexLabel( dataElement, converter.internalList ) )
236                         )
237                 );
238         }
239
240         return [ el ];
241 };
242
243 ve.dm.MWReferenceNode.static.remapInternalListIndexes = function ( dataElement, mapping, internalList ) {
244         var listKeyParts;
245         // Remap listIndex
246         dataElement.attributes.listIndex = mapping[ dataElement.attributes.listIndex ];
247
248         // Remap listKey if it was automatically generated
249         listKeyParts = dataElement.attributes.listKey.match( this.listKeyRegex );
250         if ( listKeyParts[ 1 ] === 'auto' ) {
251                 dataElement.attributes.listKey = 'auto/' + internalList.getNextUniqueNumber();
252         }
253 };
254
255 ve.dm.MWReferenceNode.static.remapInternalListKeys = function ( dataElement, internalList ) {
256         var suffix = '';
257         // Try name, name2, name3, ... until unique
258         while ( internalList.keys.indexOf( dataElement.attributes.listKey + suffix ) !== -1 ) {
259                 suffix = suffix ? suffix + 1 : 2;
260         }
261         if ( suffix ) {
262                 dataElement.attributes.listKey = dataElement.attributes.listKey + suffix;
263         }
264 };
265
266 /**
267  * Gets the index for the reference
268  *
269  * @static
270  * @param {Object} dataElement Element data
271  * @param {ve.dm.InternalList} internalList Internal list
272  * @return {number} Index
273  */
274 ve.dm.MWReferenceNode.static.getIndex = function ( dataElement, internalList ) {
275         var listIndex, listGroup, position,
276                 overrideIndex = ve.getProp( dataElement, 'internal', 'overrideIndex' );
277
278         if ( overrideIndex ) {
279                 return overrideIndex;
280         }
281
282         listIndex = dataElement.attributes.listIndex;
283         listGroup = dataElement.attributes.listGroup;
284         position = internalList.getIndexPosition( listGroup, listIndex );
285
286         return position + 1;
287 };
288
289 /**
290  * Gets the group for the reference
291  *
292  * @static
293  * @param {Object} dataElement Element data
294  * @return {string} Group
295  */
296 ve.dm.MWReferenceNode.static.getGroup = function ( dataElement ) {
297         return dataElement.attributes.refGroup;
298 };
299
300 /**
301  * Gets the index label for the reference
302  *
303  * @static
304  * @param {Object} dataElement Element data
305  * @param {ve.dm.InternalList} internalList Internal list
306  * @return {string} Reference label
307  */
308 ve.dm.MWReferenceNode.static.getIndexLabel = function ( dataElement, internalList ) {
309         var refGroup = dataElement.attributes.refGroup,
310                 index = ve.dm.MWReferenceNode.static.getIndex( dataElement, internalList );
311
312         return '[' + ( refGroup ? refGroup + ' ' : '' ) + index + ']';
313 };
314
315 /**
316  * @inheritdoc
317  */
318 ve.dm.MWReferenceNode.static.cloneElement = function () {
319         var clone = ve.dm.MWReferenceNode.super.static.cloneElement.apply( this, arguments );
320         delete clone.attributes.contentsUsed;
321         delete clone.attributes.mw;
322         delete clone.attributes.originalMw;
323         return clone;
324 };
325
326 /**
327  * @inheritdoc
328  */
329 ve.dm.MWReferenceNode.static.describeChange = function ( key, change ) {
330         if ( key === 'refGroup' ) {
331                 if ( change.from ) {
332                         if ( change.to ) {
333                                 return ve.msg( 'cite-ve-changedesc-reflist-group-both', change.from, change.to );
334                         } else {
335                                 return ve.msg( 'cite-ve-changedesc-reflist-group-from', change.from );
336                         }
337                 }
338                 return ve.msg( 'cite-ve-changedesc-reflist-group-to', change.to );
339         }
340         if ( key === 'refListItemId' ) {
341                 return ve.msg( 'cite-ve-changedesc-reflist-item-id' );
342         }
343 };
344
345 /* Methods */
346
347 /**
348  * Don't allow reference nodes to be edited if we can't find their contents.
349  *
350  * @inheritdoc
351  */
352 ve.dm.MWReferenceNode.prototype.isEditable = function () {
353         var internalItem = this.getInternalItem();
354         return internalItem && internalItem.getLength() > 0;
355 };
356
357 /**
358  * Gets the internal item node associated with this node
359  *
360  * @return {ve.dm.InternalItemNode} Item node
361  */
362 ve.dm.MWReferenceNode.prototype.getInternalItem = function () {
363         return this.getDocument().getInternalList().getItemNode( this.getAttribute( 'listIndex' ) );
364 };
365
366 /**
367  * Gets the index for the reference
368  *
369  * @return {number} Index
370  */
371 ve.dm.MWReferenceNode.prototype.getIndex = function () {
372         return this.constructor.static.getIndex( this.element, this.getDocument().getInternalList() );
373 };
374
375 /**
376  * Gets the group for the reference
377  *
378  * @return {string} Group
379  */
380 ve.dm.MWReferenceNode.prototype.getGroup = function () {
381         return this.constructor.static.getGroup( this.element );
382 };
383
384 /**
385  * Gets the index label for the reference
386  *
387  * @return {string} Reference label
388  */
389 ve.dm.MWReferenceNode.prototype.getIndexLabel = function () {
390         return this.constructor.static.getIndexLabel( this.element, this.getDocument().getInternalList() );
391 };
392
393 /**
394  * Handle the node being attached to the root
395  */
396 ve.dm.MWReferenceNode.prototype.onRoot = function () {
397         this.addToInternalList();
398 };
399
400 /**
401  * Handle the node being detached from the root
402  *
403  * @param {ve.dm.DocumentNode} oldRoot Old document root
404  */
405 ve.dm.MWReferenceNode.prototype.onUnroot = function ( oldRoot ) {
406         if ( this.getDocument().getDocumentNode() === oldRoot ) {
407                 this.removeFromInternalList();
408         }
409 };
410
411 /**
412  * Register the node with the internal list
413  */
414 ve.dm.MWReferenceNode.prototype.addToInternalList = function () {
415         if ( this.getRoot() === this.getDocument().getDocumentNode() ) {
416                 this.registeredListGroup = this.element.attributes.listGroup;
417                 this.registeredListKey = this.element.attributes.listKey;
418                 this.registeredListIndex = this.element.attributes.listIndex;
419                 this.getDocument().getInternalList().addNode(
420                         this.registeredListGroup,
421                         this.registeredListKey,
422                         this.registeredListIndex,
423                         this
424                 );
425         }
426 };
427
428 /**
429  * Unregister the node from the internal list
430  */
431 ve.dm.MWReferenceNode.prototype.removeFromInternalList = function () {
432         if ( !this.registeredListGroup ) {
433                 // Don't try to remove if we haven't been added in the first place.
434                 return;
435         }
436         this.getDocument().getInternalList().removeNode(
437                 this.registeredListGroup,
438                 this.registeredListKey,
439                 this.registeredListIndex,
440                 this
441         );
442 };
443
444 ve.dm.MWReferenceNode.prototype.onAttributeChange = function ( key, from, to ) {
445         if (
446                 ( key !== 'listGroup' && key !== 'listKey' ) ||
447                 ( key === 'listGroup' && this.registeredListGroup === to ) ||
448                 ( key === 'listKey' && this.registeredListKey === to )
449         ) {
450                 return;
451         }
452
453         // Need the old list keys and indexes, so we register them in addToInternalList
454         // They've already been updated in this.element.attributes before this code runs
455         this.removeFromInternalList();
456         this.addToInternalList();
457 };
458
459 /* Registration */
460
461 ve.dm.modelRegistry.register( ve.dm.MWReferenceNode );