Mooeypoo has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/185589

Change subject: When changing image source, use already-available API info
......................................................................

When changing image source, use already-available API info

When the user changes an image to another in the media dialog, we
already have API info that includes the original dimensions, media
type and other information that is required for the Scalable object
to compute the new current dimensions. We can use this info if it
exists instead of asking for another API call.

Change-Id: I9d86959b99b3f3dfed92255a9dba9a8fdd352dcb
---
M modules/ve-mw/dm/models/ve.dm.MWImageModel.js
M modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
2 files changed, 75 insertions(+), 37 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/89/185589/1

diff --git a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js 
b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
index 958a7d8..df3007c 100644
--- a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
+++ b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
@@ -257,11 +257,14 @@
 /**
  * Adjust the model parameters based on a new image
  * @param {Object} attrs New image source attributes
- * @param {Object} [dimensions] New dimensions of the image
+ * @param {Object} [APIinfo] The image's API info
+ * @throws {Error} Image has insufficient details to compute the imageModel 
details.
  */
-ve.dm.MWImageModel.prototype.changeImageSource = function ( attrs, dimensions 
) {
-       var newDimensions, remoteFilename,
+ve.dm.MWImageModel.prototype.changeImageSource = function ( attrs, APIinfo ) {
+       var remoteFilename,
                imageModel = this;
+
+       APIinfo = APIinfo || {};
 
        if ( attrs.mediaType ) {
                this.setMediaType( attrs.mediaType );
@@ -284,43 +287,49 @@
        this.scalable.clearMaxDimensions();
        this.scalable.clearMinDimensions();
 
-       // Call for updated scalable
-       if ( remoteFilename ) {
-               ve.dm.MWImageNode.static.getScalablePromise( remoteFilename 
).done( function ( info ) {
-                       imageModel.scalable.setOriginalDimensions( {
-                               width: info.width,
-                               height: info.height
-                       } );
-                       // Update media type
-                       imageModel.setMediaType( info.mediatype );
-                       // Update defaults
-                       ve.dm.MWImageNode.static.syncScalableToType(
-                               imageModel.getType(),
-                               info.mediatype,
-                               imageModel.scalable
-                       );
+       // If we already have dimensions from the API, use them
+       if ( APIinfo.width && APIinfo.height ) {
+               imageModel.scalable.setOriginalDimensions( {
+                       width: APIinfo.width,
+                       height: APIinfo.height
                } );
-       }
-
-       // Resize the new image's current dimensions to default or based on the 
bounding box
-       if ( this.isDefaultSize() ) {
-               // Scale to default
-               newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( 
dimensions );
+               // Update media type
+               imageModel.setMediaType( APIinfo.mediatype );
+               // Update defaults
+               ve.dm.MWImageNode.static.syncScalableToType(
+                       imageModel.getType(),
+                       APIinfo.mediatype,
+                       imageModel.scalable
+               );
+               imageModel.updateScalableDetails( {
+                       width: APIinfo.width,
+                       height: APIinfo.height
+               } );
        } else {
-               if ( this.getBoundingBox() ) {
-                       // Scale the new image by its width
-                       newDimensions = 
ve.dm.MWImageNode.static.resizeToBoundingBox(
-                               dimensions,
-                               this.boundingBox,
-                               'width'
-                       );
+               // Call for updated scalable if we don't have dimensions from 
the API info
+               if ( remoteFilename ) {
+                       // Update anyways
+                       ve.dm.MWImageNode.static.getScalablePromise( 
remoteFilename ).done( function ( info ) {
+                               imageModel.scalable.setOriginalDimensions( {
+                                       width: info.width,
+                                       height: info.height
+                               } );
+                               // Update media type
+                               imageModel.setMediaType( info.mediatype );
+                               // Update defaults
+                               ve.dm.MWImageNode.static.syncScalableToType(
+                                       imageModel.getType(),
+                                       info.mediatype,
+                                       imageModel.scalable
+                               );
+                               imageModel.updateScalableDetails( {
+                                       width: info.width,
+                                       height: info.height
+                               } );
+                       } );
                } else {
-                       newDimensions = dimensions;
+                       throw new Error( 'Cannot compute details for an image 
without remote filename and without sizing info.' );
                }
-       }
-
-       if ( newDimensions ) {
-               this.getScalable().setCurrentDimensions( newDimensions );
        }
 };
 
@@ -1112,6 +1121,35 @@
 };
 
 /**
+ * If the image changed, update scalable definitions.
+ * @param {Object} originalDimensions Image original dimensions
+ */
+ve.dm.MWImageModel.prototype.updateScalableDetails = function ( 
originalDimensions ) {
+       var newDimensions;
+
+       // Resize the new image's current dimensions to default or based on the 
bounding box
+       if ( this.isDefaultSize() ) {
+               // Scale to default
+               newDimensions = ve.dm.MWImageNode.static.scaleToThumbnailSize( 
originalDimensions );
+       } else {
+               if ( this.getBoundingBox() ) {
+                       // Scale the new image by its width
+                       newDimensions = 
ve.dm.MWImageNode.static.resizeToBoundingBox(
+                               originalDimensions,
+                               this.boundingBox,
+                               'width'
+                       );
+               } else {
+                       newDimensions = originalDimensions;
+               }
+       }
+
+       if ( newDimensions ) {
+               this.getScalable().setCurrentDimensions( newDimensions );
+       }
+};
+
+/**
  * Set image caption document.
  *
  * @param {ve.dm.Document} Image caption document
diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js 
b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
index 8153347..1e1436e 100644
--- a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
+++ b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
@@ -792,7 +792,7 @@
                                        src: info.url,
                                        resource: './' + item.title
                                },
-                               { width: info.width, height: info.height }
+                               info
                        );
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/185589
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9d86959b99b3f3dfed92255a9dba9a8fdd352dcb
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Mooeypoo <mor...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to