wisemapping-frontend/packages/mindplot/lib/components/DragManager.js
2021-10-04 17:05:34 -07:00

147 lines
4.5 KiB
JavaScript

/*
* Copyright [2015] [wisemapping]
*
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
* It is basically the Apache License, Version 2.0 (the "License") plus the
* "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.
*/
const DragTopic = require('./DragTopic').default;
const DragManager = new Class({
initialize(workspace, eventDispatcher) {
this._workspace = workspace;
this._designerModel = workspace;
this._listeners = {};
this._isDragInProcess = false;
this._eventDispatcher = eventDispatcher;
DragTopic.init(this._workspace);
},
add(node) {
// Add behaviour ...
const workspace = this._workspace;
const screen = workspace.getScreenManager();
const dragManager = this;
const me = this;
const mouseDownListener = function (event) {
if (workspace.isWorkspaceEventsEnabled()) {
// Disable double drag...
workspace.enableWorkspaceEvents(false);
// Set initial position.
const layoutManager = me._eventDispatcher.getLayoutManager();
const dragNode = node.createDragNode(layoutManager);
// Register mouse move listener ...
const mouseMoveListener = dragManager._buildMouseMoveListener(workspace, dragNode, dragManager);
screen.addEvent('mousemove', mouseMoveListener);
// Register mouse up listeners ...
const mouseUpListener = dragManager._buildMouseUpListener(workspace, node, dragNode, dragManager);
screen.addEvent('mouseup', mouseUpListener);
// Change cursor.
window.document.body.style.cursor = 'move';
}
};
node.addEvent('mousedown', mouseDownListener);
},
remove(node) {
const nodes = this._topics;
let contained = false;
let index = -1;
for (let i = 0; i < nodes.length; i++) {
if (nodes[i] == node) {
contained = true;
index = i;
}
}
},
_buildMouseMoveListener(workspace, dragNode, dragManager) {
const screen = workspace.getScreenManager();
const me = this;
const result = function (event) {
if (!me._isDragInProcess) {
// Execute Listeners ..
const startDragListener = dragManager._listeners.startdragging;
startDragListener(event, dragNode);
// Add shadow node to the workspace.
workspace.append(dragNode);
me._isDragInProcess = true;
}
const pos = screen.getWorkspaceMousePosition(event);
dragNode.setPosition(pos.x, pos.y);
// Call mouse move listeners ...
const dragListener = dragManager._listeners.dragging;
if ($defined(dragListener)) {
dragListener(event, dragNode);
}
event.preventDefault();
};
dragManager._mouseMoveListener = result;
return result;
},
_buildMouseUpListener(workspace, node, dragNode, dragManager) {
const screen = workspace.getScreenManager();
const me = this;
const result = function (event) {
$assert(dragNode.isDragTopic, 'dragNode must be an DragTopic');
// Remove all the events.
screen.removeEvent('mousemove', dragManager._mouseMoveListener);
screen.removeEvent('mouseup', dragManager._mouseUpListener);
// Help GC
dragManager._mouseMoveListener = null;
dragManager._mouseUpListener = null;
workspace.enableWorkspaceEvents(true);
// Change the cursor to the default.
window.document.body.style.cursor = 'default';
if (me._isDragInProcess) {
// Execute Listeners only if the node has been moved.
const endDragListener = dragManager._listeners.enddragging;
endDragListener(event, dragNode);
// Remove drag node from the workspace.
dragNode.removeFromWorkspace(workspace);
me._isDragInProcess = false;
}
};
dragManager._mouseUpListener = result;
return result;
},
/**
* type:
* - startdragging.
* - dragging
* - enddragging
*/
addEvent(type, listener) {
this._listeners[type] = listener;
},
});
export default DragManager;