AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
svishnev | f678490 | 2018-02-12 09:10:35 +0200 | [diff] [blame] | 2 | * Copyright © 2016-2018 European Support Limited |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import React from 'react'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 17 | import PropTypes from 'prop-types'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 18 | import classnames from 'classnames'; |
| 19 | import Collapse from 'react-bootstrap/lib/Collapse.js'; |
| 20 | |
| 21 | class NavigationSideBar extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 22 | static PropTypes = { |
| 23 | activeItemId: PropTypes.string.isRequired, |
| 24 | onSelect: PropTypes.func, |
| 25 | onToggle: PropTypes.func, |
| 26 | groups: PropTypes.array, |
| 27 | disabled: PropTypes.bool |
| 28 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 29 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 30 | constructor(props) { |
| 31 | super(props); |
| 32 | this.state = { |
| 33 | activeItemId: null |
| 34 | }; |
| 35 | this.handleItemClicked = this.handleItemClicked.bind(this); |
| 36 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 37 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 38 | render() { |
| 39 | let { groups, activeItemId, disabled = false } = this.props; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 40 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 41 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 57 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 58 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 70 | } |
| 71 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 72 | class NavigationMenu extends React.Component { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 73 | static PropTypes = { |
| 74 | activeItemId: PropTypes.string.isRequired, |
| 75 | onNavigationItemClick: PropTypes.func, |
| 76 | menu: PropTypes.array |
| 77 | }; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 78 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 79 | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | function NavigationMenuHeader(props) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 95 | return ( |
| 96 | <div className="group-name" data-test-id="navbar-group-name"> |
| 97 | {props.title} |
| 98 | </div> |
| 99 | ); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 100 | } |
| 101 | |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 102 | function getItemDataTestId(itemId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 103 | return itemId.split('|')[0]; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 104 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 105 | function NavigationMenuItems(props) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 106 | 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 | ); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | function NavigationMenuItem(props) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 123 | 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 | ); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | function NavigationLink(props) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame^] | 159 | 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 | ); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 175 | } |
| 176 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 177 | export default NavigationSideBar; |