1 | const TreeLayout = require('./layout/base');
|
2 | const indentedTree = require('./layout/indented');
|
3 | const separateTree = require('./layout/separate-root');
|
4 | const util = require('./util');
|
5 |
|
6 |
|
7 | const VALID_DIRECTIONS = [
|
8 | 'LR',
|
9 | 'RL',
|
10 | 'H'
|
11 | ];
|
12 | const DEFAULT_DIRECTION = VALID_DIRECTIONS[0];
|
13 |
|
14 | class IndentedLayout extends TreeLayout {
|
15 | execute() {
|
16 | const me = this;
|
17 | const options = me.options;
|
18 | const root = me.rootNode;
|
19 | options.isHorizontal = true;
|
20 |
|
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]) {
|
27 | indentedTree(root, indent, dropCap);
|
28 | } else if (direction === VALID_DIRECTIONS[1]) {
|
29 | indentedTree(root, indent, dropCap);
|
30 | root.right2left();
|
31 | } else if (direction === VALID_DIRECTIONS[2]) {
|
32 |
|
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 |
|
45 | const DEFAULT_OPTIONS = {
|
46 | };
|
47 |
|
48 | function indentedLayout(root, options) {
|
49 | options = util.assign({}, DEFAULT_OPTIONS, options);
|
50 | return new IndentedLayout(root, options).execute();
|
51 | }
|
52 |
|
53 | module.exports = indentedLayout;
|