jenkins-bot has submitted this change and it was merged.
Change subject: Add an event to updating current values in scalable
......................................................................
Add an event to updating current values in scalable
The scalable object can have its current values change externally
but this change should propogate to other dependent objects.
Specifically, this is crucial to allow the media dialog to switch
an image without destroying and rebuilding a new scalable, but
rather just updating the current adjusted dimension values
directly, which are then updated in the media size widget.
This allows I4327c86a to work and update dimensions properly.
Change-Id: Idbb8b29db8e47afa9a658a02b2eaf2c3478edb48
---
M src/dm/ve.dm.Scalable.js
M src/ui/widgets/ve.ui.MediaSizeWidget.js
2 files changed, 111 insertions(+), 22 deletions(-)
Approvals:
Catrope: Looks good to me, approved
jenkins-bot: Verified
diff --git a/src/dm/ve.dm.Scalable.js b/src/dm/ve.dm.Scalable.js
index 0a565ad..fbada5c 100644
--- a/src/dm/ve.dm.Scalable.js
+++ b/src/dm/ve.dm.Scalable.js
@@ -38,6 +38,13 @@
this.valid = null;
this.defaultSize = false;
+ // Initialize
+ this.currentDimensions = {};
+ this.defaultDimensions = {};
+ this.originalDimensions = {};
+ this.minDimensions = {};
+ this.maxDimensions = {};
+
// Properties
this.fixedRatio = config.fixedRatio;
if ( config.currentDimensions ) {
@@ -70,31 +77,38 @@
/* Events */
/**
+ * Current changed
+ *
+ * @event currentSizeChange
+ * @param {Object} currentDimensions Current dimensions width and height
+ */
+
+/**
* Default size or state changed
*
* @event defaultSizeChange
- * @param {boolean} The size is default
+ * @param {boolean} isDefault The size is default
*/
/**
* Original size changed
*
* @event originalSizeChange
- * @param {Object} Original dimensions width and height
+ * @param {Object} originalDimensions Original dimensions width and height
*/
/**
* Min size changed
*
* @event minSizeChange
- * @param {Object} Min dimensions width and height
+ * @param {Object} minDimensions Min dimensions width and height
*/
/**
* Max size changed
*
* @event maxSizeChange
- * @param {Object} Max dimensions width and height
+ * @param {Object} maxDimensions Max dimensions width and height
*/
/**
@@ -190,15 +204,24 @@
* Also sets the aspect ratio if not set and in fixed ratio mode.
*
* @param {Object} dimensions Dimensions object with width & height
+ * @fires currentSizeChange
*/
ve.dm.Scalable.prototype.setCurrentDimensions = function ( dimensions ) {
- if ( this.isDimensionsObjectValid( dimensions ) ) {
+ var oldDimensions = this.getCurrentDimensions();
+
+ // Only update the new value if new dimensions are valid and
+ // is different than the old dimensions
+ if (
+ this.isDimensionsObjectValid( dimensions ) &&
+ !ve.compare( dimensions, oldDimensions )
+ ) {
this.currentDimensions = ve.copy( dimensions );
// Only use current dimensions for ratio if it isn't set
if ( this.fixedRatio && !this.ratio ) {
this.setRatioFromDimensions(
this.getCurrentDimensions() );
}
this.valid = null;
+ this.emit( 'currentSizeChange', this.getCurrentDimensions() );
}
};
@@ -246,6 +269,42 @@
this.valid = null;
this.emit( 'defaultSizeChange', this.isDefault() );
}
+};
+
+/**
+ * Reset and remove the default dimensions
+ * @fires defaultSizeChange
+ */
+ve.dm.Scalable.prototype.clearDefaultDimensions = function () {
+ this.defaultDimensions = {};
+ this.valid = null;
+ this.emit( 'defaultSizeChange', this.isDefault() );
+};
+
+/**
+ * Reset and remove the max dimensions
+ */
+ve.dm.Scalable.prototype.clearMaxDimensions = function () {
+ this.maxDimensions = {};
+ this.valid = null;
+};
+
+/**
+ * Reset and remove the min dimensions
+ */
+ve.dm.Scalable.prototype.clearMinDimensions = function () {
+ this.minDimensions = {};
+ this.valid = null;
+};
+
+/**
+ * Reset and remove the default dimensions
+ * @fires originalSizeChange
+ */
+ve.dm.Scalable.prototype.clearOriginalDimensions = function () {
+ this.originalDimensions = {};
+ this.valid = null;
+ this.emit( 'originalSizeChange', this.isDefault() );
};
/**
@@ -542,8 +601,8 @@
*/
ve.dm.Scalable.prototype.isCurrentDimensionsValid = function () {
var dimensions = this.getCurrentDimensions(),
- minDimensions = this.isEnforcedMin() && this.getMinDimensions(),
- maxDimensions = this.isEnforcedMax() && this.getMaxDimensions();
+ minDimensions = this.isEnforcedMin() && !$.isEmptyObject(
this.getMinDimensions() ) && this.getMinDimensions(),
+ maxDimensions = this.isEnforcedMax() && !$.isEmptyObject(
this.getMaxDimensions() ) && this.getMaxDimensions();
this.valid = (
$.isNumeric( dimensions.width ) &&
diff --git a/src/ui/widgets/ve.ui.MediaSizeWidget.js
b/src/ui/widgets/ve.ui.MediaSizeWidget.js
index 20f1279..6817592 100644
--- a/src/ui/widgets/ve.ui.MediaSizeWidget.js
+++ b/src/ui/widgets/ve.ui.MediaSizeWidget.js
@@ -156,6 +156,20 @@
var disabled = !dimensions || $.isEmptyObject( dimensions );
this.fullSizeButton.setDisabled( disabled );
this.sizeTypeSelectWidget.getItemFromData( 'default' ).setDisabled(
disabled );
+ // Revalidate current dimensions
+ this.validateDimensions();
+};
+
+/**
+ * Respond to change in current dimensions in the scalable object.
+ *
+ * @param {Object} dimensions Original dimensions
+ */
+ve.ui.MediaSizeWidget.prototype.onScalableCurrentSizeChange = function (
dimensions ) {
+ if ( !$.isEmptyObject( dimensions ) ) {
+ this.setCurrentDimensions( dimensions );
+ this.validateDimensions();
+ }
};
/**
@@ -172,6 +186,7 @@
'default' :
'custom'
);
+ this.validateDimensions();
};
/**
@@ -215,7 +230,10 @@
* @fires changeSizeType
*/
ve.ui.MediaSizeWidget.prototype.onSizeTypeChoose = function ( item ) {
- var selectedType = item && item.getData();
+ var selectedType = item && item.getData(),
+ wasDefault = this.scalable.isDefault();
+
+ this.scalable.toggleDefault( selectedType === 'default' );
if ( selectedType === 'default' ) {
this.scaleInput.setDisabled( true );
@@ -234,13 +252,11 @@
// Disable the scale input
this.scaleInput.setDisabled( true );
// If we were default size before, set the current dimensions
to the default size
- if ( this.scalable.isDefault() && !$.isEmptyObject(
this.dimensionsWidget.getDefaults() ) ) {
+ if ( wasDefault && !$.isEmptyObject(
this.dimensionsWidget.getDefaults() ) ) {
this.setCurrentDimensions(
this.dimensionsWidget.getDefaults() );
}
this.validateDimensions();
}
-
- this.scalable.toggleDefault( selectedType === 'default' );
this.emit( 'changeSizeType', selectedType );
this.validateDimensions();
@@ -295,7 +311,8 @@
// Events
this.scalable.connect( this, {
defaultSizeChange: 'onScalableDefaultSizeChange',
- originalSizeChange: 'onScalableOriginalSizeChange'
+ originalSizeChange: 'onScalableOriginalSizeChange',
+ currentSizeChange: 'onScalableCurrentSizeChange'
} );
this.updateDefaultDimensions();
@@ -320,6 +337,7 @@
'custom'
);
}
+ this.validateDimensions();
};
/**
@@ -423,6 +441,8 @@
* @fires change
*/
ve.ui.MediaSizeWidget.prototype.setCurrentDimensions = function ( dimensions )
{
+ var normalizedDimensions;
+
// Recursion protection
if ( this.preventChangeRecursion ) {
return;
@@ -430,22 +450,28 @@
this.preventChangeRecursion = true;
// Normalize the new dimensions
- this.currentDimensions = ve.dm.Scalable.static.getDimensionsFromValue(
dimensions, this.scalable.getRatio() );
+ normalizedDimensions = ve.dm.Scalable.static.getDimensionsFromValue(
dimensions, this.scalable.getRatio() );
- if ( this.currentDimensions.width || this.currentDimensions.height ) {
+ if (
+ // Update only if the dimensions object is valid
+ this.scalable.isDimensionsObjectValid( normalizedDimensions ) &&
+ // And only if the dimensions object is not default
+ !this.scalable.isDefault()
+ ) {
+ this.currentDimensions = normalizedDimensions;
// This will only update if the value has changed
// Set width & height individually as they may be 0
this.dimensionsWidget.setWidth( this.currentDimensions.width );
this.dimensionsWidget.setHeight( this.currentDimensions.height
);
+
+ // Update scalable object
+ this.scalable.setCurrentDimensions( this.currentDimensions );
+
+ this.validateDimensions();
+
+ // Emit change event
+ this.emit( 'change', this.currentDimensions );
}
-
- // Update scalable object
- this.scalable.setCurrentDimensions( this.currentDimensions );
-
- this.validateDimensions();
-
- // Emit change event
- this.emit( 'change', this.currentDimensions );
this.preventChangeRecursion = false;
};
@@ -479,6 +505,10 @@
} else {
this.dimensionsWidget.removeDefaults();
}
+ this.sizeTypeSelectWidget.getItemFromData( 'default' ).setDisabled(
+ $.isEmptyObject( defaultDimensions )
+ );
+ this.validateDimensions();
};
/**
--
To view, visit https://gerrit.wikimedia.org/r/153069
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: Idbb8b29db8e47afa9a658a02b2eaf2c3478edb48
Gerrit-PatchSet: 9
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Jforrester <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits