UNPKG

5.14 kBJavaScriptView Raw
1/*
2 * Copyright 2015 Palantir Technologies, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import { __assign, __extends } from "tslib";
17import classNames from "classnames";
18import * as React from "react";
19import { Classes, DISPLAYNAME_PREFIX } from "../../common";
20import { TreeNode } from "./treeNode";
21/**
22 * Tree component.
23 *
24 * @see https://blueprintjs.com/docs/#core/components/tree
25 */
26// eslint-disable-next-line @typescript-eslint/ban-types
27var Tree = /** @class */ (function (_super) {
28 __extends(Tree, _super);
29 function Tree() {
30 var _this = _super !== null && _super.apply(this, arguments) || this;
31 _this.nodeRefs = {};
32 _this.handleContentRef = function (node, element) {
33 if (element != null) {
34 _this.nodeRefs[node.id] = element;
35 }
36 else {
37 // don't want our object to get bloated with old keys
38 delete _this.nodeRefs[node.id];
39 }
40 };
41 _this.handleNodeCollapse = function (node, path, e) {
42 var _a, _b;
43 (_b = (_a = _this.props).onNodeCollapse) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
44 };
45 _this.handleNodeClick = function (node, path, e) {
46 var _a, _b;
47 (_b = (_a = _this.props).onNodeClick) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
48 };
49 _this.handleNodeContextMenu = function (node, path, e) {
50 var _a, _b;
51 (_b = (_a = _this.props).onNodeContextMenu) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
52 };
53 _this.handleNodeDoubleClick = function (node, path, e) {
54 var _a, _b;
55 (_b = (_a = _this.props).onNodeDoubleClick) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
56 };
57 _this.handleNodeExpand = function (node, path, e) {
58 var _a, _b;
59 (_b = (_a = _this.props).onNodeExpand) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
60 };
61 _this.handleNodeMouseEnter = function (node, path, e) {
62 var _a, _b;
63 (_b = (_a = _this.props).onNodeMouseEnter) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
64 };
65 _this.handleNodeMouseLeave = function (node, path, e) {
66 var _a, _b;
67 (_b = (_a = _this.props).onNodeMouseLeave) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
68 };
69 return _this;
70 }
71 Tree.ofType = function () {
72 return Tree;
73 };
74 Tree.nodeFromPath = function (path, treeNodes) {
75 if (path.length === 1) {
76 return treeNodes[path[0]];
77 }
78 else {
79 return Tree.nodeFromPath(path.slice(1), treeNodes[path[0]].childNodes);
80 }
81 };
82 Tree.prototype.render = function () {
83 var _a;
84 return (React.createElement("div", { className: classNames(Classes.TREE, this.props.className, (_a = {},
85 _a[Classes.COMPACT] = this.props.compact,
86 _a)) }, this.renderNodes(this.props.contents, [], Classes.TREE_ROOT)));
87 };
88 /**
89 * Returns the underlying HTML element of the `Tree` node with an id of `nodeId`.
90 * This element does not contain the children of the node, only its label and controls.
91 * If the node is not currently mounted, `undefined` is returned.
92 */
93 Tree.prototype.getNodeContentElement = function (nodeId) {
94 return this.nodeRefs[nodeId];
95 };
96 Tree.prototype.renderNodes = function (treeNodes, currentPath, className) {
97 var _this = this;
98 if (treeNodes == null) {
99 return null;
100 }
101 var nodeItems = treeNodes.map(function (node, i) {
102 var elementPath = currentPath.concat(i);
103 return (React.createElement(TreeNode, __assign({}, node, { key: node.id, contentRef: _this.handleContentRef, depth: elementPath.length - 1, onClick: _this.handleNodeClick, onContextMenu: _this.handleNodeContextMenu, onCollapse: _this.handleNodeCollapse, onDoubleClick: _this.handleNodeDoubleClick, onExpand: _this.handleNodeExpand, onMouseEnter: _this.handleNodeMouseEnter, onMouseLeave: _this.handleNodeMouseLeave, path: elementPath }), _this.renderNodes(node.childNodes, elementPath)));
104 });
105 return React.createElement("ul", { className: classNames(Classes.TREE_NODE_LIST, className) }, nodeItems);
106 };
107 Tree.displayName = "".concat(DISPLAYNAME_PREFIX, ".Tree");
108 return Tree;
109}(React.Component));
110export { Tree };
111//# sourceMappingURL=tree.js.map
\No newline at end of file