subsystem-list.component.ts 4.87 KB
Newer Older
1
import { Component, OnInit, AfterViewInit, OnDestroy, ViewChild } 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';
8
import { InstanceVersion } from '../instance-version';
9
import { SearchComponent } from './search/search.component';
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
10
11

@Component({
12
  selector: 'app-subsystem-list',
Vitali Stupin's avatar
Vitali Stupin committed
13
  templateUrl: './subsystem-list.component.html'
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
14
})
Vitali Stupin's avatar
Vitali Stupin committed
15
export class SubsystemListComponent implements OnInit, AfterViewInit, OnDestroy {
16
  message: string;
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[]>;
23
24
  instanceVersions: BehaviorSubject<InstanceVersion[]>;
  instanceVersion: string;
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
25

Vitali Stupin's avatar
Vitali Stupin committed
26
  @ViewChild(SearchComponent, { static: true }) search;
27

28
  constructor(
Vitali Stupin's avatar
Vitali Stupin committed
29
    private subsystemsService: SubsystemsService,
30
    private route: ActivatedRoute,
31
32
33
34
35
36
    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
37
    ).subscribe(async (e) => {
38
      if ((e as Scroll).position) {
Vitali Stupin's avatar
Vitali Stupin committed
39
        this.scrollSubject.next((e as Scroll).position);
40
41
42
      }
    });
  }
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
43

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

  getInstances(): string[] {
Vitali Stupin's avatar
Vitali Stupin committed
49
    return this.subsystemsService.getInstances();
Vitali Stupin's avatar
Vitali Stupin committed
50
51
52
  }

  switchInstance(instance: string): void {
Vitali Stupin's avatar
Vitali Stupin committed
53
    this.router.navigateByUrl('/' + instance);
Vitali Stupin's avatar
Vitali Stupin committed
54
55
56
57
58
    // Reloading data if clicked on the current instance
    if (this.subsystemsService.getInstance() === instance) {
      this.instanceVersion = '';
      this.subsystemsService.setInstance(instance, this.instanceVersion);
    }
Vitali Stupin's avatar
Vitali Stupin committed
59
60
61
  }

  getApiUrl(): string {
Vitali Stupin's avatar
Vitali Stupin committed
62
    return this.subsystemsService.getApiUrl();
Vitali Stupin's avatar
Vitali Stupin committed
63
64
  }

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
  isPartialList(): boolean {
    const limit = parseInt(this.subsystemsService.getLimit(), 10);
    if (isNaN(limit)) {
      // limit is "all" (or some faulty string)
      return false;
    } else if (this.filteredSubsystems.value.length < limit) {
      return false;
    } else {
      // If subsystems length == limit then we still asume it is partial
      return true;
    }
  }

  scrollToTop() {
    this.viewportScroller.scrollToPosition([0, 0]);
  }

82
83
84
85
86
87
88
89
  setInstanceVersion() {
    // This will update URL without triggering route.params
    this.router.navigateByUrl(
      '/' + this.subsystemsService.getInstance()
      + (this.instanceVersion ? '?at=' + this.instanceVersion : ''));
    this.subsystemsService.setInstance(this.subsystemsService.getInstance(), this.instanceVersion);
  }

90
91
92
93
  setMaxLimit() {
    this.search.setLimit('all');
  }

Vitali Stupin's avatar
Vitali Stupin committed
94
  isIE() {
Vitali Stupin's avatar
Vitali Stupin committed
95
96
    const ua = window.navigator.userAgent;
    if (ua.indexOf('MSIE ') >= 0 || ua.indexOf('Trident/') >= 0) {
Vitali Stupin's avatar
Vitali Stupin committed
97
98
99
100
101
      return true;
    }
    return false;
  }

Vitali Stupin's avatar
Initial    
Vitali Stupin committed
102
  ngOnInit() {
103
    // Reset message on page load
Vitali Stupin's avatar
Vitali Stupin committed
104
    this.message = '';
105

Vitali Stupin's avatar
Vitali Stupin committed
106
    this.filteredSubsystems = this.subsystemsService.filteredSubsystemsSubject;
107
    this.instanceVersions = this.subsystemsService.instanceVersionsSubject;
108
109

    // Service will tell when data loading failed!
Vitali Stupin's avatar
Vitali Stupin committed
110
    this.warningsSubscription = this.subsystemsService.warnings.subscribe(signal => {
Vitali Stupin's avatar
Vitali Stupin committed
111
      this.message = signal;
112
    });
Vitali Stupin's avatar
Vitali Stupin committed
113

114
115
    this.routeSubscription = this.route.params.subscribe( params => {
      // Reset message on navigation
Vitali Stupin's avatar
Vitali Stupin committed
116
      this.message = '';
117

118
      // Redirect to default instance if instance is empty or invalid
Vitali Stupin's avatar
Vitali Stupin committed
119
      if (this.subsystemsService.getInstances().indexOf(params.instance) < 0) {
120
        this.router.navigateByUrl('/' + this.subsystemsService.getDefaultInstance(), { replaceUrl: true });
Vitali Stupin's avatar
Vitali Stupin committed
121
        return;
122
      }
123
124
125
126
127
128
129
130

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

131
      // Only reload on switching of instance or when no instance is selected yet on service side
Vitali Stupin's avatar
Vitali Stupin committed
132
      if (this.getInstance() === '' || this.getInstance() !== params.instance) {
133
        this.subsystemsService.setInstance(params.instance, this.instanceVersion);
134
135
      }
    });
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
136
137
  }

138
139
  ngAfterViewInit() {
    // Restoring scroll position
Vitali Stupin's avatar
Vitali Stupin committed
140
    this.scrollSubjectSubscription = this.scrollSubject.subscribe( position => {
Vitali Stupin's avatar
Vitali Stupin committed
141
      if (position) {
Vitali Stupin's avatar
Vitali Stupin committed
142
143
        this.viewportScroller.scrollToPosition(position);
      }
Vitali Stupin's avatar
Vitali Stupin committed
144
    });
145
146
147
  }

  ngOnDestroy() {
Vitali Stupin's avatar
Vitali Stupin committed
148
149
150
151
    this.routerScrollSubscription.unsubscribe();
    this.routeSubscription.unsubscribe();
    this.warningsSubscription.unsubscribe();
    this.scrollSubjectSubscription.unsubscribe();
152
  }
Vitali Stupin's avatar
Initial    
Vitali Stupin committed
153
}