sebdet | b496fda | 2021-03-30 00:30:49 +0200 | [diff] [blame] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * ONAP POLICY-CLAMP |
| 4 | * ================================================================================ |
| 5 | * Copyright (C) 2021 AT&T Intellectual Property. All rights |
| 6 | * reserved. |
| 7 | * ================================================================================ |
| 8 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | * you may not use this file except in compliance with the License. |
| 10 | * You may obtain a copy of the License at |
| 11 | * |
| 12 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | * |
| 14 | * Unless required by applicable law or agreed to in writing, software |
| 15 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | * See the License for the specific language governing permissions and |
| 18 | * limitations under the License. |
| 19 | * ============LICENSE_END============================================ |
| 20 | * =================================================================== |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | import React, { forwardRef } from 'react' |
| 25 | import TreeView from '@material-ui/lab/TreeView'; |
| 26 | import TreeItem from '@material-ui/lab/TreeItem'; |
| 27 | import FolderIcon from '@material-ui/icons/Folder'; |
| 28 | import FolderOpenIcon from '@material-ui/icons/FolderOpen'; |
| 29 | import DescriptionIcon from '@material-ui/icons/Description'; |
| 30 | |
| 31 | |
| 32 | export default class PoliciesTreeViewer extends React.Component { |
| 33 | |
| 34 | separator = "."; |
| 35 | |
| 36 | nodesList = new Map(); |
| 37 | |
| 38 | constructor(props, context) { |
| 39 | super(props, context); |
| 40 | this.createPoliciesTree = this.createPoliciesTree.bind(this); |
| 41 | this.handleTreeItemClick = this.handleTreeItemClick.bind(this); |
| 42 | this.buildNameWithParent = this.buildNameWithParent.bind(this); |
| 43 | |
| 44 | } |
| 45 | |
| 46 | state = { |
| 47 | policiesTreeData: this.createPoliciesTree(this.props.policiesData), |
| 48 | } |
| 49 | |
| 50 | componentDidUpdate(prevProps) { |
| 51 | if (prevProps.policiesData !== this.props.policiesData) { |
| 52 | this.setState({policiesTreeData: this.createPoliciesTree(this.props.policiesData)}) |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | createPoliciesTree(policiesArray) { |
| 57 | // put my policies array in a Json |
| 58 | let nodeId = 1; |
| 59 | let root = {id:nodeId, policyCount:0, name:"ROOT", children:[], parent: undefined}; |
| 60 | this.nodesList.set(nodeId++, root); |
| 61 | |
| 62 | policiesArray.forEach(policy => { |
| 63 | let currentTreeNode = root; |
| 64 | policy[this.props.valueForTreeCreation].split(this.separator).forEach((policyNamePart, index, policyNamePartsArray) => { |
| 65 | let node = currentTreeNode["children"].find(element => element.name === policyNamePart); |
| 66 | if (typeof(node) === "undefined") { |
| 67 | node = {id:nodeId, policyCount:0, children:[], name:policyNamePart, parent:currentTreeNode}; |
| 68 | this.nodesList.set(nodeId++, node); |
| 69 | currentTreeNode["children"].push(node); |
| 70 | } |
| 71 | if ((index+1) === policyNamePartsArray.length) { |
| 72 | ++currentTreeNode["policyCount"]; |
| 73 | } |
| 74 | currentTreeNode = node; |
| 75 | }) |
| 76 | }) |
| 77 | return root; |
| 78 | } |
| 79 | |
| 80 | buildNameWithParent(node) { |
| 81 | let nameToBuild = node.name; |
| 82 | if (node.parent !== undefined) { |
| 83 | nameToBuild = this.buildNameWithParent(node.parent) + this.separator + node.name; |
| 84 | } |
| 85 | return nameToBuild; |
| 86 | } |
| 87 | |
| 88 | handleTreeItemClick(event, value) { |
| 89 | let fullName = this.buildNameWithParent(this.nodesList.get(value[0])).substring(5); |
| 90 | this.props.policiesFilterFunction(fullName); |
| 91 | } |
| 92 | |
| 93 | renderTreeItems(nodes) { |
| 94 | return (<TreeItem key={nodes.id} nodeId={nodes.id} label={nodes.name + "("+ nodes.policyCount + ")"} onNodeSelect={this.handleTreeItemClick}> |
| 95 | { |
| 96 | Array.isArray(nodes.children) ? nodes.children.map((node) => this.renderTreeItems(node)) : null |
| 97 | } |
| 98 | </TreeItem>); |
| 99 | }; |
| 100 | |
| 101 | render() { |
| 102 | return ( |
| 103 | <TreeView defaultExpanded={['root']} defaultCollapseIcon={<FolderOpenIcon />} |
| 104 | defaultExpandIcon={<FolderIcon />} defaultEndIcon={<DescriptionIcon />} onNodeSelect={this.handleTreeItemClick} multiSelect> |
| 105 | {this.renderTreeItems(this.state.policiesTreeData)} |
| 106 | </TreeView> |
| 107 | ); |
| 108 | } |
| 109 | } |