forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_resolver_mock.ts
More file actions
130 lines (112 loc) · 4.03 KB
/
template_resolver_mock.ts
File metadata and controls
130 lines (112 loc) · 4.03 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
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
import {Type, isPresent, BaseException, stringify, isBlank} from 'angular2/src/facade/lang';
import {View} from 'angular2/src/core/annotations_impl/view';
import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';
export class MockTemplateResolver extends TemplateResolver {
_views: Map<Type, View>;
_inlineTemplates: Map<Type, string>;
_viewCache: Map<Type, View>;
_directiveOverrides: Map<Type, Map<Type, Type>>;
constructor() {
super();
this._views = MapWrapper.create();
this._inlineTemplates = MapWrapper.create();
this._viewCache = MapWrapper.create();
this._directiveOverrides = MapWrapper.create();
}
/**
* Overrides the {@link View} for a component.
*
* @param {Type} component
* @param {ViewDefinition} view
*/
setView(component: Type, view: View): void {
this._checkOverrideable(component);
MapWrapper.set(this._views, component, view);
}
/**
* Overrides the inline template for a component - other configuration remains unchanged.
*
* @param {Type} component
* @param {string} template
*/
setInlineTemplate(component: Type, template: string): void {
this._checkOverrideable(component);
MapWrapper.set(this._inlineTemplates, component, template);
}
/**
* Overrides a directive from the component {@link View}.
*
* @param {Type} component
* @param {Type} from
* @param {Type} to
*/
overrideViewDirective(component: Type, from: Type, to: Type): void {
this._checkOverrideable(component);
var overrides = MapWrapper.get(this._directiveOverrides, component);
if (isBlank(overrides)) {
overrides = MapWrapper.create();
MapWrapper.set(this._directiveOverrides, component, overrides);
}
MapWrapper.set(overrides, from, to);
}
/**
* Returns the {@link View} for a component:
* - Set the {@link View} to the overridden view when it exists or fallback to the default
* `TemplateResolver`,
* see `setView`.
* - Override the directives, see `overrideViewDirective`.
* - Override the @View definition, see `setInlineTemplate`.
*
* @param component
* @returns {ViewDefinition}
*/
resolve(component: Type): View {
var view = MapWrapper.get(this._viewCache, component);
if (isPresent(view)) return view;
view = MapWrapper.get(this._views, component);
if (isBlank(view)) {
view = super.resolve(component);
}
if (isBlank(view)) {
// dynamic components
return null;
}
var directives = view.directives;
var overrides = MapWrapper.get(this._directiveOverrides, component);
if (isPresent(overrides) && isPresent(directives)) {
directives = ListWrapper.clone(view.directives);
MapWrapper.forEach(overrides, (to, from) => {
var srcIndex = directives.indexOf(from);
if (srcIndex == -1) {
throw new BaseException(
`Overriden directive ${stringify(from)} not found in the template of ${stringify(component)}`);
}
directives[srcIndex] = to;
});
view = new View(
{template: view.template, templateUrl: view.templateUrl, directives: directives});
}
var inlineTemplate = MapWrapper.get(this._inlineTemplates, component);
if (isPresent(inlineTemplate)) {
view = new View({template: inlineTemplate, templateUrl: null, directives: view.directives});
}
MapWrapper.set(this._viewCache, component, view);
return view;
}
/**
* Once a component has been compiled, the AppProtoView is stored in the compiler cache.
*
* Then it should not be possible to override the component configuration after the component
* has been compiled.
*
* @param {Type} component
*/
_checkOverrideable(component: Type): void {
var cached = MapWrapper.get(this._viewCache, component);
if (isPresent(cached)) {
throw new BaseException(
`The component ${stringify(component)} has already been compiled, its configuration can not be changed`);
}
}
}