-
Notifications
You must be signed in to change notification settings - Fork 171
/
dynamic.component.ts
83 lines (73 loc) · 2.26 KB
/
dynamic.component.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Component, OnInit, ChangeDetectorRef, OnDestroy } from '@angular/core';
import { Subscription } from 'rxjs';
import {
getPathParams,
getNodeParams,
linkManager,
PathParams,
NodeParams
} from '@kyma-project/luigi-client';
import {
LuigiContextService,
IContextMessage
} from '../../services/luigi-context.service';
import { toTitleCase } from '../../services/helpers';
@Component({
selector: 'app-dynamic',
templateUrl: './dynamic.component.html',
styleUrls: ['./dynamic.component.css']
})
export class DynamicComponent implements OnInit, OnDestroy {
public linkManager = linkManager;
public pathParams: PathParams;
public nodeLabel: string;
public links: string[];
public hasBack: boolean;
public nodeParams: NodeParams = null;
public callbackValue = 'default value';
private lcSubscription: Subscription;
constructor(
private luigiService: LuigiContextService,
private cdr: ChangeDetectorRef
) {}
ngOnInit() {
this.lcSubscription = this.luigiService
.getContext()
.subscribe((ctx: IContextMessage) => {
if (!ctx.context) {
console.warn(
`To use this component properly, node configuration requires context.label to be defined.
context.links can be defined as array of strings to generate links to children`
);
return;
}
const lastPathParam = Object.values(getPathParams() || {}).pop();
// We can directly access our specified context values here
this.nodeLabel = toTitleCase(ctx.context.label || lastPathParam);
this.links = ctx.context.links;
// preserveView and node params
this.hasBack = linkManager().hasBack();
this.nodeParams =
Object.keys(getNodeParams()).length > 0 ? getNodeParams() : null;
if (!this.cdr['destroyed']) {
this.cdr.detectChanges();
}
});
}
ngOnDestroy() {
if (this.lcSubscription) {
this.lcSubscription.unsubscribe();
}
}
public slugify(str: string): string {
return str
.toLowerCase()
.split(' ')
.join('-');
}
public goBack() {
// going back with some sample callback context,
// that will be handed over to previous view
linkManager().goBack(this.callbackValue);
}
}