2013-04-18 01:07:59 +00:00
|
|
|
|
/*!
|
|
|
|
|
* VisualEditor ContentEditable ResizableNode class.
|
|
|
|
|
*
|
|
|
|
|
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
|
|
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ContentEditable resizable node.
|
|
|
|
|
*
|
|
|
|
|
* @class
|
|
|
|
|
* @abstract
|
|
|
|
|
*
|
|
|
|
|
* @constructor
|
2013-11-01 19:45:59 +00:00
|
|
|
|
* @param {jQuery} [$resizable=this.$element] Resizable DOM element
|
2013-09-25 16:20:53 +00:00
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
|
* @param {number|null} [config.snapToGrid=10] Snap to a grid of size X when the shift key is held. Null disables.
|
2013-10-14 11:44:26 +00:00
|
|
|
|
* @param {boolean} [config.outline=false] Resize using an outline of the element only, don't live preview.
|
2013-09-25 11:25:44 +00:00
|
|
|
|
* @param {boolean} [config.showSizeLabel=true] Show a label with the current dimensions while resizing
|
2013-04-18 01:07:59 +00:00
|
|
|
|
*/
|
2013-09-25 16:20:53 +00:00
|
|
|
|
ve.ce.ResizableNode = function VeCeResizableNode( $resizable, config ) {
|
2013-04-18 01:07:59 +00:00
|
|
|
|
// Properties
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$resizable = $resizable || this.$element;
|
2013-04-18 01:07:59 +00:00
|
|
|
|
this.ratio = this.model.getAttribute( 'width' ) / this.model.getAttribute( 'height' );
|
|
|
|
|
this.resizing = false;
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$resizeHandles = this.$( '<div>' );
|
2013-09-25 16:20:53 +00:00
|
|
|
|
this.snapToGrid = ( config && config.snapToGrid !== undefined ) ? config.snapToGrid : 10;
|
2013-10-14 11:44:26 +00:00
|
|
|
|
this.outline = !!( config && config.outline );
|
2013-09-25 11:25:44 +00:00
|
|
|
|
if ( !config || config.showSizeLabel !== false ) {
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$sizeText = this.$( '<span>' ).addClass( 've-ce-resizableNode-sizeText' );
|
|
|
|
|
this.$sizeLabel = this.$( '<div>' ).addClass( 've-ce-resizableNode-sizeLabel' ).append( this.$sizeText );
|
2013-09-25 11:25:44 +00:00
|
|
|
|
}
|
2013-10-22 20:15:40 +00:00
|
|
|
|
this.resizableOffset = null;
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
|
|
// Events
|
2013-05-22 10:52:53 +00:00
|
|
|
|
this.connect( this, {
|
|
|
|
|
'focus': 'onResizableFocus',
|
|
|
|
|
'blur': 'onResizableBlur',
|
2013-06-05 20:59:19 +00:00
|
|
|
|
'live': 'onResizableLive',
|
2013-10-14 11:44:26 +00:00
|
|
|
|
'resizing': 'onResizableResizing',
|
2013-10-14 11:02:08 +00:00
|
|
|
|
'resizeEnd': 'onResizableFocus'
|
2013-05-22 10:52:53 +00:00
|
|
|
|
} );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
|
this.$resizeHandles
|
|
|
|
|
.addClass( 've-ce-resizableNode-handles' )
|
2013-11-01 19:45:59 +00:00
|
|
|
|
.append( this.$( '<div>' ).addClass( 've-ce-resizableNode-nwHandle' ) )
|
|
|
|
|
.append( this.$( '<div>' ).addClass( 've-ce-resizableNode-neHandle' ) )
|
|
|
|
|
.append( this.$( '<div>' ).addClass( 've-ce-resizableNode-seHandle' ) )
|
|
|
|
|
.append( this.$( '<div>' ).addClass( 've-ce-resizableNode-swHandle' ) );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-10-14 11:02:08 +00:00
|
|
|
|
/* Events */
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @event resizeStart
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @event resizing
|
|
|
|
|
* @param {Object} dimensions Dimension object containing width & height
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @event resizeEnd
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
2013-06-07 00:51:00 +00:00
|
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
|
|
ve.ce.ResizableNode.static = {};
|
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
|
/* Methods */
|
|
|
|
|
|
2013-10-22 20:15:40 +00:00
|
|
|
|
/**
|
|
|
|
|
* Get and cache the relative offset of the $resizable node
|
|
|
|
|
*
|
|
|
|
|
* @returns {Object} Position coordinates, containing top & left
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.getResizableOffset = function () {
|
|
|
|
|
if ( !this.resizableOffset ) {
|
2013-10-09 20:09:59 +00:00
|
|
|
|
this.resizableOffset = OO.ui.Element.getRelativePosition(
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$resizable, this.getRoot().getSurface().getSurface().$element
|
2013-10-22 20:15:40 +00:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return this.resizableOffset;
|
|
|
|
|
};
|
|
|
|
|
|
2013-09-25 11:25:44 +00:00
|
|
|
|
/**
|
|
|
|
|
* Update the contents and position of the size label
|
|
|
|
|
*
|
|
|
|
|
* Omitting the dimensions object will hide the size label.
|
|
|
|
|
*
|
|
|
|
|
* @param {Object} [dimensions] Dimensions object with width, height, top & left, or undefined to hide
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.updateSizeLabel = function ( dimensions ) {
|
|
|
|
|
if ( !this.$sizeLabel ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2013-10-22 20:01:17 +00:00
|
|
|
|
var offset, node, top, height;
|
2013-09-25 11:25:44 +00:00
|
|
|
|
if ( dimensions ) {
|
2013-10-22 20:15:40 +00:00
|
|
|
|
offset = this.getResizableOffset();
|
2013-09-25 11:25:44 +00:00
|
|
|
|
// Things get a bit tight below 100px, so put the label on the outside
|
|
|
|
|
if ( dimensions.width < 100 ) {
|
2013-10-22 20:01:17 +00:00
|
|
|
|
top = offset.top + dimensions.height;
|
2013-09-25 11:25:44 +00:00
|
|
|
|
height = 30;
|
|
|
|
|
} else {
|
2013-10-22 20:01:17 +00:00
|
|
|
|
top = offset.top;
|
2013-09-25 11:25:44 +00:00
|
|
|
|
height = dimensions.height;
|
|
|
|
|
}
|
|
|
|
|
this.$sizeLabel
|
|
|
|
|
.addClass( 've-ce-resizableNode-sizeLabel-resizing' )
|
|
|
|
|
.css( {
|
|
|
|
|
'top': top,
|
2013-10-22 20:01:17 +00:00
|
|
|
|
'left': offset.left,
|
2013-09-25 11:25:44 +00:00
|
|
|
|
'width': dimensions.width,
|
|
|
|
|
'height': height,
|
|
|
|
|
'lineHeight': height + 'px'
|
|
|
|
|
} );
|
|
|
|
|
this.$sizeText.text( Math.round( dimensions.width ) + ' × ' + Math.round( dimensions.height ) );
|
|
|
|
|
} else {
|
|
|
|
|
node = this;
|
|
|
|
|
// Defer the removal of this class otherwise other DOM changes may cause
|
|
|
|
|
// the opacity transition to not play out smoothly
|
|
|
|
|
setTimeout( function () {
|
|
|
|
|
node.$sizeLabel.removeClass( 've-ce-resizableNode-sizeLabel-resizing' );
|
|
|
|
|
} );
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Handle node focus.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableFocus = function () {
|
2013-09-25 11:25:44 +00:00
|
|
|
|
if ( this.$sizeLabel ) {
|
|
|
|
|
// Attach the size label first so it doesn't mask the resize handles
|
|
|
|
|
this.$sizeLabel.appendTo( this.root.getSurface().getSurface().$localOverlayControls );
|
|
|
|
|
}
|
2013-07-01 20:23:51 +00:00
|
|
|
|
this.$resizeHandles.appendTo( this.root.getSurface().getSurface().$localOverlayControls );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
2013-07-01 20:23:51 +00:00
|
|
|
|
this.setResizableHandlesSizeAndPosition();
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
2013-07-01 21:16:22 +00:00
|
|
|
|
this.$resizeHandles
|
|
|
|
|
.find( '.ve-ce-resizableNode-neHandle' )
|
|
|
|
|
.css( { 'margin-right': -this.$resizable.width() } )
|
|
|
|
|
.end()
|
|
|
|
|
.find( '.ve-ce-resizableNode-swHandle' )
|
|
|
|
|
.css( { 'margin-bottom': -this.$resizable.height() } )
|
|
|
|
|
.end()
|
|
|
|
|
.find( '.ve-ce-resizableNode-seHandle' )
|
|
|
|
|
.css( {
|
|
|
|
|
'margin-right': -this.$resizable.width(),
|
|
|
|
|
'margin-bottom': -this.$resizable.height()
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
this.$resizeHandles.children()
|
2013-10-15 23:42:20 +00:00
|
|
|
|
.off( '.ve-ce-resizableNode' )
|
2013-07-01 21:16:22 +00:00
|
|
|
|
.on(
|
2013-10-15 23:42:20 +00:00
|
|
|
|
'mousedown.ve-ce-resizableNode',
|
2013-07-01 21:16:22 +00:00
|
|
|
|
ve.bind( this.onResizeHandlesCornerMouseDown, this )
|
|
|
|
|
);
|
2013-04-18 01:07:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle node blur.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableBlur = function () {
|
2013-07-01 21:16:22 +00:00
|
|
|
|
this.$resizeHandles.detach();
|
2013-09-25 11:25:44 +00:00
|
|
|
|
if ( this.$sizeLabel ) {
|
|
|
|
|
this.$sizeLabel.detach();
|
|
|
|
|
}
|
2013-04-18 01:07:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-05-22 10:52:53 +00:00
|
|
|
|
/**
|
|
|
|
|
* Handle live event.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
*/
|
2013-07-01 20:23:51 +00:00
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableLive = function () {
|
|
|
|
|
var surfaceModel = this.getRoot().getSurface().getModel();
|
|
|
|
|
|
|
|
|
|
if ( this.live ) {
|
2013-11-05 01:05:27 +00:00
|
|
|
|
surfaceModel.getDocument().connect( this, { 'transact': 'setResizableHandlesSizeAndPosition' } );
|
2013-07-01 20:23:51 +00:00
|
|
|
|
} else {
|
2013-11-05 01:05:27 +00:00
|
|
|
|
surfaceModel.getDocument().disconnect( this, { 'transact': 'setResizableHandlesSizeAndPosition' } );
|
2013-07-01 20:23:51 +00:00
|
|
|
|
this.onResizableBlur();
|
2013-05-22 10:52:53 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-14 11:44:26 +00:00
|
|
|
|
/**
|
|
|
|
|
* Handle resizing event.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
* @param {Object} dimensions Dimension object containing width & height
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableResizing = function ( dimensions ) {
|
2013-10-22 20:15:40 +00:00
|
|
|
|
// Clear cached resizable offset position as it may have changed
|
|
|
|
|
this.resizableOffset = null;
|
2013-10-14 11:44:26 +00:00
|
|
|
|
if ( !this.outline ) {
|
|
|
|
|
this.$resizable.css( {
|
|
|
|
|
'width': dimensions.width,
|
|
|
|
|
'height': dimensions.height
|
|
|
|
|
} );
|
|
|
|
|
this.setResizableHandlesPosition();
|
|
|
|
|
}
|
2013-09-25 11:25:44 +00:00
|
|
|
|
this.updateSizeLabel( dimensions );
|
2013-10-14 11:44:26 +00:00
|
|
|
|
};
|
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Handle bounding box handle mousedown.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
* @param {jQuery.Event} e Click event
|
2013-10-22 17:54:59 +00:00
|
|
|
|
* @fires resizeStart
|
2013-04-18 01:07:59 +00:00
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onResizeHandlesCornerMouseDown = function ( e ) {
|
2013-04-19 19:56:55 +00:00
|
|
|
|
// Hide context menu
|
|
|
|
|
// TODO: Maybe there's a more generic way to handle this sort of thing? For relocation it's
|
|
|
|
|
// handled in ve.ce.Surface
|
|
|
|
|
this.root.getSurface().getSurface().getContext().hide();
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
|
|
// Set bounding box width and undo the handle margins
|
|
|
|
|
this.$resizeHandles
|
2013-10-15 23:42:20 +00:00
|
|
|
|
.addClass( 've-ce-resizableNode-handles-resizing' )
|
2013-04-18 01:07:59 +00:00
|
|
|
|
.css( {
|
2013-04-19 19:56:55 +00:00
|
|
|
|
'width': this.$resizable.width(),
|
|
|
|
|
'height': this.$resizable.height()
|
2013-04-18 01:07:59 +00:00
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
this.$resizeHandles.children().css( 'margin', 0 );
|
|
|
|
|
|
|
|
|
|
// Values to calculate adjusted bounding box size
|
|
|
|
|
this.resizeInfo = {
|
|
|
|
|
'mouseX': e.screenX,
|
|
|
|
|
'mouseY': e.screenY,
|
|
|
|
|
'top': this.$resizeHandles.position().top,
|
|
|
|
|
'left': this.$resizeHandles.position().left,
|
|
|
|
|
'height': this.$resizeHandles.height(),
|
|
|
|
|
'width': this.$resizeHandles.width(),
|
2013-06-05 10:47:47 +00:00
|
|
|
|
'handle': e.target.className
|
2013-04-18 01:07:59 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Bind resize events
|
|
|
|
|
this.resizing = true;
|
2013-09-25 11:25:44 +00:00
|
|
|
|
this.updateSizeLabel( this.resizeInfo );
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$( this.getElementDocument() ).on( {
|
2013-04-18 01:07:59 +00:00
|
|
|
|
'mousemove.ve-ce-resizableNode': ve.bind( this.onDocumentMouseMove, this ),
|
|
|
|
|
'mouseup.ve-ce-resizableNode': ve.bind( this.onDocumentMouseUp, this )
|
|
|
|
|
} );
|
2013-10-14 11:02:08 +00:00
|
|
|
|
this.emit( 'resizeStart' );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
|
2013-07-01 20:23:51 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set the proper size and position for resize handles
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.setResizableHandlesSizeAndPosition = function () {
|
2013-10-14 11:04:59 +00:00
|
|
|
|
var width = this.$resizable.width(),
|
|
|
|
|
height = this.$resizable.height();
|
|
|
|
|
|
2013-11-05 01:05:27 +00:00
|
|
|
|
// Clear cached resizable offset position as it may have changed
|
|
|
|
|
this.resizableOffset = null;
|
|
|
|
|
|
2013-10-14 11:04:59 +00:00
|
|
|
|
this.setResizableHandlesPosition();
|
2013-07-01 20:23:51 +00:00
|
|
|
|
|
|
|
|
|
this.$resizeHandles
|
|
|
|
|
.css( {
|
|
|
|
|
'width': 0,
|
2013-10-14 11:04:59 +00:00
|
|
|
|
'height': 0
|
2013-07-01 20:23:51 +00:00
|
|
|
|
} )
|
|
|
|
|
.find( '.ve-ce-resizableNode-neHandle' )
|
2013-10-14 11:04:59 +00:00
|
|
|
|
.css( { 'margin-right': -width } )
|
2013-07-01 20:23:51 +00:00
|
|
|
|
.end()
|
|
|
|
|
.find( '.ve-ce-resizableNode-swHandle' )
|
2013-10-14 11:04:59 +00:00
|
|
|
|
.css( { 'margin-bottom': -height } )
|
2013-07-01 20:23:51 +00:00
|
|
|
|
.end()
|
|
|
|
|
.find( '.ve-ce-resizableNode-seHandle' )
|
|
|
|
|
.css( {
|
2013-10-14 11:04:59 +00:00
|
|
|
|
'margin-right': -width,
|
|
|
|
|
'margin-bottom': -height
|
2013-07-01 20:23:51 +00:00
|
|
|
|
} );
|
|
|
|
|
};
|
|
|
|
|
|
2013-10-14 11:04:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Set the proper position for resize handles
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.setResizableHandlesPosition = function () {
|
2013-10-22 20:15:40 +00:00
|
|
|
|
var offset = this.getResizableOffset();
|
2013-10-14 11:04:59 +00:00
|
|
|
|
|
|
|
|
|
this.$resizeHandles.css( {
|
|
|
|
|
'top': offset.top,
|
|
|
|
|
'left': offset.left
|
|
|
|
|
} );
|
|
|
|
|
};
|
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
|
/**
|
|
|
|
|
* Handle body mousemove.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
|
|
|
|
* @param {jQuery.Event} e Click event
|
2013-10-22 17:54:59 +00:00
|
|
|
|
* @fires resizing
|
2013-04-18 01:07:59 +00:00
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onDocumentMouseMove = function ( e ) {
|
2013-09-25 16:20:53 +00:00
|
|
|
|
var newWidth, newHeight, newRatio, snapMin, snapMax, snap,
|
2013-04-18 01:07:59 +00:00
|
|
|
|
// TODO: Make these configurable
|
|
|
|
|
min = 1,
|
|
|
|
|
max = 1000,
|
|
|
|
|
diff = {},
|
|
|
|
|
dimensions = {
|
|
|
|
|
'width': 0,
|
|
|
|
|
'height': 0,
|
|
|
|
|
'top': this.resizeInfo.top,
|
|
|
|
|
'left': this.resizeInfo.left
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if ( this.resizing ) {
|
|
|
|
|
// X and Y diff
|
|
|
|
|
switch ( this.resizeInfo.handle ) {
|
|
|
|
|
case 've-ce-resizableNode-seHandle':
|
|
|
|
|
diff.x = e.screenX - this.resizeInfo.mouseX;
|
|
|
|
|
diff.y = e.screenY - this.resizeInfo.mouseY;
|
|
|
|
|
break;
|
|
|
|
|
case 've-ce-resizableNode-nwHandle':
|
|
|
|
|
diff.x = this.resizeInfo.mouseX - e.screenX;
|
|
|
|
|
diff.y = this.resizeInfo.mouseY - e.screenY;
|
|
|
|
|
break;
|
|
|
|
|
case 've-ce-resizableNode-neHandle':
|
|
|
|
|
diff.x = e.screenX - this.resizeInfo.mouseX;
|
|
|
|
|
diff.y = this.resizeInfo.mouseY - e.screenY;
|
|
|
|
|
break;
|
|
|
|
|
case 've-ce-resizableNode-swHandle':
|
|
|
|
|
diff.x = this.resizeInfo.mouseX - e.screenX;
|
|
|
|
|
diff.y = e.screenY - this.resizeInfo.mouseY;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Unconstrained dimensions and ratio
|
2013-10-14 11:21:59 +00:00
|
|
|
|
newWidth = Math.max( Math.min( this.resizeInfo.width + diff.x, max ), min );
|
|
|
|
|
newHeight = Math.max( Math.min( this.resizeInfo.height + diff.y, max ), min );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
newRatio = newWidth / newHeight;
|
|
|
|
|
|
|
|
|
|
// Fix the ratio
|
|
|
|
|
if ( this.ratio > newRatio ) {
|
|
|
|
|
dimensions.width = newWidth;
|
2013-10-14 11:21:59 +00:00
|
|
|
|
dimensions.height = this.resizeInfo.height +
|
|
|
|
|
( newWidth - this.resizeInfo.width ) / this.ratio;
|
2013-04-18 01:07:59 +00:00
|
|
|
|
} else {
|
2013-10-14 11:21:59 +00:00
|
|
|
|
dimensions.width = this.resizeInfo.width +
|
|
|
|
|
( newHeight - this.resizeInfo.height ) * this.ratio;
|
2013-04-18 01:07:59 +00:00
|
|
|
|
dimensions.height = newHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-25 16:20:53 +00:00
|
|
|
|
if ( this.snapToGrid && e.shiftKey ) {
|
|
|
|
|
snapMin = Math.ceil( min / this.snapToGrid );
|
|
|
|
|
snapMax = Math.floor( max / this.snapToGrid );
|
|
|
|
|
snap = Math.round( dimensions.width / this.snapToGrid );
|
|
|
|
|
dimensions.width = Math.max( Math.min( snap, snapMax ), snapMin ) * this.snapToGrid;
|
|
|
|
|
dimensions.height = dimensions.width / this.ratio;
|
|
|
|
|
}
|
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
|
// Fix the position
|
|
|
|
|
switch ( this.resizeInfo.handle ) {
|
|
|
|
|
case 've-ce-resizableNode-neHandle':
|
|
|
|
|
dimensions.top = this.resizeInfo.top +
|
|
|
|
|
( this.resizeInfo.height - dimensions.height );
|
|
|
|
|
break;
|
|
|
|
|
case 've-ce-resizableNode-swHandle':
|
|
|
|
|
dimensions.left = this.resizeInfo.left +
|
|
|
|
|
( this.resizeInfo.width - dimensions.width );
|
|
|
|
|
break;
|
|
|
|
|
case 've-ce-resizableNode-nwHandle':
|
|
|
|
|
dimensions.top = this.resizeInfo.top +
|
|
|
|
|
( this.resizeInfo.height - dimensions.height );
|
|
|
|
|
dimensions.left = this.resizeInfo.left +
|
|
|
|
|
( this.resizeInfo.width - dimensions.width );
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update bounding box
|
|
|
|
|
this.$resizeHandles.css( dimensions );
|
2013-10-14 11:02:08 +00:00
|
|
|
|
this.emit( 'resizing', dimensions );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Handle body mouseup.
|
|
|
|
|
*
|
|
|
|
|
* @method
|
2013-10-22 17:54:59 +00:00
|
|
|
|
* @fires resizeEnd
|
2013-04-18 01:07:59 +00:00
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.onDocumentMouseUp = function () {
|
2013-07-16 14:06:47 +00:00
|
|
|
|
var attrChanges,
|
|
|
|
|
offset = this.model.getOffset(),
|
2013-04-18 01:07:59 +00:00
|
|
|
|
width = this.$resizeHandles.outerWidth(),
|
|
|
|
|
height = this.$resizeHandles.outerHeight(),
|
|
|
|
|
surfaceModel = this.getRoot().getSurface().getModel(),
|
|
|
|
|
documentModel = surfaceModel.getDocument(),
|
2013-07-16 14:06:47 +00:00
|
|
|
|
selection = surfaceModel.getSelection();
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
2013-10-15 23:42:20 +00:00
|
|
|
|
this.$resizeHandles.removeClass( 've-ce-resizableNode-handles-resizing' );
|
2013-11-01 19:45:59 +00:00
|
|
|
|
this.$( this.getElementDocument() ).off( '.ve-ce-resizableNode' );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
this.resizing = false;
|
2013-09-25 11:25:44 +00:00
|
|
|
|
this.updateSizeLabel();
|
2013-04-19 19:56:55 +00:00
|
|
|
|
|
2013-06-30 04:47:58 +00:00
|
|
|
|
// Apply changes to the model
|
2013-07-16 14:06:47 +00:00
|
|
|
|
attrChanges = this.getAttributeChanges( width, height );
|
2013-06-30 04:47:58 +00:00
|
|
|
|
if ( !ve.isEmptyObject( attrChanges ) ) {
|
|
|
|
|
surfaceModel.change(
|
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges( documentModel, offset, attrChanges ),
|
|
|
|
|
selection
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-06-05 20:59:19 +00:00
|
|
|
|
|
2013-10-10 12:34:54 +00:00
|
|
|
|
// Update the context menu. This usually happens with the redraw, but not if the
|
2013-09-25 20:54:59 +00:00
|
|
|
|
// user doesn't perform a drag
|
2013-10-10 12:34:54 +00:00
|
|
|
|
this.root.getSurface().getSurface().getContext().update();
|
2013-09-25 20:54:59 +00:00
|
|
|
|
|
2013-10-14 11:02:08 +00:00
|
|
|
|
this.emit( 'resizeEnd' );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
};
|
2013-07-16 14:06:47 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generate an object of attributes changes from the new width and height.
|
|
|
|
|
*
|
|
|
|
|
* @param {number} width New image width
|
|
|
|
|
* @param {number} height New image height
|
|
|
|
|
* @returns {Object} Attribute changes
|
|
|
|
|
*/
|
|
|
|
|
ve.ce.ResizableNode.prototype.getAttributeChanges = function ( width, height ) {
|
|
|
|
|
var attrChanges = {};
|
|
|
|
|
if ( this.model.getAttribute( 'width' ) !== width ) {
|
|
|
|
|
attrChanges.width = width;
|
|
|
|
|
}
|
|
|
|
|
if ( this.model.getAttribute( 'height' ) !== height ) {
|
|
|
|
|
attrChanges.height = height;
|
|
|
|
|
}
|
|
|
|
|
return attrChanges;
|
2013-09-25 16:20:53 +00:00
|
|
|
|
};
|