Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWGalleryNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki gallery node.
10 *
11 * @class
12 * @extends ve.ce.BranchNode
13 * @mixes ve.ce.FocusableNode
14 *
15 * @constructor
16 * @param {ve.dm.MWGalleryNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWGalleryNode = function VeCeMWGalleryNode() {
20 // Parent constructor
21 ve.ce.MWGalleryNode.super.apply( this, arguments );
22
23 // DOM hierarchy for MWGalleryNode:
24 // <ul> this.$element (gallery mw-gallery-{mode})
25 // <li> ve.ce.MWGalleryCaptionNode (gallerycaption)
26 // <li> ve.ce.MWGalleryImageNode (gallerybox)
27 // <li> ve.ce.MWGalleryImageNode (gallerybox)
28 // ⋮
29
30 // Mixin constructors
31 ve.ce.FocusableNode.call( this, this.getFocusableElement() );
32
33 this.onUpdateDebounced = ve.debounce( this.onUpdate.bind( this ) );
34
35 // Events
36 this.model.connect( this, {
37 update: 'onUpdateDebounced',
38 // Update $focusable when number of images changes
39 splice: 'onUpdateDebounced',
40 attributeChange: 'onAttributeChange'
41 } );
42
43 // Initialization
44 this.$element.addClass( 'gallery' );
45 this.onUpdate();
46 };
47
48 /* Inheritance */
49
50 OO.inheritClass( ve.ce.MWGalleryNode, ve.ce.BranchNode );
51
52 OO.mixinClass( ve.ce.MWGalleryNode, ve.ce.FocusableNode );
53
54 /* Static Properties */
55
56 ve.ce.MWGalleryNode.static.name = 'mwGallery';
57
58 ve.ce.MWGalleryNode.static.tagName = 'ul';
59
60 ve.ce.MWGalleryNode.static.iconWhenInvisible = 'imageGallery';
61
62 ve.ce.MWGalleryNode.static.primaryCommandName = 'gallery';
63
64 /* Methods */
65
66 /**
67 * Handle model update events.
68 */
69 ve.ce.MWGalleryNode.prototype.onUpdate = function () {
70 const mwAttrs = this.model.getAttribute( 'mw' ).attrs;
71 const defaults = mw.config.get( 'wgVisualEditorConfig' ).galleryOptions;
72 const mode = mwAttrs.mode || defaults.mode;
73
74 // `.attr( …, undefined )` does nothing - it's required to use `null` to remove an attribute.
75 // (This also clears the 'max-width', set below, if it's not needed.)
76 this.$element.attr( 'style', mwAttrs.style || null );
77
78 if ( mwAttrs.perrow && ( mode === 'traditional' || mode === 'nolines' ) ) {
79 // Magic 30 and 8 matches the code in ve.ce.MWGalleryImageNode
80 const imageWidth = parseInt( mwAttrs.widths || defaults.imageWidth );
81 const imagePadding = ( mode === 'traditional' ? 30 : 0 );
82 this.$element.css( 'max-width', mwAttrs.perrow * ( imageWidth + imagePadding + 8 ) );
83 }
84
85 // Update $focusable/$bounding, similar to ve.ce.GeneratedContentNode
86 if ( this.live ) {
87 this.emit( 'teardown' );
88 }
89 this.$focusable = this.getFocusableElement();
90 this.$bounding = this.$focusable;
91 if ( this.live ) {
92 this.emit( 'setup' );
93 }
94
95 this.updateInvisibleIcon();
96 };
97
98 /**
99 * Handle attribute changes to keep the live HTML element updated.
100 *
101 * @param {string} key Attribute name
102 * @param {any} from Old value
103 * @param {any} to New value
104 */
105 ve.ce.MWGalleryNode.prototype.onAttributeChange = function ( key, from, to ) {
106 const defaults = mw.config.get( 'wgVisualEditorConfig' ).galleryOptions;
107
108 if ( key !== 'mw' ) {
109 return;
110 }
111
112 if ( from.attrs.class !== to.attrs.class ) {
113 // We can't overwrite the whole 'class' HTML attribute, because it also contains a class
114 // generated from the 'mode' MW attribute, and VE internal classes like 've-ce-focusableNode'
115 // eslint-disable-next-line mediawiki/class-doc
116 this.$element
117 .removeClass( from.attrs.class )
118 .addClass( to.attrs.class );
119 }
120
121 if ( from.attrs.mode !== to.attrs.mode ) {
122 // The following classes are used here:
123 // * mw-gallery-traditional
124 // * mw-gallery-nolines
125 // * mw-gallery-packed
126 // * mw-gallery-packed-overlay
127 // * mw-gallery-packed-hover
128 // * mw-gallery-slideshow
129 this.$element
130 .removeClass( 'mw-gallery-' + ( from.attrs.mode || defaults.mode ) )
131 .addClass( 'mw-gallery-' + ( to.attrs.mode || defaults.mode ) );
132 }
133 };
134
135 /**
136 * Get the focusable element
137 *
138 * As seen in ve.ce.GeneratedContentNode.
139 * TODO: Consider making this a core ve.ce.FocsableNode feature.
140 *
141 * @return {jQuery} Focusable element
142 */
143 ve.ce.MWGalleryNode.prototype.getFocusableElement = function () {
144 return this.$element.find( '.gallerybox .thumb' );
145 };
146
147 /* Registration */
148
149 ve.ce.nodeFactory.register( ve.ce.MWGalleryNode );