aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/pages/composition/panel/panel-tabs/panel-tab.component.ts
blob: df8f46d7846119fca0ef5918a4c42379ca18a8fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import {
  Component,
  ViewContainerRef,
  ViewChild,
  Input,
  ComponentRef,
  ComponentFactoryResolver,
  ChangeDetectorRef,
  OnChanges, OnDestroy, AfterViewInit
} from '@angular/core';
import {Component as TopologyTemplate} from 'app/models';

// Helper component to add dynamic tabs
@Component({
  selector: 'panel-tab',
  template: `<div #content></div>`
})
export class PanelTabComponent implements OnChanges, OnDestroy, AfterViewInit {
  @ViewChild('content', { read: ViewContainerRef }) content;
  @Input() isActive:boolean;
  @Input() panelTabType;
  @Input() input;
  @Input() isViewOnly:boolean;
  @Input() component:TopologyTemplate;
  @Input() componentType;
  cmpRef: ComponentRef<any>;
  private isViewInitialized: boolean = false;

  constructor(private componentFactoryResolver: ComponentFactoryResolver,
    private cdRef: ChangeDetectorRef) { }

  updateComponent() {
    if (!this.isViewInitialized || !this.isActive) {
      return;
    }
    if (this.cmpRef) {
      this.cmpRef.destroy();
    }

    let factory = this.componentFactoryResolver.resolveComponentFactory(this.panelTabType);
    this.cmpRef = this.content.createComponent(factory);
    this.cmpRef.instance.input = this.input;
    this.cmpRef.instance.isViewOnly = this.isViewOnly;
    this.cmpRef.instance.component = this.component;
    this.cmpRef.instance.componentType = this.componentType;
    this.cdRef.detectChanges();
  }

  ngOnChanges() {
    this.updateComponent();
  }

  ngAfterViewInit() {
    this.isViewInitialized = true;
    this.updateComponent();
  }

  ngOnDestroy() {
    if (this.cmpRef) {
      this.cmpRef.destroy();
    }
  }
}