forked from rangle/augury
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirective-resolver.ts
More file actions
175 lines (154 loc) · 5.13 KB
/
directive-resolver.ts
File metadata and controls
175 lines (154 loc) · 5.13 KB
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
173
174
175
import {Type, isPresent, isBlank, stringify} from '@angular/compiler/src/facade/lang';
import {BaseException} from '@angular/compiler/src/facade/exceptions';
import {ListWrapper, StringMapWrapper} from '@angular/compiler/src/facade/collection';
import {
resolveForwardRef,
Injectable,
DirectiveMetadata,
ComponentMetadata,
InputMetadata,
OutputMetadata,
HostBindingMetadata,
HostListenerMetadata,
ContentChildrenMetadata,
ViewChildrenMetadata,
ContentChildMetadata,
ViewChildMetadata,
reflector
} from '@angular/core';
function _isDirectiveMetadata(type: any): boolean {
let className: string = type.constructor.toString().match(/\w+/g)[1];
return className === 'DirectiveMetadata';
}
function _isComponentMetadata(type: any): boolean {
let className: string = type.constructor.toString().match(/\w+/g)[1];
return className === 'ComponentMetadata';
}
/*
* Resolve a `Type` for {@link DirectiveMetadata}.
*
* This interface can be overridden by the application
developer to create custom behavior.
*
* See {@link Compiler}
*/
@Injectable()
export class DirectiveResolver {
/**
* Return {@link DirectiveMetadata} for a given `Type`.
*/
resolve(type: Type): DirectiveMetadata {
let typeMetadata: any = reflector.annotations(resolveForwardRef(type));
if (isPresent(typeMetadata)) {
let metadata = typeMetadata.find(_isDirectiveMetadata) ||
typeMetadata.find(_isComponentMetadata);
if (isPresent(metadata)) {
let propertyMetadata = reflector.propMetadata(type);
return this._mergeWithPropertyMetadata
(metadata, propertyMetadata, type);
}
}
throw new BaseException(`No Directive annotation
found on ${stringify(type)}`);
}
private _mergeWithPropertyMetadata(dm: DirectiveMetadata,
propertyMetadata: { [key: string]: any[] },
directiveType: Type): DirectiveMetadata {
let inputs = [];
let outputs = [];
let host: { [key: string]: string } = {};
let queries: { [key: string]: any } = {};
StringMapWrapper.forEach(propertyMetadata,
(metadata: any[], propName: string) => {
metadata.forEach(a => {
if (a instanceof InputMetadata) {
if (isPresent(a.bindingPropertyName)) {
inputs.push(`${propName}: ${a.bindingPropertyName}`);
} else {
inputs.push(propName);
}
}
if (a instanceof OutputMetadata) {
if (isPresent(a.bindingPropertyName)) {
outputs.push(`${propName}: ${a.bindingPropertyName}`);
} else {
outputs.push(propName);
}
}
if (a instanceof HostBindingMetadata) {
if (isPresent(a.hostPropertyName)) {
host[`[${a.hostPropertyName}]`] = propName;
} else {
host[`[${propName}]`] = propName;
}
}
if (a instanceof HostListenerMetadata) {
let args = isPresent(a.args) ? (<any[]>a.args).join(', ') : '';
host[`(${a.eventName})`] = `${propName}(${args})`;
}
if (a instanceof ContentChildrenMetadata) {
queries[propName] = a;
}
if (a instanceof ViewChildrenMetadata) {
queries[propName] = a;
}
if (a instanceof ContentChildMetadata) {
queries[propName] = a;
}
if (a instanceof ViewChildMetadata) {
queries[propName] = a;
}
});
});
return this._merge(dm, inputs, outputs, host, queries, directiveType);
}
private _merge(dm: DirectiveMetadata, inputs: string[], outputs: string[],
host: { [key: string]: string }, queries: { [key: string]: any },
directiveType: Type): DirectiveMetadata {
let mergedInputs = isPresent(dm.inputs) ?
ListWrapper.concat(dm.inputs, inputs) : inputs;
let mergedOutputs;
if (isPresent(dm.outputs)) {
dm.outputs.forEach((propName: string) => {
if (ListWrapper.contains(outputs, propName)) {
throw new BaseException(
`Output event '${propName}' defined multiple
times in '${stringify(directiveType)}'`);
}
});
mergedOutputs = ListWrapper.concat(dm.outputs, outputs);
} else {
mergedOutputs = outputs;
}
let mergedHost = isPresent(dm.host) ?
StringMapWrapper.merge(dm.host, host) : host;
let mergedQueries =
isPresent(dm.queries) ?
StringMapWrapper.merge(dm.queries, queries) : queries;
if (dm instanceof ComponentMetadata) {
return new ComponentMetadata({
selector: dm.selector,
inputs: mergedInputs,
outputs: mergedOutputs,
host: mergedHost,
exportAs: dm.exportAs,
moduleId: dm.moduleId,
queries: mergedQueries,
changeDetection: dm.changeDetection,
providers: dm.providers,
viewProviders: dm.viewProviders
});
} else {
return new DirectiveMetadata({
selector: dm.selector,
inputs: mergedInputs,
outputs: mergedOutputs,
host: mergedHost,
exportAs: dm.exportAs,
queries: mergedQueries,
providers: dm.providers
});
}
}
}
export let CODEGEN_DIRECTIVE_RESOLVER = new DirectiveResolver();