2020-03-23 15:35:58 +01:00
|
|
|
/*
|
|
|
|
|
* MIT License
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
|
|
|
|
*
|
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
|
*
|
|
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
|
* copies or substantial portions of the Software.
|
|
|
|
|
*
|
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
|
|
* SOFTWARE.
|
|
|
|
|
*/
|
2020-03-11 15:47:40 +01:00
|
|
|
import React, { FC, ReactElement, ReactNode, useContext, useEffect } from "react";
|
|
|
|
|
import styled from "styled-components";
|
2020-03-11 13:25:53 +01:00
|
|
|
import SubNavigation from "./SubNavigation";
|
2020-03-11 15:47:40 +01:00
|
|
|
import { matchPath, useLocation } from "react-router-dom";
|
|
|
|
|
import { isMenuCollapsed, MenuContext } from "./MenuContext";
|
2020-03-30 15:04:00 +02:00
|
|
|
import { ExtensionPoint, Binder, useBinder } from "@scm-manager/ui-extensions";
|
2020-03-11 13:25:53 +01:00
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
label: string;
|
2020-03-11 15:47:40 +01:00
|
|
|
children: ReactElement[];
|
|
|
|
|
collapsed: boolean;
|
|
|
|
|
onCollapse?: (newStatus: boolean) => void;
|
2020-03-11 13:25:53 +01:00
|
|
|
};
|
|
|
|
|
|
2020-03-11 15:47:40 +01:00
|
|
|
type CollapsedProps = {
|
|
|
|
|
collapsed: boolean;
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-25 15:35:46 +01:00
|
|
|
const SectionContainer = styled.aside`
|
2020-03-11 15:47:40 +01:00
|
|
|
position: sticky;
|
|
|
|
|
position: -webkit-sticky; /* Safari */
|
|
|
|
|
top: 2rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const Icon = styled.i<CollapsedProps>`
|
|
|
|
|
padding-left: ${(props: CollapsedProps) => (props.collapsed ? "0" : "0.5rem")};
|
|
|
|
|
padding-right: ${(props: CollapsedProps) => (props.collapsed ? "0" : "0.4rem")};
|
|
|
|
|
height: 1.5rem;
|
|
|
|
|
font-size: 24px;
|
|
|
|
|
margin-top: -0.75rem;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const MenuLabel = styled.p<CollapsedProps>`
|
|
|
|
|
height: 3.2rem;
|
|
|
|
|
display: flex;
|
|
|
|
|
align-items: center;
|
|
|
|
|
justify-content: ${(props: CollapsedProps) => (props.collapsed ? "center" : "inherit")};
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
`;
|
|
|
|
|
|
|
|
|
|
const SecondaryNavigation: FC<Props> = ({ label, children, collapsed, onCollapse }) => {
|
|
|
|
|
const location = useLocation();
|
2020-03-30 15:04:00 +02:00
|
|
|
const binder = useBinder();
|
2020-03-11 15:47:40 +01:00
|
|
|
const menuContext = useContext(MenuContext);
|
|
|
|
|
|
2020-03-30 15:04:00 +02:00
|
|
|
const subNavActive = isSubNavigationActive(binder, children, location.pathname);
|
2020-03-11 15:47:40 +01:00
|
|
|
const isCollapsed = collapsed && !subNavActive;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (isMenuCollapsed()) {
|
|
|
|
|
menuContext.setMenuCollapsed(!subNavActive);
|
2020-03-11 13:25:53 +01:00
|
|
|
}
|
2020-03-11 15:47:40 +01:00
|
|
|
}, [subNavActive]);
|
|
|
|
|
|
|
|
|
|
const childrenWithProps = React.Children.map(children, (child: ReactElement) =>
|
2020-03-30 15:04:00 +02:00
|
|
|
React.cloneElement(child, {
|
|
|
|
|
collapsed: isCollapsed,
|
|
|
|
|
propTransformer: (props: object) => {
|
|
|
|
|
const np = {
|
|
|
|
|
...props,
|
|
|
|
|
collapsed: isCollapsed
|
|
|
|
|
};
|
|
|
|
|
return np;
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-03-11 15:47:40 +01:00
|
|
|
);
|
|
|
|
|
const arrowIcon = isCollapsed ? <i className="fas fa-caret-down" /> : <i className="fas fa-caret-right" />;
|
|
|
|
|
|
|
|
|
|
return (
|
2020-03-25 15:35:46 +01:00
|
|
|
<SectionContainer className="menu">
|
2020-03-11 15:47:40 +01:00
|
|
|
<div>
|
|
|
|
|
<MenuLabel
|
|
|
|
|
className="menu-label"
|
|
|
|
|
collapsed={isCollapsed}
|
|
|
|
|
onClick={onCollapse && !subNavActive ? () => onCollapse(!isCollapsed) : undefined}
|
|
|
|
|
>
|
|
|
|
|
{onCollapse && !subNavActive && (
|
|
|
|
|
<Icon color="info" className="is-medium" collapsed={isCollapsed}>
|
|
|
|
|
{arrowIcon}
|
|
|
|
|
</Icon>
|
|
|
|
|
)}
|
|
|
|
|
{isCollapsed ? "" : label}
|
|
|
|
|
</MenuLabel>
|
|
|
|
|
<ul className="menu-list">{childrenWithProps}</ul>
|
|
|
|
|
</div>
|
|
|
|
|
</SectionContainer>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createParentPath = (to: string) => {
|
|
|
|
|
const parents = to.split("/");
|
|
|
|
|
parents.splice(-1, 1);
|
|
|
|
|
return parents.join("/");
|
|
|
|
|
};
|
|
|
|
|
|
2020-03-30 15:04:00 +02:00
|
|
|
const isSubNavigationActive = (binder: Binder, children: ReactNode, url: string): boolean => {
|
2020-03-11 15:47:40 +01:00
|
|
|
const childArray = React.Children.toArray(children);
|
|
|
|
|
const match = childArray
|
2020-03-30 15:04:00 +02:00
|
|
|
.filter(React.isValidElement)
|
|
|
|
|
.flatMap(child => {
|
2020-03-11 15:47:40 +01:00
|
|
|
// @ts-ignore
|
2020-03-30 15:04:00 +02:00
|
|
|
if (child.type.name === ExtensionPoint.name) {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
return binder.getExtensions(child.props.name, child.props.props);
|
|
|
|
|
}
|
|
|
|
|
return [child];
|
|
|
|
|
})
|
|
|
|
|
.filter(child => {
|
2020-03-11 15:47:40 +01:00
|
|
|
return child.type.name === SubNavigation.name;
|
|
|
|
|
})
|
|
|
|
|
.map(child => {
|
|
|
|
|
return child.props;
|
|
|
|
|
})
|
|
|
|
|
.find(props => {
|
|
|
|
|
const path = createParentPath(props.to);
|
|
|
|
|
const matches = matchPath(url, {
|
|
|
|
|
path,
|
|
|
|
|
exact: props.activeOnlyWhenExact as boolean
|
|
|
|
|
});
|
|
|
|
|
return matches != null;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return match != null;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default SecondaryNavigation;
|