wisemapping-frontend/packages/mindplot/lib/components/layout/GridSorter.js

92 lines
2.5 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-10-05 02:05:34 +02:00
const AbstractBasicSorter = require('./AbstractBasicSorter').default;
2021-07-16 16:41:58 +02:00
/**
* @class
* @extends mindplot.layout.AbstractBasicSorter
*/
const GridSorter = new Class(/** @lends GridSorter */{
2021-10-05 02:05:34 +02:00
Extends: AbstractBasicSorter,
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @param {} treeSet
* @param {} node
* @return offsets
*/
2021-10-05 02:05:34 +02:00
computeOffsets(treeSet, node) {
$assert(treeSet, 'treeSet can no be null.');
$assert(node, 'node can no be null.');
$assert('order can no be null.');
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const children = this._getSortedChildren(treeSet, node);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Compute heights ...
const me = this;
const heights = children.map((child) => ({
id: child.getId(),
height: me._computeChildrenHeight(treeSet, child),
}));
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
// Calculate the offsets ...
const result = {};
for (let i = 0; i < heights.length; i++) {
const even = i % 2 == 0 ? 1 : -1;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const zeroHeight = i == 0 ? 0 : heights[0].height / 2 * even;
let middleHeight = 0;
for (let j = i - 2; j > 0; j -= 2) {
middleHeight += heights[j].height * even;
}
const finalHeight = i == 0 ? 0 : heights[i].height / 2 * even;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
const yOffset = zeroHeight + middleHeight + finalHeight;
const xOffset = node.getSize().width + GridSorter.GRID_HORIZONTAR_SIZE;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
$assert(!isNaN(xOffset), 'xOffset can not be null');
$assert(!isNaN(yOffset), 'yOffset can not be null');
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
result[heights[i].id] = { x: xOffset, y: yOffset };
}
return result;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-07-16 16:41:58 +02:00
* @return {String} the print name of this class
*/
2021-10-05 02:05:34 +02:00
toString() {
return 'Grid Sorter';
},
2021-07-16 16:41:58 +02:00
});
/**
* @constant
* @type {Number}
* @default
*/
GridSorter.GRID_HORIZONTAR_SIZE = 20;
/**
* @constant
* @type {Number}
* @default
*/
GridSorter.INTER_NODE_VERTICAL_DISTANCE = 50;
export default GridSorter;