subsystem.component.ts 4.33 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

@Component({
  selector: 'app-subsystem',
Vitali Stupin's avatar
Vitali Stupin committed
11
  templateUrl: './subsystem.component.html'
12
})
Vitali Stupin's avatar
Vitali Stupin committed
13
14
15
export class SubsystemComponent implements OnInit, AfterViewInit, OnDestroy {
  subsystemId = '';
  message = '';
Vitali Stupin's avatar
Vitali Stupin committed
16
17
  // Contains instance from route.params (for displaying warning)
  paramsInstance = '';
Vitali Stupin's avatar
Vitali Stupin committed
18
19
20
21
22
23
  private scrollSubject: BehaviorSubject<any> = new BehaviorSubject(null);
  private routerScrollSubscription: Subscription;
  private routeSubscription: Subscription;
  private warningsSubscription: Subscription;
  private scrollSubjectSubscription: Subscription;
  private subsystemsSubscription: Subscription;
24
  private instanceVersion: string;
Vitali Stupin's avatar
Vitali Stupin committed
25
  subsystemSubject: BehaviorSubject<Subsystem> = new BehaviorSubject(null);
26
27

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

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

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

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

  goToList(): void {
58
59
60
61
    this.router.navigateByUrl(
      '/' + this.subsystemsService.getInstance()
      + (this.instanceVersion ? '?at=' + this.instanceVersion : '')
    );
Vitali Stupin's avatar
Vitali Stupin committed
62
  }
63

64
65
66
67
  scrollToTop() {
    this.viewportScroller.scrollToPosition([0, 0]);
  }

68
  ngOnInit() {
69
    // Reset message on page load
Vitali Stupin's avatar
Vitali Stupin committed
70
    this.message = '';
71

72
    // Service will tell when data loading failed!
Vitali Stupin's avatar
Vitali Stupin committed
73
    this.warningsSubscription = this.subsystemsService.warnings.subscribe(signal => {
Vitali Stupin's avatar
Vitali Stupin committed
74
      this.message = signal;
75
76
    });

77
    this.routeSubscription = this.route.params.subscribe( params => {
78
      // Checking if instance is correct
Vitali Stupin's avatar
Vitali Stupin committed
79
      if (this.subsystemsService.getInstances().indexOf(params.instance) < 0) {
Vitali Stupin's avatar
Vitali Stupin committed
80
81
        this.paramsInstance = params.instance;
        this.message = 'subsystem.incorrectInstanceWarning';
Vitali Stupin's avatar
Vitali Stupin committed
82
        return;
83
      }
84
85
86
87
88
89
90
91

      // Set selected instance version
      if (this.route.snapshot && this.route.snapshot.queryParams.at) {
        this.instanceVersion = this.route.snapshot.queryParams.at;
      } else {
        this.instanceVersion = '';
      }

Vitali Stupin's avatar
Vitali Stupin committed
92
      this.subsystemId = params.instance + '/' + params.class + '/' + params.member + '/' + params.subsystem;
93

94
      // Only reload on switching of instance or when no instance is selected yet on service side
Vitali Stupin's avatar
Vitali Stupin committed
95
      if (this.getInstance() === '' || this.getInstance() !== params.instance) {
Vitali Stupin's avatar
Vitali Stupin committed
96
        // this.subsystemsService.setInstance(params.instance ? params.instance : this.subsystemsService.getDefaultInstance());
97
        this.subsystemsService.setInstance(params.instance, this.instanceVersion);
98
      }
99

Vitali Stupin's avatar
Vitali Stupin committed
100
      this.subsystemsSubscription = this.subsystemsService.subsystemsSubject.subscribe(subsystems => {
Vitali Stupin's avatar
Vitali Stupin committed
101
        const subsystem = this.getSubsystem(subsystems, this.subsystemId);
Vitali Stupin's avatar
Vitali Stupin committed
102
103
        if (!subsystem && !this.message && subsystems.length) {
          this.message = 'subsystem.subsystemNotFoundWarning';
Vitali Stupin's avatar
Vitali Stupin committed
104
        } else {
Vitali Stupin's avatar
Vitali Stupin committed
105
          this.subsystemSubject.next(subsystem);
Vitali Stupin's avatar
Vitali Stupin committed
106
        }
Vitali Stupin's avatar
Vitali Stupin committed
107
      });
108
109
110
    });
  }

111
112
  ngAfterViewInit() {
    // Restoring scroll position
Vitali Stupin's avatar
Vitali Stupin committed
113
    this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
Vitali Stupin's avatar
Vitali Stupin committed
114
      if (position) {
Vitali Stupin's avatar
Vitali Stupin committed
115
116
        this.viewportScroller.scrollToPosition(position);
      }
Vitali Stupin's avatar
Vitali Stupin committed
117
    });
118
119
  }

120
  ngOnDestroy() {
Vitali Stupin's avatar
Vitali Stupin committed
121
    this.routerScrollSubscription.unsubscribe();
Vitali Stupin's avatar
Vitali Stupin committed
122
123
124
    this.routeSubscription.unsubscribe();
    this.warningsSubscription.unsubscribe();
    this.scrollSubjectSubscription.unsubscribe();
Vitali Stupin's avatar
Vitali Stupin committed
125
126
127
    if (this.subsystemsSubscription) {
      this.subsystemsSubscription.unsubscribe();
    }
128
129
  }
}