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