2021-12-19 19:29:56 +01:00
|
|
|
/* eslint-disable no-nested-ternary */
|
|
|
|
/* eslint-disable max-len */
|
2021-07-16 16:41:58 +02:00
|
|
|
/*
|
2021-12-25 23:39:34 +01:00
|
|
|
* Copyright [2021] [wisemapping]
|
2021-07-16 16:41:58 +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';
|
2021-12-02 01:41:56 +01:00
|
|
|
import AbstractBasicSorter from './AbstractBasicSorter';
|
2021-07-16 16:41:58 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
class BalancedSorter extends AbstractBasicSorter {
|
|
|
|
predict(graph, parent, node, position, free) {
|
|
|
|
// If its a free node...
|
|
|
|
if (free) {
|
|
|
|
$assert(
|
|
|
|
$defined(position),
|
|
|
|
'position cannot be null for predict in free positioning',
|
|
|
|
);
|
|
|
|
$assert($defined(node), 'node cannot be null for predict in free positioning');
|
|
|
|
|
2021-12-14 18:06:09 +01:00
|
|
|
const rootNode = graph.getRootNode(parent);
|
|
|
|
const direction = this._getRelativeDirection(
|
2021-12-13 22:30:37 +01:00
|
|
|
rootNode.getPosition(),
|
|
|
|
node.getPosition(),
|
|
|
|
);
|
|
|
|
|
|
|
|
const limitXPos = parent.getPosition().x
|
2021-12-19 19:29:56 +01:00
|
|
|
+ direction
|
|
|
|
* (parent.getSize().width / 2
|
|
|
|
+ node.getSize().width / 2
|
|
|
|
+ BalancedSorter.INTERNODE_HORIZONTAL_PADDING);
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-20 21:54:31 +01:00
|
|
|
let xPos;
|
|
|
|
if (direction > 0) {
|
|
|
|
xPos = position.x >= limitXPos
|
2021-12-13 22:30:37 +01:00
|
|
|
? position.x
|
2021-12-20 21:54:31 +01:00
|
|
|
: limitXPos;
|
|
|
|
} else {
|
|
|
|
xPos = position.x <= limitXPos
|
2021-12-13 22:30:37 +01:00
|
|
|
? position.x
|
|
|
|
: limitXPos;
|
2021-12-20 21:54:31 +01:00
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
return [0, { x: xPos, y: position.y }];
|
|
|
|
}
|
|
|
|
|
2021-12-14 18:06:09 +01:00
|
|
|
const rootNode = graph.getRootNode(parent);
|
2021-12-13 22:30:37 +01:00
|
|
|
|
|
|
|
// If it is a dragged node...
|
|
|
|
if (node) {
|
|
|
|
$assert($defined(position), 'position cannot be null for predict in dragging');
|
|
|
|
const nodeDirection = this._getRelativeDirection(
|
|
|
|
rootNode.getPosition(),
|
|
|
|
node.getPosition(),
|
|
|
|
);
|
|
|
|
const positionDirection = this._getRelativeDirection(
|
|
|
|
rootNode.getPosition(),
|
|
|
|
position,
|
|
|
|
);
|
|
|
|
const siblings = graph.getSiblings(node);
|
|
|
|
|
2021-12-19 19:29:56 +01:00
|
|
|
const sameParent = parent === graph.getParent(node);
|
|
|
|
if (siblings.length === 0 && nodeDirection === positionDirection && sameParent) {
|
2021-12-13 22:30:37 +01:00
|
|
|
return [node.getOrder(), node.getPosition()];
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
2021-12-14 18:06:09 +01:00
|
|
|
let right; let left;
|
2021-12-13 22:30:37 +01:00
|
|
|
if (!position) {
|
2021-12-14 18:06:09 +01:00
|
|
|
right = this._getChildrenForOrder(parent, graph, 0);
|
|
|
|
left = this._getChildrenForOrder(parent, graph, 1);
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
// Filter nodes on one side..
|
2021-12-20 21:54:31 +01:00
|
|
|
let order;
|
|
|
|
if (position) {
|
|
|
|
order = position.x > rootNode.getPosition().x
|
2021-12-13 22:30:37 +01:00
|
|
|
? 0
|
2021-12-20 21:54:31 +01:00
|
|
|
: 1;
|
|
|
|
} else {
|
|
|
|
order = right.length - left.length > 0
|
2021-12-13 22:30:37 +01:00
|
|
|
? 1
|
|
|
|
: 0;
|
2021-12-20 21:54:31 +01:00
|
|
|
}
|
2021-12-19 19:29:56 +01:00
|
|
|
const direction = order % 2 === 0 ? 1 : -1;
|
2021-12-13 22:30:37 +01:00
|
|
|
|
|
|
|
// Exclude the dragged node (if set)
|
2021-12-20 21:54:31 +01:00
|
|
|
const children = this._getChildrenForOrder(parent, graph, order)
|
|
|
|
.filter((child) => child !== node);
|
2021-12-13 22:30:37 +01:00
|
|
|
|
|
|
|
// No children?
|
2021-12-19 19:29:56 +01:00
|
|
|
if (children.length === 0) {
|
2021-12-13 22:30:37 +01:00
|
|
|
return [
|
|
|
|
order,
|
|
|
|
{
|
|
|
|
x:
|
2021-12-19 19:29:56 +01:00
|
|
|
parent.getPosition().x
|
|
|
|
+ direction
|
|
|
|
* (parent.getSize().width / 2
|
|
|
|
+ BalancedSorter.INTERNODE_HORIZONTAL_PADDING * 2),
|
2021-12-13 22:30:37 +01:00
|
|
|
y: parent.getPosition().y,
|
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fit within ...
|
|
|
|
let result = null;
|
2021-12-14 18:06:09 +01:00
|
|
|
const last = children[children.length - 1];
|
2021-12-20 21:54:31 +01:00
|
|
|
const newestPosition = position || { x: last.getPosition().x, y: last.getPosition().y + 1 };
|
2021-12-13 22:30:37 +01:00
|
|
|
children.forEach((child, index) => {
|
|
|
|
const cpos = child.getPosition();
|
2021-12-20 21:54:31 +01:00
|
|
|
if (newestPosition.y > cpos.y) {
|
2021-12-19 19:29:56 +01:00
|
|
|
const yOffset = child === last
|
2021-12-13 22:30:37 +01:00
|
|
|
? child.getSize().height + BalancedSorter.INTERNODE_VERTICAL_PADDING * 2
|
|
|
|
: (children[index + 1].getPosition().y - child.getPosition().y) / 2;
|
|
|
|
result = [child.getOrder() + 2, { x: cpos.x, y: cpos.y + yOffset }];
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
// Position wasn't below any node, so it must be inserted above
|
|
|
|
if (!result) {
|
|
|
|
const first = children[0];
|
|
|
|
result = [
|
|
|
|
position.x > 0 ? 0 : 1,
|
|
|
|
{
|
|
|
|
x: first.getPosition().x,
|
|
|
|
y:
|
2021-12-19 19:29:56 +01:00
|
|
|
first.getPosition().y
|
|
|
|
- first.getSize().height
|
|
|
|
- BalancedSorter.INTERNODE_VERTICAL_PADDING * 2,
|
2021-12-13 22:30:37 +01:00
|
|
|
},
|
|
|
|
];
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
return result;
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @param {} treeSet
|
|
|
|
* @param {} parent
|
|
|
|
* @param {} child
|
|
|
|
* @param {} order
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
insert(treeSet, parent, child, order) {
|
|
|
|
const children = this._getChildrenForOrder(parent, treeSet, order);
|
|
|
|
|
|
|
|
// If no children, return 0 or 1 depending on the side
|
2021-12-19 19:29:56 +01:00
|
|
|
if (children.length === 0) {
|
2021-12-13 22:30:37 +01:00
|
|
|
child.setOrder(order % 2);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shift all the elements by two, so side is the same.
|
|
|
|
// In case of balanced sorter, order don't need to be continuous...
|
|
|
|
let max = 0;
|
|
|
|
for (let i = 0; i < children.length; i++) {
|
|
|
|
const node = children[i];
|
|
|
|
max = Math.max(max, node.getOrder());
|
|
|
|
if (node.getOrder() >= order) {
|
|
|
|
max = Math.max(max, node.getOrder() + 2);
|
|
|
|
node.setOrder(node.getOrder() + 2);
|
2021-10-05 02:05:34 +02:00
|
|
|
}
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
const newOrder = order > max + 1 ? max + 2 : order;
|
|
|
|
child.setOrder(newOrder);
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @param {} treeSet
|
|
|
|
* @param {} node
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
detach(treeSet, node) {
|
|
|
|
const parent = treeSet.getParent(node);
|
|
|
|
// Filter nodes on one side..
|
|
|
|
const children = this._getChildrenForOrder(parent, treeSet, node.getOrder());
|
|
|
|
|
|
|
|
children.forEach((child) => {
|
|
|
|
if (child.getOrder() > node.getOrder()) {
|
|
|
|
child.setOrder(child.getOrder() - 2);
|
|
|
|
}
|
|
|
|
});
|
2021-12-19 19:29:56 +01:00
|
|
|
node.setOrder(node.getOrder() % 2 === 0 ? 0 : 1);
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @param {} treeSet
|
|
|
|
* @param {} node
|
|
|
|
* @return offsets
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
computeOffsets(treeSet, node) {
|
|
|
|
$assert(treeSet, 'treeSet can no be null.');
|
|
|
|
$assert(node, 'node can no be null.');
|
|
|
|
|
|
|
|
const children = this._getSortedChildren(treeSet, node);
|
|
|
|
|
|
|
|
// Compute heights ...
|
|
|
|
const heights = children
|
2021-12-19 19:29:56 +01:00
|
|
|
.map((child) => ({
|
|
|
|
id: child.getId(),
|
|
|
|
order: child.getOrder(),
|
|
|
|
width: child.getSize().width,
|
|
|
|
height: this._computeChildrenHeight(treeSet, child),
|
2021-12-20 21:54:31 +01:00
|
|
|
}))
|
|
|
|
.reverse();
|
2021-12-13 22:30:37 +01:00
|
|
|
|
|
|
|
// Compute the center of the branch ...
|
|
|
|
let totalPHeight = 0;
|
|
|
|
let totalNHeight = 0;
|
|
|
|
|
|
|
|
heights.forEach((elem) => {
|
|
|
|
if (elem.order % 2 === 0) {
|
|
|
|
totalPHeight += elem.height;
|
|
|
|
} else {
|
|
|
|
totalNHeight += elem.height;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let psum = totalPHeight / 2;
|
|
|
|
let nsum = totalNHeight / 2;
|
|
|
|
let ysum = 0;
|
|
|
|
|
|
|
|
// Calculate the offsets ...
|
|
|
|
const result = {};
|
|
|
|
for (let i = 0; i < heights.length; i++) {
|
|
|
|
const direction = heights[i].order % 2 ? -1 : 1;
|
|
|
|
|
|
|
|
if (direction > 0) {
|
|
|
|
psum -= heights[i].height;
|
|
|
|
ysum = psum;
|
|
|
|
} else {
|
|
|
|
nsum -= heights[i].height;
|
|
|
|
ysum = nsum;
|
|
|
|
}
|
|
|
|
|
|
|
|
const yOffset = ysum + heights[i].height / 2;
|
|
|
|
const xOffset = direction
|
2021-12-19 19:29:56 +01:00
|
|
|
* (node.getSize().width / 2
|
|
|
|
+ heights[i].width / 2
|
|
|
|
+ +BalancedSorter.INTERNODE_HORIZONTAL_PADDING);
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-19 19:29:56 +01:00
|
|
|
$assert(!Number.isNaN(xOffset), 'xOffset can not be null');
|
|
|
|
$assert(!Number.isNaN(yOffset), 'yOffset can not be null');
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
result[heights[i].id] = { x: xOffset, y: yOffset };
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2021-10-05 02:05:34 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @param {} treeSet
|
|
|
|
* @param {} node
|
|
|
|
* @throw will throw an error if order elements are missing
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
verify(treeSet, node) {
|
|
|
|
// Check that all is consistent ...
|
|
|
|
const children = this._getChildrenForOrder(node, treeSet, node.getOrder());
|
|
|
|
|
|
|
|
// All odd ordered nodes should be "continuous" by themselves
|
|
|
|
// All even numbered nodes should be "continuous" by themselves
|
2021-12-19 19:29:56 +01:00
|
|
|
const factor = node.getOrder() % 2 === 0 ? 2 : 1;
|
2021-12-13 22:30:37 +01:00
|
|
|
for (let i = 0; i < children.length; i++) {
|
2021-12-19 19:29:56 +01:00
|
|
|
const order = i === 0 && factor === 1 ? 1 : factor * i;
|
2021-12-13 22:30:37 +01:00
|
|
|
$assert(
|
2021-12-19 19:29:56 +01:00
|
|
|
children[i].getOrder() === order,
|
2021-12-13 22:30:37 +01:00
|
|
|
`Missing order elements. Missing order: ${i * factor
|
|
|
|
}. Parent:${node.getId()
|
|
|
|
},Node:${children[i].getId()}`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @param {} treeSet
|
|
|
|
* @param {} child
|
|
|
|
* @return the direction of the child within the treeSet
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
getChildDirection(treeSet, child) {
|
2021-12-19 19:29:56 +01:00
|
|
|
return child.getOrder() % 2 === 0 ? 1 : -1;
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
2021-09-02 18:32:23 +02:00
|
|
|
|
2021-12-13 22:30:37 +01:00
|
|
|
/**
|
2021-09-02 18:32:23 +02:00
|
|
|
* @return {String} the print name of this class
|
|
|
|
*/
|
2021-12-13 22:30:37 +01:00
|
|
|
toString() {
|
|
|
|
return 'Balanced Sorter';
|
|
|
|
}
|
|
|
|
|
|
|
|
_getChildrenForOrder(parent, graph, order) {
|
2021-12-20 21:54:31 +01:00
|
|
|
return this._getSortedChildren(graph, parent)
|
|
|
|
.filter((child) => child.getOrder() % 2 === order % 2);
|
2021-12-13 22:30:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
_getVerticalPadding() {
|
|
|
|
return BalancedSorter.INTERNODE_VERTICAL_PADDING;
|
|
|
|
}
|
|
|
|
}
|
2021-07-16 16:41:58 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {Number}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
BalancedSorter.INTERNODE_VERTICAL_PADDING = 5;
|
|
|
|
/**
|
|
|
|
* @constant
|
|
|
|
* @type {Number}
|
|
|
|
* @default
|
|
|
|
*/
|
|
|
|
BalancedSorter.INTERNODE_HORIZONTAL_PADDING = 30;
|
|
|
|
|
|
|
|
export default BalancedSorter;
|