2 * ============LICENSE_START========================================================================
3 * ONAP : ccsdk feature sdnr wt odlux
4 * =================================================================================================
5 * Copyright (C) 2019 highstreet technologies GmbH Intellectual Property. All rights reserved.
6 * =================================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
16 * ============LICENSE_END==========================================================================
18 import React from 'react';
20 import Breadcrumbs from '@mui/material/Breadcrumbs';
21 import Link from '@mui/material/Link';
22 import { Theme } from '@mui/material/styles';
23 import { WithStyles } from '@mui/styles';
24 import createStyles from '@mui/styles/createStyles';
25 import withStyles from '@mui/styles/withStyles';
26 import { RouteComponentProps } from 'react-router-dom';
27 import { SearchMode, TreeView, TreeViewCtorType } from '../../../../framework/src/components/material-ui/treeView';
28 import { renderObject } from '../../../../framework/src/components/objectDump';
29 import { Connect, connect, IDispatcher } from '../../../../framework/src/flux/connect';
30 import { IApplicationStoreState } from '../../../../framework/src/store/applicationStore';
32 import { selectInventoryNodeAsyncAction, setSearchTermAction, UpdateExpandedNodesAction, updateInventoryTreeAsyncAction, UpdateSelectedNodeAction } from '../actions/inventoryTreeActions';
33 import { TreeDemoItem } from '../models/inventory';
35 const styles = (theme: Theme) => createStyles({
42 wordWrap: 'break-word',
44 padding: `0px ${theme.spacing(1)}`,
48 padding: `0px ${theme.spacing(1)}`,
52 const mapProps = (state: IApplicationStoreState) => ({
53 isBusy: state.inventory.inventoryTree.isBusy,
54 rootNodes: state.inventory.inventoryTree.rootNodes,
55 searchTerm: state.inventory.inventoryTree.searchTerm,
56 selectedNode: state.inventory.inventoryTree.selectedNode,
57 expendedItems: state.inventory.inventoryTree.expandedItems,
60 const mapDispatch = (dispatcher: IDispatcher) => ({
61 updateExpendedNodes: (expendedNodes: TreeDemoItem[]) => dispatcher.dispatch(new UpdateExpandedNodesAction(expendedNodes)),
62 updateInventoryTree: (mountId: string, searchTerm?: string) => dispatcher.dispatch(updateInventoryTreeAsyncAction(mountId, searchTerm)),
63 selectTreeNode: (nodeId?: string) => nodeId ? dispatcher.dispatch(selectInventoryNodeAsyncAction(nodeId)) : dispatcher.dispatch(new UpdateSelectedNodeAction(undefined)),
64 setSearchTerm: (searchTerm: string) => dispatcher.dispatch(setSearchTermAction(searchTerm)),
67 const propsChache = Symbol('PropsCache');
68 const InventoryTree = TreeView as any as TreeViewCtorType<string>;
72 type TreeviewComponentProps = RouteComponentProps<{ mountId: string }> & WithStyles<typeof styles> & Connect<typeof mapProps, typeof mapDispatch>;
74 type TreeviewComponentState = {
76 rootNodes?: TreeDemoItem[];
78 rootNodes: TreeDemoItem[];
82 class DashboardComponent extends React.Component<TreeviewComponentProps, TreeviewComponentState> {
84 constructor(props: TreeviewComponentProps) {
93 static getDerivedStateFromProps(props: TreeviewComponentProps, state: TreeviewComponentState) {
94 if (state[propsChache].rootNodes != props.rootNodes) {
95 // eslint-disable-next-line no-param-reassign
96 state = { ...state, rootNodes: props.rootNodes };
102 const { classes, updateInventoryTree, updateExpendedNodes, expendedItems, selectedNode, selectTreeNode, searchTerm, match: { params: { mountId } } } = this.props;
103 const scrollbar = { overflow: 'auto', paddingRight: '20px' };
105 let filteredDashboardPath = `/inventory/dashboard/${this.props.match.params.mountId}`;
106 let basePath = `/inventory/${this.props.match.params.mountId}`;
109 <div style={scrollbar} >
111 <Breadcrumbs aria-label="breadcrumbs">
112 <Link underline="hover" color="inherit" href="#" aria-label="back-breadcrumb"
113 onClick={(event: React.MouseEvent<HTMLElement>) => {
114 event.preventDefault();
115 this.props.history.push(filteredDashboardPath);
117 <Link underline="hover" color="inherit" href="#"
118 aria-label={this.props.match.params.mountId + '-breadcrumb'}
119 onClick={(event: React.MouseEvent<HTMLElement>) => {
120 event.preventDefault();
121 this.props.history.push(basePath);
122 }}><span>{this.props.match.params.mountId}</span></Link>
126 <div style={scrollbar} className={classes.root}>
127 <InventoryTree className={classes.tree} items={this.state.rootNodes} enableSearchBar initialSearchTerm={searchTerm} searchMode={SearchMode.OnEnter} searchTerm={searchTerm}
128 // eslint-disable-next-line @typescript-eslint/no-shadow
129 onSearch={(searchTerm) => updateInventoryTree(mountId, searchTerm)} expandedItems={expendedItems} onFolderClick={(item) => {
130 const indexOfItemToToggle = expendedItems.indexOf(item);
131 if (indexOfItemToToggle === -1) {
132 updateExpendedNodes([...expendedItems, item]);
134 updateExpendedNodes([
135 ...expendedItems.slice(0, indexOfItemToToggle),
136 ...expendedItems.slice(indexOfItemToToggle + 1),
140 onItemClick={(elm) => selectTreeNode(elm.value)} />
141 <div className={classes.details}>{
142 selectedNode && renderObject(selectedNode, 'tree-view') || null
149 componentWillUnmount() {
150 this.props.setSearchTerm('*');
154 export const InventoryTreeView = connect(mapProps, mapDispatch)(withStyles(styles)(DashboardComponent));
155 export default InventoryTreeView;