2016-06-23 12:47:54 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
import {AnimationEntryMetadata, BaseException, Compiler, Injectable, Injector, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-04-28 20:50:03 -04:00
|
|
|
import {ViewResolver} from '../index';
|
|
|
|
import {Map} from '../src/facade/collection';
|
2016-06-08 19:38:52 -04:00
|
|
|
import {isArray, isBlank, isPresent, stringify} from '../src/facade/lang';
|
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
|
|
|
_directiveOverrides = new Map<Type, Map<Type, Type>>();
|
2015-02-24 09:32:44 -05:00
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
constructor(private _injector: Injector) { super(); }
|
|
|
|
|
|
|
|
private get _compiler(): Compiler { return this._injector.get(Compiler); }
|
|
|
|
|
|
|
|
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
|
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-06-17 19:21:40 -04:00
|
|
|
this._views.set(component, view);
|
2016-06-24 11:46:43 -04:00
|
|
|
this._clearCacheFor(component);
|
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 {
|
2015-06-17 19:21:40 -04:00
|
|
|
this._inlineTemplates.set(component, template);
|
2016-06-24 11:46:43 -04:00
|
|
|
this._clearCacheFor(component);
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
|
|
|
|
2016-05-25 15:46:22 -04:00
|
|
|
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
|
|
|
|
this._animations.set(component, animations);
|
2016-06-24 11:46:43 -04:00
|
|
|
this._clearCacheFor(component);
|
2016-05-25 15:46:22 -04:00
|
|
|
}
|
|
|
|
|
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-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);
|
2016-06-24 11:46:43 -04:00
|
|
|
this._clearCacheFor(component);
|
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
|
|
|
*/
|
2016-07-18 06:50:31 -04:00
|
|
|
resolve(component: Type, throwIfNotFound = true): ViewMetadata {
|
2016-06-24 11:46:43 -04:00
|
|
|
var view = this._views.get(component);
|
2015-04-09 15:20:11 -04:00
|
|
|
if (isBlank(view)) {
|
2016-07-18 06:50:31 -04:00
|
|
|
view = super.resolve(component, throwIfNotFound);
|
|
|
|
if (!view) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-24 09:32:44 -05:00
|
|
|
}
|
|
|
|
|
2016-06-08 18:45:15 -04:00
|
|
|
var directives: any[] /** TODO #9100 */ = [];
|
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,
|
2016-06-20 12:52:41 -04:00
|
|
|
encapsulation: view.encapsulation,
|
|
|
|
interpolation: view.interpolation
|
2016-05-25 15:46:22 -04:00
|
|
|
});
|
2015-03-30 09:45:03 -04:00
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
return view;
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
2015-02-24 09:32:44 -05:00
|
|
|
}
|
2016-04-25 02:11:30 -04:00
|
|
|
|
2016-06-08 19:38:52 -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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|