blob: d852ac847ba371c6f5de7e760415aa6da4b47c08 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
svishnevf6784902018-02-12 09:10:35 +02002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
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';
talig8e9c0652017-12-20 14:30:43 +020017import PropTypes from 'prop-types';
Michael Landoefa037d2017-02-19 12:57:33 +020018import classnames from 'classnames';
19import Collapse from 'react-bootstrap/lib/Collapse.js';
20
21class NavigationSideBar extends React.Component {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030022 static propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 activeItemId: PropTypes.string.isRequired,
24 onSelect: PropTypes.func,
25 onToggle: PropTypes.func,
26 groups: PropTypes.array,
27 disabled: PropTypes.bool
28 };
Michael Landoefa037d2017-02-19 12:57:33 +020029
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 constructor(props) {
31 super(props);
32 this.state = {
33 activeItemId: null
34 };
35 this.handleItemClicked = this.handleItemClicked.bind(this);
36 }
AviZi280f8012017-06-09 02:39:56 +030037
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020038 render() {
39 let { groups, activeItemId, disabled = false } = this.props;
Michael Landoefa037d2017-02-19 12:57:33 +020040
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 return (
42 <div
43 className={`navigation-side-content ${
44 disabled ? 'disabled' : ''
45 }`}>
46 {groups.map(group => (
47 <NavigationMenu
48 menu={group}
49 activeItemId={activeItemId}
50 onNavigationItemClick={this.handleItemClicked}
51 key={'menu_' + group.id}
52 />
53 ))}
54 </div>
55 );
56 }
Michael Landoefa037d2017-02-19 12:57:33 +020057
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020058 handleItemClicked(event, item) {
59 event.stopPropagation();
60 if (this.props.onToggle) {
61 this.props.onToggle(this.props.groups, item.id);
62 }
63 if (item.onSelect) {
64 item.onSelect();
65 }
66 if (this.props.onSelect) {
67 this.props.onSelect(item);
68 }
69 }
Michael Landoefa037d2017-02-19 12:57:33 +020070}
71
AviZi280f8012017-06-09 02:39:56 +030072class NavigationMenu extends React.Component {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030073 static propTypes = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020074 activeItemId: PropTypes.string.isRequired,
75 onNavigationItemClick: PropTypes.func,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030076 menu: PropTypes.object
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 };
AviZi280f8012017-06-09 02:39:56 +030078
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020079 render() {
80 const { menu, activeItemId, onNavigationItemClick } = this.props;
81 return (
82 <div className="navigation-group" key={menu.id}>
83 <NavigationMenuHeader title={menu.name} />
84 <NavigationMenuItems
85 items={menu.items}
86 activeItemId={activeItemId}
87 onNavigationItemClick={onNavigationItemClick}
88 />
89 </div>
90 );
91 }
AviZi280f8012017-06-09 02:39:56 +030092}
93
94function NavigationMenuHeader(props) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020095 return (
96 <div className="group-name" data-test-id="navbar-group-name">
97 {props.title}
98 </div>
99 );
AviZi280f8012017-06-09 02:39:56 +0300100}
101
talig8e9c0652017-12-20 14:30:43 +0200102function getItemDataTestId(itemId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200103 return itemId.split('|')[0];
talig8e9c0652017-12-20 14:30:43 +0200104}
AviZi280f8012017-06-09 02:39:56 +0300105function NavigationMenuItems(props) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200106 const { items, activeItemId, onNavigationItemClick } = props;
107 return (
108 <div className="navigation-group-items">
109 {items &&
110 items.map(item => (
111 <NavigationMenuItem
112 key={'menuItem_' + item.id}
113 item={item}
114 activeItemId={activeItemId}
115 onNavigationItemClick={onNavigationItemClick}
116 />
117 ))}
118 </div>
119 );
AviZi280f8012017-06-09 02:39:56 +0300120}
121
122function NavigationMenuItem(props) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200123 const { onNavigationItemClick, item, activeItemId } = props;
124 const isGroup = item.items && item.items.length > 0;
125 return (
126 <div
127 className={classnames('navigation-group-item', {
128 'selected-item': item.id === activeItemId
129 })}
130 key={'item_' + item.id}>
131 <NavigationLink
132 item={item}
133 activeItemId={activeItemId}
134 onClick={onNavigationItemClick}
135 />
136 {isGroup && (
137 <Collapse
138 in={item.expanded}
139 data-test-id={
140 'navigation-group-' + getItemDataTestId(item.id)
141 }>
142 <div>
143 {item.items.map(subItem => (
144 <NavigationMenuItem
145 key={'menuItem_' + subItem.id}
146 item={subItem}
147 onNavigationItemClick={onNavigationItemClick}
148 activeItemId={activeItemId}
149 />
150 ))}
151 </div>
152 </Collapse>
153 )}
154 </div>
155 );
AviZi280f8012017-06-09 02:39:56 +0300156}
157
158function NavigationLink(props) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200159 const { item, activeItemId, onClick } = props;
160 // todo should this be button
161 return (
162 <div
163 key={'navAction_' + item.id}
164 className={classnames('navigation-group-item-name', {
165 selected: item.id === activeItemId,
166 disabled: item.disabled,
167 'bold-name': item.expanded,
168 hidden: item.hidden
169 })}
170 onClick={event => onClick(event, item)}
171 data-test-id={'navbar-group-item-' + getItemDataTestId(item.id)}>
172 {item.name}
173 </div>
174 );
AviZi280f8012017-06-09 02:39:56 +0300175}
176
Michael Landoefa037d2017-02-19 12:57:33 +0200177export default NavigationSideBar;