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
|
|
|
|
* @param {jQuery} [$resizable=this.$] Resizable DOM element
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode = function VeCeResizableNode( $resizable ) {
|
|
|
|
// Properties
|
|
|
|
this.$resizable = $resizable || this.$;
|
|
|
|
this.ratio = this.model.getAttribute( 'width' ) / this.model.getAttribute( 'height' );
|
|
|
|
this.resizing = false;
|
2013-05-28 16:36:31 +00:00
|
|
|
this.$resizeHandles = this.$$( '<div>' );
|
|
|
|
this.onResizeHandlesCornerMouseDownHandler =
|
|
|
|
ve.bind( this.onResizeHandlesCornerMouseDown, this );
|
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',
|
|
|
|
'resize': '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-05-28 16:36:31 +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-06-07 00:51:00 +00:00
|
|
|
/* Static Properties */
|
|
|
|
|
|
|
|
ve.ce.ResizableNode.static = {};
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle node focus.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableFocus = function () {
|
2013-04-19 19:56:55 +00:00
|
|
|
var offset = this.$resizable.offset();
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
this.$resizeHandles
|
|
|
|
.css( {
|
|
|
|
'width': 0,
|
|
|
|
'height': 0,
|
|
|
|
'top': offset.top,
|
|
|
|
'left': offset.left
|
|
|
|
} )
|
2013-06-27 18:24:26 +00:00
|
|
|
.appendTo( this.root.getSurface().getSurface().$localOverlayControls );
|
2013-04-18 01:07:59 +00:00
|
|
|
|
|
|
|
this.$resizeHandles
|
2013-06-24 17:51:59 +00:00
|
|
|
.find( '.ve-ce-resizableNode-neHandle' )
|
2013-05-28 16:36:31 +00:00
|
|
|
.css( { 'margin-right': -this.$resizable.width() } )
|
|
|
|
.end()
|
2013-06-24 17:51:59 +00:00
|
|
|
.find( '.ve-ce-resizableNode-swHandle' )
|
2013-05-28 16:36:31 +00:00
|
|
|
.css( { 'margin-bottom': -this.$resizable.height() } )
|
|
|
|
.end()
|
2013-06-24 17:51:59 +00:00
|
|
|
.find( '.ve-ce-resizableNode-seHandle' )
|
2013-05-28 16:36:31 +00:00
|
|
|
.css( {
|
|
|
|
'margin-right': -this.$resizable.width(),
|
|
|
|
'margin-bottom': -this.$resizable.height()
|
|
|
|
} )
|
|
|
|
.end()
|
|
|
|
.children()
|
|
|
|
.on( 'mousedown', this.onResizeHandlesCornerMouseDownHandler );
|
2013-04-18 01:07:59 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle node blur.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableBlur = function () {
|
2013-05-28 16:36:31 +00:00
|
|
|
this.$resizeHandles
|
|
|
|
.detach()
|
|
|
|
.children()
|
|
|
|
.off( 'mousedown', this.onResizeHandlesCornerMouseDownHandler );
|
2013-04-18 01:07:59 +00:00
|
|
|
};
|
|
|
|
|
2013-05-22 10:52:53 +00:00
|
|
|
/**
|
|
|
|
* Handle live event.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {boolean} live
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode.prototype.onResizableLive = function ( live ) {
|
|
|
|
if ( !live ) {
|
|
|
|
this.$resizeHandles.remove();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-04-18 01:07:59 +00:00
|
|
|
/**
|
|
|
|
* Handle bounding box handle mousedown.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Click event
|
|
|
|
*/
|
|
|
|
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-06-18 05:04:56 +00:00
|
|
|
.addClass( 've-ui-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;
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
$( 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 )
|
|
|
|
} );
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle body mousemove.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
* @param {jQuery.Event} e Click event
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode.prototype.onDocumentMouseMove = function ( e ) {
|
|
|
|
var newWidth, newHeight, newRatio,
|
|
|
|
// 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-04-19 19:56:55 +00:00
|
|
|
newWidth = Math.max( Math.min( this.$resizable.width() + diff.x, max ), min );
|
|
|
|
newHeight = Math.max( Math.min( this.$resizable.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-04-19 19:56:55 +00:00
|
|
|
dimensions.height = this.$resizable.height() +
|
|
|
|
( newWidth - this.$resizable.width() ) / this.ratio;
|
2013-04-18 01:07:59 +00:00
|
|
|
} else {
|
2013-04-19 19:56:55 +00:00
|
|
|
dimensions.width = this.$resizable.width() +
|
|
|
|
( newHeight - this.$resizable.height() ) * this.ratio;
|
2013-04-18 01:07:59 +00:00
|
|
|
dimensions.height = newHeight;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle body mouseup.
|
|
|
|
*
|
|
|
|
* @method
|
|
|
|
*/
|
|
|
|
ve.ce.ResizableNode.prototype.onDocumentMouseUp = function () {
|
2013-06-30 04:47:58 +00:00
|
|
|
var 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-06-14 19:07:55 +00:00
|
|
|
selection = surfaceModel.getSelection(),
|
|
|
|
attrChanges = {};
|
2013-04-18 01:07:59 +00:00
|
|
|
|
2013-06-18 05:04:56 +00:00
|
|
|
this.$resizeHandles.removeClass( 've-ui-resizableNode-handles-resizing' );
|
The Great ve.ui.Surface refactor of 2013
Prologue:
Farewell ve.Editor my good chap… Oh, hey there HTML frames - I didn't
see you there! In a world where iframes are outlaws, and symbols like
document and window are global, there were more than a few assumptions
about which document or window was being used. But fear not - for this
commit (probably) tracks them all down, leaving a trail of
iframe-compatible awesomeness in its wake. With the great ve.ui.Surface
now able to be used inside of iframes, let the reference editing
commence. But there, lurking in the darkness is a DM issue so fierce it
may take Roan and/or Ed up to 3 whole hours to sort it out.
Note to Roan and/or Ed:
Editing references seems to work fine, but when saving the page there
are "no changes" which is a reasonable indication to the contrary.
Objectives:
* Make it possible to have multiple surfaces be instantiated, get along
nicely, and be embedded inside of iframes if needed.
* Make reference content editable within a dialog
Approach:
* Move what's left of ve.Editor to ve.ui.Surface and essentially
obliterate all use of it
* Make even more stuff inherit from ve.Element (long live this.$$)
* Use the correct document or window anywhere it was being assumed to be
the top level one
* Resolve stacking order issues by removing the excessive use of z-index
and introducing global and local overlay elements for each editor
* Add a surface to the reference dialog, load up the reference contents
and save them back on apply
* Actually destroy what we create in ce and ui surfaces
* Add recursive frame offset calculation method to ve.Element
* Moved ve.ce.Surface's getSelectionRect method to the prototype
Bonus:
* Move ve.ce.DocumentNode.css contents to ve.ce.Node.css (not sure why it
was separate in the first place, but I'm likely the one to blame)
* Fix blatant lies in documentation
* Whitespace cleanup here and there
* Get rid of ve.ui.Window overlays - not used or needed
Change-Id: Iede83e7d24f7cb249b6ba3dc45d770445b862e08
2013-05-20 22:45:50 +00:00
|
|
|
$( this.getElementDocument() ).off( '.ve-ce-resizableNode' );
|
2013-04-18 01:07:59 +00:00
|
|
|
this.resizing = false;
|
2013-06-07 00:51:00 +00:00
|
|
|
this.$resizable.css( { 'width': width, 'height': height } );
|
2013-04-19 19:56:55 +00:00
|
|
|
|
2013-06-30 04:47:58 +00:00
|
|
|
// Apply changes to the model
|
|
|
|
if ( this.model.getAttribute( 'width' ) !== width ) {
|
|
|
|
attrChanges.width = width;
|
|
|
|
}
|
|
|
|
if ( this.model.getAttribute( 'height' ) !== height ) {
|
|
|
|
attrChanges.height = height;
|
|
|
|
}
|
|
|
|
if ( !ve.isEmptyObject( attrChanges ) ) {
|
|
|
|
surfaceModel.change(
|
|
|
|
ve.dm.Transaction.newFromAttributeChanges( documentModel, offset, attrChanges ),
|
|
|
|
selection
|
|
|
|
);
|
|
|
|
}
|
2013-06-05 20:59:19 +00:00
|
|
|
|
2013-06-30 04:47:58 +00:00
|
|
|
this.emit( 'resize' );
|
2013-04-18 01:07:59 +00:00
|
|
|
};
|