nav-bar.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { ChangeContext } from '@angular-slider/ngx-slider';
  2. import { AfterViewChecked, ChangeDetectorRef, Component, ElementRef, ViewChild } from '@angular/core';
  3. import { BehaviorSubject, combineLatest } from 'rxjs';
  4. import { map, take } from 'rxjs/operators';
  5. import { AppConfig } from '../app.config';
  6. import { EVTModelService } from '../services/evt-model.service';
  7. import { EVTStatusService } from '../services/evt-status.service';
  8. @Component({
  9. selector: 'evt-nav-bar',
  10. templateUrl: './nav-bar.component.html',
  11. styleUrls: ['./nav-bar.component.scss'],
  12. })
  13. export class NavBarComponent implements AfterViewChecked {
  14. @ViewChild('thumbnailsContainer') thumbnailsContainer: ElementRef;
  15. private currentPageInfo$ = combineLatest([
  16. this.evtModelService.pages$,
  17. this.evtStatusService.currentPage$,
  18. ]);
  19. prevNavigationDisabled$ = this.currentPageInfo$.pipe(
  20. map(([pages, page]) => pages.findIndex((p) => p.id === page.id) === 0),
  21. );
  22. nextNavigationDisabled$ = this.currentPageInfo$.pipe(
  23. map(([pages, page]) => pages.findIndex((p) => p.id === page.id) === pages.length - 1),
  24. );
  25. currentPage$ = this.currentPageInfo$.pipe(
  26. map(([pages, page]) => pages.findIndex((p) => p.id === page.id)),
  27. );
  28. thViewerSettings$ = new BehaviorSubject({ col: 1, row: 1 });
  29. thumbnailsButton = AppConfig.evtSettings.ui.thumbnailsButton;
  30. thumbnailsPanelOpened$ = new BehaviorSubject(false);
  31. viscollButton = AppConfig.evtSettings.ui.viscollButton;
  32. viscollPanelOpened$ = new BehaviorSubject(false);
  33. navigationDisabled$ = combineLatest([this.thumbnailsPanelOpened$, this.viscollPanelOpened$]).pipe(
  34. map(([thumbnailsPanelOpened, viscollPanelOpened]) => thumbnailsPanelOpened || viscollPanelOpened),
  35. );
  36. pageSliderOptions$ = combineLatest([this.navigationDisabled$, this.evtModelService.pages$])
  37. .pipe(
  38. map(([navigationDisabled, pages]) => ({
  39. floor: 0,
  40. ceil: pages.length - 1,
  41. showSelectionBar: true,
  42. translate: (value: number): string => pages[value]?.label ?? '',
  43. disabled: navigationDisabled,
  44. })),
  45. );
  46. constructor(
  47. private evtStatusService: EVTStatusService,
  48. private cdref: ChangeDetectorRef,
  49. public evtModelService: EVTModelService,
  50. ) {
  51. }
  52. ngAfterViewChecked() {
  53. this.calculateThumbsPerPage();
  54. }
  55. changePage(event: ChangeContext) {
  56. this.evtModelService.pages$.pipe(take(1)).subscribe(
  57. (pages) => this.evtStatusService.updatePage$.next(pages[event.value]),
  58. );
  59. }
  60. goToFirstPage() {
  61. this.evtModelService.pages$.pipe(take(1)).subscribe(
  62. (pages) => this.evtStatusService.updatePage$.next(pages[0]),
  63. );
  64. }
  65. goToPrevPage() {
  66. combineLatest([
  67. this.evtModelService.pages$,
  68. this.evtStatusService.currentPage$,
  69. ]).pipe(take(1)).subscribe(([pages, page]) => {
  70. const pageIndex = pages.findIndex((p) => p.id === page.id);
  71. if (pageIndex > 0) {
  72. this.evtStatusService.updatePage$.next(pages[pageIndex - 1]);
  73. }
  74. });
  75. }
  76. goToNextPage() {
  77. combineLatest([
  78. this.evtModelService.pages$,
  79. this.evtStatusService.currentPage$,
  80. ]).pipe(take(1)).subscribe(([pages, page]) => {
  81. const pageIndex = pages.findIndex((p) => p.id === page.id);
  82. if (pageIndex < pages.length - 1) {
  83. this.evtStatusService.updatePage$.next(pages[pageIndex + 1]);
  84. }
  85. });
  86. }
  87. goToLastPage() {
  88. this.evtModelService.pages$.pipe(take(1)).subscribe(
  89. (pages) => this.evtStatusService.updatePage$.next(pages[pages.length - 1]),
  90. );
  91. }
  92. toggleThumbnailsPanel() {
  93. this.thumbnailsPanelOpened$.pipe(take(1)).subscribe(opened => {
  94. this.thumbnailsPanelOpened$.next(!opened);
  95. });
  96. this.viscollPanelOpened$.pipe(take(1)).subscribe(opened => {
  97. if (opened) {
  98. this.viscollPanelOpened$.next(!opened);
  99. }
  100. });
  101. }
  102. toggleViscollPanel() {
  103. this.viscollPanelOpened$.pipe(take(1)).subscribe(opened => {
  104. this.viscollPanelOpened$.next(!opened);
  105. });
  106. this.thumbnailsPanelOpened$.pipe(take(1)).subscribe(opened => {
  107. if (opened) {
  108. this.thumbnailsPanelOpened$.next(!opened);
  109. }
  110. });
  111. }
  112. private calculateThumbsPerPage() {
  113. const thContainer = this.thumbnailsContainer?.nativeElement;
  114. if (thContainer) {
  115. const thMaxHeight = parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--thumbnail-height'), 10);
  116. const thMaxWidth = parseInt(window.getComputedStyle(document.documentElement).getPropertyValue('--thumbnail-width'), 10);
  117. this.thViewerSettings$.next({
  118. col: Math.floor(thContainer.clientWidth / thMaxWidth),
  119. row: Math.floor(thContainer.clientHeight / thMaxHeight),
  120. });
  121. this.cdref.detectChanges();
  122. }
  123. }
  124. }