2016-05-26 15:07:51 -07:00
|
|
|
import {Injectable, ViewMetadata, Type, BaseException, AnimationEntryMetadata, resolveForwardRef} from '@angular/core';
|
2016-04-28 17:50:03 -07:00
|
|
|
import {ViewResolver} from '../index';
|
|
|
|
import {Map} from '../src/facade/collection';
|
|
|
|
import {isPresent, stringify, isBlank, isArray} from '../src/facade/lang';
|
2015-02-24 15:32:44 +01:00
|
|
|
|
2015-12-03 09:06:42 -08:00
|
|
|
@Injectable()
|
2015-06-24 10:54:04 +02:00
|
|
|
export class MockViewResolver extends ViewResolver {
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_views = new Map<Type, ViewMetadata>();
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_inlineTemplates = new Map<Type, string>();
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2016-05-25 12:46:22 -07:00
|
|
|
_animations = new Map<Type, AnimationEntryMetadata[]>();
|
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_viewCache = new Map<Type, ViewMetadata>();
|
2015-10-09 17:21:25 -07:00
|
|
|
/** @internal */
|
2015-09-29 11:11:06 -07:00
|
|
|
_directiveOverrides = new Map<Type, Map<Type, Type>>();
|
2015-02-24 15:32:44 +01:00
|
|
|
|
2015-06-24 11:10:29 +02:00
|
|
|
constructor() { super(); }
|
2015-02-24 15:32:44 +01:00
|
|
|
|
2015-03-30 15:45:03 +02:00
|
|
|
/**
|
2015-08-14 10:03:45 -07:00
|
|
|
* Overrides the {@link ViewMetadata} for a component.
|
2015-03-30 15:45:03 +02:00
|
|
|
*/
|
2015-08-14 10:03:45 -07:00
|
|
|
setView(component: Type, view: ViewMetadata): void {
|
2015-03-30 15:45:03 +02:00
|
|
|
this._checkOverrideable(component);
|
2015-06-17 16:21:40 -07:00
|
|
|
this._views.set(component, view);
|
2015-02-24 15:32:44 +01:00
|
|
|
}
|
2015-03-30 15:45:03 +02:00
|
|
|
/**
|
|
|
|
* Overrides the inline template for a component - other configuration remains unchanged.
|
|
|
|
*/
|
|
|
|
setInlineTemplate(component: Type, template: string): void {
|
|
|
|
this._checkOverrideable(component);
|
2015-06-17 16:21:40 -07:00
|
|
|
this._inlineTemplates.set(component, template);
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
|
|
|
|
2016-05-25 12:46:22 -07:00
|
|
|
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
|
|
|
|
this._checkOverrideable(component);
|
|
|
|
this._animations.set(component, animations);
|
|
|
|
}
|
|
|
|
|
2015-03-30 15:45:03 +02:00
|
|
|
/**
|
2015-08-14 10:03:45 -07:00
|
|
|
* Overrides a directive from the component {@link ViewMetadata}.
|
2015-03-30 15:45:03 +02:00
|
|
|
*/
|
2015-05-15 16:42:52 -07:00
|
|
|
overrideViewDirective(component: Type, from: Type, to: Type): void {
|
2015-03-30 15:45:03 +02:00
|
|
|
this._checkOverrideable(component);
|
|
|
|
|
2015-06-17 16:21:40 -07:00
|
|
|
var overrides = this._directiveOverrides.get(component);
|
2015-03-30 15:45:03 +02:00
|
|
|
|
|
|
|
if (isBlank(overrides)) {
|
2015-09-29 11:11:06 -07:00
|
|
|
overrides = new Map<Type, Type>();
|
2015-06-17 16:21:40 -07:00
|
|
|
this._directiveOverrides.set(component, overrides);
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
|
|
|
|
2015-06-17 16:21:40 -07:00
|
|
|
overrides.set(from, to);
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 10:03:45 -07:00
|
|
|
* Returns the {@link ViewMetadata} for a component:
|
|
|
|
* - Set the {@link ViewMetadata} to the overridden view when it exists or fallback to the default
|
2015-06-24 10:54:04 +02:00
|
|
|
* `ViewResolver`,
|
2015-04-17 13:01:07 -07:00
|
|
|
* see `setView`.
|
2015-05-15 16:42:52 -07:00
|
|
|
* - Override the directives, see `overrideViewDirective`.
|
2015-04-17 13:01:07 -07:00
|
|
|
* - Override the @View definition, see `setInlineTemplate`.
|
2015-03-30 15:45:03 +02:00
|
|
|
*/
|
2015-08-14 10:03:45 -07:00
|
|
|
resolve(component: Type): ViewMetadata {
|
2015-06-17 16:21:40 -07:00
|
|
|
var view = this._viewCache.get(component);
|
2015-04-09 21:20:11 +02:00
|
|
|
if (isPresent(view)) return view;
|
2015-02-24 15:32:44 +01:00
|
|
|
|
2015-06-17 16:21:40 -07:00
|
|
|
view = this._views.get(component);
|
2015-04-09 21:20:11 +02:00
|
|
|
if (isBlank(view)) {
|
|
|
|
view = super.resolve(component);
|
2015-02-24 15:32:44 +01:00
|
|
|
}
|
|
|
|
|
2016-04-24 23:11:30 -07:00
|
|
|
var directives = [];
|
2016-05-25 12:46:22 -07:00
|
|
|
if (isPresent(view.directives)) {
|
|
|
|
flattenArray(view.directives, directives);
|
|
|
|
}
|
|
|
|
var animations = view.animations;
|
|
|
|
var templateUrl = view.templateUrl;
|
2015-06-17 16:21:40 -07:00
|
|
|
var overrides = this._directiveOverrides.get(component);
|
2015-03-30 15:45:03 +02:00
|
|
|
|
2016-05-25 12:46:22 -07:00
|
|
|
var inlineAnimations = this._animations.get(component);
|
|
|
|
if (isPresent(inlineAnimations)) {
|
|
|
|
animations = inlineAnimations;
|
|
|
|
}
|
|
|
|
|
|
|
|
var inlineTemplate = this._inlineTemplates.get(component);
|
|
|
|
if (isPresent(inlineTemplate)) {
|
|
|
|
templateUrl = null;
|
|
|
|
} else {
|
|
|
|
inlineTemplate = view.template;
|
|
|
|
}
|
|
|
|
|
2016-04-24 23:11:30 -07:00
|
|
|
if (isPresent(overrides) && isPresent(view.directives)) {
|
2015-10-08 16:01:18 -07:00
|
|
|
overrides.forEach((to, from) => {
|
2015-03-30 15:45:03 +02:00
|
|
|
var srcIndex = directives.indexOf(from);
|
|
|
|
if (srcIndex == -1) {
|
2015-05-20 17:19:46 -07:00
|
|
|
throw new BaseException(
|
|
|
|
`Overriden directive ${stringify(from)} not found in the template of ${stringify(component)}`);
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
|
|
|
directives[srcIndex] = to;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-05-25 12:46:22 -07:00
|
|
|
view = new ViewMetadata({
|
|
|
|
template: inlineTemplate,
|
|
|
|
templateUrl: templateUrl,
|
|
|
|
directives: directives.length > 0 ? directives : null,
|
|
|
|
animations: animations,
|
|
|
|
styles: view.styles,
|
|
|
|
styleUrls: view.styleUrls,
|
|
|
|
pipes: view.pipes,
|
|
|
|
encapsulation: view.encapsulation
|
|
|
|
});
|
2015-03-30 15:45:03 +02:00
|
|
|
|
2015-06-17 16:21:40 -07:00
|
|
|
this._viewCache.set(component, view);
|
2015-04-09 21:20:11 +02:00
|
|
|
return view;
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-10-09 17:21:25 -07:00
|
|
|
* @internal
|
|
|
|
*
|
2015-04-09 21:20:11 +02:00
|
|
|
* Once a component has been compiled, the AppProtoView is stored in the compiler cache.
|
2015-03-30 15:45:03 +02:00
|
|
|
*
|
|
|
|
* Then it should not be possible to override the component configuration after the component
|
|
|
|
* has been compiled.
|
|
|
|
*/
|
|
|
|
_checkOverrideable(component: Type): void {
|
2015-06-17 16:21:40 -07:00
|
|
|
var cached = this._viewCache.get(component);
|
2015-03-30 15:45:03 +02:00
|
|
|
|
|
|
|
if (isPresent(cached)) {
|
2015-05-20 17:19:46 -07:00
|
|
|
throw new BaseException(
|
|
|
|
`The component ${stringify(component)} has already been compiled, its configuration can not be changed`);
|
2015-03-30 15:45:03 +02:00
|
|
|
}
|
2015-02-24 15:32:44 +01:00
|
|
|
}
|
|
|
|
}
|
2016-04-24 23:11:30 -07:00
|
|
|
|
|
|
|
function flattenArray(tree: any[], out: Array<Type | any[]>): void {
|
2016-05-25 12:46:22 -07:00
|
|
|
if (!isPresent(tree)) return;
|
2016-04-24 23:11:30 -07:00
|
|
|
for (var i = 0; i < tree.length; i++) {
|
|
|
|
var item = resolveForwardRef(tree[i]);
|
|
|
|
if (isArray(item)) {
|
|
|
|
flattenArray(item, out);
|
|
|
|
} else {
|
|
|
|
out.push(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|