Mooeypoo has uploaded a new change for review.

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

Change subject: Refactoring ImageModel consructor
......................................................................

Refactoring ImageModel consructor

This change creates the scalable object that's attached to the image
model through its constructor. Also adds a 'resourceName' variable
to store attributes.resource, mostly for API calls.

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


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

diff --git a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js 
b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
index 0c0eb88..8ad3676 100644
--- a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
+++ b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
@@ -12,8 +12,13 @@
  * @mixins OO.EventEmitter
  *
  * @constructor
+ * @param {Object} [config] Configuration options
  */
-ve.dm.MWImageModel = function VeDmMWImageModel() {
+ve.dm.MWImageModel = function VeDmMWImageModel( config ) {
+       var scalable;
+
+       config = config || {};
+
        // Mixin constructors
        OO.EventEmitter.call( this );
 
@@ -37,6 +42,29 @@
        // Get wiki default thumbnail size
        this.defaultThumbSize = mw.config.get( 'wgVisualEditorConfig' )
                .defaultUserOptions.defaultthumbsize;
+
+       if ( config.resourceName ) {
+               this.setResourceName( config.resourceName );
+       }
+
+       // Create scalable
+       config.currentDimensions = config.currentDimensions || {};
+       config.minDimensions = config.minDimensions || {};
+
+       scalable = new ve.dm.Scalable( {
+               'currentDimensions': {
+                       'width': config.currentDimensions.width,
+                       'height': config.currentDimensions.height
+               },
+               'minDimensions': {
+                       'width': config.minDimensions.width || 1,
+                       'height': config.minDimensions.height || 1
+               },
+               'defaultSize': config.defaultSize || false
+       } );
+       // Set the initial scalable, connect it to events
+       // and request an update from the API
+       this.updateScalable( scalable );
 };
 
 /* Inheritance */
@@ -124,25 +152,18 @@
  * @return {ve.dm.MWImageModel} Image model
  */
 ve.dm.MWImageModel.static.newFromImageAttributes = function ( attrs, dir ) {
-       var scalable,
-               imgModel = new ve.dm.MWImageModel();
+       var imgModel = new ve.dm.MWImageModel( {
+                       'resourceName': attrs.resource.replace( /^(.+\/)*/, '' 
),
+                       'currentDimensions': {
+                               'width': attrs.width,
+                               'height': attrs.height
+                       },
+                       'defaultSize': attrs.defaultSize
+               } );
 
        // Cache the attributes so we can create a new image without
        // losing any existing information
        imgModel.cacheOriginalImageAttributes( attrs );
-
-       // Create scalable
-       scalable = new ve.dm.Scalable( {
-               'currentDimensions': {
-                       'width': attrs.width,
-                       'height': attrs.height
-               },
-               'minDimensions': {
-                       'width': 1,
-                       'height': 1
-               }
-       } );
-       imgModel.setScalable( scalable );
 
        // Collect all the information
        imgModel.toggleBorder( !!attrs.borderImage );
@@ -370,6 +391,14 @@
 };
 
 /**
+ * Set the image file resource name
+ * @param {string} resourceName The resource name of the given media file
+ */
+ve.dm.MWImageModel.prototype.setResourceName = function ( resourceName ) {
+       this.imageResourceName = resourceName;
+};
+
+/**
  * Set symbolic name of media type.
  *
  * Example values: "BITMAP" for JPEG or PNG images; "DRAWING" for SVG graphics
@@ -455,6 +484,14 @@
  */
 ve.dm.MWImageModel.prototype.isBorderable = function () {
        return this.borderable;
+};
+
+/**
+ * Get the image file resource name
+ * @returns {string} resourceName The resource name of the given media file
+ */
+ve.dm.MWImageModel.prototype.getResourceName = function () {
+       return this.imageResourceName;
 };
 
 /**
@@ -803,9 +840,9 @@
  *
  * @param {ve.dm.Scalable} Scalable object
  */
-ve.dm.MWImageModel.prototype.setScalable = function ( scalable ) {
-       var imageName,
-               attrs = this.getOriginalImageAttributes();
+ve.dm.MWImageModel.prototype.updateScalable = function ( scalable ) {
+       var imageName = this.getResourceName();
+//             attrs = this.getOriginalImageAttributes();
 
        if ( this.scalable instanceof ve.dm.Scalable ) {
                this.scalable.disconnect( this );
@@ -815,24 +852,24 @@
        // Events
        this.scalable.connect( this, { 'defaultSizeChange': 
'onScalableDefaultSizeChange' } );
 
-       // Update the given scalable object according to model attributes
-       imageName = attrs.resource.replace( /^(.+\/)*/, '' );
        // Call for updated scalable
-       ve.dm.MWImageNode.static.getScalablePromise( imageName ).done( ve.bind( 
function ( info ) {
-               this.scalable.setOriginalDimensions( {
-                       'width': info.width,
-                       'height': info.height
-               } );
-               // Update media type
-               this.setMediaType( info.mediatype );
+       if ( imageName ) {
+               ve.dm.MWImageNode.static.getScalablePromise( imageName ).done( 
ve.bind( function ( info ) {
+                       this.scalable.setOriginalDimensions( {
+                               'width': info.width,
+                               'height': info.height
+                       } );
+                       // Update media type
+                       this.setMediaType( info.mediatype );
 
-               // Update according to type
-               ve.dm.MWImageNode.static.syncScalableToType(
-                       this.getType(),
-                       this.getMediaType(),
-                       this.getScalable()
-               );
-       }, this ) );
+                       // Update according to type
+                       ve.dm.MWImageNode.static.syncScalableToType(
+                               this.getType(),
+                               this.getMediaType(),
+                               this.getScalable()
+                       );
+               }, this ) );
+       }
 };
 
 /**
diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js 
b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
index 98ae27a..e79941d 100644
--- a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
+++ b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
@@ -430,6 +430,7 @@
                        attrs.align = this.imageModel.getAlignment();
                        attrs.width = dimensions.width;
                        attrs.height = dimensions.height;
+                       attrs.defaultSize = this.imageModel.isDefaultSize();
                        if ( this.imageModel.getAltText() ) {
                                attrs.alt = this.imageModel.getAltText();
                        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I713c59d1cec5f87387a87c98a59e75b3d5c1050e
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