|
| 1 | +import { Component, ElementRef, OnInit, ViewChild } from '@angular/core'; |
| 2 | +import { Router } from '@angular/router'; |
| 3 | + |
| 4 | +import { FolderService } from '@services/folder.service'; |
| 5 | + |
| 6 | +import { IBreadcrumb } from '@interfaces/breadcrumb.interface'; |
| 7 | + |
| 8 | +@Component({ |
| 9 | + selector: 'app-breadcrumb', |
| 10 | + templateUrl: './breadcrumb.component.html', |
| 11 | + styleUrls: ['./breadcrumb.component.scss'] |
| 12 | +}) |
| 13 | +export class BreadcrumbComponent implements OnInit { |
| 14 | + @ViewChild('dropdown') dropdown: ElementRef; |
| 15 | + private breadcrumb: IBreadcrumb[]; |
| 16 | + breadcrumbShown: IBreadcrumb[] = []; |
| 17 | + breadcrumbDropdown: IBreadcrumb[] = []; |
| 18 | + private rootFolder: IBreadcrumb; |
| 19 | + constructor( |
| 20 | + private folderService: FolderService, |
| 21 | + private router: Router, |
| 22 | + ) { } |
| 23 | + |
| 24 | + ngOnInit(): void { |
| 25 | + this.buildBreadcrumb(); |
| 26 | + this.router.events.subscribe(() => { |
| 27 | + this.buildBreadcrumb(); |
| 28 | + }); |
| 29 | + } |
| 30 | + |
| 31 | + buildBreadcrumb(): void { |
| 32 | + this.breadcrumb = this.folderService.breadcrumb || []; |
| 33 | + this.rootFolder = this.breadcrumb[0]; |
| 34 | + if (this.breadcrumb.length > 2) { |
| 35 | + this.breadcrumbShown = this.breadcrumb.slice(this.breadcrumb.length - 2); |
| 36 | + this.breadcrumbDropdown = this.breadcrumb.slice(0, this.breadcrumb.length - 2); |
| 37 | + } else { |
| 38 | + this.breadcrumbShown = this.breadcrumb; |
| 39 | + this.breadcrumbDropdown = []; |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + navigateToFolder(folderID: string): void { |
| 44 | + if (folderID === this.rootFolder._id) { |
| 45 | + this.router.navigate(['/']); |
| 46 | + } else { |
| 47 | + this.router.navigate(['/folder', folderID]); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + changeVisibility(): void { |
| 52 | + this.dropdown.nativeElement.classList.toggle('show'); |
| 53 | + } |
| 54 | + |
| 55 | +} |
0 commit comments