directive_resolver_mock.js
6.08 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
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
import { DirectiveResolver } from '@angular/compiler';
import { Compiler, Component, Directive, Injectable, Injector, resolveForwardRef } from '@angular/core';
import { isPresent } from './facade/lang';
/**
* An implementation of {@link DirectiveResolver} that allows overriding
* various properties of directives.
*/
export var MockDirectiveResolver = (function (_super) {
__extends(MockDirectiveResolver, _super);
function MockDirectiveResolver(_injector) {
_super.call(this);
this._injector = _injector;
this._directives = new Map();
this._providerOverrides = new Map();
this._viewProviderOverrides = new Map();
this._views = new Map();
this._inlineTemplates = new Map();
this._animations = new Map();
}
Object.defineProperty(MockDirectiveResolver.prototype, "_compiler", {
get: function () { return this._injector.get(Compiler); },
enumerable: true,
configurable: true
});
MockDirectiveResolver.prototype._clearCacheFor = function (component) { this._compiler.clearCacheFor(component); };
MockDirectiveResolver.prototype.resolve = function (type, throwIfNotFound) {
if (throwIfNotFound === void 0) { throwIfNotFound = true; }
var metadata = this._directives.get(type);
if (!metadata) {
metadata = _super.prototype.resolve.call(this, type, throwIfNotFound);
}
if (!metadata) {
return null;
}
var providerOverrides = this._providerOverrides.get(type);
var viewProviderOverrides = this._viewProviderOverrides.get(type);
var providers = metadata.providers;
if (isPresent(providerOverrides)) {
var originalViewProviders = metadata.providers || [];
providers = originalViewProviders.concat(providerOverrides);
}
if (metadata instanceof Component) {
var viewProviders = metadata.viewProviders;
if (isPresent(viewProviderOverrides)) {
var originalViewProviders = metadata.viewProviders || [];
viewProviders = originalViewProviders.concat(viewProviderOverrides);
}
var view = this._views.get(type);
if (!view) {
view = metadata;
}
var animations = view.animations;
var templateUrl = view.templateUrl;
var inlineAnimations = this._animations.get(type);
if (isPresent(inlineAnimations)) {
animations = inlineAnimations;
}
var inlineTemplate = this._inlineTemplates.get(type);
if (isPresent(inlineTemplate)) {
templateUrl = null;
}
else {
inlineTemplate = view.template;
}
return new Component({
selector: metadata.selector,
inputs: metadata.inputs,
outputs: metadata.outputs,
host: metadata.host,
exportAs: metadata.exportAs,
moduleId: metadata.moduleId,
queries: metadata.queries,
changeDetection: metadata.changeDetection,
providers: providers,
viewProviders: viewProviders,
entryComponents: metadata.entryComponents,
template: inlineTemplate,
templateUrl: templateUrl,
animations: animations,
styles: view.styles,
styleUrls: view.styleUrls,
encapsulation: view.encapsulation,
interpolation: view.interpolation
});
}
return new Directive({
selector: metadata.selector,
inputs: metadata.inputs,
outputs: metadata.outputs,
host: metadata.host,
providers: providers,
exportAs: metadata.exportAs,
queries: metadata.queries
});
};
/**
* Overrides the {@link Directive} for a directive.
*/
MockDirectiveResolver.prototype.setDirective = function (type, metadata) {
this._directives.set(type, metadata);
this._clearCacheFor(type);
};
MockDirectiveResolver.prototype.setProvidersOverride = function (type, providers) {
this._providerOverrides.set(type, providers);
this._clearCacheFor(type);
};
MockDirectiveResolver.prototype.setViewProvidersOverride = function (type, viewProviders) {
this._viewProviderOverrides.set(type, viewProviders);
this._clearCacheFor(type);
};
/**
* Overrides the {@link ViewMetadata} for a component.
*/
MockDirectiveResolver.prototype.setView = function (component, view) {
this._views.set(component, view);
this._clearCacheFor(component);
};
/**
* Overrides the inline template for a component - other configuration remains unchanged.
*/
MockDirectiveResolver.prototype.setInlineTemplate = function (component, template) {
this._inlineTemplates.set(component, template);
this._clearCacheFor(component);
};
MockDirectiveResolver.prototype.setAnimations = function (component, animations) {
this._animations.set(component, animations);
this._clearCacheFor(component);
};
MockDirectiveResolver.decorators = [
{ type: Injectable },
];
/** @nocollapse */
MockDirectiveResolver.ctorParameters = function () { return [
{ type: Injector, },
]; };
return MockDirectiveResolver;
}(DirectiveResolver));
function flattenArray(tree, out) {
if (!isPresent(tree))
return;
for (var i = 0; i < tree.length; i++) {
var item = resolveForwardRef(tree[i]);
if (Array.isArray(item)) {
flattenArray(item, out);
}
else {
out.push(item);
}
}
}
//# sourceMappingURL=directive_resolver_mock.js.map