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-08-16 16:59:06 -04:00
|
|
|
import {AnimationEntryMetadata, Compiler, ComponentMetadata, DirectiveMetadata, Injectable, Injector, Type, resolveForwardRef} from '@angular/core';
|
|
|
|
import {ViewMetadata} from '../core_private';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
|
|
|
import {DirectiveResolver} from '../src/directive_resolver';
|
2016-04-28 20:50:03 -04:00
|
|
|
import {Map} from '../src/facade/collection';
|
2016-08-10 21:21:28 -04:00
|
|
|
import {BaseException} from '../src/facade/exceptions';
|
|
|
|
import {isArray, isPresent, stringify} from '../src/facade/lang';
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
|
2016-08-10 18:55:18 -04:00
|
|
|
|
2015-12-03 18:49:09 -05:00
|
|
|
/**
|
|
|
|
* An implementation of {@link DirectiveResolver} that allows overriding
|
|
|
|
* various properties of directives.
|
|
|
|
*/
|
2015-12-03 12:06:42 -05:00
|
|
|
@Injectable()
|
2015-09-08 13:14:57 -04:00
|
|
|
export class MockDirectiveResolver extends DirectiveResolver {
|
2016-08-10 21:21:28 -04:00
|
|
|
private _directives = new Map<Type<any>, DirectiveMetadata>();
|
|
|
|
private _providerOverrides = new Map<Type<any>, any[]>();
|
|
|
|
private _viewProviderOverrides = new Map<Type<any>, any[]>();
|
|
|
|
private _views = new Map<Type<any>, ViewMetadata>();
|
|
|
|
private _inlineTemplates = new Map<Type<any>, string>();
|
|
|
|
private _animations = new Map<Type<any>, AnimationEntryMetadata[]>();
|
|
|
|
private _directiveOverrides = new Map<Type<any>, Map<Type<any>, Type<any>>>();
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2016-06-24 11:46:43 -04:00
|
|
|
constructor(private _injector: Injector) { super(); }
|
|
|
|
|
|
|
|
private get _compiler(): Compiler { return this._injector.get(Compiler); }
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
private _clearCacheFor(component: Type<any>) { this._compiler.clearCacheFor(component); }
|
2016-07-28 09:31:26 -04:00
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): DirectiveMetadata {
|
2016-07-28 09:45:22 -04:00
|
|
|
let metadata = this._directives.get(type);
|
|
|
|
if (!metadata) {
|
|
|
|
metadata = super.resolve(type, throwIfNotFound);
|
|
|
|
}
|
|
|
|
if (!metadata) {
|
2016-07-18 06:50:31 -04:00
|
|
|
return null;
|
|
|
|
}
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2016-07-28 09:31:26 -04:00
|
|
|
const providerOverrides = this._providerOverrides.get(type);
|
2016-07-28 09:45:22 -04:00
|
|
|
const viewProviderOverrides = this._viewProviderOverrides.get(type);
|
2015-09-08 13:14:57 -04:00
|
|
|
|
2016-07-28 09:45:22 -04:00
|
|
|
let providers = metadata.providers;
|
2015-10-11 01:11:13 -04:00
|
|
|
if (isPresent(providerOverrides)) {
|
2016-07-28 09:45:22 -04:00
|
|
|
const originalViewProviders: any[] = isPresent(metadata.providers) ? metadata.providers : [];
|
2016-01-06 17:13:44 -05:00
|
|
|
providers = originalViewProviders.concat(providerOverrides);
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 09:45:22 -04:00
|
|
|
if (metadata instanceof ComponentMetadata) {
|
|
|
|
let viewProviders = metadata.viewProviders;
|
2015-10-11 01:11:13 -04:00
|
|
|
if (isPresent(viewProviderOverrides)) {
|
2016-07-28 07:54:49 -04:00
|
|
|
const originalViewProviders: any[] =
|
|
|
|
isPresent(metadata.viewProviders) ? metadata.viewProviders : [];
|
2016-01-06 17:13:44 -05:00
|
|
|
viewProviders = originalViewProviders.concat(viewProviderOverrides);
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
|
2016-07-28 09:31:26 -04:00
|
|
|
let view = this._views.get(type);
|
|
|
|
if (!view) {
|
2016-07-28 09:45:22 -04:00
|
|
|
view = metadata;
|
2016-07-28 09:31:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const directives: any[] = [];
|
|
|
|
if (isPresent(view.directives)) {
|
|
|
|
flattenArray(view.directives, directives);
|
|
|
|
}
|
|
|
|
let animations = view.animations;
|
|
|
|
let templateUrl = view.templateUrl;
|
|
|
|
const directiveOverrides = this._directiveOverrides.get(type);
|
|
|
|
|
|
|
|
const inlineAnimations = this._animations.get(type);
|
|
|
|
if (isPresent(inlineAnimations)) {
|
|
|
|
animations = inlineAnimations;
|
|
|
|
}
|
|
|
|
|
|
|
|
let inlineTemplate = this._inlineTemplates.get(type);
|
|
|
|
if (isPresent(inlineTemplate)) {
|
|
|
|
templateUrl = null;
|
|
|
|
} else {
|
|
|
|
inlineTemplate = view.template;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isPresent(directiveOverrides) && isPresent(view.directives)) {
|
|
|
|
directiveOverrides.forEach((to, from) => {
|
|
|
|
var srcIndex = directives.indexOf(from);
|
|
|
|
if (srcIndex == -1) {
|
|
|
|
throw new BaseException(
|
|
|
|
`Overriden directive ${stringify(from)} not found in the template of ${stringify(type)}`);
|
|
|
|
}
|
|
|
|
directives[srcIndex] = to;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:14:57 -04:00
|
|
|
return new ComponentMetadata({
|
2016-07-28 09:45:22 -04:00
|
|
|
selector: metadata.selector,
|
|
|
|
inputs: metadata.inputs,
|
|
|
|
outputs: metadata.outputs,
|
|
|
|
host: metadata.host,
|
|
|
|
exportAs: metadata.exportAs,
|
|
|
|
moduleId: metadata.moduleId,
|
|
|
|
queries: metadata.queries,
|
|
|
|
changeDetection: metadata.changeDetection,
|
2015-10-11 01:11:13 -04:00
|
|
|
providers: providers,
|
2016-06-22 17:06:23 -04:00
|
|
|
viewProviders: viewProviders,
|
2016-07-28 09:45:22 -04:00
|
|
|
entryComponents: metadata.entryComponents,
|
2016-07-28 09:31:26 -04:00
|
|
|
template: inlineTemplate,
|
|
|
|
templateUrl: templateUrl,
|
|
|
|
directives: directives.length > 0 ? directives : null,
|
|
|
|
animations: animations,
|
|
|
|
styles: view.styles,
|
|
|
|
styleUrls: view.styleUrls,
|
|
|
|
pipes: view.pipes,
|
|
|
|
encapsulation: view.encapsulation,
|
|
|
|
interpolation: view.interpolation
|
2015-09-08 13:14:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return new DirectiveMetadata({
|
2016-07-28 09:45:22 -04:00
|
|
|
selector: metadata.selector,
|
|
|
|
inputs: metadata.inputs,
|
|
|
|
outputs: metadata.outputs,
|
|
|
|
host: metadata.host,
|
2015-10-11 01:11:13 -04:00
|
|
|
providers: providers,
|
2016-07-28 09:45:22 -04:00
|
|
|
exportAs: metadata.exportAs,
|
|
|
|
queries: metadata.queries
|
2015-09-08 13:14:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-07-28 09:45:22 -04:00
|
|
|
/**
|
|
|
|
* Overrides the {@link DirectiveMetadata} for a directive.
|
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
setDirective(type: Type<any>, metadata: DirectiveMetadata): void {
|
2016-07-28 09:45:22 -04:00
|
|
|
this._directives.set(type, metadata);
|
|
|
|
this._clearCacheFor(type);
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
setProvidersOverride(type: Type<any>, providers: any[]): void {
|
2015-11-01 19:37:24 -05:00
|
|
|
this._providerOverrides.set(type, providers);
|
2016-07-28 09:31:26 -04:00
|
|
|
this._clearCacheFor(type);
|
2015-10-11 01:11:13 -04:00
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
setViewProvidersOverride(type: Type<any>, viewProviders: any[]): void {
|
2016-07-28 09:45:22 -04:00
|
|
|
this._viewProviderOverrides.set(type, viewProviders);
|
2016-07-28 09:31:26 -04:00
|
|
|
this._clearCacheFor(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides the {@link ViewMetadata} for a component.
|
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
setView(component: Type<any>, view: ViewMetadata): void {
|
2016-07-28 09:31:26 -04:00
|
|
|
this._views.set(component, view);
|
|
|
|
this._clearCacheFor(component);
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* Overrides the inline template for a component - other configuration remains unchanged.
|
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
setInlineTemplate(component: Type<any>, template: string): void {
|
2016-07-28 09:31:26 -04:00
|
|
|
this._inlineTemplates.set(component, template);
|
|
|
|
this._clearCacheFor(component);
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
setAnimations(component: Type<any>, animations: AnimationEntryMetadata[]): void {
|
2016-07-28 09:31:26 -04:00
|
|
|
this._animations.set(component, animations);
|
|
|
|
this._clearCacheFor(component);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Overrides a directive from the component {@link ViewMetadata}.
|
|
|
|
*/
|
2016-08-10 21:21:28 -04:00
|
|
|
overrideViewDirective(component: Type<any>, from: Type<any>, to: Type<any>): void {
|
2016-07-28 09:31:26 -04:00
|
|
|
var overrides = this._directiveOverrides.get(component);
|
|
|
|
|
|
|
|
if (!overrides) {
|
2016-08-10 21:21:28 -04:00
|
|
|
overrides = new Map<Type<any>, Type<any>>();
|
2016-07-28 09:31:26 -04:00
|
|
|
this._directiveOverrides.set(component, overrides);
|
|
|
|
}
|
|
|
|
|
|
|
|
overrides.set(from, to);
|
|
|
|
this._clearCacheFor(component);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-10 21:21:28 -04:00
|
|
|
function flattenArray(tree: any[], out: Array<Type<any>|any[]>): void {
|
2016-07-28 09:31:26 -04:00
|
|
|
if (!isPresent(tree)) return;
|
|
|
|
for (var i = 0; i < tree.length; i++) {
|
|
|
|
var item = resolveForwardRef(tree[i]);
|
|
|
|
if (isArray(item)) {
|
|
|
|
flattenArray(item, out);
|
|
|
|
} else {
|
|
|
|
out.push(item);
|
|
|
|
}
|
2015-09-08 13:14:57 -04:00
|
|
|
}
|
|
|
|
}
|