Mooeypoo has uploaded a new change for review.

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

Change subject: Check for changes in media dialog
......................................................................

Check for changes in media dialog

Create an image model hash and check for changes to the image every
time the dialog is changed, so we can activate and deactivate the
apply button properly.

Bug: 68058
Change-Id: Ic68d3d2e464d8311ef415dc2bfa41168f5d9285f
---
M modules/ve-mw/dm/models/ve.dm.MWImageModel.js
M modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
2 files changed, 99 insertions(+), 20 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/78/150478/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..cc8bfba 100644
--- a/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
+++ b/modules/ve-mw/dm/models/ve.dm.MWImageModel.js
@@ -19,12 +19,13 @@
 
        // Properties
        this.attributesCache = null;
+       this.initialHash = null;
 
        // Image properties
        this.captionDoc = null;
        this.caption = null;
        this.mediaType = null;
-       this.altText = null;
+       this.altText = '';
        this.type = null;
        this.alignment = null;
        this.scalable = null;
@@ -167,10 +168,40 @@
                'default' :
                'custom'
        );
+
+       // Save the initial hash
+       imgModel.storeInitialHash();
+
        return imgModel;
 };
 
 /* Methods */
+
+/**
+ * Get a hash of the current state of the model
+ * @return {Object} State hash
+ */
+ve.dm.MWImageModel.prototype.getHash = function () {
+       var hash = ve.copy( {
+               src: this.getImageSource(),
+               altText: this.getAltText(),
+               type: this.getType(),
+               alignment: this.getAlignment(),
+               sizeType: this.getSizeType(),
+               border: this.hasBorder(),
+               borderable: this.isBorderable()
+       } );
+
+       // Flatten the scalable
+       if ( this.getScalable() ) {
+               hash.scalable = {
+                       currentDimensions: ve.copy( 
this.getScalable().getCurrentDimensions() ),
+                       isDefault: !!this.getScalable().isDefault()
+               };
+       }
+
+       return hash;
+};
 
 /**
  * Get the current image node type according to the attributes.
@@ -619,7 +650,7 @@
  * @param {string} text Alternate text
  */
 ve.dm.MWImageModel.prototype.setAltText = function ( text ) {
-       this.altText = text;
+       this.altText = text || '';
 };
 
 /**
@@ -795,7 +826,10 @@
  * @return {string} Source attribute
  */
 ve.dm.MWImageModel.prototype.getImageSource = function () {
-       return this.getOriginalImageAttributes().src;
+       if ( this.getOriginalImageAttributes() ) {
+               return this.getOriginalImageAttributes().src;
+       }
+       return '';
 };
 
 /**
@@ -843,3 +877,17 @@
 ve.dm.MWImageModel.prototype.setCaptionDocument = function ( doc ) {
        this.captionDoc = doc;
 };
+
+/**
+ * Store the initial hash of the model state.
+ */
+ve.dm.MWImageModel.prototype.storeInitialHash = function () {
+       this.initialHash = this.getHash();
+};
+
+/**
+ * Compare the current state of the model to the initial state
+ */
+ve.dm.MWImageModel.prototype.hasBeenModified = function () {
+       return !ve.compare( this.initialHash, this.getHash() );
+};
diff --git a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js 
b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
index a799a59..c958380 100644
--- a/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
+++ b/modules/ve-mw/ui/dialogs/ve.ui.MWMediaDialog.js
@@ -20,10 +20,12 @@
        ve.ui.MWMediaDialog.super.call( this, manager, config );
 
        // Properties
+       this.setupModelProcess = false;
        this.imageModel = null;
        this.store = null;
        this.fileRepoPromise = null;
        this.pageTitle = '';
+       this.isInsertion = true;
 
        this.$element.addClass( 've-ui-mwMediaDialog' );
 };
@@ -372,7 +374,7 @@
        this.positionInput.connect( this, { 'choose': 'onPositionInputChoose' } 
);
        this.typeInput.connect( this, { 'choose': 'onTypeInputChoose' } );
        this.search.connect( this, { 'select': 'onSearchSelect' } );
-       this.altTextInput.connect( this, { 'change': 'setChanged' } );
+       this.altTextInput.connect( this, { 'change': 'onAlternateTextChange' } 
);
 
        // Panel classes
        this.mediaSearchPanel.$element.addClass( 
've-ui-mwMediaDialog-panel-search' );
@@ -437,7 +439,7 @@
 
                this.setImageModel( attrs );
 
-               this.setChanged();
+               this.checkChanged();
                this.switchPanels( 'edit' );
        }
 };
@@ -456,7 +458,8 @@
        this.positionInput.selectItem( item );
 
        this.positionCheckbox.setValue( alignment !== 'none' );
