subsystem-list.component.ts 3.16 KB
Newer Older
Vitali Stupin's avatar
Vitali Stupin committed
1
import { Component, OnInit, AfterViewInit, OnDestroy } from '@angular/core';
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
2
import { Subsystem } from '../subsystem';
Vitali Stupin's avatar
Vitali Stupin committed
3
import { SubsystemsService } from '../subsystems.service';
4
import { ActivatedRoute, Router, Scroll } from '@angular/router';
Vitali Stupin's avatar
Vitali Stupin committed
5
import { Subscription, BehaviorSubject } from 'rxjs';
6
7
import { ViewportScroller } from '@angular/common';
import { filter } from 'rxjs/operators';
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
8
9

@Component({
10
11
12
  selector: 'app-subsystem-list',
  templateUrl: './subsystem-list.component.html',
  styleUrls: ['./subsystem-list.component.css']
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
13
})
Vitali Stupin's avatar
Vitali Stupin committed
14
15
16
export class SubsystemListComponent implements OnInit, AfterViewInit, OnDestroy {
  subsystems: Subsystem[];
  message = '';
Vitali Stupin's avatar
Vitali Stupin committed
17
  scrollSubject: BehaviorSubject<any> = new BehaviorSubject(null);
Vitali Stupin's avatar
Vitali Stupin committed
18
19
20
21
22
  routerScrollSubscription: Subscription;
  routeSubscription: Subscription;
  warningsSubscription: Subscription;
  scrollSubjectSubscription: Subscription;
  filteredSubsystems: BehaviorSubject<Subsystem[]>;
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
23

24
  constructor(
Vitali Stupin's avatar
Vitali Stupin committed
25
    private subsystemsService: SubsystemsService,
26
    private route: ActivatedRoute,
27
28
29
30
31
32
    private router: Router,
    private viewportScroller: ViewportScroller
  ) {
    // Geting previous scroll position
    this.routerScrollSubscription = this.router.events.pipe(
      filter(e => e instanceof Scroll)
Vitali Stupin's avatar
Vitali Stupin committed
33
    ).subscribe(async (e) => {
34
      if ((e as Scroll).position) {
Vitali Stupin's avatar
Vitali Stupin committed
35
        this.scrollSubject.next((e as Scroll).position);
36
37
38
      }
    });
  }
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
39

Vitali Stupin's avatar
Vitali Stupin committed
40
  getInstance(): string {
Vitali Stupin's avatar
Vitali Stupin committed
41
    return this.subsystemsService.getInstance();
Vitali Stupin's avatar
Vitali Stupin committed
42
43
44
  }

  getInstances(): string[] {
Vitali Stupin's avatar
Vitali Stupin committed
45
    return this.subsystemsService.getInstances();
Vitali Stupin's avatar
Vitali Stupin committed
46
47
48
  }

  switchInstance(instance: string): void {
Vitali Stupin's avatar
Vitali Stupin committed
49
    this.router.navigateByUrl('/' + instance);
Vitali Stupin's avatar
Vitali Stupin committed
50
51
52
  }

  getApiUrl(): string {
Vitali Stupin's avatar
Vitali Stupin committed
53
    return this.subsystemsService.getApiUrl();
Vitali Stupin's avatar
Vitali Stupin committed
54
55
  }

Vitali Stupin's avatar
Initial    
Vitali Stupin committed
56
  ngOnInit() {
57
    // Reset message on page load
Vitali Stupin's avatar
Vitali Stupin committed
58
59
    this.message = '';
    this.filteredSubsystems = this.subsystemsService.filteredSubsystemsSubject;
60
61

    // Service will tell when data loading failed!
Vitali Stupin's avatar
Vitali Stupin committed
62
    this.warningsSubscription = this.subsystemsService.warnings.subscribe(signal => {
Vitali Stupin's avatar
Vitali Stupin committed
63
      this.message = signal;
64
    });
Vitali Stupin's avatar
Vitali Stupin committed
65

66
67
    this.routeSubscription = this.route.params.subscribe( params => {
      // Reset message on navigation
Vitali Stupin's avatar
Vitali Stupin committed
68
      this.message = '';
69

70
      // Redirect to default instance if instance is empty or invalid
Vitali Stupin's avatar
Vitali Stupin committed
71
72
73
      if (!this.subsystemsService.getInstances().includes(params.instance)) {
        this.router.navigateByUrl('/' + this.subsystemsService.getDefaultInstance());
        return;
74
75
      }
      // Only reload on switching of instance or when no instance is selected yet on service side
Vitali Stupin's avatar
Vitali Stupin committed
76
77
      if (this.getInstance() === '' || this.getInstance() !== params.instance) {
        this.subsystemsService.setInstance(params.instance ? params.instance : this.subsystemsService.getDefaultInstance());
78
79
      }
    });
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
80
81
  }

82
83
  ngAfterViewInit() {
    // Restoring scroll position
Vitali Stupin's avatar
Vitali Stupin committed
84
    this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
Vitali Stupin's avatar
Vitali Stupin committed
85
      if (position) {
Vitali Stupin's avatar
Vitali Stupin committed
86
87
        this.viewportScroller.scrollToPosition(position);
      }
Vitali Stupin's avatar
Vitali Stupin committed
88
    });
89
90
91
  }

  ngOnDestroy() {
Vitali Stupin's avatar
Vitali Stupin committed
92
93
94
95
    this.routerScrollSubscription.unsubscribe();
    this.routeSubscription.unsubscribe();
    this.warningsSubscription.unsubscribe();
    this.scrollSubjectSubscription.unsubscribe();
96
  }
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
97
}