manuscript-thumbnails-viewer.component.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { Component, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges } from '@angular/core';
  2. import { map } from 'rxjs/operators';
  3. import { GridItem, Page } from '../../models/evt-models';
  4. import { EVTStatusService } from '../../services/evt-status.service';
  5. @Component({
  6. selector: 'evt-manuscript-thumbnails',
  7. templateUrl: './manuscript-thumbnails-viewer.component.html',
  8. styleUrls: ['./manuscript-thumbnails-viewer.component.scss'],
  9. })
  10. export class ManuscriptThumbnailsViewerComponent implements OnInit, OnChanges {
  11. @Output() clickedItem = new EventEmitter<GridItem>();
  12. @Input() pages: Page[] = [];
  13. @Input() col = 1;
  14. @Input() row = 1;
  15. public indexPage = 0;
  16. private items: GridItem[];
  17. public grid: GridItem[][][] = [];
  18. public currentItem$ = this.evtStatusService.currentPage$.pipe(
  19. map(p => this.items.find(i => i.id === p.id)),
  20. );
  21. constructor(
  22. private evtStatusService: EVTStatusService,
  23. ) {
  24. }
  25. ngOnInit() {
  26. this._setup();
  27. }
  28. ngOnChanges(changes: SimpleChanges) {
  29. if (Object.keys(changes).some(k => changes[k].currentValue !== changes[k].previousValue)) {
  30. this._setup();
  31. }
  32. }
  33. private _setup() {
  34. this.items = this.pages.map((page) => ({ url: page.url, name: page.label, id: page.id }));
  35. this.col = this.isValid(this.col) ? this.col : 1;
  36. this.row = this.isValid(this.row) ? this.row : 1;
  37. const gridSize = this.col * this.row;
  38. this.grid = Array(Math.ceil(this.items.length / gridSize)).fill(1)
  39. .map((_, i) => this.items.slice(i * gridSize, i * gridSize + gridSize))
  40. .map((p) => Array(this.row).fill(1).map((_, i) => p.slice(i * this.col, i * this.col + this.col)))
  41. ;
  42. }
  43. isValid(value) {
  44. return !(isNaN(value) || value <= 0);
  45. }
  46. goToPrevPage() {
  47. this.indexPage = Math.max(0, this.indexPage - 1);
  48. }
  49. goToNextPage() {
  50. this.indexPage = Math.min(this.indexPage + 1, this.grid.length - 1);
  51. }
  52. goToThumbPage(item) {
  53. this.evtStatusService.updatePage$.next(this.pages.find(p => p.id === item.id));
  54. this.clickedItem.emit(item);
  55. }
  56. }