All files / src/app/editor/modals/maps-menu maps-menu.component.ts

74.6% Statements 47/63
100% Branches 0/0
58.33% Functions 14/24
78.43% Lines 40/51

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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139              1x                           1x   11x 11x 11x 11x   11x   1x 1x   11x 11x 11x 11x 11x   11x     11x     1x 11x               1x 44x       22x                                     1x       1x                       11x 11x                 11x 11x   11x 11x   11x       11x 11x 11x       11x 11x       11x 11x   11x         11x       1x  
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
import { BehaviorSubject, Subscription, Observable, combineLatest } from 'rxjs';
import { map, tap } from 'rxjs/operators';
 
import { MapsDatabaseService } from '../../../map/maps-database/maps-database.service';
import { ABAMap } from '../../../map/maps-database/utils';
 
const nbMapsPerPage = 10;
 
import {
  faFilter,
  faChevronLeft,
  faChevronRight,
  faEye
} from '@fortawesome/free-solid-svg-icons';
 
@Component({
  selector: 'app-maps-menu',
  templateUrl: './maps-menu.component.html',
  styleUrls: ['./maps-menu.component.css']
})
export class MapsMenuComponent implements OnInit, OnDestroy {
 
  faFilter = faFilter;
  faPrevious = faChevronLeft;
  faNext = faChevronRight;
  faEye = faEye;
 
  _open = false;
 
  @Input() mapSelected: (id: number) => void;
  @Input() backToMainMenu: () => void;
 
  filter$ = new BehaviorSubject<string>('');
  pageNumber$ = new BehaviorSubject<number>(0);
  pages$ = new BehaviorSubject<number[]>([]);
  filteredMaps$ = new BehaviorSubject<ABAMap[]>([]);
  mapsToDisplay$ = new BehaviorSubject<ABAMap[]>([]);
 
  subscriptions: Subscription[] = [];
 
  constructor(
    private mapsDBService: MapsDatabaseService
  ) { }
 
  ngOnInit() {
    this.subscriptions.push(
      this.initFilteredMaps().subscribe(),
      this.initPages().subscribe(),
      this.initPageNumber().subscribe(),
      this.initMapsToDisplay().subscribe()
    );
  }
 
  ngOnDestroy() {
    this.subscriptions.forEach((subscription) => subscription.unsubscribe());
  }
 
  get open(): boolean {
    return this._open;
  }
 
  set open(value: boolean) {
    this._open = value;
 
    /* Reload the maps from the ABAplans server every time the
       modal is shown. */
    if (value) {
      this.subscriptions[0] = combineLatest(
        this.mapsDBService.getMapsList(),
        this.filter$
      ).pipe(
        map(([maps, filter]) => this.filterMaps(maps, filter)),
        tap((maps) => this.filteredMaps$.next(maps))
      ).subscribe();
    }
  }
 
  int(value: string): number {
    return parseInt(value, 10);
  }
 
  private filterMaps(maps: ABAMap[], filter: string): ABAMap[] {
    return maps.filter(
      (abaMap) => [
        abaMap.uid.toString(),
        abaMap.title.toLowerCase(),
        abaMap.creationDate.toLowerCase()
      ].some(
        (candidate) => candidate.includes(filter)
      )
    );
  }
 
  private initFilteredMaps(): Observable<ABAMap[]> {
    return combineLatest(
      this.mapsDBService.getMapsList(),
      this.filter$
    ).pipe(
      map(([maps, filter]) => this.filterMaps(maps, filter)),
      tap((maps) => this.filteredMaps$.next(maps))
    );
  }
 
  private initPages(): Observable<number[]> {
    return this.filteredMaps$.pipe(
      map((maps) => {
        const numberOfPages = Math.ceil(maps.length / nbMapsPerPage);
        return Array(numberOfPages).fill(null).map(({}, i) => i);
      }),
      tap((pages) => this.pages$.next(pages))
    );
  }
 
  private initPageNumber(): Observable<any> {
    return this.filteredMaps$.pipe(
      tap(() => this.pageNumber$.next(0)),
    );
  }
 
  private initMapsToDisplay(): Observable<any> {
    return combineLatest(
      this.filteredMaps$,
      this.pageNumber$,
    ).pipe(
      map(([maps, pageNumber]) => {
        const startOffset = nbMapsPerPage * pageNumber;
 
        return maps.slice(
          startOffset,
          startOffset + nbMapsPerPage,
        );
      }),
      tap((maps) => this.mapsToDisplay$.next(maps)),
    );
  }
 
}