component.ts 3.9 KB
Newer Older
E
Elijah Oyekunle 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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.

import {Component, ElementRef, OnDestroy, OnInit, Renderer2, ViewChild} from '@angular/core';
import {ActivatedRoute} from '@angular/router';
S
Sebastian Florek 已提交
17
import {MatButtonToggleGroup} from '@angular/material/button-toggle';
E
Elijah Oyekunle 已提交
18
import {HttpClient} from '@angular/common/http';
S
Sebastian Florek 已提交
19
import {dump as toYaml, load as fromYaml} from 'js-yaml';
E
Elijah Oyekunle 已提交
20 21 22
import {Subscription} from 'rxjs';
import {CRDObjectDetail} from '@api/backendapi';
import {highlightAuto} from 'highlight.js';
S
Sebastian Florek 已提交
23
import {EditorMode} from '../../common/components/textinput/component';
E
Elijah Oyekunle 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37
import {ActionbarService, ResourceMeta} from '../../common/services/global/actionbar';
import {NamespacedResourceService} from '../../common/services/resource/resource';
import {EndpointManager, Resource} from '../../common/services/resource/endpoint';
import {NotificationsService} from '../../common/services/global/notifications';
import {RawResource} from '../../common/resources/rawresource';

@Component({selector: 'kd-crd-object-detail', templateUrl: './template.html'})
export class CRDObjectDetailComponent implements OnInit, OnDestroy {
  @ViewChild('group', {static: true}) buttonToggleGroup: MatButtonToggleGroup;
  @ViewChild('code', {static: true}) codeRef: ElementRef;

  private objectSubscription_: Subscription;
  private readonly endpoint_ = EndpointManager.resource(Resource.crd, true);
  object: CRDObjectDetail;
S
Sebastian Florek 已提交
38
  modes = EditorMode;
E
Elijah Oyekunle 已提交
39
  isInitialized = false;
S
Sebastian Florek 已提交
40 41
  selectedMode = EditorMode.YAML;
  text = '';
42
  eventListEndpoint: string;
E
Elijah Oyekunle 已提交
43 44 45 46 47 48 49 50 51 52 53 54

  constructor(
    private readonly object_: NamespacedResourceService<CRDObjectDetail>,
    private readonly actionbar_: ActionbarService,
    private readonly activatedRoute_: ActivatedRoute,
    private readonly notifications_: NotificationsService,
    private readonly http_: HttpClient,
    private readonly renderer_: Renderer2,
  ) {}

  ngOnInit(): void {
    const {crdName, namespace, objectName} = this.activatedRoute_.snapshot.params;
55 56 57 58 59 60
    this.eventListEndpoint = this.endpoint_.child(
      `${crdName}/${objectName}`,
      Resource.event,
      namespace,
    );

E
Elijah Oyekunle 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
    this.objectSubscription_ = this.object_
      .get(this.endpoint_.child(crdName, objectName, namespace))
      .subscribe((d: CRDObjectDetail) => {
        this.object = d;
        this.notifications_.pushErrors(d.errors);
        this.actionbar_.onInit.emit(new ResourceMeta(d.typeMeta.kind, d.objectMeta, d.typeMeta));
        this.isInitialized = true;

        // Get raw resource
        const url = RawResource.getUrl(this.object.typeMeta, this.object.objectMeta);
        this.http_
          .get(url)
          .toPromise()
S
Sebastian Florek 已提交
74
          .then(response => (this.text = toYaml(response)));
E
Elijah Oyekunle 已提交
75 76
      });

S
Sebastian Florek 已提交
77
    this.buttonToggleGroup.valueChange.subscribe((selectedMode: EditorMode) => {
E
Elijah Oyekunle 已提交
78 79
      this.selectedMode = selectedMode;

S
Sebastian Florek 已提交
80 81
      if (this.text) {
        this.updateText_();
E
Elijah Oyekunle 已提交
82 83 84 85
      }
    });
  }

S
Sebastian Florek 已提交
86 87 88 89
  getSelectedMode(): EditorMode {
    return this.selectedMode;
  }

E
Elijah Oyekunle 已提交
90 91 92 93 94
  ngOnDestroy(): void {
    this.objectSubscription_.unsubscribe();
    this.actionbar_.onDetailsLeave.emit();
  }

S
Sebastian Florek 已提交
95 96 97 98 99 100
  private updateText_(): void {
    if (this.selectedMode === EditorMode.YAML) {
      this.text = toYaml(JSON.parse(this.text));
    } else {
      this.text = this.toRawJSON_(fromYaml(this.text));
    }
E
Elijah Oyekunle 已提交
101 102
  }

S
Sebastian Florek 已提交
103 104
  private toRawJSON_(object: {}): string {
    return JSON.stringify(object, null, '\t');
E
Elijah Oyekunle 已提交
105 106
  }
}