mirror of
https://bitbucket.org/wisemapping/wisemapping-frontend.git
synced 2024-11-15 19:47:57 +01:00
147 lines
4.6 KiB
JavaScript
147 lines
4.6 KiB
JavaScript
/*
|
|
* Copyright [2021] [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.
|
|
*/
|
|
import { $assert, $defined } from '@wisemapping/core-js';
|
|
import DragTopic from './DragTopic';
|
|
|
|
class DragManager {
|
|
constructor(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 mouseDownListener(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) {
|
|
throw new Error('Not implemented: DragManager.prototype.remove');
|
|
}
|
|
|
|
_buildMouseMoveListener(workspace, dragNode, dragManager) {
|
|
const screen = workspace.getScreenManager();
|
|
const me = this;
|
|
const result = (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();
|
|
};
|
|
|
|
// allowed param reassign to avoid risks of existing code relying in this side-effect
|
|
dragManager._mouseMoveListener = result;
|
|
return result;
|
|
}
|
|
|
|
_buildMouseUpListener(workspace, node, dragNode, dragManager) {
|
|
const screen = workspace.getScreenManager();
|
|
const me = this;
|
|
const result = (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
|
|
// allowed param reassign to avoid risks of existing code relying in this side-effect
|
|
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;
|