Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ce/ve.ce.ResizableNode.js

raw symbols SCIP
1 /*!
2 * VisualEditor ContentEditable ResizableNode class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * ContentEditable resizable node.
9 *
10 * @class
11 * @abstract
12 *
13 * @constructor
14 * @param {jQuery} [$resizable=this.$element] Resizable DOM element
15 * @param {Object} [config] Configuration options
16 * @param {number|null} [config.snapToGrid=10] Snap to a grid of size X when the shift key is held. Null disables.
17 * @param {boolean} [config.outline=false] Resize using an outline of the element only, don't live preview.
18 * @param {boolean} [config.showSizeLabel=true] Show a label with the current dimensions while resizing
19 * @param {boolean} [config.showScaleLabel=true] Show a label with the current scale while resizing
20 */
21 ve.ce.ResizableNode = function VeCeResizableNode( $resizable, config ) {
22 config = config || {};
23
24 // Properties
25 this.$resizable = $resizable || this.$element;
26 this.resizing = false;
27 this.$resizeHandles = $( '<div>' );
28 this.snapToGrid = config.snapToGrid !== undefined ? config.snapToGrid : 10;
29 this.outline = !!config.outline;
30 this.showSizeLabel = config.showSizeLabel !== false;
31 this.showScaleLabel = config.showScaleLabel !== false;
32 // Only gets enabled when the original dimensions are provided
33 this.canShowScaleLabel = false;
34 if ( this.showSizeLabel || this.showScaleLabel ) {
35 this.$sizeText = $( '<span>' ).addClass( 've-ce-resizableNode-sizeText' );
36 this.$sizeLabel = $( '<div>' ).addClass( 've-ce-resizableNode-sizeLabel' ).append( this.$sizeText );
37 }
38 this.resizableOffset = null;
39 this.resizableSurface = null;
40
41 // Events
42 this.connect( this, {
43 focus: 'onResizableFocus',
44 blur: 'onResizableBlur',
45 setup: 'onResizableSetup',
46 teardown: 'onResizableTeardown',
47 resizing: 'onResizableResizing',
48 resizeEnd: 'onResizableFocus',
49 rerender: 'onResizableFocus',
50 align: 'onResizableAlign'
51 } );
52 this.model.connect( this, {
53 attributeChange: 'onResizableAttributeChange'
54 } );
55
56 // Initialization
57 this.$resizeHandles
58 .addClass( 've-ce-resizableNode-handles' )
59 .append( $( '<div>' )
60 .addClass( 've-ce-resizableNode-nwHandle' )
61 .data( 'handle', 'nw' ) )
62 .append( $( '<div>' )
63 .addClass( 've-ce-resizableNode-neHandle' )
64 .data( 'handle', 'ne' ) )
65 .append( $( '<div>' )
66 .addClass( 've-ce-resizableNode-seHandle' )
67 .data( 'handle', 'se' ) )
68 .append( $( '<div>' )
69 .addClass( 've-ce-resizableNode-swHandle' )
70 .data( 'handle', 'sw' ) );
71 };
72
73 /* Inheritance */
74
75 OO.initClass( ve.ce.ResizableNode );
76
77 /* Events */
78
79 /**
80 * @event ve.ce.ResizableNode#resizeStart
81 */
82
83 /**
84 * @event ve.ce.ResizableNode#resizing
85 * @param {Object} dimensions Dimension object containing width & height
86 */
87
88 /**
89 * @event ve.ce.ResizableNode#resizeEnd
90 */
91
92 /* Methods */
93
94 /**
95 * Check if the node is resizable in its current state
96 *
97 * @return {boolean} The node is currently resizable
98 */
99 ve.ce.ResizableNode.prototype.isResizable = function () {
100 return this.$resizable && !!this.$resizable.length && !OO.ui.isMobile() &&
101 !( this.root && this.root.getSurface() && this.root.getSurface().isReadOnly() );
102 };
103
104 /**
105 * Get and cache the relative offset of the $resizable node
106 *
107 * @return {Object} Position coordinates, containing top & left
108 */
109 ve.ce.ResizableNode.prototype.getResizableOffset = function () {
110 if ( !this.resizableOffset ) {
111 this.resizableOffset = OO.ui.Element.static.getRelativePosition(
112 this.$resizable, this.resizableSurface.getSurface().$element
113 );
114 }
115 return this.resizableOffset;
116 };
117
118 /**
119 * Set the original dimensions of the scalable object
120 *
121 * @param {Object} dimensions
122 */
123 ve.ce.ResizableNode.prototype.setOriginalDimensions = function ( dimensions ) {
124 if ( !this.isResizable() ) {
125 return;
126 }
127
128 const scalable = this.model.getScalable();
129
130 scalable.setOriginalDimensions( dimensions );
131
132 // If dimensions are valid and the scale label is desired, enable it
133 this.canShowScaleLabel = this.showScaleLabel &&
134 scalable.getOriginalDimensions().width &&
135 scalable.getOriginalDimensions().height;
136 };
137
138 /**
139 * Hide the size label
140 */
141 ve.ce.ResizableNode.prototype.hideSizeLabel = function () {
142 if ( !this.isResizable() ) {
143 return;
144 }
145
146 // Defer the removal of this class otherwise other DOM changes may cause
147 // the opacity transition to not play out smoothly
148 setTimeout( () => {
149 this.$sizeLabel.removeClass( 've-ce-resizableNode-sizeLabel-resizing' );
150 } );
151 // Actually hide the size label after it's done animating
152 setTimeout( () => {
153 this.$sizeLabel.addClass( 'oo-ui-element-hidden' );
154 }, 200 );
155 };
156
157 /**
158 * Update the contents and position of the size label
159 */
160 ve.ce.ResizableNode.prototype.updateSizeLabel = function () {
161 if ( !this.isResizable() ) {
162 return;
163 }
164 if ( !this.showSizeLabel && !this.canShowScaleLabel ) {
165 return;
166 }
167
168 const scalable = this.model.getScalable();
169 const dimensions = scalable.getCurrentDimensions();
170 const offset = this.getResizableOffset();
171 const minWidth = ( this.showSizeLabel ? 100 : 0 ) + ( this.showScaleLabel ? 30 : 0 );
172
173 let top, height;
174 // Put the label on the outside when too narrow
175 if ( dimensions.width < minWidth ) {
176 top = offset.top + dimensions.height;
177 height = 30;
178 } else {
179 top = offset.top;
180 height = dimensions.height;
181 }
182 this.$sizeLabel
183 .removeClass( 'oo-ui-element-hidden' )
184 .addClass( 've-ce-resizableNode-sizeLabel-resizing' )
185 .css( {
186 top: top,
187 left: offset.left,
188 width: dimensions.width,
189 height: height,
190 lineHeight: height + 'px'
191 } );
192 this.$sizeText.empty();
193 if ( this.showSizeLabel ) {
194 this.$sizeText.append( $( '<span>' )
195 .addClass( 've-ce-resizableNode-sizeText-size' )
196 // TODO: i18n?
197 .text( Math.round( dimensions.width ) + ' × ' + Math.round( dimensions.height ) )
198 );
199 }
200 if ( this.canShowScaleLabel ) {
201 this.$sizeText.append( $( '<span>' )
202 .addClass( 've-ce-resizableNode-sizeText-scale' )
203 .text( Math.round( 100 * scalable.getCurrentScale() ) + '%' )
204 );
205 }
206 this.$sizeText.toggleClass( 've-ce-resizableNode-sizeText-warning', scalable.isTooSmall() || scalable.isTooLarge() );
207 };
208
209 /**
210 * Show specific resize handles
211 *
212 * @param {string[]} [handles] List of handles to show: 'nw', 'ne', 'sw', 'se'. Show all if undefined.
213 */
214 ve.ce.ResizableNode.prototype.showHandles = function ( handles ) {
215 if ( !this.isResizable() ) {
216 return;
217 }
218
219 const add = [],
220 remove = [],
221 allDirections = [ 'nw', 'ne', 'sw', 'se' ];
222
223 for ( let i = 0, len = allDirections.length; i < len; i++ ) {
224 if ( handles === undefined || handles.indexOf( allDirections[ i ] ) !== -1 ) {
225 remove.push( 've-ce-resizableNode-hide-' + allDirections[ i ] );
226 } else {
227 add.push( 've-ce-resizableNode-hide-' + allDirections[ i ] );
228 }
229 }
230
231 // The following classes are used here:
232 // * ve-ce-resizableNode-hide-nw
233 // * ve-ce-resizableNode-hide-ne
234 // * ve-ce-resizableNode-hide-sw
235 // * ve-ce-resizableNode-hide-se
236 this.$resizeHandles
237 .addClass( add )
238 .removeClass( remove );
239 };
240
241 /**
242 * Handle node focus.
243 */
244 ve.ce.ResizableNode.prototype.onResizableFocus = function () {
245 // Also check the node is focused as this method is also triggered by rerender.
246 if ( !this.isResizable() || !this.isFocused() ) {
247 return;
248 }
249 this.$resizeHandles.appendTo( this.resizableSurface.getSurface().$controls );
250 if ( this.$sizeLabel ) {
251 this.$sizeLabel.appendTo( this.resizableSurface.getSurface().$controls );
252 }
253
254 // Call getScalable to pre-fetch the extended data
255 this.model.getScalable();
256
257 this.setResizableHandlesSizeAndPosition();
258
259 this.$resizeHandles
260 .find( '.ve-ce-resizableNode-neHandle' )
261 .css( { marginRight: -this.$resizable.outerWidth() } );
262 this.$resizeHandles
263 .find( '.ve-ce-resizableNode-swHandle' )
264 .css( { marginBottom: -this.$resizable.outerHeight() } );
265 this.$resizeHandles
266 .find( '.ve-ce-resizableNode-seHandle' )
267 .css( {
268 marginRight: -this.$resizable.outerWidth(),
269 marginBottom: -this.$resizable.outerHeight()
270 } );
271
272 this.$resizeHandles.children()
273 .off( '.ve-ce-resizableNode' )
274 .on(
275 'mousedown.ve-ce-resizableNode',
276 this.onResizeHandlesCornerMouseDown.bind( this )
277 );
278
279 this.resizableSurface.connect( this, { position: 'setResizableHandlesSizeAndPosition' } );
280
281 };
282
283 /**
284 * Handle node blur.
285 */
286 ve.ce.ResizableNode.prototype.onResizableBlur = function () {
287 // Node may have already been torn down, e.g. after delete
288 if ( !this.isResizableSetup || !this.root ) {
289 return;
290 }
291
292 this.$resizeHandles.detach();
293 if ( this.$sizeLabel ) {
294 this.$sizeLabel.detach();
295 }
296
297 this.resizableSurface.disconnect( this, { position: 'setResizableHandlesSizeAndPosition' } );
298 };
299
300 /**
301 * Respond to AlignableNodes changing their alignment by hiding useless resize handles.
302 *
303 * @param {string} align Alignment
304 */
305 ve.ce.ResizableNode.prototype.onResizableAlign = function ( align ) {
306 if ( !this.isResizable() ) {
307 return;
308 }
309
310 this.showHandles( {
311 right: [ 'sw' ],
312 left: [ 'se' ],
313 center: [ 'sw', 'se' ]
314 // Defaults to undefined
315 }[ align ] );
316 };
317
318 /**
319 * Handle setup event.
320 */
321 ve.ce.ResizableNode.prototype.onResizableSetup = function () {
322 // Exit if already setup or not attached
323 if ( this.isResizableSetup || !this.root ) {
324 return;
325 }
326
327 this.resizableSurface = this.root.getSurface();
328 this.isResizableSetup = true;
329 };
330
331 /**
332 * Handle teardown event.
333 */
334 ve.ce.ResizableNode.prototype.onResizableTeardown = function () {
335 // Exit if not setup or not attached
336 if ( !this.isResizableSetup || !this.root ) {
337 return;
338 }
339
340 this.onResizableBlur();
341 this.resizableSurface = null;
342 this.isResizableSetup = false;
343 };
344
345 /**
346 * Handle resizing event.
347 *
348 * @param {Object} dimensions Dimension object containing width & height
349 */
350 ve.ce.ResizableNode.prototype.onResizableResizing = function ( dimensions ) {
351 if ( !this.isResizable() ) {
352 return;
353 }
354 // Clear cached resizable offset position as it may have changed
355 this.resizableOffset = null;
356 this.model.getScalable().setCurrentDimensions( dimensions );
357 if ( !this.outline ) {
358 this.$resizable.css( this.model.getScalable().getCurrentDimensions() );
359 this.setResizableHandlesPosition();
360 }
361 this.updateSizeLabel();
362 };
363
364 /**
365 * Handle attribute change events from the model.
366 *
367 * @param {string} key Attribute key
368 * @param {string} from Old value
369 * @param {string} to New value
370 */
371 ve.ce.ResizableNode.prototype.onResizableAttributeChange = function () {
372 if ( !this.isResizable() ) {
373 return;
374 }
375 this.$resizable.css( this.model.getCurrentDimensions() );
376 };
377
378 /**
379 * Handle bounding box handle mousedown.
380 *
381 * @param {jQuery.Event} e Click event
382 * @fires ve.ce.ResizableNode#resizeStart
383 */
384 ve.ce.ResizableNode.prototype.onResizeHandlesCornerMouseDown = function ( e ) {
385 // Hide context menu
386 // TODO: Maybe there's a more generic way to handle this sort of thing? For relocation it's
387 // handled in ve.ce.Surface
388 this.root.getSurface().getSurface().getContext().toggle( false );
389
390 // Set bounding box width and undo the handle margins
391 this.$resizeHandles
392 .addClass( 've-ce-resizableNode-handles-resizing' )
393 .css( {
394 width: this.$resizable.outerWidth(),
395 height: this.$resizable.outerHeight()
396 } );
397
398 this.$resizeHandles.children().css( 'margin', 0 );
399
400 // Values to calculate adjusted bounding box size
401 this.resizeInfo = {
402 mouseX: e.screenX,
403 mouseY: e.screenY,
404 top: this.$resizeHandles.position().top,
405 left: this.$resizeHandles.position().left,
406 height: this.$resizeHandles.height(),
407 width: this.$resizeHandles.width(),
408 handle: $( e.target ).data( 'handle' )
409 };
410
411 // Bind resize events
412 this.resizing = true;
413 this.root.getSurface().resizing = true;
414
415 this.model.getScalable().setCurrentDimensions( {
416 width: this.resizeInfo.width,
417 height: this.resizeInfo.height
418 } );
419 this.updateSizeLabel();
420 $( this.getElementDocument() ).on( {
421 'mousemove.ve-ce-resizableNode': this.onDocumentMouseMove.bind( this ),
422 'mouseup.ve-ce-resizableNode': this.onDocumentMouseUp.bind( this )
423 } );
424 this.emit( 'resizeStart' );
425
426 e.preventDefault();
427 };
428
429 /**
430 * Set the proper size and position for resize handles
431 */
432 ve.ce.ResizableNode.prototype.setResizableHandlesSizeAndPosition = function () {
433 if ( !this.isResizable() ) {
434 return;
435 }
436
437 const width = this.$resizable.outerWidth();
438 const height = this.$resizable.outerHeight();
439
440 // Clear cached resizable offset position as it may have changed
441 this.resizableOffset = null;
442
443 this.setResizableHandlesPosition();
444
445 this.$resizeHandles
446 .css( {
447 width: 0,
448 height: 0
449 } )
450 .find( '.ve-ce-resizableNode-neHandle' )
451 .css( { marginRight: -width } );
452 this.$resizeHandles
453 .find( '.ve-ce-resizableNode-swHandle' )
454 .css( { marginBottom: -height } );
455 this.$resizeHandles
456 .find( '.ve-ce-resizableNode-seHandle' )
457 .css( {
458 marginRight: -width,
459 marginBottom: -height
460 } );
461 };
462
463 /**
464 * Set the proper position for resize handles
465 */
466 ve.ce.ResizableNode.prototype.setResizableHandlesPosition = function () {
467 if ( !this.isResizable() ) {
468 return;
469 }
470
471 const offset = this.getResizableOffset();
472
473 this.$resizeHandles.css( {
474 top: offset.top,
475 left: offset.left
476 } );
477 };
478
479 /**
480 * Handle body mousemove.
481 *
482 * @param {jQuery.Event} e Click event
483 * @fires ve.ce.ResizableNode#resizing
484 */
485 ve.ce.ResizableNode.prototype.onDocumentMouseMove = function ( e ) {
486 const diff = {};
487 let dimensions = {
488 width: 0,
489 height: 0,
490 top: this.resizeInfo.top,
491 left: this.resizeInfo.left
492 };
493
494 if ( this.resizing ) {
495 // X and Y diff
496 switch ( this.resizeInfo.handle ) {
497 case 'se':
498 diff.x = e.screenX - this.resizeInfo.mouseX;
499 diff.y = e.screenY - this.resizeInfo.mouseY;
500 break;
501 case 'nw':
502 diff.x = this.resizeInfo.mouseX - e.screenX;
503 diff.y = this.resizeInfo.mouseY - e.screenY;
504 break;
505 case 'ne':
506 diff.x = e.screenX - this.resizeInfo.mouseX;
507 diff.y = this.resizeInfo.mouseY - e.screenY;
508 break;
509 case 'sw':
510 diff.x = this.resizeInfo.mouseX - e.screenX;
511 diff.y = e.screenY - this.resizeInfo.mouseY;
512 break;
513 }
514
515 dimensions = this.model.getScalable().getBoundedDimensions( {
516 width: this.resizeInfo.width + diff.x,
517 height: this.resizeInfo.height + diff.y
518 }, e.shiftKey && this.snapToGrid );
519
520 // Fix the position
521 switch ( this.resizeInfo.handle ) {
522 case 'ne':
523 dimensions.top = this.resizeInfo.top +
524 ( this.resizeInfo.height - dimensions.height );
525 break;
526 case 'sw':
527 dimensions.left = this.resizeInfo.left +
528 ( this.resizeInfo.width - dimensions.width );
529 break;
530 case 'nw':
531 dimensions.top = this.resizeInfo.top +
532 ( this.resizeInfo.height - dimensions.height );
533 dimensions.left = this.resizeInfo.left +
534 ( this.resizeInfo.width - dimensions.width );
535 break;
536 }
537
538 // Update bounding box
539 this.$resizeHandles.css( dimensions );
540 this.emit( 'resizing', {
541 width: dimensions.width,
542 height: dimensions.height
543 } );
544 }
545 };
546
547 /**
548 * Handle body mouseup.
549 *
550 * @fires ve.ce.ResizableNode#resizeEnd
551 */
552 ve.ce.ResizableNode.prototype.onDocumentMouseUp = function () {
553 const width = this.$resizeHandles.outerWidth(),
554 height = this.$resizeHandles.outerHeight();
555
556 this.$resizeHandles.removeClass( 've-ce-resizableNode-handles-resizing' );
557 $( this.getElementDocument() ).off( '.ve-ce-resizableNode' );
558 this.resizing = false;
559 this.root.getSurface().resizing = false;
560 this.hideSizeLabel();
561
562 // Apply changes to the model
563 const attrChanges = this.getAttributeChanges( width, height );
564 if ( !ve.isEmptyObject( attrChanges ) ) {
565 this.resizableSurface.getModel().getFragment().changeAttributes( attrChanges );
566 }
567
568 // Update the context menu. This usually happens with the redraw, but not if the
569 // user doesn't perform a drag
570 this.root.getSurface().getSurface().getContext().updateDimensions();
571
572 this.emit( 'resizeEnd' );
573 };
574
575 /**
576 * Generate an object of attributes changes from the new width and height.
577 *
578 * @param {number} width New image width
579 * @param {number} height New image height
580 * @return {Object} Attribute changes
581 */
582 ve.ce.ResizableNode.prototype.getAttributeChanges = function ( width, height ) {
583 const attrChanges = {},
584 currentDimensions = this.model.getCurrentDimensions();
585
586 if ( currentDimensions.width !== width ) {
587 attrChanges.width = width;
588 }
589 if ( currentDimensions.height !== height ) {
590 attrChanges.height = height;
591 }
592 return attrChanges;
593 };