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-30 21:07:40 -04:00
|
|
|
import {DirectiveResolver} from '@angular/compiler';
|
2016-09-12 22:14:17 -04:00
|
|
|
import {AnimationEntryMetadata, Compiler, Component, Directive, Injectable, Injector, Provider, Type, resolveForwardRef} from '@angular/core';
|
2016-06-08 19:38:52 -04:00
|
|
|
|
2016-08-30 21:07:40 -04:00
|
|
|
import {Map} from './facade/collection';
|
|
|
|
import {isArray, isPresent} from './facade/lang';
|
|
|
|
import {ViewMetadata} from './private_import_core';
|
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-09-12 22:14:17 -04:00
|
|
|
private _directives = new Map<Type<any>, Directive>();
|
2016-08-10 21:21:28 -04:00
|
|
|
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[]>();
|
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-09-12 22:14:17 -04:00
|
|
|
resolve(type: Type<any>, throwIfNotFound = true): Directive {
|
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-08-24 16:39:44 -04:00
|
|
|
const originalViewProviders: Provider[] =
|
|
|
|
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-09-12 22:14:17 -04:00
|
|
|
if (metadata instanceof Component) {
|
2016-07-28 09:45:22 -04:00
|
|
|
let viewProviders = metadata.viewProviders;
|
2015-10-11 01:11:13 -04:00
|
|
|
if (isPresent(viewProviderOverrides)) {
|
2016-08-24 16:39:44 -04:00
|
|
|
const originalViewProviders: Provider[] =
|
2016-07-28 07:54:49 -04:00
|
|
|
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-09-12 12:44:20 -04:00
|
|
|
view = <any>metadata;
|
2016-07-28 09:31:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
let animations = view.animations;
|
|
|
|
let templateUrl = view.templateUrl;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-09-12 22:14:17 -04:00
|
|
|
return new Component({
|
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,
|
|
|
|
animations: animations,
|
|
|
|
styles: view.styles,
|
|
|
|
styleUrls: view.styleUrls,
|
|
|
|
encapsulation: view.encapsulation,
|
|
|
|
interpolation: view.interpolation
|
2015-09-08 13:14:57 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-09-12 22:14:17 -04:00
|
|
|
return new Directive({
|
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
|
|
|
/**
|
2016-09-12 22:14:17 -04:00
|
|
|
* Overrides the {@link Directive} for a directive.
|
2016-07-28 09:45:22 -04:00
|
|
|
*/
|
2016-09-12 22:14:17 -04:00
|
|
|
setDirective(type: Type<any>, metadata: Directive): void {
|
2016-07-28 09:45:22 -04:00
|
|
|
this._directives.set(type, metadata);
|
|
|
|
this._clearCacheFor(type);
|
|
|
|
}
|
|
|
|
|
2016-08-24 16:39:44 -04:00
|
|
|
setProvidersOverride(type: Type<any>, providers: Provider[]): 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-24 16:39:44 -04:00
|
|
|
setViewProvidersOverride(type: Type<any>, viewProviders: Provider[]): 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|