This repository was archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathdescription.ts
172 lines (152 loc) · 5.17 KB
/
description.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import {AUGURY_TOKEN_ID_METADATA_KEY} from './parse-modules';
import {pathExists, getAtPath} from '../../utils/property-path';
import {functionName} from '../../utils';
export interface Dependency {
id: string;
name: string;
decorators: Array<string>;
}
export interface Property {
id?: string;
key: string;
value;
}
export const isDebugElementComponent = (element) => !!element.componentInstance &&
!componentInstanceExistsInParentChain(element);
export const getComponentName = (element): string => {
if (element.componentInstance &&
element.componentInstance.constructor &&
!componentInstanceExistsInParentChain(element)) {
return functionName(element.componentInstance.constructor);
}
else if (element.name) {
return element.name;
}
return element.nativeElement.tagName.toLowerCase();
};
const componentInstanceExistsInParentChain = (debugElement) => {
const componentInstanceRef = debugElement.componentInstance;
while (componentInstanceRef && debugElement.parent) {
if (componentInstanceRef === debugElement.parent.componentInstance) {
return true;
}
debugElement = debugElement.parent;
}
return false;
};
/*
* addPropsIfTheyExist([
* ['text'], // result: { key: 'text', value: '<value>'}
* ['text, 'text'], // result: { key: 'text', value: '<value>'}
* ['some_label', 'text'], // result: { key: 'some_label', value: '<value>'}
* ...
* ]);
*/
const getPropsIfTheyExist = (object: any, props: Array<any[]>): Array<any> => {
const properties: Array<any> = [];
props.forEach((prop: any[]) => {
const label = prop[0];
const path = prop.length > 1 ? prop.slice(1, prop.length) : prop[0];
if (pathExists(object, ...path)) {
properties.push({key: label, value: getAtPath(object, ...path).value });
}
});
return properties;
};
export abstract class Description {
public static getProviderDescription(provider, instance): Property {
if (typeof provider === 'string') {
return {
key: provider,
value: null
};
}
return {
id: Reflect.getMetadata(AUGURY_TOKEN_ID_METADATA_KEY, provider),
key: provider.name,
value: null,
};
}
public static getComponentDescription(debugElement: any): Array<Property> {
if (debugElement == null) {
return [];
}
let componentName: any;
const element: any = pathExists(debugElement, 'nativeElement') ? debugElement.nativeElement : null;
if (debugElement.componentInstance && !componentInstanceExistsInParentChain(debugElement)) {
componentName = pathExists(debugElement, 'componentInstance', 'constructor', 'name') ?
debugElement.componentInstance.constructor.name : null;
} else {
componentName = pathExists(element, 'tagName') ?
element.tagName.toLowerCase() : null;
}
const properties = [];
switch (componentName) {
case 'a':
return getPropsIfTheyExist(element, [
['text'],
['hash'],
]);
case 'form':
return getPropsIfTheyExist(element, [
['method']
]);
case 'input':
return getPropsIfTheyExist(element, [
['id'],
['name'],
['type'],
['required']
]);
case 'router-outlet':
const routerOutletProvider = debugElement.providerTokens.reduce((prev, curr) =>
prev ? prev : curr.name === 'RouterOutlet' ? curr : null, null);
return getPropsIfTheyExist(debugElement.injector.get(routerOutletProvider), [['name']]);
case 'NgSelectOption':
return (element) ? Description._getSelectOptionDesc(element) : [];
case 'NgIf':
return Description._getNgIfDesc(debugElement.componentInstance);
case 'NgControlName':
return Description._getControlNameDesc(debugElement.componentInstance);
case 'NgSwitch':
return Description._getNgSwitchDesc(debugElement.componentInstance);
case 'NgSwitchWhen':
case 'NgSwitchDefault':
return Description._getNgSwitchWhenDesc(debugElement.componentInstance);
}
return properties;
}
private static _getSelectOptionDesc(element: HTMLElement): Array<Property> {
return getPropsIfTheyExist(element, [
['label', 'innerText'],
]).concat([{key: 'value', value: element.getAttribute('value')}]);
}
private static _getControlNameDesc(instance: any): Array<Property> {
return getPropsIfTheyExist(instance, [
['name'],
['value'],
['valid'],
]);
}
private static _getNgSwitchDesc(instance: any): Array<Property> {
const properties = getPropsIfTheyExist(instance, [
['useDefault', '_useDefault'],
['switchDefault', '_switchValue'],
['valuesCount', '_valueViews'],
]);
properties
.filter(element => element.key === 'valuesCount')
.forEach(element => element.value = element.value ? element.value.size : 0);
return properties;
}
private static _getNgSwitchWhenDesc(instance: any): Array<Property> {
return getPropsIfTheyExist(instance, [
['value', '_value'],
]);
}
private static _getNgIfDesc(instance: any): Array<Property> {
return getPropsIfTheyExist(instance, [
['condition', '_prevCondition'],
]);
}
}