thingsboard/ui-ngx/src/app/modules/home/pages/rulechain/rulechain-routing.module.ts

224 lines
6.6 KiB
TypeScript
Raw Normal View History

///
2023-01-31 10:43:56 +02:00
/// Copyright © 2016-2023 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
2020-08-17 16:11:01 +03:00
import { Inject, Injectable, NgModule, Optional } from '@angular/core';
import {
ActivatedRouteSnapshot,
CanActivate,
Resolve,
Router,
RouterModule,
RouterStateSnapshot,
Routes,
UrlTree
} from '@angular/router';
2020-01-14 16:57:42 +02:00
import { EntitiesTableComponent } from '../../components/entity/entities-table.component';
import { Authority } from '@shared/models/authority.enum';
import { RuleChainsTableConfigResolver } from '@modules/home/pages/rulechain/rulechains-table-config.resolver';
2021-01-05 11:37:05 +02:00
import { from, Observable } from 'rxjs';
2019-11-15 16:12:24 +02:00
import { BreadCrumbConfig, BreadCrumbLabelFunction } from '@shared/components/breadcrumb';
import {
RuleChainMetaData,
2021-01-13 18:43:49 +02:00
RuleChain, RuleChainType
} from '@shared/models/rule-chain.models';
2019-11-15 16:12:24 +02:00
import { RuleChainService } from '@core/http/rule-chain.service';
import { RuleChainPageComponent } from '@home/pages/rulechain/rulechain-page.component';
2019-11-22 17:58:13 +02:00
import { RuleNodeComponentDescriptor } from '@shared/models/rule-node.models';
import { ConfirmOnExitGuard } from '@core/guards/confirm-on-exit.guard';
import { ItemBufferService } from '@core/public-api';
2020-08-17 16:11:01 +03:00
import { MODULES_MAP } from '@shared/public-api';
2021-12-06 12:54:48 +02:00
import { IModulesMap } from '@modules/common/modules-map.models';
2019-12-23 14:36:44 +02:00
2019-11-15 16:12:24 +02:00
@Injectable()
export class RuleChainResolver implements Resolve<RuleChain> {
constructor(private ruleChainService: RuleChainService) {
}
resolve(route: ActivatedRouteSnapshot): Observable<RuleChain> {
const ruleChainId = route.params.ruleChainId;
return this.ruleChainService.getRuleChain(ruleChainId);
}
}
2019-11-22 17:58:13 +02:00
@Injectable()
export class RuleChainMetaDataResolver implements Resolve<RuleChainMetaData> {
2019-11-22 17:58:13 +02:00
constructor(private ruleChainService: RuleChainService) {
}
resolve(route: ActivatedRouteSnapshot): Observable<RuleChainMetaData> {
2019-11-22 17:58:13 +02:00
const ruleChainId = route.params.ruleChainId;
return this.ruleChainService.getRuleChainMetadata(ruleChainId);
2019-11-22 17:58:13 +02:00
}
}
@Injectable()
export class RuleNodeComponentsResolver implements Resolve<Array<RuleNodeComponentDescriptor>> {
2020-08-17 16:11:01 +03:00
constructor(private ruleChainService: RuleChainService,
2021-12-06 12:54:48 +02:00
@Optional() @Inject(MODULES_MAP) private modulesMap: IModulesMap) {
2019-11-22 17:58:13 +02:00
}
resolve(route: ActivatedRouteSnapshot): Observable<Array<RuleNodeComponentDescriptor>> {
return this.ruleChainService.getRuleNodeComponents(this.modulesMap, route.data.ruleChainType);
2019-11-22 17:58:13 +02:00
}
}
2021-01-05 11:37:05 +02:00
@Injectable()
export class TooltipsterResolver implements Resolve<any> {
constructor() {
}
resolve(route: ActivatedRouteSnapshot): Observable<any> {
return from(import('tooltipster'));
}
}
@Injectable()
export class RuleChainImportGuard implements CanActivate {
constructor(private itembuffer: ItemBufferService,
private router: Router) {
}
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot):
Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
if (this.itembuffer.hasRuleChainImport()) {
return true;
} else {
return this.router.parseUrl('ruleChains');
}
}
}
2020-04-07 17:06:04 +03:00
export const ruleChainBreadcumbLabelFunction: BreadCrumbLabelFunction<RuleChainPageComponent>
= ((route, translate, component) => {
2019-11-15 16:12:24 +02:00
let label: string = component.ruleChain.name;
if (component.ruleChain.root) {
label += ` (${translate.instant('rulechain.root')})`;
}
return label;
});
2020-04-07 17:06:04 +03:00
export const importRuleChainBreadcumbLabelFunction: BreadCrumbLabelFunction<RuleChainPageComponent> =
((route, translate, component) => {
2019-11-22 17:58:13 +02:00
return `${translate.instant('rulechain.import')}: ${component.ruleChain.name}`;
});
2023-02-13 19:09:05 +02:00
export const ruleChainsRoutes: Routes = [
{
path: 'ruleChains',
data: {
breadcrumb: {
label: 'rulechain.rulechains',
icon: 'settings_ethernet'
}
},
children: [
{
path: '',
component: EntitiesTableComponent,
data: {
auth: [Authority.TENANT_ADMIN],
title: 'rulechain.rulechains',
ruleChainsType: 'tenant'
},
resolve: {
entitiesTableConfig: RuleChainsTableConfigResolver
}
2019-11-15 16:12:24 +02:00
},
{
path: ':ruleChainId',
component: RuleChainPageComponent,
canDeactivate: [ConfirmOnExitGuard],
2019-11-15 16:12:24 +02:00
data: {
breadcrumb: {
labelFunction: ruleChainBreadcumbLabelFunction,
2019-11-15 16:12:24 +02:00
icon: 'settings_ethernet'
} as BreadCrumbConfig<RuleChainPageComponent>,
auth: [Authority.TENANT_ADMIN],
title: 'rulechain.rulechain',
import: false,
ruleChainType: RuleChainType.CORE
2019-11-15 16:12:24 +02:00
},
resolve: {
ruleChain: RuleChainResolver,
ruleChainMetaData: RuleChainMetaDataResolver,
2021-01-05 11:37:05 +02:00
ruleNodeComponents: RuleNodeComponentsResolver,
tooltipster: TooltipsterResolver
}
2019-11-22 17:58:13 +02:00
},
{
path: 'ruleChain/import',
component: RuleChainPageComponent,
canActivate: [RuleChainImportGuard],
canDeactivate: [ConfirmOnExitGuard],
2019-11-22 17:58:13 +02:00
data: {
breadcrumb: {
labelFunction: importRuleChainBreadcumbLabelFunction,
2019-11-22 17:58:13 +02:00
icon: 'settings_ethernet'
} as BreadCrumbConfig<RuleChainPageComponent>,
auth: [Authority.TENANT_ADMIN],
title: 'rulechain.rulechain',
import: true,
ruleChainType: RuleChainType.CORE
2019-11-22 17:58:13 +02:00
},
resolve: {
2021-01-05 11:37:05 +02:00
ruleNodeComponents: RuleNodeComponentsResolver,
tooltipster: TooltipsterResolver
}
}
]
}
];
2023-02-13 19:09:05 +02:00
const routes: Routes = [
{
path: 'ruleChains',
pathMatch: 'full',
redirectTo: '/features/ruleChains'
},
{
path: 'ruleChains/:ruleChainId',
pathMatch: 'full',
redirectTo: '/features/ruleChains/:ruleChainId'
},
{
path: 'ruleChains/ruleChain/import',
pathMatch: 'full',
redirectTo: '/features/ruleChains/ruleChain/import'
}
];
2019-12-23 14:36:44 +02:00
// @dynamic
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
providers: [
2019-11-15 16:12:24 +02:00
RuleChainsTableConfigResolver,
2019-11-22 17:58:13 +02:00
RuleChainResolver,
RuleChainMetaDataResolver,
2019-11-22 17:58:13 +02:00
RuleNodeComponentsResolver,
2021-01-05 11:37:05 +02:00
TooltipsterResolver,
RuleChainImportGuard
]
})
export class RuleChainRoutingModule { }