2023-02-09 19:13:44 +02:00
|
|
|
///
|
2025-02-25 09:39:16 +02:00
|
|
|
/// Copyright © 2016-2025 The Thingsboard Authors
|
2023-02-09 19:13:44 +02:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2023-03-24 19:24:31 +02:00
|
|
|
import { Component, ComponentRef, OnInit, Type, ViewChild } from '@angular/core';
|
2023-02-09 19:13:44 +02:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
2023-03-16 13:07:10 +02:00
|
|
|
import { ActivatedRoute, NavigationEnd, Router } from '@angular/router';
|
2023-02-09 19:13:44 +02:00
|
|
|
import { MenuService } from '@core/services/menu.service';
|
2023-07-28 16:58:12 +03:00
|
|
|
import { distinctUntilChanged, filter, map, mergeMap, startWith, take } from 'rxjs/operators';
|
|
|
|
|
import { merge, Observable } from 'rxjs';
|
2023-02-09 19:13:44 +02:00
|
|
|
import { MenuSection } from '@core/services/menu.models';
|
|
|
|
|
import { ActiveComponentService } from '@core/services/active-component.service';
|
2023-03-24 19:24:31 +02:00
|
|
|
import { TbAnchorComponent } from '@shared/components/tb-anchor.component';
|
2024-10-22 12:14:14 +03:00
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
2023-02-09 19:13:44 +02:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-router-tabs',
|
|
|
|
|
templateUrl: './router-tabs.component.html',
|
2023-03-16 13:07:10 +02:00
|
|
|
styleUrls: ['./router-tabs.component.scss']
|
2023-02-09 19:13:44 +02:00
|
|
|
})
|
2023-03-24 19:24:31 +02:00
|
|
|
export class RouterTabsComponent extends PageComponent implements OnInit {
|
|
|
|
|
|
|
|
|
|
@ViewChild('tabsHeaderComponent', {static: true}) tabsHeaderComponentAnchor: TbAnchorComponent;
|
|
|
|
|
|
|
|
|
|
tabsHeaderComponentRef: ComponentRef<any>;
|
2023-02-09 19:13:44 +02:00
|
|
|
|
2023-03-16 13:07:10 +02:00
|
|
|
hideCurrentTabs = false;
|
|
|
|
|
|
2023-07-31 12:25:48 +03:00
|
|
|
replaceUrl = false;
|
|
|
|
|
|
2023-07-28 16:58:12 +03:00
|
|
|
tabs$: Observable<Array<MenuSection>>;
|
2023-02-09 19:13:44 +02:00
|
|
|
|
|
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
private activatedRoute: ActivatedRoute,
|
|
|
|
|
public router: Router,
|
|
|
|
|
private menuService: MenuService,
|
2023-03-24 19:24:31 +02:00
|
|
|
private activeComponentService: ActiveComponentService) {
|
2023-02-09 19:13:44 +02:00
|
|
|
super(store);
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 19:24:31 +02:00
|
|
|
ngOnInit() {
|
2023-07-28 16:58:12 +03:00
|
|
|
if (this.activatedRoute.snapshot.data.useChildrenRoutesForTabs) {
|
|
|
|
|
this.tabs$ = this.router.events.pipe(
|
|
|
|
|
filter((event) => event instanceof NavigationEnd),
|
|
|
|
|
startWith(''),
|
|
|
|
|
map(() => this.buildTabsForRoutes(this.activatedRoute))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
this.tabs$ = merge(this.menuService.menuSections(),
|
|
|
|
|
this.router.events.pipe(
|
|
|
|
|
filter((event) => event instanceof NavigationEnd ),
|
|
|
|
|
distinctUntilChanged())
|
|
|
|
|
).pipe(
|
|
|
|
|
mergeMap(() => this.menuService.menuSections().pipe(take(1))),
|
|
|
|
|
map((sections) => this.buildTabs(this.activatedRoute, sections))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-31 12:25:48 +03:00
|
|
|
if (this.activatedRoute.snapshot.data.replaceUrl) {
|
|
|
|
|
this.replaceUrl = true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-24 19:24:31 +02:00
|
|
|
this.activatedRoute.data.subscribe(
|
|
|
|
|
(data) => this.buildTabsHeaderComponent(data)
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 19:13:44 +02:00
|
|
|
activeComponentChanged(activeComponent: any) {
|
|
|
|
|
this.activeComponentService.setCurrentActiveComponent(activeComponent);
|
2023-03-16 13:07:10 +02:00
|
|
|
let snapshot = this.router.routerState.snapshot.root;
|
|
|
|
|
this.hideCurrentTabs = false;
|
|
|
|
|
let found = false;
|
|
|
|
|
while (snapshot.children.length) {
|
|
|
|
|
if (snapshot.component && snapshot.component === RouterTabsComponent) {
|
|
|
|
|
if (this.activatedRoute.snapshot === snapshot) {
|
|
|
|
|
found = true;
|
|
|
|
|
} else if (found) {
|
|
|
|
|
this.hideCurrentTabs = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
snapshot = snapshot.children[0];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 16:58:12 +03:00
|
|
|
private getSectionPath(activatedRoute: ActivatedRoute): string {
|
|
|
|
|
return '/' + activatedRoute.pathFromRoot.map(r => r.snapshot.url)
|
2023-03-16 13:07:10 +02:00
|
|
|
.filter(f => !!f[0]).map(f => f.map(f1 => f1.path).join('/')).join('/');
|
2023-07-28 16:58:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private buildTabs(activatedRoute: ActivatedRoute, sections: MenuSection[]): Array<MenuSection> {
|
|
|
|
|
const sectionPath = this.getSectionPath(activatedRoute);
|
2023-03-16 13:07:10 +02:00
|
|
|
const found = this.findRootSection(sections, sectionPath);
|
2023-03-17 16:22:55 +02:00
|
|
|
if (found) {
|
|
|
|
|
const rootPath = sectionPath.substring(0, sectionPath.length - found.path.length);
|
|
|
|
|
const isRoot = rootPath === '';
|
2024-08-20 13:11:57 +03:00
|
|
|
const tabs: Array<MenuSection> = found ? found.pages.filter(page => !page.rootOnly || isRoot) : [];
|
2023-03-17 16:22:55 +02:00
|
|
|
return tabs.map((tab) => ({...tab, path: rootPath + tab.path}));
|
2023-07-28 16:58:12 +03:00
|
|
|
}
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private buildTabsForRoutes(activatedRoute: ActivatedRoute): Array<MenuSection> {
|
|
|
|
|
const sectionPath = this.getSectionPath(activatedRoute);
|
2024-10-22 12:14:14 +03:00
|
|
|
const authority = getCurrentAuthUser(this.store).authority;
|
|
|
|
|
const children = activatedRoute.routeConfig.children.filter(page => {
|
|
|
|
|
if (page.path !== '') {
|
|
|
|
|
if (page.data?.auth) {
|
|
|
|
|
return page.data?.auth.includes(authority);
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
if (children.length) {
|
|
|
|
|
return children.map(tab => ({
|
2023-07-24 17:41:12 +03:00
|
|
|
id: tab.component.name,
|
|
|
|
|
type: 'link',
|
|
|
|
|
name: tab.data?.breadcrumb?.label ?? '',
|
|
|
|
|
icon: tab.data?.breadcrumb?.icon ?? '',
|
|
|
|
|
path: `${sectionPath}/${tab.path}`
|
|
|
|
|
}));
|
2023-03-17 16:22:55 +02:00
|
|
|
}
|
2023-07-28 16:58:12 +03:00
|
|
|
return [];
|
2023-03-16 13:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private findRootSection(sections: MenuSection[], sectionPath: string): MenuSection {
|
|
|
|
|
for (const section of sections) {
|
|
|
|
|
if (sectionPath.endsWith(section.path)) {
|
|
|
|
|
return section;
|
|
|
|
|
}
|
|
|
|
|
if (section.pages?.length) {
|
|
|
|
|
const found = this.findRootSection(section.pages, sectionPath);
|
|
|
|
|
if (found) {
|
|
|
|
|
return found;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
2023-02-09 19:13:44 +02:00
|
|
|
}
|
|
|
|
|
|
2023-03-24 19:24:31 +02:00
|
|
|
private buildTabsHeaderComponent(snapshotData: any) {
|
|
|
|
|
const viewContainerRef = this.tabsHeaderComponentAnchor.viewContainerRef;
|
|
|
|
|
if (this.tabsHeaderComponentRef) {
|
|
|
|
|
this.tabsHeaderComponentRef.destroy();
|
|
|
|
|
this.tabsHeaderComponentRef = null;
|
|
|
|
|
viewContainerRef.clear();
|
|
|
|
|
}
|
|
|
|
|
const tabsHeaderComponentType: Type<any> = snapshotData.routerTabsHeaderComponent;
|
|
|
|
|
if (tabsHeaderComponentType) {
|
|
|
|
|
this.tabsHeaderComponentRef = viewContainerRef.createComponent(tabsHeaderComponentType, {index: 0});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-09 19:13:44 +02:00
|
|
|
}
|