blob: fb3b71f0c8ee795847a50af69ae678ea070f2fdd [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
2 * Copyright (C) 2017 AT&T Intellectual Property. 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
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import React from 'react';
17import classnames from 'classnames';
18import Collapse from 'react-bootstrap/lib/Collapse.js';
19
20class NavigationSideBar extends React.Component {
Michael Landoefa037d2017-02-19 12:57:33 +020021 static PropTypes = {
22 activeItemId: React.PropTypes.string.isRequired,
23 onSelect: React.PropTypes.func,
24 onToggle: React.PropTypes.func,
25 groups: React.PropTypes.array
26 };
27
AviZi280f8012017-06-09 02:39:56 +030028 constructor(props) {
29 super(props);
30 this.state = {
31 activeItemId: null
32 };
33 this.handleItemClicked = this.handleItemClicked.bind(this);
34 }
35
Michael Landoefa037d2017-02-19 12:57:33 +020036 render() {
37 let {groups, activeItemId} = this.props;
38
39 return (
40 <div className='navigation-side-content'>
41 {groups.map(group => (
AviZi280f8012017-06-09 02:39:56 +030042 <NavigationMenu menu={group} activeItemId={activeItemId} onNavigationItemClick={this.handleItemClicked} key={'menu_' + group.id} />
Michael Landoefa037d2017-02-19 12:57:33 +020043 ))}
44 </div>
45 );
46 }
47
Michael Landoefa037d2017-02-19 12:57:33 +020048 handleItemClicked(event, item) {
49 event.stopPropagation();
50 if(this.props.onToggle) {
51 this.props.onToggle(this.props.groups, item.id);
52 }
53 if(item.onSelect) {
54 item.onSelect();
55 }
56 if(this.props.onSelect) {
57 this.props.onSelect(item);
58 }
59 }
60}
61
AviZi280f8012017-06-09 02:39:56 +030062class NavigationMenu extends React.Component {
63 static PropTypes = {
64 activeItemId: React.PropTypes.string.isRequired,
65 onNavigationItemClick: React.PropTypes.func,
66 menu: React.PropTypes.array
67 };
68
69 render() {
70 const {menu, activeItemId, onNavigationItemClick} = this.props;
71 return (
72 <div className='navigation-group' key={menu.id}>
73 <NavigationMenuHeader title={menu.name} />
74 <NavigationMenuItems items={menu.items} activeItemId={activeItemId} onNavigationItemClick={onNavigationItemClick} />
75 </div>);
76 }
77}
78
79function NavigationMenuHeader(props) {
80 return <div className='group-name' data-test-id='navbar-group-name'>{props.title}</div>;
81}
82
83function NavigationMenuItems(props) {
84 const {items, activeItemId, onNavigationItemClick} = props;
85 return (
86 <div className='navigation-group-items'>
87 {
88 items && items.map(item => (<NavigationMenuItem key={'menuItem_' + item.id} item={item} activeItemId={activeItemId} onNavigationItemClick={onNavigationItemClick} />))
89 }
90 </div>
91 );
92}
93
94function NavigationMenuItem(props) {
95 const {onNavigationItemClick, item, activeItemId} = props;
96 const isGroup = item.items && item.items.length > 0;
97 return (
98 <div className={classnames('navigation-group-item', {'selected-item': item.id === activeItemId})} key={'item_' + item.id}>
99 <NavigationLink item={item} activeItemId={activeItemId} onClick={onNavigationItemClick} />
100 {isGroup && <Collapse in={item.expanded} data-test-id={'navigation-group-' + item.id}>
101 <div>
102 {item.items.map(subItem => (<NavigationMenuItem key={'menuItem_' + subItem.id} item={subItem} onNavigationItemClick={onNavigationItemClick} activeItemId={activeItemId} />)) }
103 </div>
104 </Collapse>
105 }
106 </div>
107 );
108}
109
110function NavigationLink(props) {
111 const {item, activeItemId, onClick} = props;
az2497644017c2017-08-10 17:49:40 +0300112 // todo should this be button
AviZi280f8012017-06-09 02:39:56 +0300113 return (
114 <div
115 key={'navAction_' + item.id}
116 className={classnames('navigation-group-item-name', {
117 'selected': item.id === activeItemId,
118 'disabled': item.disabled,
119 'bold-name': item.expanded,
120 'hidden': item.hidden
121 })}
122 onClick={(event) => onClick(event, item)}
123 data-test-id={'navbar-group-item-' + item.id}>
124 {item.name}
125 </div>
126 );
127}
128
Michael Landoefa037d2017-02-19 12:57:33 +0200129export default NavigationSideBar;