All files / componenets/md-reference-tree md-reference-tree.component.ts

100% Statements 17/17
100% Branches 4/4
100% Functions 6/6
100% Lines 16/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66                                                    1x 2x   2x   2x   8x               2x 2x 2x 2x 2x 2x   2x 2x       2x         4x       3x      
import { ChangeDetectionStrategy, Component, inject } from '@angular/core';
import { MatTreeNestedDataSource } from '@angular/material/tree';
import { logMethod } from '@app/client-util-decorators';
import { Store } from '@ngrx/store';
 
import { DOCUMENTATION_ENVIRONMENT } from '../../interfaces/environment.interface';
import { mdFilesAction } from '../../modules/md-files/md-files.actions';
import { IMdFilesState } from '../../modules/md-files/md-files.interface';
 
/**
 * Nodes data with nested structure.
 * Each node has a name and an optional list of children.
 */
interface IMarkdownReferenceNode {
  name: string;
  filePath?: string;
  children?: IMarkdownReferenceNode[];
}
 
@Component({
  selector: 'app-documentation-md-reference-tree',
  templateUrl: './md-reference-tree.component.html',
  styleUrls: ['./md-reference-tree.component.scss'],
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: false,
})
export class AppDocMarkdownReferenceTreeComponent {
  private readonly store = inject(Store<IMdFilesState>);
 
  private readonly env = inject(DOCUMENTATION_ENVIRONMENT);
 
  public readonly dataSource = new MatTreeNestedDataSource<IMarkdownReferenceNode>();
 
  public readonly childrenAccessor = (dataNode: IMarkdownReferenceNode) => dataNode.children ?? [];
 
  @logMethod()
  private treeData() {
    const mdFilePaths = [
      '/README.md', // the root readme in not present in the autogenerated array and should be added here
      ...this.env.mdFilePaths,
    ];
    const basePath = 'md/';
    const treeNodes = mdFilePaths.map(item => {
      const name = item.replace(/\/[A-Za-z]+\.md/, '/');
      const filePath = `${basePath}${item}`;
      const children: IMarkdownReferenceNode[] = [{ name: item.replace(/^.*\/(?=[A-Za-z]+\.md$)/, ''), filePath }];
      return { name, children } as IMarkdownReferenceNode;
    });
    this.showReadme(`md${mdFilePaths[0]}`);
    return treeNodes;
  }
 
  constructor() {
    this.dataSource.data = this.treeData();
  }
 
  @logMethod()
  public hasChild(index: number, node: IMarkdownReferenceNode) {
    return typeof node.children !== 'undefined' && node.children.length > 0;
  }
 
  public showReadme(filePath: string): void {
    this.store.dispatch(mdFilesAction.showReadme({ filePath }));
  }
}