Debian Codegraph @

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

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable MWBlockImageNode class.
3 *
4 * @copyright See AUTHORS.txt
5 * @license The MIT License (MIT); see LICENSE.txt
6 */
7
8 /**
9 * ContentEditable MediaWiki image node.
10 *
11 * @class
12 * @extends ve.ce.BranchNode
13 * @mixes ve.ce.MWImageNode
14 *
15 * @constructor
16 * @param {ve.dm.MWBlockImageNode} model Model to observe
17 * @param {Object} [config] Configuration options
18 */
19 ve.ce.MWBlockImageNode = function VeCeMWBlockImageNode() {
20 // Parent constructor
21 ve.ce.MWBlockImageNode.super.apply( this, arguments );
22
23 const type = this.model.getAttribute( 'type' );
24 const isError = this.model.getAttribute( 'isError' );
25
26 // DOM Hierarchy for MWBlockImageNode:
27 // <figure> this.$element (ve-ce-mwBlockImageNode-{type})
28 // <a> this.$a
29 // <img> this.$image
30 // <figcaption> ve.ce.MWImageCaptionNode
31
32 let $image, $focusable;
33 // Build DOM:
34 if ( isError ) {
35 $image = $( [] );
36 const $missingImage = $( '<span>' )
37 .addClass( 'mw-file-element mw-broken-media' )
38 .text( this.model.getAttribute( 'errorText' ) );
39 this.$a = $( '<a>' )
40 .addClass( 'new' )
41 .append( $missingImage );
42 $focusable = $missingImage;
43 } else {
44 $image = $( '<img>' )
45 .addClass( 'mw-file-element' )
46 .attr( 'src', this.getResolvedAttribute( 'src' ) );
47 this.$a = $( '<a>' )
48 .addClass( 'mw-file-description' )
49 .append( $image );
50 // T322704
51 ve.setAttributeSafe( this.$a[ 0 ], 'href', this.getResolvedAttribute( 'href' ) || '', '#' );
52 $focusable = $image;
53 }
54
55 this.$element
56 .prepend( this.$a )
57 // The following classes are used here:
58 // * ve-ce-mwBlockImageNode-type-thumb
59 // * ve-ce-mwBlockImageNode-type-frame
60 // * ve-ce-mwBlockImageNode-type-frameless
61 // * ve-ce-mwBlockImageNode-type-none
62 .addClass( 've-ce-mwBlockImageNode ve-ce-mwBlockImageNode-type-' + type )
63 // 'typeof' should appear with the proper Parsoid-generated
64 // type. The model deals with converting it
65 .attr( 'typeof', this.model.getRdfa() );
66
67 // Mixin constructors
68 ve.ce.MWImageNode.call( this, $focusable, $image );
69
70 // Initialization
71 this.updateSize();
72 };
73
74 /* Inheritance */
75
76 OO.inheritClass( ve.ce.MWBlockImageNode, ve.ce.BranchNode );
77
78 // Need to mixin base class as well (T92540)
79 OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.GeneratedContentNode );
80
81 OO.mixinClass( ve.ce.MWBlockImageNode, ve.ce.MWImageNode );
82
83 /* Static Properties */
84
85 ve.ce.MWBlockImageNode.static.name = 'mwBlockImage';
86
87 ve.ce.MWBlockImageNode.static.tagName = 'figure';
88
89 ve.ce.MWBlockImageNode.static.renderHtmlAttributes = false;
90
91 ve.ce.MWBlockImageNode.static.autoFocus = false;
92
93 ve.ce.MWBlockImageNode.static.cssClasses = {
94 default: {
95 left: 'mw-halign-left',
96 right: 'mw-halign-right',
97 center: 'mw-halign-center',
98 none: 'mw-halign-none'
99 },
100 none: {
101 left: 'mw-halign-left',
102 right: 'mw-halign-right',
103 center: 'mw-halign-center',
104 none: 'mw-halign-none'
105 }
106 };
107
108 /* Methods */
109
110 /**
111 * Update CSS classes based on alignment and type
112 *
113 * @param {string} [oldAlign] The old alignment, for removing classes
114 */
115 ve.ce.MWBlockImageNode.prototype.updateClasses = function ( oldAlign ) {
116 const align = this.model.getAttribute( 'align' );
117
118 if ( oldAlign && oldAlign !== align ) {
119 // Remove previous alignment
120 // See static.cssClasses
121 // eslint-disable-next-line mediawiki/class-doc
122 this.$element
123 .removeClass( this.getCssClass( 'none', oldAlign ) )
124 .removeClass( this.getCssClass( 'default', oldAlign ) );
125 }
126
127 const type = this.model.getAttribute( 'type' );
128 const framed = type !== 'none' && type !== 'frameless';
129 const alignClass = this.getCssClass( framed ? 'default' : 'none', align );
130 this.$image.toggleClass( 've-ce-mwBlockImageNode-thumbimage', framed );
131 // eslint-disable-next-line mediawiki/class-doc
132 this.$element.addClass( alignClass );
133
134 // Border
135 this.$element.toggleClass( 'mw-image-border', !!this.model.getAttribute( 'borderImage' ) );
136
137 this.showHandles( {
138 'mw-halign-right': [ 'sw' ],
139 'mw-halign-left': [ 'se' ],
140 'mw-halign-center': [ 'sw', 'se' ]
141 // Defaults to undefined
142 }[ alignClass ] );
143 };
144
145 /**
146 * Redraw the image and its wrappers at the specified dimensions
147 *
148 * The current dimensions from the model are used if none are specified.
149 *
150 * @param {Object} [dimensions] Dimension object containing width & height
151 */
152 ve.ce.MWBlockImageNode.prototype.updateSize = function ( dimensions ) {
153 const isError = this.model.getAttribute( 'isError' );
154
155 if ( isError ) {
156 this.$element.css( { width: '', height: '' } );
157 return;
158 }
159
160 if ( !dimensions ) {
161 dimensions = {
162 width: this.model.getAttribute( 'width' ),
163 height: this.model.getAttribute( 'height' )
164 };
165 }
166
167 this.$image.css( dimensions );
168
169 const type = this.model.getAttribute( 'type' );
170 const borderImage = this.model.getAttribute( 'borderImage' );
171 const hasBorderOrFrame = ( type !== 'none' && type !== 'frameless' ) || borderImage;
172
173 // Make sure $element is sharing the dimensions, otherwise 'middle' and 'none'
174 // positions don't work properly
175 this.$element.css( {
176 width: dimensions.width + ( hasBorderOrFrame ? 2 : 0 ),
177 height: hasBorderOrFrame ? 'auto' : dimensions.height
178 } );
179 this.$element.toggleClass( 'mw-default-size', !!this.model.getAttribute( 'defaultSize' ) );
180 };
181
182 /**
183 * Get the right CSS class to use for alignment
184 *
185 * @param {string} type 'none' or 'default'
186 * @param {string} alignment 'left', 'right', 'center', 'none' or 'default'
187 * @return {string} CSS class
188 */
189 ve.ce.MWBlockImageNode.prototype.getCssClass = function ( type, alignment ) {
190 // TODO use this.model.getAttribute( 'type' ) etc., see T54065
191 // Default is different between RTL and LTR wikis:
192 if ( type === 'default' && alignment === 'default' ) {
193 if ( this.getModel().getDocument().getDir() === 'rtl' ) {
194 return 'mw-halign-left';
195 } else {
196 return 'mw-halign-right';
197 }
198 } else {
199 return this.constructor.static.cssClasses[ type ][ alignment ];
200 }
201 };
202
203 /**
204 * Override the default onSetup to add direction-dependent
205 * classes to the image thumbnail.
206 */
207 ve.ce.MWBlockImageNode.prototype.onSetup = function () {
208 // Parent method
209 ve.ce.MWBlockImageNode.super.prototype.onSetup.call( this );
210
211 this.updateClasses();
212 };
213
214 /**
215 * @inheritdoc
216 */
217 ve.ce.MWBlockImageNode.prototype.onAttributeChange = function ( key, from, to ) {
218 // Mixin method
219 ve.ce.MWImageNode.prototype.onAttributeChange.apply( this, arguments );
220
221 if ( key === 'height' || key === 'width' ) {
222 to = parseInt( to, 10 );
223 }
224
225 if ( from !== to ) {
226 switch ( key ) {
227 case 'align':
228 this.updateClasses( from );
229 break;
230 case 'src':
231 this.$image.attr( 'src', this.getResolvedAttribute( 'src' ) );
232 break;
233 case 'width':
234 this.updateSize( {
235 width: to,
236 height: this.model.getAttribute( 'height' )
237 } );
238 break;
239 case 'height':
240 this.updateSize( {
241 width: this.model.getAttribute( 'width' ),
242 height: to
243 } );
244 break;
245 case 'type':
246 // See constructor for types used
247 // eslint-disable-next-line mediawiki/class-doc
248 this.$element
249 .removeClass( 've-ce-mwBlockImageNode-type-' + from )
250 .addClass( 've-ce-mwBlockImageNode-type-' + to )
251 .attr( 'typeof', this.model.getRdfa() );
252
253 this.updateClasses();
254 this.updateSize();
255 break;
256 case 'borderImage':
257 this.updateClasses();
258 this.updateSize();
259 break;
260 // Other image attributes if they exist
261 case 'alt':
262 if ( !to ) {
263 this.$image.removeAttr( key );
264 } else {
265 this.$image.attr( key, to );
266 }
267 break;
268 case 'mediaType':
269 this.updateMediaType();
270 break;
271 case 'defaultSize':
272 this.$element.toggleClass( 'mw-default-size', to );
273 break;
274 }
275 }
276 };
277
278 /**
279 * @param {Object} dimensions New dimensions
280 */
281 ve.ce.MWBlockImageNode.prototype.onResizableResizing = function ( dimensions ) {
282 if ( !this.outline ) {
283 ve.ce.ResizableNode.prototype.onResizableResizing.call( this, dimensions );
284
285 this.updateSize( dimensions );
286 }
287 };
288
289 ve.ce.MWBlockImageNode.prototype.getDomPosition = function () {
290 // We need to override this because this.$element can have children other than renderings of child
291 // CE nodes (specifically, the image itself, this.$a), which throws the calculations out of whack.
292 // Luckily, MWBlockImageNode is very simple and can contain at most one other node: its caption,
293 // which is always inserted at the end.
294 const domNode = this.$element.last()[ 0 ];
295 return {
296 node: domNode,
297 offset: domNode.childNodes.length
298 };
299 };
300
301 /* Registration */
302
303 ve.ce.nodeFactory.register( ve.ce.MWBlockImageNode );