UNPKG

2.35 kBJavaScriptView Raw
1function _inheritsLoose(subClass, superClass) { subClass.prototype = Object.create(superClass.prototype); subClass.prototype.constructor = subClass; subClass.__proto__ = superClass; }
2
3var TreeLayout = require('./layout/base');
4
5var indentedTree = require('./layout/indented');
6
7var separateTree = require('./layout/separate-root');
8
9var util = require('./util');
10
11var VALID_DIRECTIONS = ['LR', // left to right
12'RL', // right to left
13'H' // horizontal
14];
15var DEFAULT_DIRECTION = VALID_DIRECTIONS[0];
16
17var IndentedLayout = /*#__PURE__*/function (_TreeLayout) {
18 _inheritsLoose(IndentedLayout, _TreeLayout);
19
20 function IndentedLayout() {
21 return _TreeLayout.apply(this, arguments) || this;
22 }
23
24 var _proto = IndentedLayout.prototype;
25
26 _proto.execute = function execute() {
27 var me = this;
28 var options = me.options;
29 var root = me.rootNode;
30 options.isHorizontal = true; // default indent 20 and sink first children;
31
32 var _options$indent = options.indent,
33 indent = _options$indent === void 0 ? 20 : _options$indent,
34 _options$dropCap = options.dropCap,
35 dropCap = _options$dropCap === void 0 ? true : _options$dropCap;
36 var direction = options.direction || DEFAULT_DIRECTION;
37
38 if (direction && VALID_DIRECTIONS.indexOf(direction) === -1) {
39 throw new TypeError("Invalid direction: " + direction);
40 }
41
42 if (direction === VALID_DIRECTIONS[0]) {
43 // LR
44 indentedTree(root, indent, dropCap);
45 } else if (direction === VALID_DIRECTIONS[1]) {
46 // RL
47 indentedTree(root, indent, dropCap);
48 root.right2left();
49 } else if (direction === VALID_DIRECTIONS[2]) {
50 // H
51 // separate into left and right trees
52 var _separateTree = separateTree(root, options),
53 left = _separateTree.left,
54 right = _separateTree.right;
55
56 indentedTree(left, indent, dropCap);
57 left.right2left();
58 indentedTree(right, indent, dropCap);
59 var bbox = left.getBoundingBox();
60 right.translate(bbox.width, 0);
61 root.x = right.x - root.width / 2;
62 }
63
64 return root;
65 };
66
67 return IndentedLayout;
68}(TreeLayout);
69
70var DEFAULT_OPTIONS = {};
71
72function indentedLayout(root, options) {
73 options = util.assign({}, DEFAULT_OPTIONS, options);
74 return new IndentedLayout(root, options).execute();
75}
76
77module.exports = indentedLayout;
\No newline at end of file