Improve drag. This should solve the drag bug ...

This commit is contained in:
Paulo Veiga 2012-01-21 10:26:58 -03:00
parent 9748711011
commit d8e05d7ce0
3 changed files with 32 additions and 29 deletions

View File

@ -20,7 +20,7 @@ mindplot.DragManager = new Class({
initialize:function(workspace) { initialize:function(workspace) {
this._workspace = workspace; this._workspace = workspace;
this._listeners = {}; this._listeners = {};
this._isDragInProcess = false;
}, },
add : function(node) { add : function(node) {
@ -36,8 +36,6 @@ mindplot.DragManager = new Class({
// Set initial position. // Set initial position.
var dragNode = node.createDragNode(); var dragNode = node.createDragNode();
var mousePos = screen.getWorkspaceMousePosition(event);
// dragNode.setPosition(mousePos.x, mousePos.y);
// Register mouse move listener ... // Register mouse move listener ...
var mouseMoveListener = dragManager._buildMouseMoveListener(workspace, dragNode, dragManager); var mouseMoveListener = dragManager._buildMouseMoveListener(workspace, dragNode, dragManager);
@ -47,14 +45,10 @@ mindplot.DragManager = new Class({
var mouseUpListener = dragManager._buildMouseUpListener(workspace, node, dragNode, dragManager); var mouseUpListener = dragManager._buildMouseUpListener(workspace, node, dragNode, dragManager);
screen.addEvent('mouseup', mouseUpListener); screen.addEvent('mouseup', mouseUpListener);
// Execute Listeners ..
var startDragListener = dragManager._listeners['startdragging'];
startDragListener(event, node);
// Change cursor. // Change cursor.
window.document.body.style.cursor = 'move'; window.document.body.style.cursor = 'move';
} }
}; }.bind(this);
node.addEvent('mousedown', mouseDownListener); node.addEvent('mousedown', mouseDownListener);
}, },
@ -72,12 +66,18 @@ mindplot.DragManager = new Class({
_buildMouseMoveListener : function(workspace, dragNode, dragManager) { _buildMouseMoveListener : function(workspace, dragNode, dragManager) {
var screen = workspace.getScreenManager(); var screen = workspace.getScreenManager();
var result = function(event) { var result = function(event) {
if (!dragNode._isInTheWorkspace) { if (!this._isDragInProcess) {
// Execute Listeners ..
var startDragListener = dragManager._listeners['startdragging'];
startDragListener(event, dragNode);
// Add shadow node to the workspace. // Add shadow node to the workspace.
workspace.appendChild(dragNode); workspace.appendChild(dragNode);
dragNode._isInTheWorkspace = true;
this._isDragInProcess = true;
} }
var pos = screen.getWorkspaceMousePosition(event); var pos = screen.getWorkspaceMousePosition(event);
@ -99,15 +99,8 @@ mindplot.DragManager = new Class({
_buildMouseUpListener : function(workspace, node, dragNode, dragManager) { _buildMouseUpListener : function(workspace, node, dragNode, dragManager) {
var screen = workspace.getScreenManager(); var screen = workspace.getScreenManager();
var result = function(event) { var result = function(event) {
$assert(dragNode.isDragTopic, 'dragNode must be an DragTopic'); $assert(dragNode.isDragTopic, 'dragNode must be an DragTopic');
// Remove drag node from the workspace.
var hasBeenDragged = dragNode._isInTheWorkspace;
if (dragNode._isInTheWorkspace) {
dragNode.removeFromWorkspace(workspace);
}
// Remove all the events. // Remove all the events.
screen.removeEvent('mousemove', dragManager._mouseMoveListener); screen.removeEvent('mousemove', dragManager._mouseMoveListener);
screen.removeEvent('mouseup', dragManager._mouseUpListener); screen.removeEvent('mouseup', dragManager._mouseUpListener);
@ -116,20 +109,24 @@ mindplot.DragManager = new Class({
dragManager._mouseMoveListener = null; dragManager._mouseMoveListener = null;
dragManager._mouseUpListener = null; dragManager._mouseUpListener = null;
workspace.enableWorkspaceEvents(true);
// Change the cursor to the default.
window.document.body.style.cursor = 'default';
if (this._isDragInProcess) {
// Execute Listeners only if the node has been moved. // Execute Listeners only if the node has been moved.
var endDragListener = dragManager._listeners['enddragging']; var endDragListener = dragManager._listeners['enddragging'];
endDragListener(event, dragNode); endDragListener(event, dragNode);
if (hasBeenDragged) { // Remove drag node from the workspace.
dragNode._isInTheWorkspace = false; dragNode.removeFromWorkspace(workspace);
this._isDragInProcess = false;
} }
// Change the cursor to the default.
window.document.body.style.cursor = 'default';
workspace.enableWorkspaceEvents(true); }.bind(this);
};
dragManager._mouseUpListener = result; dragManager._mouseUpListener = result;
return result; return result;
}, },

View File

@ -25,6 +25,7 @@ mindplot.DragTopic = new Class({
this._order = null; this._order = null;
this._draggedNode = draggedNode; this._draggedNode = draggedNode;
this._position = new core.Point(); this._position = new core.Point();
this._isInWorkspace = false;
this._isFreeLayoutEnabled = false; this._isFreeLayoutEnabled = false;
}, },
@ -132,17 +133,23 @@ mindplot.DragTopic = new Class({
removeFromWorkspace : function(workspace) { removeFromWorkspace : function(workspace) {
// Remove drag shadow. // Remove drag shadow.
workspace.removeChild(this._elem2d); workspace.removeChild(this._elem2d);
this._isInWorkspace = false;
// Remove pivot shape. To improve performace it will not be removed. Only the visibility will be changed. // Remove pivot shape. To improve performace it will not be removed. Only the visibility will be changed.
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
dragPivot.setVisibility(false); dragPivot.setVisibility(false);
},
isInWorkspace: function(){
return this._isInWorkspace;
}, },
addToWorkspace : function(workspace) { addToWorkspace : function(workspace) {
workspace.appendChild(this._elem2d); workspace.appendChild(this._elem2d);
var dragPivot = this._getDragPivot(); var dragPivot = this._getDragPivot();
dragPivot.addToWorkspace(workspace); dragPivot.addToWorkspace(workspace);
this._isInWorkspace = true;
}, },
_getDragPivot : function() { _getDragPivot : function() {

View File

@ -23,7 +23,6 @@ web2d.Element = new Class({
throw "Element peer can not be null"; throw "Element peer can not be null";
} }
this._dispatcherByEventType = new Hash({});
if ($defined(attributes)) { if ($defined(attributes)) {
this._initialize(attributes); this._initialize(attributes);
} }