wisemapping-frontend/packages/mindplot/src/components/DragManager.ts

163 lines
5.0 KiB
TypeScript
Raw Normal View History

2021-10-05 02:05:34 +02:00
/*
2021-12-25 23:39:34 +01:00
* Copyright [2021] [wisemapping]
2021-10-05 02:05:34 +02:00
*
* 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.
*/
2021-12-04 01:11:17 +01:00
import { $assert, $defined } from '@wisemapping/core-js';
import DragTopic from './DragTopic';
2022-02-10 04:44:55 +01:00
import EventBusDispatcher from './layout/EventBusDispatcher';
import Workspace from './Workspace';
2021-10-05 02:05:34 +02:00
2021-12-05 00:39:20 +01:00
class DragManager {
2022-02-10 04:44:55 +01:00
private _workspace: Workspace;
private _designerModel: Workspace;
private _isDragInProcess: boolean;
private _eventDispatcher: EventBusDispatcher;
private _listeners;
private _mouseMoveListener;
private _mouseUpListener;
constructor(workspace: Workspace, eventDispatcher: EventBusDispatcher) {
2021-10-05 02:05:34 +02:00
this._workspace = workspace;
this._designerModel = workspace;
this._listeners = {};
this._isDragInProcess = false;
this._eventDispatcher = eventDispatcher;
DragTopic.init(this._workspace);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
add(node) {
// Add behaviour ...
const workspace = this._workspace;
const screen = workspace.getScreenManager();
const dragManager = this;
const me = this;
2022-02-10 04:44:55 +01:00
const mouseDownListener = function mouseDownListener() {
2021-10-05 02:05:34 +02:00
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,
);
2021-10-05 02:05:34 +02:00
screen.addEvent('mousemove', mouseMoveListener);
// Register mouse up listeners ...
const mouseUpListener = dragManager._buildMouseUpListener(
workspace, node, dragNode, dragManager,
);
2021-10-05 02:05:34 +02:00
screen.addEvent('mouseup', mouseUpListener);
// Change cursor.
window.document.body.style.cursor = 'move';
}
};
node.addEvent('mousedown', mouseDownListener);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-10 04:44:55 +01:00
remove() {
throw new Error('Not implemented: DragManager.prototype.remove');
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-10 04:44:55 +01:00
protected _buildMouseMoveListener(workspace: Workspace, dragNode, dragManager: DragManager) {
2021-10-05 02:05:34 +02:00
const screen = workspace.getScreenManager();
const me = this;
2021-12-23 20:58:50 +01:00
const result = (event) => {
2021-10-05 02:05:34 +02:00
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();
};
2022-01-25 07:09:30 +01:00
// allowed param reassign to avoid risks of existing code relying in this side-effect
2021-10-05 02:05:34 +02:00
dragManager._mouseMoveListener = result;
return result;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
2022-02-10 04:44:55 +01:00
protected _buildMouseUpListener(workspace: Workspace, node, dragNode, dragManager: DragManager) {
2021-10-05 02:05:34 +02:00
const screen = workspace.getScreenManager();
const me = this;
2021-12-23 20:58:50 +01:00
const result = (event) => {
2021-10-05 02:05:34 +02:00
$assert(dragNode.isDragTopic, 'dragNode must be an DragTopic');
// Remove all the events.
screen.removeEvent('mousemove', dragManager._mouseMoveListener);
screen.removeEvent('mouseup', dragManager._mouseUpListener);
// Help GC
// allowed param reassign to avoid risks of existing code relying in this side-effect
2021-10-05 02:05:34 +02:00
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;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
/**
* type:
* - startdragging.
* - dragging
* - enddragging
*/
addEvent(type, listener) {
this._listeners[type] = listener;
2021-12-05 00:39:20 +01:00
}
}
2021-10-05 02:05:34 +02:00
export default DragManager;