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

View File

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

View File

@ -24,15 +24,15 @@ abstract class ChildrenSorterStrategy {
abstract computeOffsets(treeSet: RootedTreeSet, node: Node); 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 predict(treeSet: RootedTreeSet, parent, node: Node, position: PositionType);
abstract verify(treeSet: RootedTreeSet, node: Node); abstract verify(treeSet: RootedTreeSet, node: Node);
abstract getChildDirection(treeSet: RootedTreeSet, node: Node); abstract getChildDirection(treeSet: RootedTreeSet, node: Node): 1 | -1;
abstract toString(): string; abstract toString(): string;
} }