Store original dimensions results in IV store

* Remove the this.mediaSize custom store object and replace
  with a hash function and the document's IV store.
* Remove this.inputs/this.fieldsets namespaces.
* Calculate this.filename as soon as this.mediaNode is set.
* Rename getMediaSize to getOriginalDimensions

Change-Id: I2030aade5d96555451f6a390d0aa3d44b860841f
This commit is contained in:
Ed Sanders 2014-01-26 14:26:57 +00:00
parent ed7678eb6b
commit f6acb93d5a

View file

@ -24,12 +24,8 @@ ve.ui.MWMediaEditDialog = function VeUiMWMediaEditDialog( windowSet, config ) {
// Properties // Properties
this.mediaNode = null; this.mediaNode = null;
this.captionNode = null; this.captionNode = null;
// Cache for image original size, if requested this.store = null;
this.mediaSize = {};
this.filename = null; this.filename = null;
// GUI properties
this.inputs = {};
this.fieldsets = {};
}; };
/* Inheritance */ /* Inheritance */
@ -132,14 +128,14 @@ ve.ui.MWMediaEditDialog.prototype.initialize = function () {
// Define fieldsets for image settings // Define fieldsets for image settings
// Caption // Caption
this.fieldsets.caption = new OO.ui.FieldsetLayout( { this.captionFieldset = new OO.ui.FieldsetLayout( {
'$': this.$, '$': this.$,
'label': ve.msg( 'visualeditor-dialog-media-content-section' ), 'label': ve.msg( 'visualeditor-dialog-media-content-section' ),
'icon': 'parameter' 'icon': 'parameter'
} ); } );
// Size // Size
this.fieldsets.size = new OO.ui.FieldsetLayout( { this.sizeFieldset = new OO.ui.FieldsetLayout( {
'$': this.$, '$': this.$,
'label': ve.msg( 'visualeditor-dialog-media-size-section' ), 'label': ve.msg( 'visualeditor-dialog-media-size-section' ),
'icon': 'parameter' 'icon': 'parameter'
@ -150,12 +146,12 @@ ve.ui.MWMediaEditDialog.prototype.initialize = function () {
'label': ve.msg( 'visualeditor-dialog-media-size-originalsize-error' ) 'label': ve.msg( 'visualeditor-dialog-media-size-originalsize-error' )
} ); } );
this.inputs.size = new ve.ui.MediaSizeWidget( { this.sizeWidget = new ve.ui.MediaSizeWidget( {
'$': this.$ '$': this.$
} ); } );
this.fieldsets.size.$element.append( [ this.sizeFieldset.$element.append( [
this.inputs.size.$element, this.sizeWidget.$element,
this.sizeErrorLabel.$element, this.sizeErrorLabel.$element,
this.$spinner this.$spinner
] ); ] );
@ -172,8 +168,8 @@ ve.ui.MWMediaEditDialog.prototype.initialize = function () {
this.applyButton.connect( this, { 'click': [ 'close', { 'action': 'apply' } ] } ); this.applyButton.connect( this, { 'click': [ 'close', { 'action': 'apply' } ] } );
// Initialization // Initialization
this.generalSettingsPage.$element.append( this.fieldsets.caption.$element ); this.generalSettingsPage.$element.append( this.captionFieldset.$element );
this.advancedSettingsPage.$element.append( this.fieldsets.size.$element ); this.advancedSettingsPage.$element.append( this.sizeFieldset.$element );
this.$body.append( this.bookletLayout.$element ); this.$body.append( this.bookletLayout.$element );
this.$foot.append( this.applyButton.$element ); this.$foot.append( this.applyButton.$element );
@ -183,16 +179,13 @@ ve.ui.MWMediaEditDialog.prototype.initialize = function () {
* Get the original size of the media object from the API, if it exists * Get the original size of the media object from the API, if it exists
* @returns {jQuery.Promise} * @returns {jQuery.Promise}
*/ */
ve.ui.MWMediaEditDialog.prototype.getMediaSize = function () { ve.ui.MWMediaEditDialog.prototype.getOriginalDimensions = function () {
var rawfilename = this.mediaNode.getAttribute( 'resource' ), var index = this.store.indexOfHash( this.constructor.static.getSizeHash( this.filename ) ),
deferred = $.Deferred(); deferred = $.Deferred();
// Strip the raw filename up to the 'File:' namespage if ( index ) {
this.filename = rawfilename.substring( rawfilename.indexOf( 'File:' ) );
if ( this.mediaSize && !$.isEmptyObject( this.mediaSize[this.filename] ) ) {
// The image size is already cached // The image size is already cached
deferred.resolve( this.mediaSize[this.filename] ); deferred.resolve( this.store.value( index ) );
} else { } else {
// Look for the media size through the API // Look for the media size through the API
$.ajax( { $.ajax( {
@ -232,7 +225,7 @@ ve.ui.MWMediaEditDialog.prototype.getMediaSize = function () {
* @inheritdoc * @inheritdoc
*/ */
ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) { ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) {
var attrs, newDoc, var attrs, newDoc, originalSize, resource,
dimensions = {}, dimensions = {},
doc = this.surface.getModel().getDocument(); doc = this.surface.getModel().getDocument();
@ -242,6 +235,12 @@ ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) {
// Properties // Properties
this.mediaNode = this.surface.getView().getFocusedNode().getModel(); this.mediaNode = this.surface.getView().getFocusedNode().getModel();
this.captionNode = this.mediaNode.getCaptionNode(); this.captionNode = this.mediaNode.getCaptionNode();
this.store = this.surface.getModel().getDocument().getStore();
// Strip the raw filename up to the 'File:' namespage
resource = this.mediaNode.getAttribute( 'resource' );
this.filename = resource.substring( resource.indexOf( 'File:' ) );
if ( this.captionNode && this.captionNode.getLength() > 0 ) { if ( this.captionNode && this.captionNode.getLength() > 0 ) {
newDoc = doc.cloneFromRange( this.captionNode.getRange() ); newDoc = doc.cloneFromRange( this.captionNode.getRange() );
} else { } else {
@ -268,10 +267,10 @@ ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) {
this.$spinner.show(); this.$spinner.show();
// Save original size for later calculations // Save original size for later calculations
this.getMediaSize().done( ve.bind( function ( sizeObj ) { this.getOriginalDimensions().done( ve.bind( function ( sizeObj ) {
if ( sizeObj && sizeObj.width && sizeObj.height ) { if ( sizeObj && sizeObj.width && sizeObj.height ) {
// Set the original dimensions in the widget // Set the original dimensions in the widget
this.inputs.size.setOriginalDimensions( { this.sizeWidget.setOriginalDimensions( {
'width': sizeObj.width, 'width': sizeObj.width,
'height': sizeObj.height 'height': sizeObj.height
} ); } );
@ -279,18 +278,19 @@ ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) {
// Check if we need to limit the size // Check if we need to limit the size
if ( sizeObj.mediatype === 'BITMAP' ) { if ( sizeObj.mediatype === 'BITMAP' ) {
// Set the max dimensions // Set the max dimensions
this.inputs.size.setMaxDimensions( { this.sizeWidget.setMaxDimensions( {
'width': sizeObj.width, 'width': sizeObj.width,
'height': sizeObj.height 'height': sizeObj.height
} ); } );
} }
// Cache the size and mediatype // Cache the originalSize and mediatype
this.mediaSize[this.filename] = { originalSize = {
'height': sizeObj.height, 'height': sizeObj.height,
'width': sizeObj.width, 'width': sizeObj.width,
'mediatype': sizeObj.mediatype 'mediatype': sizeObj.mediatype
}; };
this.store.index( originalSize, this.constructor.static.getSizeHash( this.filename ) );
} else { } else {
// Original dimensions couldn't be fetched. Display an error message // Original dimensions couldn't be fetched. Display an error message
this.sizeErrorLabel.$element.hide(); this.sizeErrorLabel.$element.hide();
@ -304,13 +304,13 @@ ve.ui.MWMediaEditDialog.prototype.setup = function ( data ) {
if ( attrs.width !== undefined && Number( attrs.width ) > 0 ) { if ( attrs.width !== undefined && Number( attrs.width ) > 0 ) {
dimensions.width = attrs.width; dimensions.width = attrs.width;
} }
this.inputs.size.setDimensions( dimensions ); this.sizeWidget.setDimensions( dimensions );
this.$spinner.hide(); this.$spinner.hide();
}, this ) ); }, this ) );
// Initialization // Initialization
this.fieldsets.caption.$element.append( this.captionSurface.$element ); this.captionFieldset.$element.append( this.captionSurface.$element );
this.captionSurface.initialize(); this.captionSurface.initialize();
}; };
@ -344,8 +344,8 @@ ve.ui.MWMediaEditDialog.prototype.teardown = function ( data ) {
); );
// Change attributes only if the values are valid // Change attributes only if the values are valid
if ( this.inputs.size.isValid() ) { if ( this.sizeWidget.isValid() ) {
attrs = this.inputs.size.getDimensions(); attrs = this.sizeWidget.getDimensions();
} }
surfaceModel.change( surfaceModel.change(
@ -354,7 +354,7 @@ ve.ui.MWMediaEditDialog.prototype.teardown = function ( data ) {
} }
// Clean size values // Clean size values
this.inputs.size.clear(); this.sizeWidget.clear();
// Cleanup // Cleanup
this.captionSurface.destroy(); this.captionSurface.destroy();
@ -365,6 +365,18 @@ ve.ui.MWMediaEditDialog.prototype.teardown = function ( data ) {
ve.ui.MWDialog.prototype.teardown.call( this, data ); ve.ui.MWDialog.prototype.teardown.call( this, data );
}; };
/* Static methods */
/**
* Get the store hash for the original dimensions of a given filename
*
* @param {string} filename Filename
* @returns {string} Store hash
*/
ve.ui.MWMediaEditDialog.static.getSizeHash = function ( filename ) {
return 'MWOriginalSize:' + filename;
};
/* Registration */ /* Registration */
ve.ui.dialogFactory.register( ve.ui.MWMediaEditDialog ); ve.ui.dialogFactory.register( ve.ui.MWMediaEditDialog );