2010-11-20 23:43:54 +01:00
|
|
|
/*
|
2011-01-24 00:34:05 +01:00
|
|
|
* Copyright [2011] [wisemapping]
|
|
|
|
*
|
2011-01-24 01:03:12 +01:00
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
2011-01-24 00:34:05 +01:00
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2010-11-20 23:43:54 +01:00
|
|
|
*/
|
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
mindplot.DragManager = function(workspace)
|
|
|
|
{
|
|
|
|
this._workspace = workspace;
|
|
|
|
this._listeners = {};
|
2011-08-13 17:33:37 +02:00
|
|
|
|
2009-06-07 20:59:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
mindplot.DragManager.prototype.add = function(node)
|
|
|
|
{
|
|
|
|
// Add behaviour ...
|
|
|
|
var workspace = this._workspace;
|
|
|
|
var screen = workspace.getScreenManager();
|
|
|
|
var dragManager = this;
|
|
|
|
|
|
|
|
var mouseDownListener = function(event)
|
|
|
|
{
|
|
|
|
if (workspace.isWorkspaceEventsEnabled())
|
|
|
|
{
|
|
|
|
// Disable double drag...
|
|
|
|
workspace.enableWorkspaceEvents(false);
|
|
|
|
|
|
|
|
// Set initial position.
|
|
|
|
var dragNode = node.createDragNode();
|
|
|
|
var mousePos = screen.getWorkspaceMousePosition(event);
|
|
|
|
dragNode.setPosition(mousePos.x, mousePos.y);
|
|
|
|
|
|
|
|
// Register mouse move listener ...
|
|
|
|
var mouseMoveListener = dragManager._buildMouseMoveListener(workspace, dragNode, dragManager);
|
2011-08-13 17:33:37 +02:00
|
|
|
screen.addEvent('mousemove', mouseMoveListener);
|
2009-06-07 20:59:43 +02:00
|
|
|
|
|
|
|
// Register mouse up listeners ...
|
|
|
|
var mouseUpListener = dragManager._buildMouseUpListener(workspace, node, dragNode, dragManager);
|
2011-08-13 17:33:37 +02:00
|
|
|
screen.addEvent('mouseup', mouseUpListener);
|
2009-06-07 20:59:43 +02:00
|
|
|
|
|
|
|
// Execute Listeners ..
|
|
|
|
var startDragListener = dragManager._listeners['startdragging'];
|
|
|
|
startDragListener(event, node);
|
|
|
|
|
|
|
|
// Change cursor.
|
|
|
|
window.document.body.style.cursor = 'move';
|
|
|
|
}
|
|
|
|
};
|
2011-08-21 17:42:00 +02:00
|
|
|
node.addEvent('mousedown', mouseDownListener);
|
2009-06-07 20:59:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
mindplot.DragManager.prototype.remove = function(node)
|
|
|
|
{
|
|
|
|
var nodes = this._topics;
|
|
|
|
var contained = false;
|
|
|
|
var index = -1;
|
|
|
|
for (var i = 0; i < nodes.length; i++) {
|
|
|
|
if (nodes[i] == node) {
|
|
|
|
contained = true;
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (contained)
|
|
|
|
{
|
|
|
|
elem = new Array();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
mindplot.DragManager.prototype._buildMouseMoveListener = function(workspace, dragNode, dragManager)
|
|
|
|
{
|
|
|
|
var screen = workspace.getScreenManager();
|
|
|
|
var result = function(event) {
|
2010-11-20 23:43:54 +01:00
|
|
|
|
|
|
|
if (!dragNode._isInTheWorkspace)
|
2009-06-07 20:59:43 +02:00
|
|
|
{
|
2010-11-20 23:43:54 +01:00
|
|
|
// Add shadow node to the workspace.
|
|
|
|
workspace.appendChild(dragNode);
|
|
|
|
dragNode._isInTheWorkspace = true;
|
2009-06-07 20:59:43 +02:00
|
|
|
}
|
2010-11-20 23:43:54 +01:00
|
|
|
|
|
|
|
var pos = screen.getWorkspaceMousePosition(event);
|
|
|
|
dragNode.setPosition(pos.x, pos.y);
|
|
|
|
|
|
|
|
// Call mouse move listeners ...
|
|
|
|
var dragListener = dragManager._listeners['dragging'];
|
2011-07-27 18:44:09 +02:00
|
|
|
if ($defined(dragListener))
|
2010-11-20 23:43:54 +01:00
|
|
|
{
|
|
|
|
dragListener(event, dragNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
event.preventDefault();
|
2011-08-13 17:33:37 +02:00
|
|
|
|
|
|
|
}.bind(this);
|
2009-06-07 20:59:43 +02:00
|
|
|
dragManager._mouseMoveListener = result;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
mindplot.DragManager.prototype._buildMouseUpListener = function(workspace, node, dragNode, dragManager)
|
|
|
|
{
|
|
|
|
var screen = workspace.getScreenManager();
|
|
|
|
var result = function(event) {
|
|
|
|
|
2011-07-27 19:33:02 +02:00
|
|
|
$assert(dragNode.isDragTopic, 'dragNode must be an DragTopic');
|
2009-06-07 20:59:43 +02:00
|
|
|
|
|
|
|
// Remove drag node from the workspace.
|
|
|
|
var hasBeenDragged = dragNode._isInTheWorkspace;
|
|
|
|
if (dragNode._isInTheWorkspace)
|
|
|
|
{
|
|
|
|
dragNode.removeFromWorkspace(workspace);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove all the events.
|
2011-08-13 17:33:37 +02:00
|
|
|
screen.removeEvent('mousemove', dragManager._mouseMoveListener);
|
|
|
|
screen.removeEvent('mouseup', dragManager._mouseUpListener);
|
2009-06-07 20:59:43 +02:00
|
|
|
|
|
|
|
// Help GC
|
|
|
|
dragManager._mouseMoveListener = null;
|
|
|
|
dragManager._mouseUpListener = null;
|
|
|
|
|
|
|
|
// Execute Listeners only if the node has been moved.
|
|
|
|
var endDragListener = dragManager._listeners['enddragging'];
|
|
|
|
endDragListener(event, dragNode);
|
|
|
|
|
|
|
|
if (hasBeenDragged)
|
|
|
|
{
|
|
|
|
dragNode._isInTheWorkspace = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Change the cursor to the default.
|
|
|
|
window.document.body.style.cursor = 'default';
|
|
|
|
|
|
|
|
workspace.enableWorkspaceEvents(true);
|
|
|
|
|
|
|
|
};
|
|
|
|
dragManager._mouseUpListener = result;
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* type:
|
|
|
|
* - startdragging.
|
|
|
|
* - dragging
|
|
|
|
* - enddragging
|
|
|
|
*/
|
2011-08-21 17:42:00 +02:00
|
|
|
mindplot.DragManager.prototype. addEvent = function(type, listener)
|
2009-06-07 20:59:43 +02:00
|
|
|
{
|
|
|
|
this._listeners[type] = listener;
|
2011-08-13 17:33:37 +02:00
|
|
|
};
|