2019-10-20 16:59:02 +02:00
|
|
|
import React from "react";
|
|
|
|
|
import { binder } from "@scm-manager/ui-extensions";
|
|
|
|
|
import { NavLink } from "../navigation";
|
|
|
|
|
import { Route } from "react-router-dom";
|
2019-10-23 15:47:08 +02:00
|
|
|
import { WithTranslation, withTranslation } from "react-i18next";
|
2019-10-20 16:59:02 +02:00
|
|
|
import { Repository, Links, Link } from "@scm-manager/ui-types";
|
|
|
|
|
|
|
|
|
|
type GlobalRouteProps = {
|
|
|
|
|
url: string;
|
|
|
|
|
links: Links;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
type RepositoryRouteProps = {
|
|
|
|
|
url: string;
|
|
|
|
|
repository: Repository;
|
|
|
|
|
};
|
2019-10-19 16:38:07 +02:00
|
|
|
|
2019-10-23 15:47:08 +02:00
|
|
|
type RepositoryNavProps = WithTranslation & { url: string };
|
|
|
|
|
|
2019-10-19 16:38:07 +02:00
|
|
|
class ConfigurationBinder {
|
2019-10-21 10:57:56 +02:00
|
|
|
i18nNamespace = "plugins";
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
navLink(to: string, labelI18nKey: string, t: any) {
|
|
|
|
|
return <NavLink to={to} label={t(labelI18nKey)} />;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
route(path: string, Component: any) {
|
|
|
|
|
return <Route path={path} render={() => Component} exact />;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 10:57:56 +02:00
|
|
|
bindGlobal(to: string, labelI18nKey: string, linkName: string, ConfigurationComponent: any) {
|
2019-10-19 16:38:07 +02:00
|
|
|
// create predicate based on the link name of the index resource
|
|
|
|
|
// if the linkname is not available, the navigation link and the route are not bound to the extension points
|
2019-10-20 16:59:02 +02:00
|
|
|
const configPredicate = (props: any) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
return props.links && props.links[linkName];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// create NavigationLink with translated label
|
2019-10-23 15:47:08 +02:00
|
|
|
const ConfigNavLink = withTranslation(this.i18nNamespace)(({ t }) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
return this.navLink("/admin/settings" + to, labelI18nKey, t);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// bind navigation link to extension point
|
2020-02-24 15:02:03 +01:00
|
|
|
binder.bind("admin.setting", ConfigNavLink, configPredicate, labelI18nKey);
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
// route for global configuration, passes the link from the index resource to component
|
2019-10-21 10:57:56 +02:00
|
|
|
const ConfigRoute = ({ url, links, ...additionalProps }: GlobalRouteProps) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
const link = links[linkName];
|
|
|
|
|
if (link) {
|
|
|
|
|
return this.route(
|
|
|
|
|
url + "/settings" + to,
|
2019-10-21 10:57:56 +02:00
|
|
|
<ConfigurationComponent link={(link as Link).href} {...additionalProps} />
|
2019-10-20 16:59:02 +02:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// bind config route to extension point
|
2019-10-20 16:59:02 +02:00
|
|
|
binder.bind("admin.route", ConfigRoute, configPredicate);
|
2019-10-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 10:57:56 +02:00
|
|
|
bindRepository(to: string, labelI18nKey: string, linkName: string, RepositoryComponent: any) {
|
2019-10-19 16:38:07 +02:00
|
|
|
// create predicate based on the link name of the current repository route
|
|
|
|
|
// if the linkname is not available, the navigation link and the route are not bound to the extension points
|
2019-10-20 16:59:02 +02:00
|
|
|
const repoPredicate = (props: any) => {
|
2019-10-21 10:57:56 +02:00
|
|
|
return props.repository && props.repository._links && props.repository._links[linkName];
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// create NavigationLink with translated label
|
2019-10-23 15:47:08 +02:00
|
|
|
const RepoNavLink = withTranslation(this.i18nNamespace)(({ t, url }: RepositoryNavProps) => {
|
2019-10-19 16:38:07 +02:00
|
|
|
return this.navLink(url + to, labelI18nKey, t);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// bind navigation link to extension point
|
2019-10-20 16:59:02 +02:00
|
|
|
binder.bind("repository.navigation", RepoNavLink, repoPredicate);
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
// route for global configuration, passes the current repository to component
|
2019-10-21 10:57:56 +02:00
|
|
|
const RepoRoute = ({ url, repository, ...additionalProps }: RepositoryRouteProps) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
const link = repository._links[linkName];
|
|
|
|
|
if (link) {
|
|
|
|
|
return this.route(
|
|
|
|
|
url + to,
|
2019-10-21 10:57:56 +02:00
|
|
|
<RepositoryComponent repository={repository} link={(link as Link).href} {...additionalProps} />
|
2019-10-20 16:59:02 +02:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// bind config route to extension point
|
2019-10-20 16:59:02 +02:00
|
|
|
binder.bind("repository.route", RepoRoute, repoPredicate);
|
2019-10-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 10:57:56 +02:00
|
|
|
bindRepositorySetting(to: string, labelI18nKey: string, linkName: string, RepositoryComponent: any) {
|
2019-10-19 16:38:07 +02:00
|
|
|
// create predicate based on the link name of the current repository route
|
|
|
|
|
// if the linkname is not available, the navigation link and the route are not bound to the extension points
|
2019-10-20 16:59:02 +02:00
|
|
|
const repoPredicate = (props: any) => {
|
2019-10-21 10:57:56 +02:00
|
|
|
return props.repository && props.repository._links && props.repository._links[linkName];
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// create NavigationLink with translated label
|
2019-10-23 15:47:08 +02:00
|
|
|
const RepoNavLink = withTranslation(this.i18nNamespace)(({ t, url }: RepositoryNavProps) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
return this.navLink(url + "/settings" + to, labelI18nKey, t);
|
2019-10-19 16:38:07 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// bind navigation link to extension point
|
2019-10-20 16:59:02 +02:00
|
|
|
binder.bind("repository.setting", RepoNavLink, repoPredicate);
|
2019-10-19 16:38:07 +02:00
|
|
|
|
|
|
|
|
// route for global configuration, passes the current repository to component
|
2019-10-21 10:57:56 +02:00
|
|
|
const RepoRoute = ({ url, repository, ...additionalProps }: RepositoryRouteProps) => {
|
2019-10-20 16:59:02 +02:00
|
|
|
const link = repository._links[linkName];
|
|
|
|
|
if (link) {
|
|
|
|
|
return this.route(
|
|
|
|
|
url + "/settings" + to,
|
2019-10-21 10:57:56 +02:00
|
|
|
<RepositoryComponent repository={repository} link={(link as Link).href} {...additionalProps} />
|
2019-10-20 16:59:02 +02:00
|
|
|
);
|
|
|
|
|
}
|
2019-10-19 16:38:07 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// bind config route to extension point
|
2019-10-20 16:59:02 +02:00
|
|
|
binder.bind("repository.route", RepoRoute, repoPredicate);
|
2019-10-19 16:38:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default new ConfigurationBinder();
|