component.ts 3.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright 2017 The Kubernetes Authors.
//
// 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 16
import {Component, OnInit} from '@angular/core';
import {NgForm} from '@angular/forms';
S
Sebastian Florek 已提交
17
import {MatDialog} from '@angular/material/dialog';
18 19 20
import {GlobalSettings} from '@api/backendapi';
import {GlobalSettingsService} from '../../common/services/global/globalsettings';
import {TitleService} from '../../common/services/global/title';
21

22
import {SaveAnywayDialog} from './saveanywaysdialog/dialog';
23

24
@Component({selector: 'kd-global-settings', templateUrl: './template.html'})
25 26 27 28
export class GlobalSettingsComponent implements OnInit {
  // Keep it in sync with ConcurrentSettingsChangeError constant from the backend.
  private readonly concurrentChangeErr_ = 'settings changed since last reload';
  settings: GlobalSettings = {} as GlobalSettings;
29
  hasLoadError = false;
30 31

  constructor(
S
Shu Muto 已提交
32 33 34 35
    private readonly settings_: GlobalSettingsService,
    private readonly dialog_: MatDialog,
    private readonly title_: TitleService,
  ) {}
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55

  ngOnInit(): void {
    this.load();
  }

  isInitialized(): boolean {
    return this.settings_.isInitialized();
  }

  load(form?: NgForm): void {
    if (form) {
      form.resetForm();
    }

    this.settings_.load(this.onLoad.bind(this), this.onLoadError.bind(this));
  }

  onLoad(): void {
    this.settings.itemsPerPage = this.settings_.getItemsPerPage();
    this.settings.clusterName = this.settings_.getClusterName();
56
    this.settings.logsAutoRefreshTimeInterval = this.settings_.getLogsAutoRefreshTimeInterval();
S
Shu Muto 已提交
57
    this.settings.resourceAutoRefreshTimeInterval = this.settings_.getResourceAutoRefreshTimeInterval();
58 59
  }

60
  onLoadError(): void {
61
    this.hasLoadError = true;
62 63 64
  }

  save(form: NgForm): void {
S
Shu Muto 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
    this.settings_.save(this.settings).subscribe(
      () => {
        this.load(form);
        this.title_.update();
        this.settings_.onSettingsUpdate.next();
      },
      err => {
        if (err && err.data.indexOf(this.concurrentChangeErr_) !== -1) {
          this.dialog_
            .open(SaveAnywayDialog, {width: '420px'})
            .afterClosed()
            .subscribe(result => {
              if (result === true) {
                // Backend was refreshed with the PUT request, so the second try will be
                // successful unless yet another concurrent change will happen. In that case
                // "save anyways" dialog will be shown again.
                this.save(form);
              } else {
                this.load(form);
84 85
              }
            });
S
Shu Muto 已提交
86 87 88
        }
      },
    );
89 90
  }
}