Debian Codegraph @

extensions/VisualEditor/lib/ve/src/ui/widgets/ve.ui.MediaSizeWidget.js

raw symbols SCIP
1 /*!
2 * VisualEditor UserInterface MediaSizeWidget class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Widget that lets the user edit dimensions (width and height),
9 * based on a scalable object.
10 *
11 * @class
12 * @extends OO.ui.Widget
13 *
14 * @constructor
15 * @param {ve.dm.Scalable} [scalable]
16 * @param {Object} [config] Configuration options
17 * @param {boolean} [config.noDefaultDimensions] The item being sized doesn't have default dimensions
18 * @param {string} [config.dimensionsAlign] Alignment for the dimensions widget
19 */
20 ve.ui.MediaSizeWidget = function VeUiMediaSizeWidget( scalable, config ) {
21 // Configuration
22 config = config || {};
23
24 this.scalable = scalable;
25
26 // Parent constructor
27 ve.ui.MediaSizeWidget.super.call( this, config );
28
29 // Properties
30 this.ratio = {};
31 this.currentDimensions = {};
32 this.maxDimensions = {};
33 this.valid = null;
34 this.noDefaultDimensions = !!config.noDefaultDimensions;
35 this.dimensionsAlign = config.dimensionsAlign || 'right';
36
37 // Define button select widget
38 this.sizeTypeSelect = new OO.ui.ButtonSelectWidget( {
39 classes: [ 've-ui-mediaSizeWidget-section-sizetype' ]
40 } );
41 this.sizeTypeSelect.addItems( [
42 new OO.ui.ButtonOptionWidget( {
43 data: 'default',
44 label: ve.msg( 'visualeditor-mediasizewidget-sizeoptions-default' )
45 } ),
46 // TODO: when upright is supported by Parsoid
47 // new OO.ui.ButtonOptionWidget( {
48 // data: 'scale',
49 // label: ve.msg( 'visualeditor-mediasizewidget-sizeoptions-scale' )
50 // } ),
51 new OO.ui.ButtonOptionWidget( {
52 data: 'custom',
53 label: ve.msg( 'visualeditor-mediasizewidget-sizeoptions-custom' )
54 } )
55 ] );
56 const sizeTypeField = new OO.ui.FieldLayout( this.sizeTypeSelect );
57
58 // Define scale
59 /*
60 this.scaleInput = new OO.ui.TextInputWidget();
61 scalePercentLabel = new OO.ui.LabelWidget( {
62 input: this.scaleInput,
63 label: ve.msg( 'visualeditor-mediasizewidget-label-scale-percent' )
64 } );
65 */
66
67 this.dimensions = new ve.ui.DimensionsWidget( { validate: this.isValid.bind( this ) } );
68
69 // Error label is available globally so it can be displayed and
70 // hidden as needed
71 this.errorLabel = new OO.ui.LabelWidget( {
72 label: ve.msg( 'visualeditor-mediasizewidget-label-defaulterror' )
73 } );
74
75 // Field layouts
76 /*
77 scaleField = new OO.ui.FieldLayout(
78 this.scaleInput, {
79 align: 'right',
80 // TODO: when upright is supported by Parsoid
81 // classes: ['ve-ui-mediaSizeWidget-section-scale'],
82 label: ve.msg( 'visualeditor-mediasizewidget-label-scale' )
83 }
84 );
85 TODO: when upright is supported by Parsoid
86 this.scaleInput.$element.append( scalePercentLabel.$element );
87 */
88 const dimensionsField = new OO.ui.FieldLayout(
89 this.dimensions, {
90 align: this.dimensionsAlign,
91 classes: [ 've-ui-mediaSizeWidget-section-custom' ]
92 }
93 );
94
95 // Build GUI
96 this.$element.addClass( 've-ui-mediaSizeWidget' );
97 if ( !this.noDefaultDimensions ) {
98 this.$element.append( sizeTypeField.$element );
99 }
100 this.$element.append( dimensionsField.$element );
101 // TODO: when upright is supported by Parsoid
102 // this.$element.append( scaleField.$element );
103 this.$element.append(
104 $( '<div>' )
105 .addClass( 've-ui-mediaSizeWidget-label-error' )
106 .append( this.errorLabel.$element )
107 );
108
109 // Events
110 this.dimensions.connect( this, {
111 widthChange: [ 'onDimensionsChange', 'width' ],
112 heightChange: [ 'onDimensionsChange', 'height' ]
113 } );
114 // TODO: when upright is supported by Parsoid
115 // this.scaleInput.connect( this, { change: 'onScaleChange' } );
116 this.sizeTypeSelect.connect( this, { choose: 'onSizeTypeChoose' } );
117
118 };
119
120 /* Inheritance */
121
122 OO.inheritClass( ve.ui.MediaSizeWidget, OO.ui.Widget );
123
124 /* Events */
125
126 /**
127 * @event ve.ui.MediaSizeWidget#change
128 * @param {Object} dimensions Width and height dimensions
129 */
130
131 /**
132 * @event ve.ui.MediaSizeWidget#valid
133 * @param {boolean} isValid Current dimensions are valid
134 */
135
136 /**
137 * @event ve.ui.MediaSizeWidget#changeSizeType
138 * @param {string} sizeType 'default', 'custom' or 'scale'
139 */
140
141 /* Methods */
142
143 /**
144 * Respond to change in original dimensions in the scalable object.
145 * Specifically, enable or disable the 'default' option.
146 *
147 * @param {Object} dimensions Original dimensions
148 */
149 ve.ui.MediaSizeWidget.prototype.onScalableOriginalSizeChange = function () {
150 // Revalidate current dimensions
151 this.updateDisabled();
152 this.validateDimensions();
153 };
154
155 /**
156 * Respond to change in current dimensions in the scalable object.
157 *
158 * @param {Object} dimensions Original dimensions
159 */
160 ve.ui.MediaSizeWidget.prototype.onScalableCurrentSizeChange = function ( dimensions ) {
161 if ( !ve.isEmptyObject( dimensions ) ) {
162 this.setCurrentDimensions( dimensions );
163 this.validateDimensions();
164 }
165 };
166
167 /**
168 * Respond to default size or status change in the scalable object.
169 *
170 * @param {boolean} isDefault Current default state
171 */
172 ve.ui.MediaSizeWidget.prototype.onScalableDefaultSizeChange = function ( isDefault ) {
173 // Update the default size into the dimensions widget
174 this.updateDefaultDimensions();
175 // TODO: When 'scale' ('upright' support) is ready, this will need to be adjusted
176 // to support that as well
177 this.setSizeType(
178 isDefault ?
179 'default' :
180 'custom'
181 );
182 this.validateDimensions();
183 };
184
185 /**
186 * Respond to width/height input value change. Only update dimensions if
187 * the value is numeric. Invoke validation for every change.
188 *
189 * This is triggered every time the dimension widget has its values changed
190 * either by the user or externally. The external call to 'setCurrentDimensions'
191 * will result in this event being evoked if the dimension inputs have changed,
192 * and same with changing dimensions type.
193 *
194 * The 'change' event for the entire widget is emitted through this method, as
195 * it means that the actual values have changed, regardless of whether they
196 * are valid or not.
197 *
198 * @param {string} type The input that was updated, 'width' or 'height'
199 * @param {string} value The new value of the input
200 * @fires ve.ui.MediaSizeWidget#change
201 */
202 ve.ui.MediaSizeWidget.prototype.onDimensionsChange = function ( type, value ) {
203 if ( +value === 0 && !this.noDefaultDimensions ) {
204 this.setSizeType( 'default' );
205 } else {
206 this.setSizeType( 'custom' );
207 if ( !isNaN( +value ) ) {
208 const dimensions = {};
209 dimensions[ type ] = +value;
210 this.setCurrentDimensions( dimensions );
211 } else {
212 this.validateDimensions();
213 }
214 }
215 };
216
217 // /**
218 // * Respond to change of the scale input
219 // */
220 /*
221 ve.ui.MediaSizeWidget.prototype.onScaleChange = function () {
222 // If the input changed (and not empty), set to 'custom'
223 // Otherwise, set to 'default'
224 if ( !this.dimensions.isEmpty() ) {
225 this.sizeTypeSelect.selectItemByData( 'scale' );
226 } else {
227 this.sizeTypeSelect.selectItemByData( 'default' );
228 }
229 };
230 */
231
232 /**
233 * Respond to size type change
234 *
235 * @param {OO.ui.OptionWidget} item Selected size type item
236 * @fires ve.ui.MediaSizeWidget#changeSizeType
237 */
238 ve.ui.MediaSizeWidget.prototype.onSizeTypeChoose = function ( item ) {
239 const selectedType = item.getData(),
240 wasDefault = this.scalable.isDefault();
241
242 this.scalable.toggleDefault( selectedType === 'default' );
243
244 if ( selectedType === 'default' ) {
245 // If there are defaults, put them into the values
246 if ( !ve.isEmptyObject( this.dimensions.getDefaults() ) ) {
247 this.dimensions.clear();
248 }
249 } else if ( selectedType === 'custom' ) {
250 // If we were default size before, set the current dimensions to the default size
251 if ( wasDefault && !ve.isEmptyObject( this.dimensions.getDefaults() ) ) {
252 this.setCurrentDimensions( this.dimensions.getDefaults() );
253 }
254 this.validateDimensions();
255 }
256
257 this.emit( 'changeSizeType', selectedType );
258 this.updateDisabled();
259 this.validateDimensions();
260 };
261
262 // /**
263 // * Set the placeholder value of the scale input
264 // *
265 // * @param {number} value Placeholder value
266 // * @chainable
267 // * @return {ve.ui.MediaSizeWidget}
268 // */
269 /*
270 ve.ui.MediaSizeWidget.prototype.setScalePlaceholder = function ( value ) {
271 this.scaleInput.$element.prop( 'placeholder', value );
272 return this;
273 };
274 */
275
276 // /**
277 // * Get the placeholder value of the scale input
278 // *
279 // * @return {string} Placeholder value
280 // */
281 /*
282 ve.ui.MediaSizeWidget.prototype.getScalePlaceholder = function () {
283 return this.scaleInput.$element.prop( 'placeholder' );
284 };
285 */
286
287 /**
288 * Select a size type in the select widget
289 *
290 * @param {string} sizeType The size type to select
291 * @chainable
292 * @return {ve.ui.MediaSizeWidget}
293 */
294 ve.ui.MediaSizeWidget.prototype.setSizeType = function ( sizeType ) {
295 if (
296 this.getSizeType() !== sizeType ||
297 // If the dimensions widget has zeros make sure to
298 // allow for the change in size type
299 +this.dimensions.getWidth() === 0 ||
300 +this.dimensions.getHeight() === 0
301 ) {
302 this.sizeTypeSelect.chooseItem(
303 this.sizeTypeSelect.findItemFromData( sizeType )
304 );
305 }
306 return this;
307 };
308 /**
309 * Get the size type from the select widget
310 *
311 * @return {string} The size type
312 */
313 ve.ui.MediaSizeWidget.prototype.getSizeType = function () {
314 return this.sizeTypeSelect.findSelectedItem() ? this.sizeTypeSelect.findSelectedItem().getData() : '';
315 };
316
317 /**
318 * Set the scalable object the widget deals with
319 *
320 * @param {ve.dm.Scalable} scalable A scalable object representing the media source being resized.
321 * @chainable
322 * @return {ve.ui.MediaSizeWidget}
323 */
324 ve.ui.MediaSizeWidget.prototype.setScalable = function ( scalable ) {
325 if ( this.scalable instanceof ve.dm.Scalable ) {
326 this.scalable.disconnect( this );
327 }
328 this.scalable = scalable;
329 // Events
330 this.scalable.connect( this, {
331 defaultSizeChange: 'onScalableDefaultSizeChange',
332 originalSizeChange: 'onScalableOriginalSizeChange',
333 currentSizeChange: 'onScalableCurrentSizeChange'
334 } );
335
336 this.updateDefaultDimensions();
337
338 if ( !this.scalable.isDefault() ) {
339 // Reset current dimensions to new scalable object
340 this.setCurrentDimensions( this.scalable.getCurrentDimensions() );
341 }
342
343 // Call for the set size type according to default or custom settings of the scalable
344 if ( this.scalable.getOriginalDimensions() ) {
345 this.setSizeType( this.scalable.isDefault() ? 'default' : 'custom' );
346 }
347 this.updateDisabled();
348 this.validateDimensions();
349 return this;
350 };
351
352 /**
353 * Get the attached scalable object
354 *
355 * @return {ve.dm.Scalable} The scalable object representing the media
356 * source being resized.
357 */
358 ve.ui.MediaSizeWidget.prototype.getScalable = function () {
359 return this.scalable;
360 };
361
362 /**
363 * Set the image aspect ratio explicitly
364 *
365 * @param {number} ratio Numerical value of an aspect ratio
366 * @chainable
367 * @return {ve.ui.MediaSizeWidget}
368 */
369 ve.ui.MediaSizeWidget.prototype.setRatio = function ( ratio ) {
370 this.scalable.setRatio( ratio );
371 return this;
372 };
373
374 /**
375 * Get the current aspect ratio
376 *
377 * @return {number} Aspect ratio
378 */
379 ve.ui.MediaSizeWidget.prototype.getRatio = function () {
380 return this.scalable.getRatio();
381 };
382
383 /**
384 * Set the maximum dimensions for the image. These will be limited only if
385 * enforcedMax is true.
386 *
387 * @param {Object} dimensions Height and width
388 * @chainable
389 * @return {ve.ui.MediaSizeWidget}
390 */
391 ve.ui.MediaSizeWidget.prototype.setMaxDimensions = function ( dimensions ) {
392 // Normalize dimensions before setting
393 const maxDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() );
394 this.scalable.setMaxDimensions( maxDimensions );
395 return this;
396 };
397
398 /**
399 * Retrieve the currently defined maximum dimensions
400 *
401 * @return {Object} dimensions Height and width
402 */
403 ve.ui.MediaSizeWidget.prototype.getMaxDimensions = function () {
404 return this.scalable.getMaxDimensions();
405 };
406
407 /**
408 * Retrieve the current dimensions
409 *
410 * @return {Object} Width and height
411 */
412 ve.ui.MediaSizeWidget.prototype.getCurrentDimensions = function () {
413 return this.currentDimensions;
414 };
415
416 /**
417 * @inheritdoc
418 */
419 ve.ui.MediaSizeWidget.prototype.setDisabled = function ( disabled ) {
420 // Parent method
421 ve.ui.MediaSizeWidget.super.prototype.setDisabled.call( this, disabled );
422
423 this.updateDisabled();
424 return this;
425 };
426
427 /**
428 * Update the disabled state of sub widgets
429 *
430 * @chainable
431 * @return {ve.ui.MediaSizeWidget}
432 */
433 ve.ui.MediaSizeWidget.prototype.updateDisabled = function () {
434 const disabled = this.isDisabled();
435
436 // The 'updateDisabled' method may called before the widgets
437 // are fully defined. So, before disabling/enabling anything,
438 // make sure the objects exist
439 if ( this.sizeTypeSelect &&
440 this.dimensions &&
441 this.scalable
442 ) {
443 const sizeType = this.getSizeType();
444
445 // Disable the type select
446 this.sizeTypeSelect.setDisabled( disabled );
447
448 // Disable the default type options
449 this.sizeTypeSelect.findItemFromData( 'default' ).setDisabled(
450 ve.isEmptyObject( this.scalable.getDefaultDimensions() )
451 );
452
453 // Disable the dimensions widget
454 this.dimensions.setDisabled( disabled || sizeType !== 'custom' );
455
456 // Disable the scale widget
457 // this.scaleInput.setDisabled( disabled || sizeType !== 'scale' );
458 }
459 return this;
460 };
461
462 /**
463 * Updates the current dimensions in the inputs, either one at a time or both
464 *
465 * @param {Object} dimensions Dimensions with width and height
466 * @fires ve.ui.MediaSizeWidget#change
467 */
468 ve.ui.MediaSizeWidget.prototype.setCurrentDimensions = function ( dimensions ) {
469 // Recursion protection
470 if ( this.preventChangeRecursion ) {
471 return;
472 }
473 this.preventChangeRecursion = true;
474
475 if ( !this.scalable.isFixedRatio() ) {
476 dimensions = ve.extendObject( {}, this.getCurrentDimensions(), dimensions );
477 }
478
479 // Normalize the new dimensions
480 const normalizedDimensions = ve.dm.Scalable.static.getDimensionsFromValue( dimensions, this.scalable.getRatio() );
481
482 if (
483 // Update only if the dimensions object is valid
484 ve.dm.Scalable.static.isDimensionsObjectValid( normalizedDimensions ) &&
485 // And only if the dimensions object is not default
486 !this.scalable.isDefault()
487 ) {
488 this.currentDimensions = normalizedDimensions;
489 // This will only update if the value has changed
490 // Set width & height individually as they may be 0
491 this.dimensions.setWidth( this.currentDimensions.width );
492 this.dimensions.setHeight( this.currentDimensions.height );
493
494 // Update scalable object
495 this.scalable.setCurrentDimensions( this.currentDimensions );
496
497 this.validateDimensions();
498 // Emit change event
499 this.emit( 'change', this.currentDimensions );
500 }
501 this.preventChangeRecursion = false;
502 };
503
504 /**
505 * Validate current dimensions.
506 * Explicitly call for validating the current dimensions. This is especially
507 * useful if we've changed conditions for the widget, like limiting image
508 * dimensions for thumbnails when the image type changes. Triggers the error
509 * class if needed.
510 *
511 * @return {boolean} Current dimensions are valid
512 * @fires ve.ui.MediaSizeWidget#valid
513 */
514 ve.ui.MediaSizeWidget.prototype.validateDimensions = function () {
515 const isValid = this.isValid();
516
517 if ( this.valid !== isValid ) {
518 this.valid = isValid;
519 this.errorLabel.toggle( !isValid );
520 this.dimensions.setValidityFlag();
521 // Emit validation change event
522 this.emit( 'valid', this.valid );
523 }
524 return isValid;
525 };
526
527 /**
528 * Set default dimensions for the widget. Values are given by scalable's
529 * defaultDimensions. If no default dimensions are available,
530 * the defaults are removed.
531 */
532 ve.ui.MediaSizeWidget.prototype.updateDefaultDimensions = function () {
533 const defaultDimensions = this.scalable.getDefaultDimensions();
534
535 if ( !ve.isEmptyObject( defaultDimensions ) ) {
536 this.dimensions.setDefaults( defaultDimensions );
537 } else {
538 this.dimensions.removeDefaults();
539 }
540 this.updateDisabled();
541 this.validateDimensions();
542 };
543
544 /**
545 * Check if the custom dimensions are empty.
546 *
547 * @return {boolean} Both width/height values are empty
548 */
549 ve.ui.MediaSizeWidget.prototype.isCustomEmpty = function () {
550 return this.dimensions.isEmpty();
551 };
552
553 // /**
554 // * Check if the scale input is empty.
555 // *
556 // * @return {boolean} Scale input value is empty
557 // */
558 /*
559 ve.ui.MediaSizeWidget.prototype.isScaleEmpty = function () {
560 return ( this.scaleInput.getValue() === '' );
561 };
562 */
563
564 /**
565 * Check if all inputs are empty.
566 *
567 * @return {boolean} All input values are empty
568 */
569 ve.ui.MediaSizeWidget.prototype.isEmpty = function () {
570 return this.isCustomEmpty();
571 // return this.isCustomEmpty() && this.isScaleEmpty();
572 };
573
574 /**
575 * Check whether the current value inputs are valid
576 * 1. If placeholders are visible, the input is valid
577 * 2. If inputs have non numeric values, input is invalid
578 * 3. If inputs have numeric values, validate through scalable
579 * calculations to see if the dimensions follow the rules.
580 *
581 * @return {boolean} Valid or invalid dimension values
582 */
583 ve.ui.MediaSizeWidget.prototype.isValid = function () {
584 const itemType = this.sizeTypeSelect.findSelectedItem() ?
585 this.sizeTypeSelect.findSelectedItem().getData() : 'custom';
586
587 // TODO: when upright is supported by Parsoid add validation for scale
588
589 if ( itemType === 'custom' ) {
590 if (
591 this.dimensions.getDefaults() &&
592 this.dimensions.isEmpty()
593 ) {
594 return true;
595 } else if (
596 !isNaN( +this.dimensions.getWidth() ) &&
597 !isNaN( +this.dimensions.getHeight() )
598 ) {
599 return this.scalable.isCurrentDimensionsValid();
600 } else {
601 return false;
602 }
603 } else {
604 // Default images are always valid size
605 return true;
606 }
607 };