UNPKG

1.67 kBJavaScriptView Raw
1const TreeLayout = require('./layout/base');
2const indentedTree = require('./layout/indented');
3const separateTree = require('./layout/separate-root');
4const util = require('./util');
5
6
7const VALID_DIRECTIONS = [
8 'LR', // left to right
9 'RL', // right to left
10 'H' // horizontal
11];
12const DEFAULT_DIRECTION = VALID_DIRECTIONS[0];
13
14class IndentedLayout extends TreeLayout {
15 execute() {
16 const me = this;
17 const options = me.options;
18 const root = me.rootNode;
19 options.isHorizontal = true;
20 // default indent 20 and sink first children;
21 const { indent = 20, dropCap = true, direction = DEFAULT_DIRECTION, align } = options;
22 if (direction && VALID_DIRECTIONS.indexOf(direction) === -1) {
23 throw new TypeError(`Invalid direction: ${direction}`);
24 }
25 if (direction === VALID_DIRECTIONS[0]) { // LR
26 indentedTree(root, indent, dropCap, align);
27 } else if (direction === VALID_DIRECTIONS[1]) { // RL
28 indentedTree(root, indent, dropCap, align);
29 root.right2left();
30 } else if (direction === VALID_DIRECTIONS[2]) { // H
31 // separate into left and right trees
32 const { left, right } = separateTree(root, options);
33 indentedTree(left, indent, dropCap, align);
34 left.right2left();
35 indentedTree(right, indent, dropCap, align);
36 const bbox = left.getBoundingBox();
37 right.translate(bbox.width, 0);
38 root.x = right.x - root.width / 2;
39 }
40 return root;
41 }
42}
43
44const DEFAULT_OPTIONS = {
45};
46
47function indentedLayout(root, options) {
48 options = util.assign({}, DEFAULT_OPTIONS, options);
49 return new IndentedLayout(root, options).execute();
50}
51
52module.exports = indentedLayout;