|
| 1 | +import React from 'react'; |
| 2 | + |
| 3 | +var SortableTree = require('react-sortable-tree').default; |
| 4 | +var toggleExpandedForAll = require('react-sortable-tree').toggleExpandedForAll; |
| 5 | +var changeNodeAtPath = require('react-sortable-tree').changeNodeAtPath; |
| 6 | +var walk = require('react-sortable-tree').walk; |
| 7 | +require('./Tree.less'); |
| 8 | + |
| 9 | +export default class Tree extends React.Component { |
| 10 | + constructor (props) { |
| 11 | + super(props); |
| 12 | + |
| 13 | + this.updateTreeData = this.updateTreeData.bind(this); |
| 14 | + this.expandAll = this.expandAll.bind(this); |
| 15 | + this.collapseAll = this.collapseAll.bind(this); |
| 16 | + this.state = { treeData: this.props.treeData }; |
| 17 | + } |
| 18 | + |
| 19 | + updateTreeData(treeData) { |
| 20 | + this.setState({ treeData }); |
| 21 | + } |
| 22 | + |
| 23 | + expand(expanded) { |
| 24 | + this.setState({ |
| 25 | + treeData: toggleExpandedForAll({ |
| 26 | + treeData: this.state.treeData, |
| 27 | + expanded, |
| 28 | + }), |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + expandAll() { |
| 33 | + this.expand(true); |
| 34 | + } |
| 35 | + |
| 36 | + collapseAll() { |
| 37 | + this.expand(false); |
| 38 | + } |
| 39 | + |
| 40 | + handleClick(event, rowInfo) { |
| 41 | + var toggleMode = this.props.toggleMode; |
| 42 | + var currentTreeData = this.state.treeData; |
| 43 | + // If node has children, we expand/collapse the node |
| 44 | + if ( |
| 45 | + rowInfo.node.children != undefined && |
| 46 | + rowInfo.node.children.length > 0 |
| 47 | + ) { |
| 48 | + // If parents can be activate, iterate over the whole tree |
| 49 | + if (this.props.activateParentsNodeOnClick) { |
| 50 | + walk({ |
| 51 | + treeData: currentTreeData, |
| 52 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 53 | + ignoreCollapsed: true, |
| 54 | + callback: (rowInfoIter) => { |
| 55 | + var isActive = rowInfoIter.treeIndex == rowInfo.treeIndex; |
| 56 | + /* |
| 57 | + * If toggleMode just toggle to activate/inactivate selected node and expand/collapse |
| 58 | + * If non toggle mode inactive all nodes but selected and expand/collapse |
| 59 | + */ |
| 60 | + if (isActive && toggleMode) { |
| 61 | + rowInfoIter.node.active = !rowInfoIter.node.active; |
| 62 | + rowInfoIter.node.expanded = !rowInfoIter.node.expanded; |
| 63 | + currentTreeData = changeNodeAtPath({ |
| 64 | + treeData: currentTreeData, |
| 65 | + path: rowInfoIter.path, |
| 66 | + newNode: rowInfoIter.node, |
| 67 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 68 | + ignoreCollapsed: true, |
| 69 | + }); |
| 70 | + } else if (isActive && !toggleMode) { |
| 71 | + rowInfoIter.node.active = isActive; |
| 72 | + rowInfoIter.node.expanded = !rowInfoIter.node.expanded; |
| 73 | + currentTreeData = changeNodeAtPath({ |
| 74 | + treeData: currentTreeData, |
| 75 | + path: rowInfoIter.path, |
| 76 | + newNode: rowInfoIter.node, |
| 77 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 78 | + ignoreCollapsed: true, |
| 79 | + }); |
| 80 | + } else if (isActive != rowInfoIter.node.active && !toggleMode) { |
| 81 | + rowInfoIter.node.active = isActive; |
| 82 | + currentTreeData = changeNodeAtPath({ |
| 83 | + treeData: currentTreeData, |
| 84 | + path: rowInfoIter.path, |
| 85 | + newNode: rowInfoIter.node, |
| 86 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 87 | + ignoreCollapsed: true, |
| 88 | + }); |
| 89 | + } |
| 90 | + }, |
| 91 | + }); |
| 92 | + } else { |
| 93 | + rowInfo.node.expanded = !rowInfo.node.expanded; |
| 94 | + currentTreeData = changeNodeAtPath({ |
| 95 | + treeData: currentTreeData, |
| 96 | + path: rowInfo.path, |
| 97 | + newNode: rowInfo.node, |
| 98 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 99 | + ignoreCollapsed: true, |
| 100 | + }); |
| 101 | + } |
| 102 | + } else if (rowInfo.node.children == undefined) { |
| 103 | + // If node has no children, we select the node |
| 104 | + walk({ |
| 105 | + treeData: currentTreeData, |
| 106 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 107 | + ignoreCollapsed: true, |
| 108 | + callback: (rowInfoIter) => { |
| 109 | + var isActive = rowInfoIter.treeIndex == rowInfo.treeIndex; |
| 110 | + /* |
| 111 | + * If toggleMode just toggle to activate/inactivate selected node |
| 112 | + * If non toggle mode inactive all nodes but selected |
| 113 | + */ |
| 114 | + if (isActive && toggleMode) { |
| 115 | + rowInfoIter.node.active = !rowInfoIter.node.active; |
| 116 | + currentTreeData = changeNodeAtPath({ |
| 117 | + treeData: currentTreeData, |
| 118 | + path: rowInfoIter.path, |
| 119 | + newNode: rowInfoIter.node, |
| 120 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 121 | + ignoreCollapsed: true, |
| 122 | + }); |
| 123 | + } else if (isActive != rowInfoIter.node.active && !toggleMode) { |
| 124 | + rowInfoIter.node.active = isActive; |
| 125 | + currentTreeData = changeNodeAtPath({ |
| 126 | + treeData: currentTreeData, |
| 127 | + path: rowInfoIter.path, |
| 128 | + newNode: rowInfoIter.node, |
| 129 | + getNodeKey: ({ treeIndex }) => treeIndex, |
| 130 | + ignoreCollapsed: true, |
| 131 | + }); |
| 132 | + } |
| 133 | + }, |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + // Update tree with latest changes |
| 138 | + this.updateTreeData(currentTreeData); |
| 139 | + |
| 140 | + // If there is a callback, we use it |
| 141 | + if (this.props.handleClick != undefined) { |
| 142 | + this.props.handleClick(event, rowInfo); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + getNodeProps(rowInfo) { |
| 147 | + var nodeProps = {}; |
| 148 | + nodeProps['onClick'] = (event) => this.handleClick(event, rowInfo); |
| 149 | + |
| 150 | + if (this.props.getButtons !== undefined) { |
| 151 | + nodeProps['buttons'] = this.props.getButtons(rowInfo); |
| 152 | + } |
| 153 | + if (rowInfo.node.instance !== undefined) { |
| 154 | + nodeProps['style'] = { cursor: 'pointer' }; |
| 155 | + } |
| 156 | + if (rowInfo.node.active) { |
| 157 | + nodeProps['className'] = 'activeNode'; |
| 158 | + } |
| 159 | + if (this.props.getNodesProps !== undefined) { |
| 160 | + nodeProps['title'] = this.props.getNodesProps(rowInfo); |
| 161 | + } |
| 162 | + return nodeProps; |
| 163 | + } |
| 164 | + |
| 165 | + render() { |
| 166 | + var onlyExpandSearchedNodes = |
| 167 | + this.props.searchQuery !== undefined && this.props.searchQuery !== null; |
| 168 | + return ( |
| 169 | + <div |
| 170 | + key={this.props.id + '_component'} |
| 171 | + id={this.props.id + '_component'} |
| 172 | + className="treeViewer" |
| 173 | + style={this.props.style} |
| 174 | + > |
| 175 | + {this.props.controls ? this.props.controls : null} |
| 176 | + <SortableTree |
| 177 | + style={this.props.style} |
| 178 | + treeData={this.state.treeData} |
| 179 | + canDrag={false} |
| 180 | + rowHeight={this.props.rowHeight} |
| 181 | + scaffoldBlockPxWidth={22} |
| 182 | + generateNodeProps={(rowInfo) => this.getNodeProps(rowInfo)} |
| 183 | + onChange={(treeData) => this.updateTreeData(treeData)} |
| 184 | + searchQuery={ |
| 185 | + this.props.searchQuery !== undefined ? this.props.searchQuery : null |
| 186 | + } |
| 187 | + onlyExpandSearchedNodes={ |
| 188 | + this.props.onlyExpandSearchedNodes !== undefined |
| 189 | + ? this.props.onlyExpandSearchedNodes |
| 190 | + : false |
| 191 | + } |
| 192 | + /> |
| 193 | + </div> |
| 194 | + ); |
| 195 | + } |
| 196 | +}; |
0 commit comments