Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/dm/nodes/ve.dm.MWGalleryNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel MWGalleryNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * DataModel MediaWiki gallery node.
10 *
11 * @class
12 * @extends ve.dm.BranchNode
13 * @mixes ve.dm.FocusableNode
14 *
15 * @constructor
16 * @param {Object} [element] Reference to element in linear model
17 */
18 ve.dm.MWGalleryNode = function VeDmMWGalleryNode() {
19 // Parent constructor
20 ve.dm.MWGalleryNode.super.apply( this, arguments );
21
22 // Mixin constructors
23 ve.dm.FocusableNode.call( this );
24 };
25
26 /* Inheritance */
27
28 OO.inheritClass( ve.dm.MWGalleryNode, ve.dm.BranchNode );
29
30 OO.mixinClass( ve.dm.MWGalleryNode, ve.dm.FocusableNode );
31
32 /* Static members */
33
34 ve.dm.MWGalleryNode.static.name = 'mwGallery';
35
36 ve.dm.MWGalleryNode.static.matchRdfaTypes = [ 'mw:Extension/gallery' ];
37
38 ve.dm.MWGalleryNode.static.matchTagNames = [ 'ul' ];
39
40 ve.dm.MWGalleryNode.static.childNodeTypes = [ 'mwGalleryCaption', 'mwGalleryImage' ];
41
42 ve.dm.MWGalleryNode.static.disallowedAnnotationTypes = [ 'link' ];
43
44 ve.dm.MWGalleryNode.static.cloneElement = function () {
45 // Parent method
46 const clone = ve.dm.LeafNode.static.cloneElement.apply( this, arguments );
47 delete clone.attributes.originalMw;
48 return clone;
49 };
50
51 ve.dm.MWGalleryNode.static.getHashObject = function ( dataElement ) {
52 return {
53 type: dataElement.type,
54 mw: ve.copy( dataElement.attributes.mw )
55 };
56 };
57
58 ve.dm.MWGalleryNode.static.toDataElement = function ( domElements ) {
59 const mwDataJSON = domElements[ 0 ].getAttribute( 'data-mw' ),
60 mwData = mwDataJSON ? JSON.parse( mwDataJSON ) : {};
61
62 return {
63 type: this.name,
64 attributes: {
65 mw: mwData,
66 originalMw: mwDataJSON
67 }
68 };
69 };
70
71 ve.dm.MWGalleryNode.static.toDomElements = function ( data, doc ) {
72 const ul = doc.createElement( 'ul' );
73
74 // Build ul
75 ul.setAttribute( 'typeof', 'mw:Extension/gallery' );
76 ul.setAttribute( 'data-mw', JSON.stringify( data.attributes.mw ) );
77
78 return [ ul ];
79 };
80
81 ve.dm.MWGalleryNode.static.describeChanges = function ( attributeChanges, attributes, element ) {
82 // Only do a comparison on the 'mw.attrs' attribute
83 if ( attributeChanges.mw ) {
84 return ve.dm.MWGalleryNode.super.static.describeChanges.call(
85 this,
86 ve.ui.DiffElement.static.compareAttributes( attributeChanges.mw.from.attrs || {}, attributeChanges.mw.to.attrs || {} ),
87 attributes,
88 element
89 );
90 }
91 return [];
92 };
93
94 ve.dm.MWGalleryNode.static.describeChange = function ( key ) {
95 // Caption diff is shown in the DOM
96 if ( key === 'caption' ) {
97 return null;
98 }
99 // Parent method
100 return ve.dm.MWGalleryNode.super.static.describeChange.apply( this, arguments );
101 };
102
103 /* Methods */
104
105 ve.dm.MWGalleryNode.prototype.isDiffedAsDocument = function () {
106 return true;
107 };
108
109 /**
110 * Get the gallery's caption node.
111 *
112 * @return {ve.dm.MWImageCaptionNode|null} Caption node, if present
113 */
114 ve.dm.MWGalleryNode.prototype.getCaptionNode = function () {
115 const node = this.children[ 0 ];
116 return node instanceof ve.dm.MWGalleryCaptionNode ? node : null;
117 };
118
119 /**
120 * Get the gallery's image nodes.
121 *
122 * @return {ve.dm.MWGalleryImageNode[]} Gallery image nodes (may be empty if none are present)
123 */
124 ve.dm.MWGalleryNode.prototype.getImageNodes = function () {
125 const images = this.children.filter( ( child ) => child instanceof ve.dm.MWGalleryImageNode );
126 return images;
127 };
128
129 /* Registration */
130
131 ve.dm.modelRegistry.register( ve.dm.MWGalleryNode );