component.ts 3.0 KB
Newer Older
1
// Copyright 2017 The Kubernetes Authors.
B
bryk 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

15
import {DOCUMENT} from '@angular/common';
16
import {HttpClient} from '@angular/common/http';
17
import {Component, Inject, OnInit} from '@angular/core';
18
import {Router} from '@angular/router';
B
bryk 已提交
19

20
import {AssetsService} from '../common/services/global/assets';
21
import {GlobalSettingsService} from '../common/services/global/globalsettings';
22

23 24 25 26 27
class SystemBanner {
  message: string;
  severity: string;
}

28 29 30 31 32
@Component({
  selector: 'kd-chrome',
  templateUrl: './template.html',
  styleUrls: ['./style.scss'],
})
33 34 35
export class ChromeComponent implements OnInit {
  private static readonly systemBannerEndpoint = 'api/v1/systembanner';
  private systemBanner_: SystemBanner;
36
  loading = false;
B
bryk 已提交
37

38
  constructor(
S
Shu Muto 已提交
39 40 41
    public assets: AssetsService,
    private readonly http_: HttpClient,
    private readonly router_: Router,
42 43
    @Inject(DOCUMENT) private readonly document_: Document,
    private readonly globalSettings_: GlobalSettingsService,
S
Shu Muto 已提交
44
  ) {}
45 46

  ngOnInit(): void {
S
Shu Muto 已提交
47 48 49 50 51 52
    this.http_
      .get<SystemBanner>(ChromeComponent.systemBannerEndpoint)
      .toPromise()
      .then(sb => {
        this.systemBanner_ = sb;
      });
53 54

    this.registerVisibilityChangeHandler_();
55
  }
B
bryk 已提交
56

57
  getOverviewStateName(): string {
58
    return '/overview';
B
bryk 已提交
59 60
  }

61
  isSystemBannerVisible(): boolean {
62
    return this.systemBanner_ && this.systemBanner_.message.length > 0;
B
bryk 已提交
63 64
  }

65
  getSystemBannerClass(): string {
M
Marcin Maciaszczyk 已提交
66
    const severity = this.systemBanner_ && this.systemBanner_.severity ? this.systemBanner_.severity.toLowerCase() : '';
67 68 69 70 71 72 73 74
    switch (severity) {
      case 'warning':
        return 'kd-bg-warning-light';
      case 'error':
        return 'kd-bg-error-light';
      default:
        return 'kd-bg-success-light';
    }
75
  }
B
bryk 已提交
76

77
  getSystemBannerMessage(): string {
78
    return this.systemBanner_ ? this.systemBanner_.message : '';
79 80 81
  }

  goToCreateState(): void {
82
    this.router_.navigate(['create'], {queryParamsHandling: 'preserve'});
83
  }
84 85 86 87 88 89 90 91 92

  private registerVisibilityChangeHandler_(): void {
    if (typeof this.document_.addEventListener === 'undefined') {
      console.log(
        'Your browser does not support Page Visibility API. Page cannot properly stop background tasks when tab is inactive.',
      );
      return;
    }

M
Marcin Maciaszczyk 已提交
93
    this.document_.addEventListener('visibilitychange', this.handleVisibilityChange_.bind(this), false);
94 95 96 97 98
  }

  private handleVisibilityChange_(): void {
    this.globalSettings_.onPageVisibilityChange.emit(!this.document_.hidden);
  }
B
bryk 已提交
99
}