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

@Component({
  selector: 'app-subsystem',
  templateUrl: './subsystem.component.html',
  styleUrls: ['./subsystem.component.css']
})
Vitali Stupin's avatar
Vitali Stupin committed
14
15
16
17
18
19
20
21
22
23
export class SubsystemComponent implements OnInit, AfterViewInit, OnDestroy {
  subsystemId = '';
  message = '';
  private scrollSubject: BehaviorSubject<any> = new BehaviorSubject(null);
  private routerScrollSubscription: Subscription;
  private routeSubscription: Subscription;
  private warningsSubscription: Subscription;
  private scrollSubjectSubscription: Subscription;
  private subsystemsSubscription: Subscription;
  subsystemSubject: BehaviorSubject<Subsystem> = new BehaviorSubject(null);
24
25

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

Vitali Stupin's avatar
Vitali Stupin committed
41
  private getSubsystem(subsystems: Subsystem[], name: string): Subsystem {
Vitali Stupin's avatar
Vitali Stupin committed
42
    return subsystems.find((element) => {
Vitali Stupin's avatar
Vitali Stupin committed
43
      return element.fullSubsystemName === name;
Vitali Stupin's avatar
Vitali Stupin committed
44
    });
Vitali Stupin's avatar
Vitali Stupin committed
45
46
47
  }

  getInstance(): string {
Vitali Stupin's avatar
Vitali Stupin committed
48
    return this.subsystemsService.getInstance();
Vitali Stupin's avatar
Vitali Stupin committed
49
50
51
  }

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

  goToList(): void {
Vitali Stupin's avatar
Vitali Stupin committed
56
    this.router.navigateByUrl('/' + this.subsystemsService.getInstance());
Vitali Stupin's avatar
Vitali Stupin committed
57
  }
58

59
  ngOnInit() {
60
    // Reset message on page load
Vitali Stupin's avatar
Vitali Stupin committed
61
    this.message = '';
62

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

68
    this.routeSubscription = this.route.params.subscribe( params => {
69
      // Checking if instance is correct
Vitali Stupin's avatar
Vitali Stupin committed
70
71
72
      if (!this.subsystemsService.getInstances().includes(params.instance)) {
        this.message = 'Incorrect instance!';
        return;
73
      }
Vitali Stupin's avatar
Vitali Stupin committed
74
      this.subsystemId = params.instance + '/' + params.class + '/' + params.member + '/' + params.subsystem;
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
      }
Vitali Stupin's avatar
Vitali Stupin committed
79
      this.subsystemsSubscription = this.subsystemsService.subsystemsSubject.subscribe(subsystems => {
Vitali Stupin's avatar
Vitali Stupin committed
80
        const subsystem = this.getSubsystem(subsystems, this.subsystemId);
Vitali Stupin's avatar
Vitali Stupin committed
81
        if (!subsystem && !this.message) {
Vitali Stupin's avatar
Vitali Stupin committed
82
          this.message = 'Subsystem "' + this.subsystemId + '" cannot be found!';
Vitali Stupin's avatar
Vitali Stupin committed
83
        } else {
Vitali Stupin's avatar
Vitali Stupin committed
84
          this.subsystemSubject.next(subsystem);
Vitali Stupin's avatar
Vitali Stupin committed
85
        }
Vitali Stupin's avatar
Vitali Stupin committed
86
      });
87
88
89
    });
  }

90
91
  ngAfterViewInit() {
    // Restoring scroll position
Vitali Stupin's avatar
Vitali Stupin committed
92
    this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
Vitali Stupin's avatar
Vitali Stupin committed
93
      if (position) {
Vitali Stupin's avatar
Vitali Stupin committed
94
95
        this.viewportScroller.scrollToPosition(position);
      }
Vitali Stupin's avatar
Vitali Stupin committed
96
    });
97
98
  }

99
  ngOnDestroy() {
Vitali Stupin's avatar
Vitali Stupin committed
100
101
102
103
104
    this.routerScrollSubscription.unsubscribe();
    this.routeSubscription.unsubscribe();
    this.warningsSubscription.unsubscribe();
    this.scrollSubjectSubscription.unsubscribe();
    this.subsystemsSubscription.unsubscribe();
105
106
  }
}