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

97 lines
2.7 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-09-02 18:32:23 +02:00
const ChildrenSorterStrategy = require('./ChildrenSorterStrategy').default;
2021-07-16 16:41:58 +02:00
/**
* @class
* @extends mindplot.layout.ChildrenSorterStrategy
*/
2021-09-02 18:32:23 +02:00
const AbstractBasicSorter = new Class(
2021-10-05 02:05:34 +02:00
/** @lends AbstractBasicSorter */ {
Extends: ChildrenSorterStrategy,
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
/**
2021-09-02 18:32:23 +02:00
* @param {} treeSet
* @param {} node
* @return the height of a node and its children if existing and not shrunken
*/
2021-10-05 02:05:34 +02:00
computeChildrenIdByHeights(treeSet, node) {
const result = {};
this._computeChildrenHeight(treeSet, node, result);
return result;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
_getVerticalPadding() {
return AbstractBasicSorter.INTERNODE_VERTICAL_PADDING;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
_computeChildrenHeight(treeSet, node, heightCache) {
const height = node.getSize().height + this._getVerticalPadding() * 2; // 2* Top and down padding;
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
let result;
const children = treeSet.getChildren(node);
if (children.length == 0 || node.areChildrenShrunken()) {
result = height;
} else {
let childrenHeight = 0;
_.each(
children,
function (child) {
childrenHeight += this._computeChildrenHeight(treeSet, child, heightCache);
},
this,
);
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
result = Math.max(height, childrenHeight);
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
if (heightCache) {
heightCache[node.getId()] = result;
}
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
return result;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
_getSortedChildren(treeSet, node) {
const result = treeSet.getChildren(node);
result.sort((a, b) => a.getOrder() - b.getOrder());
return result;
},
2021-07-16 16:41:58 +02:00
2021-10-05 02:05:34 +02:00
_getRelativeDirection(reference, position) {
const offset = position.x - reference.x;
return offset >= 0 ? 1 : -1;
},
},
2021-09-02 18:32:23 +02:00
);
2021-07-16 16:41:58 +02:00
/**
* @constant
* @type {Number}
* @default
*/
AbstractBasicSorter.INTERNODE_VERTICAL_PADDING = 5;
/**
* @constant
* @type {Number}
* @default
*/
AbstractBasicSorter.INTERNODE_HORIZONTAL_PADDING = 30;
export default AbstractBasicSorter;