Files
smartguide/webapp/src/app/modules/hierarchy/hierarchy.component.ts
T

162 lines
5.5 KiB
TypeScript

import {
Component,
computed,
DestroyRef,
inject,
input,
OnInit,
signal,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { ActivatedRoute, Router } from '@angular/router';
import { HierarchyChartComponent } from './hierarchy-chart/hierarchy-chart.component';
import { OrgminerService } from '../../api/api/orgminer.service';
import { OrgminerContentComponent } from './orgminer-content/orgminer-content.component';
import { OrgminerContent } from '../../api/model/orgminerContent';
import { ClassifiedPagesComponent } from './classified-pages/classified-pages.component';
import { CategoryBucket } from '../../api/model/categoryBucket';
import { MatExpansionModule } from '@angular/material/expansion';
import { MatIconModule } from '@angular/material/icon';
const ORGANIZATION_URL_PARAM = 'orgUrl';
const ORGANIZATION_NAME_PARAM = 'orgName';
const ORGMINER_EXPANDED_PARAM = 'orgExpanded';
const ORGMINER_SELECTION_CATEGORY_PARAM = 'orgSelectionCategory';
const ORGMINER_SELECTION_INDEX_PARAM = 'orgSelectionIndex';
const CLASSIFIED_CATEGORY_PARAM = 'classifiedCategory';
const CLASSIFIED_PAGE_PARAM = 'classifiedPage';
const HIERARCHY_PANEL_PARAM = 'hierarchyExpanded';
const ORGANIZATION_PANEL_PARAM = 'organizationExpanded';
const CLASSIFIED_PAGES_PANEL_PARAM = 'classifiedPagesExpanded';
@Component({
selector: 'app-hierarchy',
imports: [
HierarchyChartComponent,
OrgminerContentComponent,
ClassifiedPagesComponent,
MatExpansionModule,
MatIconModule,
],
templateUrl: './hierarchy.component.html',
styleUrl: './hierarchy.component.scss',
})
export class HierarchyComponent implements OnInit {
hierarchyData = input.required();
hierarchyExpanded = signal(true);
organizationExpanded = signal(true);
classifiedPagesExpanded = signal(false);
orgminerContent = signal<OrgminerContent | null>(null);
orgName = signal<string | null>(null);
classifiedBuckets = signal<CategoryBucket[] | null>(null);
orgminerService = inject(OrgminerService);
private readonly router = inject(Router);
private readonly route = inject(ActivatedRoute);
private readonly destroyRef = inject(DestroyRef);
private isOwnNavigation = false;
totalPagesClassified = computed(
() => this.classifiedBuckets()?.reduce((sum, b) => sum + b.count, 0) ?? 0,
);
ngOnInit(): void {
this.route.queryParamMap
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((params) => {
if (this.isOwnNavigation) {
this.isOwnNavigation = false;
return;
}
const organizationUrl = params.get(ORGANIZATION_URL_PARAM);
this.hierarchyExpanded.set(
this.readBooleanParam(params.get(HIERARCHY_PANEL_PARAM), true),
);
this.organizationExpanded.set(
this.readBooleanParam(params.get(ORGANIZATION_PANEL_PARAM), true),
);
this.classifiedPagesExpanded.set(
this.readBooleanParam(
params.get(CLASSIFIED_PAGES_PANEL_PARAM),
false,
),
);
if (organizationUrl) {
this.loadOrganization({
name: params.get(ORGANIZATION_NAME_PARAM) ?? 'Organisation',
url: organizationUrl,
});
}
});
}
onHierarchyExpandedChange(expanded: boolean): void {
this.hierarchyExpanded.set(expanded);
this.syncPanelState({ [HIERARCHY_PANEL_PARAM]: expanded });
}
onOrganizationExpandedChange(expanded: boolean): void {
this.organizationExpanded.set(expanded);
this.syncPanelState({ [ORGANIZATION_PANEL_PARAM]: expanded });
}
onClassifiedPagesExpandedChange(expanded: boolean): void {
this.classifiedPagesExpanded.set(expanded);
this.syncPanelState({ [CLASSIFIED_PAGES_PANEL_PARAM]: expanded });
}
analyzeOrg(org: { name: string; url: string }) {
this.hierarchyExpanded.set(false);
this.organizationExpanded.set(true);
this.isOwnNavigation = true;
this.router
.navigate([], {
relativeTo: this.route,
queryParams: {
[ORGANIZATION_URL_PARAM]: org.url,
[ORGANIZATION_NAME_PARAM]: org.name,
[ORGMINER_EXPANDED_PARAM]: null,
[ORGMINER_SELECTION_CATEGORY_PARAM]: null,
[ORGMINER_SELECTION_INDEX_PARAM]: null,
[CLASSIFIED_CATEGORY_PARAM]: null,
[CLASSIFIED_PAGE_PARAM]: null,
[HIERARCHY_PANEL_PARAM]: false,
[ORGANIZATION_PANEL_PARAM]: true,
},
queryParamsHandling: 'merge',
replaceUrl: true,
})
.then((navigated) => {
if (!navigated) this.isOwnNavigation = false;
});
this.loadOrganization(org);
}
private syncPanelState(queryParams: Record<string, boolean>): void {
this.isOwnNavigation = true;
this.router
.navigate([], {
relativeTo: this.route,
queryParams,
queryParamsHandling: 'merge',
replaceUrl: true,
})
.then((navigated) => {
if (!navigated) this.isOwnNavigation = false;
});
}
private readBooleanParam(value: string | null, fallback: boolean): boolean {
return value === null ? fallback : value === 'true';
}
private loadOrganization(org: { name: string; url: string }) {
console.log('Analyze organization event triggered', org.url);
this.orgName.set(org.name);
this.orgminerService.getOrgminerContent(org.url).subscribe((data) => {
console.log('Orgminer content:', data);
this.orgminerContent.set(data);
});
this.orgminerService.getClassifiedPagesAggs(org.url).subscribe((data) => {
this.classifiedBuckets.set(data);
});
}
}