UNPKG

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