-       this.setChanged();
+
+       this.checkChanged();
 };
 
 /**
@@ -476,7 +479,17 @@
        this.borderCheckbox.setValue(
                this.imageModel.isBorderable() && this.imageModel.hasBorder()
        );
-       this.setChanged();
+
+       this.checkChanged();
+};
+
+/**
+ * Respond to change of alternate text
+ * @param {string} text Changed alternate text
+ */
+ve.ui.MWMediaDialog.prototype.onAlternateTextChange = function ( text ) {
+       this.imageModel.setAltText( text );
+       this.checkChanged();
 };
 
 /**
@@ -489,7 +502,7 @@
                currentModelAlignment = this.imageModel.getAlignment();
 
        this.positionInput.setDisabled( !checked );
-       this.setChanged();
+       this.checkChanged();
        // Only update the model if the current value is different than that
        // of the image model
        if (
@@ -521,7 +534,7 @@
        if ( this.imageModel.hasBorder() !== checked ) {
                // Update the image model
                this.imageModel.toggleBorder( checked );
-               this.setChanged();
+               this.checkChanged();
        }
 };
 
@@ -536,7 +549,7 @@
        // Only update if the value is different than the model
        if ( this.imageModel.getAlignment() !== position ) {
                this.imageModel.setAlignment( position );
-               this.setChanged();
+               this.checkChanged();
        }
 };
 
@@ -551,7 +564,7 @@
        // Only update if the value is different than the model
        if ( this.imageModel.getType() !== type ) {
                this.imageModel.setType( type );
-               this.setChanged();
+               this.checkChanged();
        }
 
        // If type is 'frame', disable the size input widget completely
@@ -559,12 +572,24 @@
 };
 
 /**
- * When changes occur, enable the apply button.
+ * Check if changes occured to enable or disable the insert/apply buttons
  */
-ve.ui.MWMediaDialog.prototype.setChanged = function () {
-       // TODO: Set up a better and deeper test of whether the new
-       // image parameters are different than the original image
-       this.actions.setAbilities( { 'insert': true, 'apply': true } );
+ve.ui.MWMediaDialog.prototype.checkChanged = function () {
+       var captionChanged = false;
+
+       // Only check 'changed' status after the model has finished
+       // building itself
+       if ( !this.setupModelProcess ) {
+               if ( this.captionSurface && this.captionSurface.getSurface() ) {
+                       captionChanged = 
this.captionSurface.getSurface().getModel().hasBeenModified();
+               }
+
+               if ( this.imageModel.hasBeenModified() || captionChanged || 
this.isInsertion ) {
+                       this.actions.setAbilities( { 'insert': true, 'apply': 
true } );
+               } else {
+                       this.actions.setAbilities( { 'insert': false, 'apply': 
false } );
+               }
+       }
 };
 
 /**
@@ -605,6 +630,7 @@
                                namespace = mw.config.get( 'wgNamespaceNumber' 
),
                                namespacesWithSubpages = mw.config.get( 
'wgVisualEditor' ).namespacesWithSubpages;
 
+                       this.setupModelProcess = false;
                        // Read the page title
                        if ( namespacesWithSubpages[ namespace ] ) {
                                // If we are in a namespace that allows for 
subpages, strip the entire
@@ -615,6 +641,9 @@
 
                        if ( this.selectedNode ) {
                                this.setImageModel( 
this.selectedNode.getAttributes() );
+                               this.isInsertion = false;
+                       } else {
+                               this.isInsertion = true;
                        }
 
                        this.resetCaption();
@@ -720,16 +749,17 @@
        this.imageModel.connect( this, {
                'alignmentChange': 'onImageModelAlignmentChange',
                'typeChange': 'onImageModelTypeChange',
-               'sizeDefaultChange': 'setChanged'
+               'sizeDefaultChange': 'checkChanged'
        } );
 
        // Set up
+       this.setupModelProcess = true;
 
        // Size widget
        this.sizeErrorLabel.$element.hide();
        this.sizeWidget.setScalable( this.imageModel.getScalable() );
-       this.sizeWidget.connect( this, { 'changeSizeType': 'setChanged' } );
-       this.sizeWidget.connect( this, { 'change': 'setChanged' } );
+       this.sizeWidget.connect( this, { 'changeSizeType': 'checkChanged' } );
+       this.sizeWidget.connect( this, { 'change': 'checkChanged' } );
 
        // Initialize size
        this.sizeWidget.setSizeType(
@@ -773,6 +803,7 @@
                        this.imageModel.getType() || 'none'
                )
        );
+       this.setupModelProcess = false;
 };
 
 /**
@@ -838,7 +869,7 @@
                                this.captionSurface.getSurface(),
                                this.wikitextWarning
                        );
-                       this.setChanged();
+                       this.checkChanged();
                }
        } );
 };

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

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