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.
|
|
|
|
|
*/
|
2019-10-21 10:57:56 +02:00
|
|
|
import React, { ReactNode } from "react";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 16:59:02 +02:00
|
|
|
import PrimaryNavigationLink from "./PrimaryNavigationLink";
|
|
|
|
|
import { Links } from "@scm-manager/ui-types";
|
|
|
|
|
import { binder, ExtensionPoint } from "@scm-manager/ui-extensions";
|
2018-09-03 16:17:36 +02:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type Props = WithTranslation & {
|
2019-10-19 16:38:07 +02:00
|
|
|
links: Links;
|
2018-09-03 16:17:36 +02:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
type Appender = (to: string, match: string, label: string, linkName: string) => void;
|
|
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
class PrimaryNavigation extends React.Component<Props> {
|
2019-10-20 16:59:02 +02:00
|
|
|
createNavigationAppender = (navigationItems: ReactNode[]): Appender => {
|
2019-01-02 11:53:51 +01:00
|
|
|
const { t, links } = this.props;
|
|
|
|
|
|
|
|
|
|
return (to: string, match: string, label: string, linkName: string) => {
|
|
|
|
|
const link = links[linkName];
|
|
|
|
|
if (link) {
|
2019-10-21 10:57:56 +02:00
|
|
|
const navigationItem = <PrimaryNavigationLink to={to} match={match} label={t(label)} key={linkName} />;
|
2019-01-02 11:53:51 +01:00
|
|
|
navigationItems.push(navigationItem);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
appendLogout = (navigationItems: ReactNode[], append: Appender) => {
|
2019-01-02 11:53:51 +01:00
|
|
|
const { t, links } = this.props;
|
|
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
|
links,
|
2019-10-20 16:59:02 +02:00
|
|
|
label: t("primary-navigation.logout")
|
2019-01-02 11:53:51 +01:00
|
|
|
};
|
|
|
|
|
|
2019-10-20 16:59:02 +02:00
|
|
|
if (binder.hasExtension("primary-navigation.logout", props)) {
|
2019-01-02 12:28:52 +01:00
|
|
|
navigationItems.push(
|
2019-10-21 10:57:56 +02:00
|
|
|
<ExtensionPoint key="primary-navigation.logout" name="primary-navigation.logout" props={props} />
|
2019-01-02 12:28:52 +01:00
|
|
|
);
|
|
|
|
|
} else {
|
2019-10-20 16:59:02 +02:00
|
|
|
append("/logout", "/logout", "primary-navigation.logout", "logout");
|
2019-01-02 12:28:52 +01:00
|
|
|
}
|
2019-01-02 11:53:51 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
createNavigationItems = () => {
|
2019-10-20 16:59:02 +02:00
|
|
|
const navigationItems: ReactNode[] = [];
|
2019-02-01 17:22:10 +01:00
|
|
|
const { t, links } = this.props;
|
|
|
|
|
|
|
|
|
|
const props = {
|
|
|
|
|
links,
|
2019-10-20 16:59:02 +02:00
|
|
|
label: t("primary-navigation.first-menu")
|
2019-02-01 17:22:10 +01:00
|
|
|
};
|
2019-01-02 11:53:51 +01:00
|
|
|
|
|
|
|
|
const append = this.createNavigationAppender(navigationItems);
|
2019-10-20 16:59:02 +02:00
|
|
|
if (binder.hasExtension("primary-navigation.first-menu", props)) {
|
2019-02-01 17:22:10 +01:00
|
|
|
navigationItems.push(
|
2019-10-21 10:57:56 +02:00
|
|
|
<ExtensionPoint key="primary-navigation.first-menu" name="primary-navigation.first-menu" props={props} />
|
2019-02-01 17:22:10 +01:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-21 10:57:56 +02:00
|
|
|
append("/repos/", "/(repo|repos)", "primary-navigation.repositories", "repositories");
|
2019-10-20 16:59:02 +02:00
|
|
|
append("/users/", "/(user|users)", "primary-navigation.users", "users");
|
2019-10-21 10:57:56 +02:00
|
|
|
append("/groups/", "/(group|groups)", "primary-navigation.groups", "groups");
|
2019-10-20 16:59:02 +02:00
|
|
|
append("/admin", "/admin", "primary-navigation.admin", "config");
|
2019-01-02 11:53:51 +01:00
|
|
|
|
2019-01-07 10:10:06 +01:00
|
|
|
navigationItems.push(
|
|
|
|
|
<ExtensionPoint
|
2019-10-17 09:14:42 +02:00
|
|
|
key="primary-navigation"
|
2019-01-07 10:10:06 +01:00
|
|
|
name="primary-navigation"
|
|
|
|
|
renderAll={true}
|
2019-10-19 16:38:07 +02:00
|
|
|
props={{
|
2019-10-20 16:59:02 +02:00
|
|
|
links: this.props.links
|
2019-10-19 16:38:07 +02:00
|
|
|
}}
|
2019-10-20 16:59:02 +02:00
|
|
|
/>
|
2019-01-07 10:10:06 +01:00
|
|
|
);
|
|
|
|
|
|
2019-01-02 12:28:52 +01:00
|
|
|
this.appendLogout(navigationItems, append);
|
2019-01-02 11:53:51 +01:00
|
|
|
|
|
|
|
|
return navigationItems;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const navigationItems = this.createNavigationItems();
|
2018-10-05 13:48:21 +02:00
|
|
|
|
2018-09-03 16:17:36 +02:00
|
|
|
return (
|
|
|
|
|
<nav className="tabs is-boxed">
|
2019-04-17 12:05:44 +02:00
|
|
|
<ul>{navigationItems}</ul>
|
2018-09-03 16:17:36 +02:00
|
|
|
</nav>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
export default withTranslation("commons")(PrimaryNavigation);
|