@blueprintjs/core
Version:
Core styles & components
109 lines • 4.95 kB
JavaScript
"use strict";
/*
* Copyright 2015 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.Tree = void 0;
const tslib_1 = require("tslib");
const classnames_1 = tslib_1.__importDefault(require("classnames"));
const React = tslib_1.__importStar(require("react"));
const common_1 = require("../../common");
const treeNode_1 = require("./treeNode");
/**
* Tree component.
*
* @see https://blueprintjs.com/docs/#core/components/tree
*/
// eslint-disable-next-line @typescript-eslint/no-empty-object-type
class Tree extends React.Component {
constructor() {
super(...arguments);
this.nodeRefs = {};
this.handleContentRef = (node, element) => {
if (element != null) {
this.nodeRefs[node.id] = element;
}
else {
// don't want our object to get bloated with old keys
delete this.nodeRefs[node.id];
}
};
this.handleNodeCollapse = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeCollapse) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeClick = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeClick) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeContextMenu = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeContextMenu) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeDoubleClick = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeDoubleClick) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeExpand = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeExpand) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeMouseEnter = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeMouseEnter) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
this.handleNodeMouseLeave = (node, path, e) => {
var _a, _b;
(_b = (_a = this.props).onNodeMouseLeave) === null || _b === void 0 ? void 0 : _b.call(_a, node, path, e);
};
}
static ofType() {
return Tree;
}
static nodeFromPath(path, treeNodes) {
if (path.length === 1) {
return treeNodes[path[0]];
}
else {
return Tree.nodeFromPath(path.slice(1), treeNodes[path[0]].childNodes);
}
}
render() {
return (React.createElement("div", { className: (0, classnames_1.default)(common_1.Classes.TREE, this.props.className, {
[common_1.Classes.COMPACT]: this.props.compact,
}) }, this.renderNodes(this.props.contents, [], common_1.Classes.TREE_ROOT)));
}
/**
* Returns the underlying HTML element of the `Tree` node with an id of `nodeId`.
* This element does not contain the children of the node, only its label and controls.
* If the node is not currently mounted, `undefined` is returned.
*/
getNodeContentElement(nodeId) {
return this.nodeRefs[nodeId];
}
renderNodes(treeNodes, currentPath, className) {
if (treeNodes == null) {
return null;
}
const nodeItems = treeNodes.map((node, i) => {
const elementPath = currentPath.concat(i);
return (React.createElement(treeNode_1.TreeNode, { ...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)));
});
return React.createElement("ul", { className: (0, classnames_1.default)(common_1.Classes.TREE_NODE_LIST, className) }, nodeItems);
}
}
exports.Tree = Tree;
Tree.displayName = `${common_1.DISPLAYNAME_PREFIX}.Tree`;
//# sourceMappingURL=tree.js.map