/
object-tree.component.ts
59 lines (55 loc) · 1.63 KB
/
object-tree.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
import {
ChangeDetectionStrategy,
Component,
Input,
OnChanges,
OnInit,
Optional,
SkipSelf,
} from '@angular/core';
/**
* @description
* This component displays a plain JavaScript object as an expandable tree.
*
* @example
* ```HTML
* <vdr-object-tree [value]="payment.metadata"></vdr-object-tree>
* ```
*
* @docsCategory components
*/
@Component({
selector: 'vdr-object-tree',
templateUrl: './object-tree.component.html',
styleUrls: ['./object-tree.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ObjectTreeComponent implements OnChanges {
@Input() value: { [key: string]: any } | string;
@Input() isArrayItem = false;
depth: number;
expanded: boolean;
valueIsArray: boolean;
entries: Array<{ key: string; value: any }>;
constructor(@Optional() @SkipSelf() parent: ObjectTreeComponent) {
if (parent) {
this.depth = parent.depth + 1;
} else {
this.depth = 0;
}
}
ngOnChanges() {
this.entries = this.getEntries(this.value);
this.expanded = this.depth === 0 || this.isArrayItem;
this.valueIsArray = Object.keys(this.value).every(v => Number.isInteger(+v));
}
isObject(value: any): boolean {
return typeof value === 'object' && value !== null;
}
private getEntries(inputValue: { [key: string]: any } | string): Array<{ key: string; value: any }> {
if (!this.isObject(inputValue)) {
return [{ key: '', value: inputValue }];
}
return Object.entries(inputValue).map(([key, value]) => ({ key, value }));
}
}