subsystem-list.component.ts 3.2 KB
Newer Older
1
import { Component, OnInit, OnDestroy } from '@angular/core';
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
2
3
import { Subsystem } from '../subsystem';
import { MethodsService } from '../methods.service';
4
5
6
7
import { ActivatedRoute, Router, Scroll } from '@angular/router';
import { Subscription } from 'rxjs';
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
})
14
export class SubsystemListComponent implements OnInit, OnDestroy {
15
16
  subsystems: Subsystem[]
  message: string = ''
17
18
19
20
21
  scrollPosition: [number, number]
  routerScrollSubscription: Subscription
  routeSubscription: Subscription
  updatedSubscription: Subscription
  warningsSubscription: Subscription
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
22

23
24
25
  constructor(
    private methodsService: MethodsService,
    private route: ActivatedRoute,
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    private router: Router,
    private viewportScroller: ViewportScroller
  ) {
    // Geting previous scroll position
    this.routerScrollSubscription = this.router.events.pipe(
      filter(e => e instanceof Scroll)
    ).subscribe(e => {
      if ((e as Scroll).position) {
        this.scrollPosition = (e as Scroll).position;
      } else {
        this.scrollPosition = [0, 0];
      }
    });
  }
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
40
41

  ngOnInit() {
42
43
44
45
    // Reset message on page load
    this.message = ''

    // Service will tell when data loading failed!
46
    this.warningsSubscription = this.methodsService.warnings.subscribe(signal => {
47
48
49
      this.message = signal
    });
    
50
51
52
53
    this.routeSubscription = this.route.params.subscribe( params => {
      // Reset message on navigation
      this.message = ''

54
55
56
57
58
59
60
61
62
63
64
      // Redirect to default instance if instance is empty or invalid
      if (!this.methodsService.getInstances().includes(params['instance'])) {
        this.router.navigateByUrl('/' + this.methodsService.getDefaultInstance())
        return
      }
      // Only reload on switching of instance or when no instance is selected yet on service side
      if (this.getInstance() == '' || this.getInstance() != params['instance']) {
        this.methodsService.setInstance(params['instance'] ? params['instance'] : this.methodsService.getDefaultInstance())
      }
    });

Vitali Stupin's avatar
Vitali Stupin committed
65
    // Service will tell when updated data is available!
66
    this.updatedSubscription = this.methodsService.subsystemsUpdated.subscribe(signal => {
Vitali Stupin's avatar
Vitali Stupin committed
67
68
      this.getMethods();
    });
Vitali Stupin's avatar
Vitali Stupin committed
69
70
71
    // If json data is loaded update event will not be emited.
    // This line must be after subscription (data may be changed while we start subscription)
    this.getMethods();
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
72
73
  }

74
75
76
77
78
79
80
81
82
83
84
85
  ngAfterViewInit() {
    // Restoring scroll position
    this.viewportScroller.scrollToPosition(this.scrollPosition);
  }

  ngOnDestroy() {
    this.routerScrollSubscription.unsubscribe()
    this.routeSubscription.unsubscribe()
    this.updatedSubscription.unsubscribe()
    this.warningsSubscription.unsubscribe()
  }

86
87
88
89
90
91
92
93
  getInstance(): string {
    return this.methodsService.getInstance()
  }

  getInstances(): string[] {
    return this.methodsService.getInstances()
  }

Vitali Stupin's avatar
Initial  
Vitali Stupin committed
94
  getMethods(): void {
95
96
97
98
99
    this.subsystems = this.methodsService.getMethods()
  }

  switchInstance(instance: string): void {
    this.router.navigateByUrl('/' + instance)
Vitali Stupin's avatar
Initial  
Vitali Stupin committed
100
101
  }
}