Migrate AbstractBasicSorter to TS

This commit is contained in:
Paulo Gustavo Veiga 2022-06-23 21:42:08 -07:00
parent 95da8db7a1
commit c469d82599
3 changed files with 29 additions and 106 deletions

View File

@ -16,24 +16,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import PositionType from '../PositionType';
import ChildrenSorterStrategy from './ChildrenSorterStrategy';
import Node from './Node';
import RootedTreeSet from './RootedTreeSet';
/**
* @class
* @extends mindplot.layout.ChildrenSorterStrategy
*/
class AbstractBasicSorter extends ChildrenSorterStrategy {
computeChildrenIdByHeights(treeSet, node) {
abstract class AbstractBasicSorter extends ChildrenSorterStrategy {
private INTERNODE_VERTICAL_PADDING = 5;
computeChildrenIdByHeights(treeSet: RootedTreeSet, node: Node) {
const result = {};
this._computeChildrenHeight(treeSet, node, result);
return result;
}
_getVerticalPadding() {
return AbstractBasicSorter.INTERNODE_VERTICAL_PADDING;
protected _getVerticalPadding() {
return this.INTERNODE_VERTICAL_PADDING;
}
_computeChildrenHeight(treeSet, node, heightCache) {
protected _computeChildrenHeight(treeSet: RootedTreeSet, node: Node, heightCache?) {
// 2* Top and down padding;
const height = node.getSize().height + this._getVerticalPadding() * 2;
@ -59,29 +60,16 @@ class AbstractBasicSorter extends ChildrenSorterStrategy {
return result;
}
_getSortedChildren(treeSet, node) {
protected _getSortedChildren(treeSet: RootedTreeSet, node: Node) {
const result = treeSet.getChildren(node);
result.sort((a, b) => a.getOrder() - b.getOrder());
return result;
}
_getRelativeDirection(reference, position) {
protected _getRelativeDirection(reference: PositionType, position: PositionType): 1 | -1 {
const offset = position.x - reference.x;
return offset >= 0 ? 1 : -1;
}
}
/**
* @constant
* @type {Number}
* @default
*/
AbstractBasicSorter.INTERNODE_VERTICAL_PADDING = 5;
/**
* @constant
* @type {Number}
* @default
*/
AbstractBasicSorter.INTERNODE_HORIZONTAL_PADDING = 30;
export default AbstractBasicSorter;

View File

@ -18,42 +18,18 @@
* limitations under the License.
*/
import { $assert, $defined } from '@wisemapping/core-js';
import PositionType from '../PositionType';
import AbstractBasicSorter from './AbstractBasicSorter';
import Node from './Node';
import RootedTreeSet from './RootedTreeSet';
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');
const rootNode = graph.getRootNode(parent);
const direction = this._getRelativeDirection(
rootNode.getPosition(),
node.getPosition(),
);
private static INTERNODE_VERTICAL_PADDING = 5;
const limitXPos = parent.getPosition().x
+ direction
* (parent.getSize().width / 2
+ node.getSize().width / 2
+ BalancedSorter.INTERNODE_HORIZONTAL_PADDING);
private static INTERNODE_HORIZONTAL_PADDING = 30;
let xPos;
if (direction > 0) {
xPos = position.x >= limitXPos
? position.x
: limitXPos;
} else {
xPos = position.x <= limitXPos
? position.x
: limitXPos;
}
return [0, { x: xPos, y: position.y }];
}
predict(graph, parent, node: Node, position: PositionType) {
const rootNode = graph.getRootNode(parent);
@ -145,13 +121,7 @@ class BalancedSorter extends AbstractBasicSorter {
return result;
}
/**
* @param {} treeSet
* @param {} parent
* @param {} child
* @param {} order
*/
insert(treeSet, parent, child, order) {
insert(treeSet: RootedTreeSet, parent: Node, child: Node, order: number) {
const children = this._getChildrenForOrder(parent, treeSet, order);
// If no children, return 0 or 1 depending on the side
@ -176,11 +146,7 @@ class BalancedSorter extends AbstractBasicSorter {
child.setOrder(newOrder);
}
/**
* @param {} treeSet
* @param {} node
*/
detach(treeSet, node) {
detach(treeSet: RootedTreeSet, node: Node): void {
const parent = treeSet.getParent(node);
// Filter nodes on one side..
const children = this._getChildrenForOrder(parent, treeSet, node.getOrder());
@ -193,12 +159,7 @@ class BalancedSorter extends AbstractBasicSorter {
node.setOrder(node.getOrder() % 2 === 0 ? 0 : 1);
}
/**
* @param {} treeSet
* @param {} node
* @return offsets
*/
computeOffsets(treeSet, node) {
computeOffsets(treeSet: RootedTreeSet, node: Node) {
$assert(treeSet, 'treeSet can no be null.');
$assert(node, 'node can no be null.');
@ -256,12 +217,7 @@ class BalancedSorter extends AbstractBasicSorter {
return result;
}
/**
* @param {} treeSet
* @param {} node
* @throw will throw an error if order elements are missing
*/
verify(treeSet, node) {
verify(treeSet: RootedTreeSet, node: Node): void {
// Check that all is consistent ...
const children = this._getChildrenForOrder(node, treeSet, node.getOrder());
@ -279,43 +235,22 @@ class BalancedSorter extends AbstractBasicSorter {
}
}
/**
* @param {} treeSet
* @param {} child
* @return the direction of the child within the treeSet
*/
getChildDirection(treeSet, child) {
getChildDirection(treeSet: RootedTreeSet, child: Node): 1 | -1 {
return child.getOrder() % 2 === 0 ? 1 : -1;
}
/**
* @return {String} the print name of this class
*/
toString() {
toString(): string {
return 'Balanced Sorter';
}
_getChildrenForOrder(parent, graph, order) {
protected _getChildrenForOrder(parent: Node, graph: RootedTreeSet, order: number) {
return this._getSortedChildren(graph, parent)
.filter((child) => child.getOrder() % 2 === order % 2);
}
_getVerticalPadding() {
protected _getVerticalPadding(): number {
return BalancedSorter.INTERNODE_VERTICAL_PADDING;
}
}
/**
* @constant
* @type {Number}
* @default
*/
BalancedSorter.INTERNODE_VERTICAL_PADDING = 5;
/**
* @constant
* @type {Number}
* @default
*/
BalancedSorter.INTERNODE_HORIZONTAL_PADDING = 30;
export default BalancedSorter;

View File

@ -24,15 +24,15 @@ abstract class ChildrenSorterStrategy {
abstract computeOffsets(treeSet: RootedTreeSet, node: Node);
abstract insert(treeSet: RootedTreeSet, parent: Node, child: Node, order: number);
abstract insert(treeSet: RootedTreeSet, parent: Node, child: Node, order: number): void;
abstract detach(treeSet: RootedTreeSet, node: Node);
abstract detach(treeSet: RootedTreeSet, node: Node): void;
abstract predict(treeSet: RootedTreeSet, parent, node: Node, position: PositionType);
abstract verify(treeSet: RootedTreeSet, node: Node);
abstract getChildDirection(treeSet: RootedTreeSet, node: Node);
abstract getChildDirection(treeSet: RootedTreeSet, node: Node): 1 | -1;
abstract toString(): string;
}