Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWGalleryImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki gallery image node.
10 *
11 * @class
12 * @extends ve.ce.BranchNode
13 *
14 * @constructor
15 * @param {ve.dm.MWGalleryImageNode} model Model to observe
16 * @param {Object} [config] Configuration options
17 */
18 ve.ce.MWGalleryImageNode = function VeCeMWGalleryImageNode( model ) {
19 // Parent constructor
20 ve.ce.MWGalleryImageNode.super.apply( this, arguments );
21
22 // DOM hierarchy for MWGalleryImageNode:
23 // <li> this.$element (gallerybox)
24 // <div> thumbDiv
25 // <span> innerDiv
26 // <a> a
27 // <img> img
28 // <a> filenameA (galleryfilename)
29 // <figcaption> ve.ce.MWGalleryImageCaptionNode
30
31 const defaults = mw.config.get( 'wgVisualEditorConfig' ).galleryOptions;
32 const attributes = model.getAttributes();
33 const galleryMwAttrs = model.parent.getAttribute( 'mw' ).attrs;
34
35 // Putting all this setup in the constructor works because MWGalleryImageNodes are never updated,
36 // only created from scratch
37
38 // These dimensions are different depending on the gallery mode.
39 // (This only vaguely approximates the actual rendering.)
40 const mode = galleryMwAttrs.mode || defaults.mode;
41 let innerDivWidth, innerDivHeight, innerDivMargin, outerDivWidth;
42 if ( mode === 'traditional' || mode === 'nolines' || mode === 'slideshow' ) {
43 const imagePadding = ( mode === 'traditional' ? 30 : 0 );
44 innerDivWidth = parseInt( galleryMwAttrs.widths || defaults.imageWidth ) + imagePadding;
45 innerDivHeight = parseInt( galleryMwAttrs.heights || defaults.imageHeight ) + imagePadding;
46 if ( mode === 'traditional' ) {
47 const imageHeight = parseInt( attributes.height );
48 innerDivMargin = ( ( innerDivHeight - imageHeight ) / 2 ) + 'px auto';
49 } else {
50 innerDivMargin = 0;
51 }
52 outerDivWidth = innerDivWidth + 8;
53 } else {
54 innerDivWidth = parseInt( attributes.width );
55 innerDivHeight = parseInt( galleryMwAttrs.heights || defaults.imageHeight );
56 innerDivMargin = 0;
57 outerDivWidth = innerDivWidth + 4;
58 }
59
60 const resourceTitle = mw.Title.newFromText( mw.libs.ve.normalizeParsoidResourceName( attributes.resource ) );
61
62 this.$element
63 .addClass( 'gallerybox' )
64 .css( 'width', outerDivWidth + 'px' );
65 const $thumbDiv = $( '<div>' )
66 .addClass( 'thumb' )
67 .css( 'width', innerDivWidth + 'px' )
68 .css( 'height', innerDivHeight + 'px' );
69 const $innerDiv = $( '<span>' )
70 .css( 'margin', innerDivMargin );
71 const $a = $( '<a>' ).addClass( 'mw-file-description' );
72
73 let $img;
74 if ( model.getAttribute( 'isError' ) ) {
75 $img = $( '<span>' )
76 .addClass( 'mw-file-element mw-broken-media' )
77 .text( model.getAttribute( 'errorText' ) );
78 } else {
79 $img = $( '<img>' )
80 .addClass( 'mw-file-element' )
81 .attr( 'resource', attributes.resource )
82 .attr( 'alt', attributes.altText )
83 .attr( 'src', attributes.src )
84 .attr( 'height', attributes.height )
85 .attr( 'width', attributes.width );
86 }
87
88 this.$filenameA = $( '<a>' )
89 .attr( 'href', '#' ) // Just to make it look like a link
90 .text( resourceTitle ? resourceTitle.getMainText() : attributes.resource )
91 .toggleClass( 'oo-ui-element-hidden', galleryMwAttrs.showfilename !== 'yes' );
92
93 this.$element.prepend(
94 $thumbDiv.append(
95 $innerDiv.append(
96 $a.append(
97 $img
98 )
99 )
100 ),
101 this.$filenameA
102 );
103
104 this.model.parent.connect( this, { attributeChange: 'onGalleryAttributeChange' } );
105 };
106
107 /* Inheritance */
108
109 OO.inheritClass( ve.ce.MWGalleryImageNode, ve.ce.BranchNode );
110
111 /* Static Properties */
112
113 ve.ce.MWGalleryImageNode.static.name = 'mwGalleryImage';
114
115 ve.ce.MWGalleryImageNode.static.tagName = 'li';
116
117 /* Methods */
118
119 ve.ce.MWGalleryImageNode.prototype.onGalleryAttributeChange = function ( key, from, to ) {
120 if ( key !== 'mw' ) {
121 return;
122 }
123
124 this.$filenameA.toggleClass( 'oo-ui-element-hidden', to.attrs.showfilename !== 'yes' );
125 };
126
127 ve.ce.MWGalleryImageNode.prototype.getDomPosition = function () {
128 // We need to override this because this.$element can have children other than renderings of child
129 // CE nodes (specifically, the image, this.$thumbDiv), which throws the calculations out of whack.
130 // Luckily, MWGalleryImageNode is very simple and can contain at most one other node: its caption,
131 // which is always inserted at the end.
132 const domNode = this.$element.last()[ 0 ];
133 return {
134 node: domNode,
135 offset: domNode.childNodes.length
136 };
137 };
138
139 /* Registration */
140
141 ve.ce.nodeFactory.register( ve.ce.MWGalleryImageNode );