File

libs/common/frame/src/frame-parts/navigation/jump-navigation-menu.component.ts

Implements

OnInit AfterViewInit OnDestroy

Metadata

Index

Properties
Inputs
Outputs

Inputs

Type : JumpNavigationMenuConfiguration

Outputs

resizeEvent
Type : EventEmitter

Properties

containsChildren
Type : boolean
dataSource
Type : NxTreeFlatDataSource<JumpNavigationMenuEntry | FlatTreeNode>
Default value : new NxTreeFlatDataSource(this.treeControl, [] as JumpNavigationMenuEntry[])
sidebar
Type : ElementRef<HTMLElement>
Decorators :
@ViewChild('sidebar', {read: ElementRef})
treeControl
Type : NxFlatTreeControl<FlatTreeNode>
Default value : new NxFlatTreeControl()
import { TalyPageDataService } from '@allianz/taly-core';
import {
  JumpNavigationMenuConfiguration,
  JumpNavigationMenuEntry,
  JumpNavigationMenuLeafEntry
} from '@allianz/taly-core/schemas';
import { CommonModule } from '@angular/common';
import {
  Component,
  DestroyRef,
  ElementRef,
  EventEmitter,
  inject,
  Input,
  NgZone,
  OnDestroy,
  OnInit,
  Output,
  ViewChild,
  type AfterViewInit
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { RouterModule } from '@angular/router';
import { NxActionModule } from '@aposin/ng-aquila/action';
import { NxIconModule } from '@aposin/ng-aquila/icon';
import { NxSidebarModule } from '@aposin/ng-aquila/sidebar';
import {
  NxFlatTreeControl,
  NxFlatTreeNode,
  NxTreeFlatDataSource,
  NxTreeModule
} from '@aposin/ng-aquila/tree';

type FlatTreeNode = JumpNavigationMenuLeafEntry & NxFlatTreeNode;

@Component({
  selector: 'frame-jump-navigation-menu',
  templateUrl: './jump-navigation-menu.component.html',
  styleUrls: ['./jump-navigation-menu.component.scss'],
  imports: [CommonModule, RouterModule, NxSidebarModule, NxActionModule, NxTreeModule, NxIconModule]
})
export class JumpNavigationMenuComponent implements OnInit, AfterViewInit, OnDestroy {
  @Input({ required: true }) jumpNavigationMenu!: JumpNavigationMenuConfiguration;
  @Output() resizeEvent = new EventEmitter();
  @ViewChild('sidebar', { read: ElementRef }) sidebar!: ElementRef<HTMLElement>;

  private talyPageDataService = inject(TalyPageDataService);
  private destroyRef = inject(DestroyRef);
  private resizeObserver!: ResizeObserver;
  private ngZone = inject(NgZone);

  containsChildren!: boolean;
  treeControl: NxFlatTreeControl<FlatTreeNode> = new NxFlatTreeControl();
  dataSource: NxTreeFlatDataSource<JumpNavigationMenuEntry, FlatTreeNode> =
    new NxTreeFlatDataSource(this.treeControl, [] as JumpNavigationMenuEntry[]);

  ngOnInit() {
    this.containsChildren = this.jumpNavigationMenu.items.some((item) =>
      Boolean(item.children?.length)
    );

    this.dataSource.data = this.jumpNavigationMenu.items as JumpNavigationMenuEntry[];

    this.talyPageDataService.pageId$
      .pipe(takeUntilDestroyed(this.destroyRef))
      .subscribe((pageId) => {
        const nodeToExpand = this.treeControl.dataNodes.find((node) => {
          if (node.expandable) {
            return this.treeControl.getDescendants(node).some((child) => child.pageId === pageId);
          }

          return node.pageId === pageId;
        }) as FlatTreeNode | undefined;

        if (nodeToExpand) {
          this.treeControl.expand(nodeToExpand);
        }
      });
  }

  ngAfterViewInit() {
    this.listenToWidthChanges();
  }

  protected hasChild(_: number, node: NxFlatTreeNode) {
    return node.expandable;
  }

  private listenToWidthChanges(): void {
    this.ngZone.runOutsideAngular(() => {
      this.resizeObserver = new ResizeObserver((entries) => {
        const value = entries[0].contentRect.width;
        this.resizeEvent.next(value);
      });
    });

    this.resizeObserver.observe(this.sidebar.nativeElement);
  }

  ngOnDestroy(): void {
    this.resizeObserver.disconnect();
  }
}
@if (containsChildren) {
<nx-sidebar
  data-testid="sidebar"
  resizeable
  minWidth="140"
  maxWidth="288"
  resizeHandleAriaLabel="Toggle width of jump navigation menu"
  #sidebar
>
  <nx-tree [dataSource]="dataSource" [treeControl]="treeControl">
    <nx-tree-node *nxTreeNodeDef="let node">
      <a
        nxAction
        nxTreeNodePadding
        nxTreeNodeActionItem
        nxTreeNodePaddingOffset="20"
        [routerLink]="node.pageId"
        routerLinkActive="is-selected"
        title="{{ node.label }}"
      >
        @if (node.icon) { <nx-icon nxActionIcon [name]="node.icon"></nx-icon> }
        {{ node.label }}
      </a>
    </nx-tree-node>
    <nx-tree-node *nxTreeNodeDef="let node; when: hasChild">
      <button
        nxAction
        nxTreeNodeToggle
        nxTreeNodeActionItem
        nxTreeNodePadding
        nxTreeNodePaddingOffset="20"
        expandable
        [expanded]="treeControl.isExpanded(node)"
        title="{{ node.label }}"
        type="button"
      >
        @if (node.icon) { <nx-icon nxActionIcon [name]="node.icon"></nx-icon> } {{ node.label }}
      </button>
    </nx-tree-node>
  </nx-tree>
</nx-sidebar>
} @else {
<nx-sidebar
  data-testid="sidebar"
  resizeable
  minWidth="56"
  maxWidth="288"
  resizeHandleAriaLabel="Toggle width of jump navigation menu"
  #sidebar
>
  @for (node of this.dataSource.data; track node) {
  <a nxAction [routerLink]="node.pageId" routerLinkActive="is-selected" title="{{ node.label }}">
    @if (node.icon) {
    <nx-icon nxActionIcon [name]="node.icon" aria-hidden="true"></nx-icon> }
    {{ node.label }}
  </a>
  }
</nx-sidebar>
}

./jump-navigation-menu.component.scss

@use '../../../styles/navigation.scss' as *;

nx-sidebar {
  width: #{$navigation-width};
}
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""