mediawiki-extensions-Visual.../modules/ve/ce/ve.ce.RelocatableNode.js
Christian Williams 5d44800b53 Correct Rendering for Block Images After Resize Undo
If a block image is resized and undone, there are lots of problems.

* The block image gets the wrong size. When rolling back the
transaction, height and width are passed as strings.
MWBlockImage tries to set the width to: width + 2. A 300px image
became 3002px.

* Resize handles didn't reposition and resize

* Relocatable drag marker didn't reposition and resize

* Focusable highlights didn't reposition and resize.

Change-Id: I8792b567b77da90ce5a2ad132e306705c5706606
2013-07-02 12:08:42 -07:00

134 lines
3.1 KiB
JavaScript

/*!
* VisualEditor ContentEditable RelocatableNode class.
*
* @copyright 2011-2013 VisualEditor Team and others; see AUTHORS.txt
* @license The MIT License (MIT); see LICENSE.txt
*/
/**
* ContentEditable relocatable node.
*
* Requires that the node also is Focusable
*
* @class
* @abstract
*
* @constructor
*/
ve.ce.RelocatableNode = function VeCeRelocatableNode() {
// Properties
this.relocatingSurface = null;
this.$relocatableMarker = this.$$( '<img>' );
// Events
this.connect( this, {
'focus': 'onRelocatableFocus',
'blur': 'onRelocatableBlur',
'resize': 'onRelocatableResize',
'live': 'onRelocatableLive'
} );
// Initialization
this.$relocatableMarker
.addClass( 've-ce-relocatableNode-marker' )
.attr( 'src', 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==' )
.on( {
'dragstart': ve.bind( this.onRelocatableDragStart, this ),
'dragend': ve.bind( this.onRelocatableDragEnd, this )
} );
};
/* Static Properties */
/* Methods */
/**
* Handle node live.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableLive = function () {
var surfaceModel = this.root.getSurface().getModel();
if ( this.live ) {
surfaceModel.connect( this, { 'history': 'setRelocatableMarkerSizeAndPosition' } );
} else {
surfaceModel.disconnect( this, { 'history': 'setRelocatableMarkerSizeAndPosition' } );
}
};
/**
* Handle node focus.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableFocus = function () {
this.setRelocatableMarkerSizeAndPosition();
this.$relocatableMarker.appendTo( this.root.getSurface().getSurface().$localOverlayControls );
};
/**
* Handle node blur.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableBlur = function () {
this.$relocatableMarker.detach();
};
/**
* Handle node resize.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableResize = function () {
this.setRelocatableMarkerSizeAndPosition();
};
/**
* Handle element drag start.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableDragStart = function () {
// Store a copy of the surface, when dragend occurs the node will be detached
this.relocatingSurface = this.root.getSurface();
if ( this.relocatingSurface ) {
// Allow dragging this node in the surface
this.relocatingSurface.startRelocation( this );
}
this.$relocatableMarker.addClass( 'relocating' );
setTimeout( ve.bind( function () {
this.$relocatableMarker.css( { 'top': -10000, 'left': -10000 } );
}, this ), 0 );
};
/**
* Handle element drag end.
*
* @method
*/
ve.ce.RelocatableNode.prototype.onRelocatableDragEnd = function () {
if ( this.relocatingSurface ) {
this.relocatingSurface.endRelocation();
this.relocatingSurface = null;
}
this.$relocatableMarker.removeClass( 'relocating' );
};
/**
* Set the correct size and position of the relocatable marker.
*
* @method
*/
ve.ce.RelocatableNode.prototype.setRelocatableMarkerSizeAndPosition = function () {
this.$relocatableMarker.css( {
'height': this.$.height(),
'width': this.$.width(),
'top': this.$.offset().top,
'left': this.$.offset().left
} );
};