Debian Codegraph @

extensions/VisualEditor/lib/ve/src/dm/ve.dm.Scalable.js

raw symbols SCIP
1 /*!
2 * VisualEditor DataModel Scalable class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Scalable object.
9 *
10 * @class
11 * @mixes OO.EventEmitter
12 *
13 * @constructor
14 * @param {Object} [config] Configuration options
15 * @param {boolean} [config.fixedRatio=true] Object has a fixed aspect ratio
16 * @param {Object} [config.currentDimensions] Current dimensions, width & height
17 * @param {Object} [config.originalDimensions] Original dimensions, width & height
18 * @param {Object} [config.defaultDimensions] Default dimensions, width & height
19 * @param {boolean} [config.isDefault] Object is using its default dimensions
20 * @param {Object} [config.minDimensions] Minimum dimensions, width & height
21 * @param {Object} [config.maxDimensions] Maximum dimensions, width & height
22 * @param {boolean} [config.enforceMin=true] Enforce the minimum dimensions
23 * @param {boolean} [config.enforceMax=true] Enforce the maximum dimensions
24 */
25 ve.dm.Scalable = function VeDmScalable( config ) {
26 config = ve.extendObject( {
27 fixedRatio: true,
28 enforceMin: true,
29 enforceMax: true
30 }, config );
31
32 // Mixin constructors
33 OO.EventEmitter.call( this );
34
35 // Computed properties
36 this.ratio = null;
37 this.valid = null;
38 this.defaultSize = false;
39
40 // Initialize
41 this.currentDimensions = null;
42 this.defaultDimensions = null;
43 this.originalDimensions = null;
44 this.minDimensions = null;
45 this.maxDimensions = null;
46
47 // Properties
48 this.fixedRatio = config.fixedRatio;
49 if ( config.currentDimensions ) {
50 this.setCurrentDimensions( config.currentDimensions );
51 }
52 if ( config.originalDimensions ) {
53 this.setOriginalDimensions( config.originalDimensions );
54 }
55 if ( config.defaultDimensions ) {
56 this.setDefaultDimensions( config.defaultDimensions );
57 }
58 if ( config.isDefault ) {
59 this.toggleDefault( !!config.isDefault );
60 }
61 if ( config.minDimensions ) {
62 this.setMinDimensions( config.minDimensions );
63 }
64 if ( config.maxDimensions ) {
65 this.setMaxDimensions( config.maxDimensions );
66 }
67
68 this.setEnforcedMin( config.enforceMin );
69 this.setEnforcedMax( config.enforceMax );
70 };
71
72 /* Inheritance */
73
74 OO.mixinClass( ve.dm.Scalable, OO.EventEmitter );
75
76 /* Events */
77
78 /**
79 * Current changed
80 *
81 * @event ve.dm.Scalable#currentSizeChange
82 * @param {Object} currentDimensions Current dimensions width and height
83 */
84
85 /**
86 * Default size or state changed
87 *
88 * @event ve.dm.Scalable#defaultSizeChange
89 * @param {boolean} isDefault The size is default
90 */
91
92 /**
93 * Original size changed
94 *
95 * @event ve.dm.Scalable#originalSizeChange
96 * @param {Object} originalDimensions Original dimensions width and height
97 */
98
99 /**
100 * Min size changed
101 *
102 * @event ve.dm.Scalable#minSizeChange
103 * @param {Object} minDimensions Min dimensions width and height
104 */
105
106 /**
107 * Max size changed
108 *
109 * @event ve.dm.Scalable#maxSizeChange
110 * @param {Object} maxDimensions Max dimensions width and height
111 */
112
113 /**
114 * Calculate the dimensions from a given value of either width or height.
115 * This method doesn't take into account any restrictions of minimum or maximum,
116 * it simply calculates the new dimensions according to the aspect ratio in case
117 * it exists.
118 *
119 * If aspect ratio does not exist, or if the original object is empty, or if the
120 * original object is fully specified, the object is returned as-is without
121 * calculations.
122 *
123 * @param {Object} dimensions Dimensions object with either width or height
124 * if both are given, the object will be returned as-is.
125 * @param {number} [dimensions.width] The width of the image
126 * @param {number} [dimensions.height] The height of the image
127 * @param {number} [ratio] The image width/height ratio, if it exists
128 * @return {Object} Dimensions object with width and height
129 */
130 ve.dm.Scalable.static.getDimensionsFromValue = function ( dimensions, ratio ) {
131 dimensions = ve.copy( dimensions );
132
133 // Normalize for 'empty' values that are specifically given
134 // so if '' is explicitly given, it should be translated to 0
135 if ( dimensions.width === '' ) {
136 dimensions.width = 0;
137 }
138 if ( dimensions.height === '' ) {
139 dimensions.height = 0;
140 }
141
142 // Calculate the opposite size if needed
143 if ( !dimensions.height && ratio !== null && +dimensions.width ) {
144 dimensions.height = Math.round( dimensions.width / ratio );
145 }
146 if ( !dimensions.width && ratio !== null && +dimensions.height ) {
147 dimensions.width = Math.round( dimensions.height * ratio );
148 }
149
150 return dimensions;
151 };
152
153 /**
154 * Check if an object is a dimensions object.
155 * Make sure that if width or height are set, they are not 'undefined'.
156 *
157 * @param {Object} dimensions A dimensions object to test
158 * @return {boolean} Valid or invalid dimensions object
159 */
160 ve.dm.Scalable.static.isDimensionsObjectValid = function ( dimensions ) {
161 if (
162 dimensions &&
163 !ve.isEmptyObject( dimensions ) &&
164 (
165 dimensions.width !== undefined ||
166 dimensions.height !== undefined
167 )
168 ) {
169 return true;
170 }
171 return false;
172 };
173
174 /* Methods */
175
176 /**
177 * Clone the current scalable object
178 *
179 * @return {ve.dm.Scalable} Cloned scalable object
180 */
181 ve.dm.Scalable.prototype.clone = function () {
182 const currentDimensions = this.getCurrentDimensions(),
183 originalDimensions = this.getOriginalDimensions(),
184 defaultDimensions = this.getDefaultDimensions(),
185 minDimensions = this.getMinDimensions(),
186 maxDimensions = this.getMaxDimensions(),
187 config = {
188 isDefault: !!this.isDefault(),
189 enforceMin: !!this.isEnforcedMin(),
190 enforceMax: !!this.isEnforcedMax()
191 };
192 if ( currentDimensions ) {
193 config.currentDimensions = ve.copy( currentDimensions );
194 }
195 if ( originalDimensions ) {
196 config.originalDimensions = ve.copy( originalDimensions );
197 }
198 if ( defaultDimensions ) {
199 config.defaultDimensions = ve.copy( defaultDimensions );
200 }
201 if ( minDimensions ) {
202 config.minDimensions = ve.copy( minDimensions );
203 }
204 if ( maxDimensions ) {
205 config.maxDimensions = ve.copy( maxDimensions );
206 }
207 return new this.constructor( config );
208 };
209
210 /**
211 * Set the fixed aspect ratio from specified dimensions.
212 *
213 * @param {Object} dimensions Dimensions object with width & height
214 */
215 ve.dm.Scalable.prototype.setRatioFromDimensions = function ( dimensions ) {
216 if ( dimensions && dimensions.width && dimensions.height ) {
217 this.ratio = dimensions.width / dimensions.height;
218 }
219 this.valid = null;
220 };
221
222 /**
223 * Set the current dimensions
224 *
225 * Also sets the aspect ratio if not set and in fixed ratio mode.
226 *
227 * @param {Object} dimensions Dimensions object with width & height
228 * @fires ve.dm.Scalable#currentSizeChange
229 */
230 ve.dm.Scalable.prototype.setCurrentDimensions = function ( dimensions ) {
231 if (
232 this.constructor.static.isDimensionsObjectValid( dimensions ) &&
233 !ve.compare( dimensions, this.getCurrentDimensions() )
234 ) {
235 this.currentDimensions = ve.copy( dimensions );
236 // Only use current dimensions for ratio if it isn't set
237 if ( this.fixedRatio && !this.ratio ) {
238 this.setRatioFromDimensions( this.getCurrentDimensions() );
239 }
240 this.valid = null;
241 this.emit( 'currentSizeChange', this.getCurrentDimensions() );
242 }
243 };
244
245 /**
246 * Set the original dimensions
247 *
248 * Also resets the aspect ratio if in fixed ratio mode.
249 *
250 * @param {Object} dimensions Dimensions object with width & height
251 * @fires ve.dm.Scalable#originalSizeChange
252 */
253 ve.dm.Scalable.prototype.setOriginalDimensions = function ( dimensions ) {
254 if (
255 this.constructor.static.isDimensionsObjectValid( dimensions ) &&
256 !ve.compare( dimensions, this.getOriginalDimensions() )
257 ) {
258 this.originalDimensions = ve.copy( dimensions );
259 // Always overwrite ratio
260 if ( this.fixedRatio ) {
261 this.setRatioFromDimensions( this.getOriginalDimensions() );
262 }
263 this.valid = null;
264 this.emit( 'originalSizeChange', this.getOriginalDimensions() );
265 }
266 };
267
268 /**
269 * Set the default dimensions
270 *
271 * @param {Object} dimensions Dimensions object with width & height
272 * @fires ve.dm.Scalable#defaultSizeChange
273 */
274 ve.dm.Scalable.prototype.setDefaultDimensions = function ( dimensions ) {
275 if (
276 this.constructor.static.isDimensionsObjectValid( dimensions ) &&
277 !ve.compare( dimensions, this.getDefaultDimensions() )
278 ) {
279 this.defaultDimensions = ve.copy( dimensions );
280 this.valid = null;
281 this.emit( 'defaultSizeChange', this.isDefault() );
282 }
283 };
284
285 /**
286 * Reset and remove the default dimensions
287 *
288 * @fires ve.dm.Scalable#defaultSizeChange
289 */
290 ve.dm.Scalable.prototype.clearDefaultDimensions = function () {
291 if ( this.defaultDimensions !== null ) {
292 this.defaultDimensions = null;
293 this.valid = null;
294 this.emit( 'defaultSizeChange', this.isDefault() );
295 }
296 };
297
298 /**
299 * Reset and remove the default dimensions
300 *
301 * @fires ve.dm.Scalable#originalSizeChange
302 */
303 ve.dm.Scalable.prototype.clearOriginalDimensions = function () {
304 if ( this.originalDimensions !== null ) {
305 this.originalDimensions = null;
306 this.valid = null;
307 this.emit( 'originalSizeChange', this.isDefault() );
308 }
309 };
310
311 /**
312 * Toggle the default size setting, or set it to particular value
313 *
314 * @param {boolean} [isDefault] Default or not, toggles if unset
315 * @fires ve.dm.Scalable#defaultSizeChange
316 */
317 ve.dm.Scalable.prototype.toggleDefault = function ( isDefault ) {
318 if ( isDefault === undefined ) {
319 isDefault = !this.isDefault();
320 }
321 if ( this.isDefault() !== isDefault ) {
322 this.defaultSize = isDefault;
323 if ( isDefault ) {
324 this.setCurrentDimensions(
325 this.getDefaultDimensions()
326 );
327 }
328 this.emit( 'defaultSizeChange', this.isDefault() );
329 }
330 };
331
332 /**
333 * Set the minimum dimensions
334 *
335 * @param {Object} dimensions Dimensions object with width & height
336 * @fires ve.dm.Scalable#minSizeChange
337 */
338 ve.dm.Scalable.prototype.setMinDimensions = function ( dimensions ) {
339 if (
340 this.constructor.static.isDimensionsObjectValid( dimensions ) &&
341 !ve.compare( dimensions, this.getMinDimensions() )
342 ) {
343 this.minDimensions = ve.copy( dimensions );
344 this.valid = null;
345 this.emit( 'minSizeChange', dimensions );
346 }
347 };
348
349 /**
350 * Set the maximum dimensions
351 *
352 * @param {Object} dimensions Dimensions object with width & height
353 * @fires ve.dm.Scalable#maxSizeChange
354 */
355 ve.dm.Scalable.prototype.setMaxDimensions = function ( dimensions ) {
356 if (
357 this.constructor.static.isDimensionsObjectValid( dimensions ) &&
358 !ve.compare( dimensions, this.getMaxDimensions() )
359 ) {
360 this.maxDimensions = ve.copy( dimensions );
361 this.emit( 'maxSizeChange', dimensions );
362 this.valid = null;
363 }
364 };
365
366 /**
367 * Clear the minimum dimensions
368 *
369 * @fires ve.dm.Scalable#minSizeChange
370 */
371 ve.dm.Scalable.prototype.clearMinDimensions = function () {
372 if ( this.minDimensions !== null ) {
373 this.minDimensions = null;
374 this.valid = null;
375 this.emit( 'minSizeChange', this.minDimensions );
376 }
377 };
378
379 /**
380 * Clear the maximum dimensions
381 *
382 * @fires ve.dm.Scalable#maxSizeChange
383 */
384 ve.dm.Scalable.prototype.clearMaxDimensions = function () {
385 if ( this.maxDimensions !== null ) {
386 this.maxDimensions = null;
387 this.valid = null;
388 this.emit( 'maxSizeChange', this.maxDimensions );
389 }
390 };
391
392 /**
393 * Get the original dimensions
394 *
395 * @return {Object} Dimensions object with width & height
396 */
397 ve.dm.Scalable.prototype.getCurrentDimensions = function () {
398 return this.currentDimensions;
399 };
400
401 /**
402 * Get the original dimensions
403 *
404 * @return {Object} Dimensions object with width & height
405 */
406 ve.dm.Scalable.prototype.getOriginalDimensions = function () {
407 return this.originalDimensions;
408 };
409
410 /**
411 * Get the default dimensions
412 *
413 * @return {Object} Dimensions object with width & height
414 */
415 ve.dm.Scalable.prototype.getDefaultDimensions = function () {
416 return this.defaultDimensions;
417 };
418
419 /**
420 * Get the default state of the scalable object
421 *
422 * @return {boolean} Default size or custom
423 */
424 ve.dm.Scalable.prototype.isDefault = function () {
425 return this.defaultSize;
426 };
427
428 /**
429 * Get the minimum dimensions
430 *
431 * @return {Object} Dimensions object with width & height
432 */
433 ve.dm.Scalable.prototype.getMinDimensions = function () {
434 return this.minDimensions;
435 };
436
437 /**
438 * Get the maximum dimensions
439 *
440 * @return {Object} Dimensions object with width & height
441 */
442 ve.dm.Scalable.prototype.getMaxDimensions = function () {
443 return this.maxDimensions;
444 };
445
446 /**
447 * The object enforces the minimum dimensions when scaling
448 *
449 * @return {boolean} Enforces the minimum dimensions
450 */
451 ve.dm.Scalable.prototype.isEnforcedMin = function () {
452 return this.enforceMin;
453 };
454
455 /**
456 * The object enforces the maximum dimensions when scaling
457 *
458 * @return {boolean} Enforces the maximum dimensions
459 */
460 ve.dm.Scalable.prototype.isEnforcedMax = function () {
461 return this.enforceMax;
462 };
463
464 /**
465 * Set enforcement of minimum dimensions
466 *
467 * @param {boolean} enforceMin Enforces the minimum dimensions
468 */
469 ve.dm.Scalable.prototype.setEnforcedMin = function ( enforceMin ) {
470 this.valid = null;
471 this.enforceMin = !!enforceMin;
472 };
473
474 /**
475 * Set enforcement of maximum dimensions
476 *
477 * @param {boolean} enforceMax Enforces the maximum dimensions
478 */
479 ve.dm.Scalable.prototype.setEnforcedMax = function ( enforceMax ) {
480 this.valid = null;
481 this.enforceMax = !!enforceMax;
482 };
483
484 /**
485 * Get the fixed aspect ratio (width/height)
486 *
487 * @return {number} Aspect ratio
488 */
489 ve.dm.Scalable.prototype.getRatio = function () {
490 return this.ratio;
491 };
492
493 /**
494 * Check if the object has a fixed ratio
495 *
496 * @return {boolean} The object has a fixed ratio
497 */
498 ve.dm.Scalable.prototype.isFixedRatio = function () {
499 return this.fixedRatio;
500 };
501
502 /**
503 * Get the current scale of the object
504 *
505 * @return {number|null} A scale (1=100%), or null if not applicable
506 */
507 ve.dm.Scalable.prototype.getCurrentScale = function () {
508 if ( !this.isFixedRatio() || !this.getCurrentDimensions() || !this.getOriginalDimensions() ) {
509 return null;
510 }
511 return this.getCurrentDimensions().width / this.getOriginalDimensions().width;
512 };
513
514 /**
515 * Check if current dimensions are smaller than minimum dimensions in either direction
516 *
517 * Only possible if enforceMin is false.
518 *
519 * @return {boolean} Current dimensions are greater than maximum dimensions
520 */
521 ve.dm.Scalable.prototype.isTooSmall = function () {
522 return !!( this.getCurrentDimensions() && this.getMinDimensions() &&
523 (
524 this.getCurrentDimensions().width < this.getMinDimensions().width ||
525 this.getCurrentDimensions().height < this.getMinDimensions().height
526 )
527 );
528 };
529
530 /**
531 * Check if current dimensions are greater than maximum dimensions in either direction
532 *
533 * Only possible if enforceMax is false.
534 *
535 * @return {boolean} Current dimensions are greater than maximum dimensions
536 */
537 ve.dm.Scalable.prototype.isTooLarge = function () {
538 return !!( this.getCurrentDimensions() && this.getMaxDimensions() &&
539 (
540 this.getCurrentDimensions().width > this.getMaxDimensions().width ||
541 this.getCurrentDimensions().height > this.getMaxDimensions().height
542 )
543 );
544 };
545
546 /**
547 * Get a set of dimensions bounded by current restrictions, from specified dimensions
548 *
549 * @param {Object} dimensions Dimensions object with width & height
550 * @param {number} [grid] Optional grid size to snap to
551 * @return {Object} Dimensions object with width & height
552 */
553 ve.dm.Scalable.prototype.getBoundedDimensions = function ( dimensions, grid ) {
554 const minDimensions = this.isEnforcedMin() && this.getMinDimensions(),
555 maxDimensions = this.isEnforcedMax() && this.getMaxDimensions();
556
557 dimensions = ve.copy( dimensions );
558
559 // Bound to min/max
560 if ( minDimensions ) {
561 dimensions.width = Math.max( dimensions.width, this.minDimensions.width );
562 dimensions.height = Math.max( dimensions.height, this.minDimensions.height );
563 }
564 if ( maxDimensions ) {
565 dimensions.width = Math.min( dimensions.width, this.maxDimensions.width );
566 dimensions.height = Math.min( dimensions.height, this.maxDimensions.height );
567 }
568
569 // Bound to ratio
570 if ( this.isFixedRatio() ) {
571 const ratio = dimensions.width / dimensions.height;
572 if ( ratio < this.getRatio() ) {
573 dimensions.height = Math.round( dimensions.width / this.getRatio() );
574 } else {
575 dimensions.width = Math.round( dimensions.height * this.getRatio() );
576 }
577 }
578
579 // Snap to grid
580 if ( grid ) {
581 let snapMin = minDimensions ? Math.ceil( minDimensions.width / grid ) : -Infinity;
582 let snapMax = maxDimensions ? Math.floor( maxDimensions.width / grid ) : Infinity;
583 let snap = Math.round( dimensions.width / grid );
584 dimensions.width = Math.max( Math.min( snap, snapMax ), snapMin ) * grid;
585 if ( this.isFixedRatio() ) {
586 // If the ratio is fixed we can't snap both to the grid, so just snap the width
587 dimensions.height = Math.round( dimensions.width / this.getRatio() );
588 } else {
589 snapMin = minDimensions ? Math.ceil( minDimensions.height / grid ) : -Infinity;
590 snapMax = maxDimensions ? Math.floor( maxDimensions.height / grid ) : Infinity;
591 snap = Math.round( dimensions.height / grid );
592 dimensions.height = Math.max( Math.min( snap, snapMax ), snapMin ) * grid;
593 }
594 }
595
596 return dimensions;
597 };
598
599 /**
600 * Checks whether the current dimensions are numeric and within range
601 *
602 * @return {boolean} Current dimensions are valid
603 */
604 ve.dm.Scalable.prototype.isCurrentDimensionsValid = function () {
605 const dimensions = this.getCurrentDimensions(),
606 minDimensions = this.isEnforcedMin() && !ve.isEmptyObject( this.getMinDimensions() ) && this.getMinDimensions(),
607 maxDimensions = this.isEnforcedMax() && !ve.isEmptyObject( this.getMaxDimensions() ) && this.getMaxDimensions();
608
609 this.valid = (
610 !!dimensions &&
611 // Dimensions must be non-zero
612 +dimensions.width &&
613 +dimensions.height &&
614 (
615 !minDimensions || (
616 dimensions.width >= minDimensions.width &&
617 dimensions.height >= minDimensions.height
618 )
619 ) &&
620 (
621 !maxDimensions || (
622 dimensions.width <= maxDimensions.width &&
623 dimensions.height <= maxDimensions.height
624 )
625 )
626 );
627 return this.valid;
628 };