Commit 17d20b9c authored by techird's avatar techird

dragview on root

parent e02893e6
Subproject commit df7b361ae5098710eca0fec1080dce4bc822bf24 Subproject commit a9630e638b29633f62cdc2df239bc702118b231a
...@@ -161,7 +161,7 @@ var DragBox = kity.createClass( "DragBox", { ...@@ -161,7 +161,7 @@ var DragBox = kity.createClass( "DragBox", {
_drawForDragMode: function () { _drawForDragMode: function () {
this._text.setContent( this._dragSources.length + ' items' ); this._text.setContent( this._dragSources.length + ' items' );
this._text.setPosition( this._startPosition.x, this._startPosition.y + 5 ); this._text.setPosition( this._startPosition.x, this._startPosition.y + 5 );
this._minder.getRenderContainer().addShape( this ); this._minder.getPaper().addShape( this );
}, },
_shrink: function () { _shrink: function () {
// 合并所有拖放源图形的矩形即可 // 合并所有拖放源图形的矩形即可
......
kity.Draggable = ( function () { var ViewDragger = kity.createClass( "ViewDragger", {
var Paper = kity.Paper; constructor: function ( minder ) {
this._minder = minder;
var touchable = window.ontouchstart !== undefined; this._enabled = false;
var DRAG_START_EVENT = touchable ? 'touchstart' : 'mousedown', this._offset = {
DRAG_MOVE_EVENT = touchable ? 'touchmove' : 'mousemove', x: 0,
DRAG_END_EVENT = touchable ? 'touchend' : 'mouseup'; y: 0
};
return kity.createClass( { this._bind();
drag: function ( opt ) { },
isEnabled: function () {
if ( this.dragEnabled ) { return this._enabled;
return; },
setEnabled: function ( value ) {
var paper = this._minder.getPaper();
paper.setStyle( 'cursor', value ? 'pointer' : 'default' );
paper.setStyle( 'cursor', value ? '-webkit-grab' : 'default' );
this._enabled = value;
},
_bind: function () {
var dragger = this,
isRootDrag = false,
startPosition = null,
lastPosition = null;
this._minder.on( 'beforemousedown', function ( e ) {
if ( dragger.isEnabled() ) {
startPosition = e.getPosition();
e.stopPropagation();
} else if ( !this.getRoot().isSelected() && e.getTargetNode() == this.getRoot() ) {
startPosition = e.getPosition();
dragger.setEnabled( true );
isRootDrag = true;
} }
var dragStart = opt && opt.start || this.dragStart, } ).on( 'beforemousemove', function ( e ) {
dragMove = opt && opt.move || this.dragMove, if ( startPosition ) {
dragEnd = opt && opt.end || this.dragEnd, lastPosition = e.getPosition();
dragTarget = opt && opt.target || this.dragTarget || this, var offset = kity.Vector.fromPoints( startPosition, lastPosition );
me = this; offset = kity.Vector.add( dragger._offset, offset );
this.getRenderContainer().setTransform( new kity.Matrix().translate( offset.x, offset.y ) );
this.dragEnabled = true; e.stopPropagation();
this.dragTarget = dragTarget;
function bindEvents( paper ) {
var startPosition, lastPosition, dragging = false;
var dragFn = function ( e ) {
if ( !dragging ) {
paper.off( DRAG_MOVE_EVENT, dragFn );
}
if ( e.originEvent.touches && e.originEvent.touches.length !== 1 ) return;
var currentPosition = e.getPosition();
var movement = {
x: currentPosition.x - startPosition.x,
y: currentPosition.y - startPosition.y
};
var delta = {
x: currentPosition.x - lastPosition.x,
y: currentPosition.y - lastPosition.y
};
var dragInfo = {
position: currentPosition,
movement: movement,
delta: delta
};
lastPosition = currentPosition;
if ( dragMove ) {
dragMove.call( me, dragInfo );
} else if ( me instanceof Paper ) {
// treate paper drag different
var view = me.getViewPort();
view.center.x -= movement.x;
view.center.y -= movement.y;
me.setViewPort( view );
} else {
me.translate( delta.x, delta.y );
}
dragTarget.trigger( 'dragmove', dragInfo );
e.stopPropagation();
e.preventDefault();
};
dragTarget.on( DRAG_START_EVENT, dragTarget._dragStartHandler = function ( e ) {
if ( e.originEvent.button ) {
return;
}
dragging = true;
var dragInfo = {
position: lastPosition = startPosition = e.getPosition()
};
if ( dragStart ) {
var cancel = dragStart.call( me, dragInfo ) === false;
if ( cancel ) {
return;
}
}
paper.on( DRAG_MOVE_EVENT, dragFn );
dragTarget.trigger( 'dragstart', dragInfo );
e.stopPropagation();
e.preventDefault();
} );
paper.on( DRAG_END_EVENT, dragTarget._dragEndHandler = function ( e ) {
if ( dragging ) {
dragging = false;
if ( dragEnd ) {
dragEnd.call( me );
}
paper.off( DRAG_MOVE_EVENT, dragFn );
dragTarget.trigger( 'dragend' );
if ( e ) {
e.stopPropagation();
e.preventDefault();
}
}
} );
} }
} ).on( 'mouseup', function ( e ) {
if ( me instanceof Paper ) { if ( startPosition && lastPosition ) {
bindEvents( me ); dragger._offset.x += lastPosition.x - startPosition.x;
} else if ( me.getPaper() ) { dragger._offset.y += lastPosition.y - startPosition.y;
bindEvents( me.getPaper() );
} else {
var listener = function ( e ) {
if ( e.targetShape.getPaper() ) {
bindEvents( e.targetShape.getPaper() );
me.off( 'add', listener );
me.off( 'treeadd', listener );
}
};
me.on( 'add treeadd', listener );
} }
return this; startPosition = null;
}, // end of drag if ( isRootDrag ) {
dragger.setEnabled( false );
isRootDrag = false;
undrag: function () { }
var target = this.dragTarget; } );
target.off( DRAG_START_EVENT, target._dragStartHandler ); }
target._dragEndHandler(); } );
target.getPaper().off( DRAG_END_EVENT, target._dragEndHandler );
delete target._dragStartHandler;
delete target._dragEndHandler;
this.dragEnabled = false;
return this;
}
} );
} )();
KityMinder.registerModule( 'Hand', function () { KityMinder.registerModule( 'Hand', function () {
var ToggleHandCommand = kity.createClass( "ToggleHandCommand", { var ToggleHandCommand = kity.createClass( "ToggleHandCommand", {
base: Command, base: Command,
execute: function ( minder ) { execute: function ( minder ) {
var drag = minder._onDragMode = !minder._onDragMode; minder._viewDragger.setEnabled( !minder._viewDragger.isEnabled() );
minder.getPaper().setStyle( 'cursor', drag ? 'pointer' : 'default' );
minder.getPaper().setStyle( 'cursor', drag ? '-webkit-grab' : 'default' );
if ( drag ) {
minder.getPaper().drag();
} else {
minder.getPaper().undrag();
}
}, },
queryState: function ( minder ) { queryState: function ( minder ) {
return minder._onDragMode ? 1 : 0; return minder._viewDragger.isEnabled() ? 1 : 0;
} }
} ); } );
return { return {
init: function () { init: function () {
this._onDragMode = false; this._viewDragger = new ViewDragger( this );
kity.extendClass( kity.Paper, kity.Draggable );
}, },
commands: { commands: {
'hand': ToggleHandCommand 'hand': ToggleHandCommand
...@@ -172,11 +81,6 @@ KityMinder.registerModule( 'Hand', function () { ...@@ -172,11 +81,6 @@ KityMinder.registerModule( 'Hand', function () {
this.execCommand( 'hand' ); this.execCommand( 'hand' );
e.preventDefault(); e.preventDefault();
} }
},
beforemousemove: function ( e ) {
if ( this._onDragMode ) {
e.stopPropagation();
}
} }
} }
}; };
......
...@@ -29,8 +29,9 @@ KityMinder.registerModule( "LayoutModule", function () { ...@@ -29,8 +29,9 @@ KityMinder.registerModule( "LayoutModule", function () {
}, },
initStyle: function () { initStyle: function () {
var curStyle = this.getCurrentStyle(); var curStyle = this.getCurrentStyle();
var lastTransform = this._rc.getTransform();
this._rc.remove(); this._rc.remove();
this._rc = new kity.Group(); this._rc = new kity.Group().setTransform( lastTransform );
this._paper.addShape( this._rc ); this._paper.addShape( this._rc );
var _root = this.getRoot(); var _root = this.getRoot();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment