UNPKG

4.83 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 * as Classes from "../../common/classes";
20import { DISPLAYNAME_PREFIX } from "../../common/props";
21import { isFunction } from "../../common/utils";
22import { TreeNode } from "./treeNode";
23// eslint-disable-next-line @typescript-eslint/ban-types
24var Tree = /** @class */ (function (_super) {
25 __extends(Tree, _super);
26 function Tree() {
27 var _this = _super !== null && _super.apply(this, arguments) || this;
28 _this.nodeRefs = {};
29 _this.handleNodeCollapse = function (node, e) {
30 _this.handlerHelper(_this.props.onNodeCollapse, node, e);
31 };
32 _this.handleNodeClick = function (node, e) {
33 _this.handlerHelper(_this.props.onNodeClick, node, e);
34 };
35 _this.handleContentRef = function (node, element) {
36 if (element != null) {
37 _this.nodeRefs[node.props.id] = element;
38 }
39 else {
40 // don't want our object to get bloated with old keys
41 delete _this.nodeRefs[node.props.id];
42 }
43 };
44 _this.handleNodeContextMenu = function (node, e) {
45 _this.handlerHelper(_this.props.onNodeContextMenu, node, e);
46 };
47 _this.handleNodeDoubleClick = function (node, e) {
48 _this.handlerHelper(_this.props.onNodeDoubleClick, node, e);
49 };
50 _this.handleNodeExpand = function (node, e) {
51 _this.handlerHelper(_this.props.onNodeExpand, node, e);
52 };
53 _this.handleNodeMouseEnter = function (node, e) {
54 _this.handlerHelper(_this.props.onNodeMouseEnter, node, e);
55 };
56 _this.handleNodeMouseLeave = function (node, e) {
57 _this.handlerHelper(_this.props.onNodeMouseLeave, node, e);
58 };
59 return _this;
60 }
61 Tree.ofType = function () {
62 return Tree;
63 };
64 Tree.nodeFromPath = function (path, treeNodes) {
65 if (path.length === 1) {
66 return treeNodes[path[0]];
67 }
68 else {
69 return Tree.nodeFromPath(path.slice(1), treeNodes[path[0]].childNodes);
70 }
71 };
72 Tree.prototype.render = function () {
73 return (React.createElement("div", { className: classNames(Classes.TREE, this.props.className) }, this.renderNodes(this.props.contents, [], Classes.TREE_ROOT)));
74 };
75 /**
76 * Returns the underlying HTML element of the `Tree` node with an id of `nodeId`.
77 * This element does not contain the children of the node, only its label and controls.
78 * If the node is not currently mounted, `undefined` is returned.
79 */
80 Tree.prototype.getNodeContentElement = function (nodeId) {
81 return this.nodeRefs[nodeId];
82 };
83 Tree.prototype.renderNodes = function (treeNodes, currentPath, className) {
84 var _this = this;
85 if (treeNodes == null) {
86 return null;
87 }
88 var nodeItems = treeNodes.map(function (node, i) {
89 var elementPath = currentPath.concat(i);
90 var TypedTreeNode = TreeNode.ofType();
91 return (React.createElement(TypedTreeNode, __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)));
92 });
93 return React.createElement("ul", { className: classNames(Classes.TREE_NODE_LIST, className) }, nodeItems);
94 };
95 Tree.prototype.handlerHelper = function (handlerFromProps, node, e) {
96 if (isFunction(handlerFromProps)) {
97 var nodeData = Tree.nodeFromPath(node.props.path, this.props.contents);
98 handlerFromProps(nodeData, node.props.path, e);
99 }
100 };
101 Tree.displayName = DISPLAYNAME_PREFIX + ".Tree";
102 return Tree;
103}(React.Component));
104export { Tree };
105//# sourceMappingURL=tree.js.map
\No newline at end of file