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 } = options;
22 const direction = options.direction || DEFAULT_DIRECTION;
23 if (direction && VALID_DIRECTIONS.indexOf(direction) === -1) {
24 throw new TypeError(`Invalid direction: ${direction}`);
25 }
26 if (direction === VALID_DIRECTIONS[0]) { // LR
27 indentedTree(root, indent, dropCap);
28 } else if (direction === VALID_DIRECTIONS[1]) { // RL
29 indentedTree(root, indent, dropCap);
30 root.right2left();
31 } else if (direction === VALID_DIRECTIONS[2]) { // H
32 // separate into left and right trees
33 const { left, right } = separateTree(root, options);
34 indentedTree(left, indent, dropCap);
35 left.right2left();
36 indentedTree(right, indent, dropCap);
37 const bbox = left.getBoundingBox();
38 right.translate(bbox.width, 0);
39 root.x = right.x - root.width / 2;
40 }
41 return root;
42 }
43}
44
45const DEFAULT_OPTIONS = {
46};
47
48function indentedLayout(root, options) {
49 options = util.assign({}, DEFAULT_OPTIONS, options);
50 return new IndentedLayout(root, options).execute();
51}
52
53module.exports = indentedLayout;