wisemapping-frontend/packages/mindplot/src/components/DragTopic.js

230 lines
6.3 KiB
JavaScript
Raw Normal View History

2021-07-16 16:41:58 +02:00
/*
* 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.
*/
2021-12-04 01:11:17 +01:00
import { $assert, $defined } from '@wisemapping/core-js';
2021-12-05 00:39:20 +01:00
import * as web2d from '@wisemapping/web2d';
2021-10-05 02:05:34 +02:00
import ActionDispatcher from './ActionDispatcher';
import DragPivot from './DragPivot';
2021-07-16 16:41:58 +02:00
2021-12-05 00:39:20 +01:00
class DragTopic {
constructor(dragShape, draggedNode, layoutManger) {
2021-10-05 02:05:34 +02:00
$assert(dragShape, 'Rect can not be null.');
$assert(draggedNode, 'draggedNode can not be null.');
$assert(layoutManger, 'layoutManger can not be null.');
this._elem2d = dragShape;
this._order = null;
this._draggedNode = draggedNode;
this._layoutManager = layoutManger;
this._position = new web2d.Point();
2021-10-05 02:05:34 +02:00
this._isInWorkspace = false;
this._isFreeLayoutEnabled = false;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setOrder(order) {
this._order = order;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setPosition(x, y) {
// Update drag shadow position ....
let position = { x, y };
if (this.isFreeLayoutOn() && this.isConnected()) {
const { _layoutManager } = this;
const par = this.getConnectedToTopic();
position = _layoutManager.predict(
par.getId(),
this._draggedNode.getId(),
position,
true,
).position;
2021-10-05 02:05:34 +02:00
}
this._position.setValue(position.x, position.y);
// Elements are positioned in the center.
// All topic element must be positioned based on the innerShape.
const draggedNode = this._draggedNode;
const size = draggedNode.getSize();
const cx = position.x - (position.x > 0 ? 0 : size.width);
const cy = Math.ceil(position.y - size.height / 2);
2021-10-05 02:05:34 +02:00
this._elem2d.setPosition(cx, cy);
// In case is not free, pivot must be draw ...
if (this.isConnected() && !this.isFreeLayoutOn()) {
const parent = this.getConnectedToTopic();
const predict = this._layoutManager.predict(
parent.getId(),
this._draggedNode.getId(),
this.getPosition(),
);
2021-10-05 02:05:34 +02:00
if (this._order != predict.order) {
const dragPivot = this._getDragPivot();
const pivotPosition = predict.position;
dragPivot.connectTo(parent, pivotPosition);
2021-07-16 16:41:58 +02:00
this.setOrder(predict.order);
2021-10-05 02:05:34 +02:00
}
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
updateFreeLayout(event) {
const isFreeEnabled = (event.metaKey && Browser.Platform.mac) || (event.ctrlKey && !Browser.Platform.mac);
if (this.isFreeLayoutOn() != isFreeEnabled) {
const dragPivot = this._getDragPivot();
dragPivot.setVisibility(!isFreeEnabled);
this._isFreeLayoutEnabled = isFreeEnabled;
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
setVisibility(value) {
const dragPivot = this._getDragPivot();
dragPivot.setVisibility(value);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isVisible() {
const dragPivot = this._getDragPivot();
return dragPivot.isVisible();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getInnerShape() {
return this._elem2d;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
disconnect(workspace) {
// Clear connection line ...
const dragPivot = this._getDragPivot();
dragPivot.disconnect(workspace);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
connectTo(parent) {
$assert(parent, 'Parent connection node can not be null.');
// Where it should be connected ?
const predict = designer._eventBussDispatcher._layoutManager.predict(
parent.getId(),
this._draggedNode.getId(),
this.getPosition(),
);
2021-10-05 02:05:34 +02:00
// Connect pivot ...
const dragPivot = this._getDragPivot();
const { position } = predict;
dragPivot.connectTo(parent, position);
dragPivot.setVisibility(true);
this.setOrder(predict.order);
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getDraggedTopic() {
return this._draggedNode;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
removeFromWorkspace(workspace) {
if (this._isInWorkspace) {
// Remove drag shadow.
workspace.removeChild(this._elem2d);
// Remove pivot shape. To improve performance it will not be removed. Only the visibility will be changed.
const dragPivot = this._getDragPivot();
dragPivot.setVisibility(false);
this._isInWorkspace = false;
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isInWorkspace() {
return this._isInWorkspace;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
addToWorkspace(workspace) {
if (!this._isInWorkspace) {
workspace.append(this._elem2d);
const dragPivot = this._getDragPivot();
dragPivot.addToWorkspace(workspace);
this._isInWorkspace = true;
2021-07-16 16:41:58 +02:00
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
_getDragPivot() {
return DragTopic.__getDragPivot();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getPosition() {
return this._position;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isDragTopic() {
return true;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
applyChanges(workspace) {
$assert(workspace, 'workspace can not be null');
const actionDispatcher = ActionDispatcher.getInstance();
const draggedTopic = this.getDraggedTopic();
const topicId = draggedTopic.getId();
const position = this.getPosition();
if (!this.isFreeLayoutOn()) {
let order = null;
let parent = null;
const isDragConnected = this.isConnected();
if (isDragConnected) {
const targetTopic = this.getConnectedToTopic();
order = this._order;
parent = targetTopic;
}
// If the node is not connected, position based on the original drag topic position.
actionDispatcher.dragTopic(topicId, position, order, parent);
} else {
actionDispatcher.moveTopic(topicId, position);
}
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
getConnectedToTopic() {
const dragPivot = this._getDragPivot();
return dragPivot.getTargetTopic();
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isConnected() {
return this.getConnectedToTopic() != null;
2021-12-05 00:39:20 +01:00
}
2021-10-05 02:05:34 +02:00
isFreeLayoutOn() {
// return this._isFreeLayoutEnabled;
// Disable free layout ...
return false;
2021-12-05 00:39:20 +01:00
}
}
2021-07-16 16:41:58 +02:00
DragTopic.init = function (workspace) {
2021-10-05 02:05:34 +02:00
$assert(workspace, 'workspace can not be null');
const pivot = DragTopic.__getDragPivot();
workspace.append(pivot);
2021-07-16 16:41:58 +02:00
};
DragTopic.__getDragPivot = function () {
2021-10-05 02:05:34 +02:00
let result = DragTopic._dragPivot;
if (!$defined(result)) {
result = new DragPivot();
DragTopic._dragPivot = result;
}
return result;
2021-07-16 16:41:58 +02:00
};
export default DragTopic;