refactor(core): remove ViewResolver and ViewResolverMock

The methods on `ViewResolverMock` have been merged into `DirectiveResolver`.

BREAKING CHANGE:
- ES5 users can no longer use the `View(…)` function to provide `ViewMetadata`.
  This mirrors the removal of the `@View` decorator a while ago.
This commit is contained in:
Tobias Bosch
2016-07-28 06:31:26 -07:00
parent 20b03bad11
commit 0988cc82b0
25 changed files with 308 additions and 535 deletions

View File

@ -6,12 +6,12 @@
* found in the LICENSE file at https://angular.io/license
*/
import {Compiler, ComponentMetadata, DirectiveMetadata, Injectable, Injector} from '@angular/core';
import {AnimationEntryMetadata, Compiler, ComponentMetadata, DirectiveMetadata, Injectable, Injector, ViewMetadata, resolveForwardRef} from '@angular/core';
import {DirectiveResolver} from '../src/directive_resolver';
import {Map} from '../src/facade/collection';
import {Type, isPresent} from '../src/facade/lang';
import {BaseException} from '../src/facade/exceptions';
import {Type, isArray, isPresent, stringify} from '../src/facade/lang';
/**
@ -22,33 +22,75 @@ import {Type, isPresent} from '../src/facade/lang';
export class MockDirectiveResolver extends DirectiveResolver {
private _providerOverrides = new Map<Type, any[]>();
private viewProviderOverrides = new Map<Type, any[]>();
private _views = new Map<Type, ViewMetadata>();
private _inlineTemplates = new Map<Type, string>();
private _animations = new Map<Type, AnimationEntryMetadata[]>();
private _directiveOverrides = new Map<Type, Map<Type, Type>>();
constructor(private _injector: Injector) { super(); }
private get _compiler(): Compiler { return this._injector.get(Compiler); }
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
resolve(type: Type, throwIfNotFound = true): DirectiveMetadata {
var dm = super.resolve(type, throwIfNotFound);
const dm = super.resolve(type, throwIfNotFound);
if (!dm) {
return null;
}
var providerOverrides = this._providerOverrides.get(type);
var viewProviderOverrides = this.viewProviderOverrides.get(type);
const providerOverrides = this._providerOverrides.get(type);
const viewProviderOverrides = this.viewProviderOverrides.get(type);
var providers = dm.providers;
let providers = dm.providers;
if (isPresent(providerOverrides)) {
var originalViewProviders: any[] = isPresent(dm.providers) ? dm.providers : [];
const originalViewProviders: any[] = isPresent(dm.providers) ? dm.providers : [];
providers = originalViewProviders.concat(providerOverrides);
}
if (dm instanceof ComponentMetadata) {
var viewProviders = dm.viewProviders;
let viewProviders = dm.viewProviders;
if (isPresent(viewProviderOverrides)) {
var originalViewProviders: any[] = isPresent(dm.viewProviders) ? dm.viewProviders : [];
const originalViewProviders: any[] = isPresent(dm.viewProviders) ? dm.viewProviders : [];
viewProviders = originalViewProviders.concat(viewProviderOverrides);
}
let view = this._views.get(type);
if (!view) {
view = dm;
}
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;
});
}
return new ComponentMetadata({
selector: dm.selector,
inputs: dm.inputs,
@ -60,7 +102,16 @@ export class MockDirectiveResolver extends DirectiveResolver {
changeDetection: dm.changeDetection,
providers: providers,
viewProviders: viewProviders,
entryComponents: dm.entryComponents
entryComponents: dm.entryComponents,
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
});
}
@ -77,11 +128,58 @@ export class MockDirectiveResolver extends DirectiveResolver {
setProvidersOverride(type: Type, providers: any[]): void {
this._providerOverrides.set(type, providers);
this._compiler.clearCacheFor(type);
this._clearCacheFor(type);
}
setViewProvidersOverride(type: Type, viewProviders: any[]): void {
this.viewProviderOverrides.set(type, viewProviders);
this._compiler.clearCacheFor(type);
this._clearCacheFor(type);
}
/**
* Overrides the {@link ViewMetadata} for a component.
*/
setView(component: Type, view: ViewMetadata): void {
this._views.set(component, view);
this._clearCacheFor(component);
}
/**
* Overrides the inline template for a component - other configuration remains unchanged.
*/
setInlineTemplate(component: Type, template: string): void {
this._inlineTemplates.set(component, template);
this._clearCacheFor(component);
}
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
this._animations.set(component, animations);
this._clearCacheFor(component);
}
/**
* Overrides a directive from the component {@link ViewMetadata}.
*/
overrideViewDirective(component: Type, from: Type, to: Type): void {
var overrides = this._directiveOverrides.get(component);
if (!overrides) {
overrides = new Map<Type, Type>();
this._directiveOverrides.set(component, overrides);
}
overrides.set(from, to);
this._clearCacheFor(component);
}
}
function flattenArray(tree: any[], out: Array<Type|any[]>): void {
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);
}
}
}

View File

@ -9,7 +9,7 @@
import {AnimationEntryMetadata, Compiler, ComponentFactory, Inject, Injectable, Injector, NgZone, ViewMetadata} from '@angular/core';
import {ComponentFixture, ComponentFixtureNoNgZone, TestBed, TestComponentBuilder} from '@angular/core/testing';
import {DirectiveResolver, ViewResolver} from '../index';
import {DirectiveResolver} from '../index';
import {MapWrapper} from '../src/facade/collection';
import {ConcreteType, IS_DART, Type, isPresent} from '../src/facade/lang';
@ -101,15 +101,14 @@ export class OverridingTestComponentBuilder extends TestComponentBuilder {
private _applyMetadataOverrides() {
let mockDirectiveResolver = this._injector.get(DirectiveResolver);
let mockViewResolver = this._injector.get(ViewResolver);
this._viewOverrides.forEach((view, type) => { mockViewResolver.setView(type, view); });
this._viewOverrides.forEach((view, type) => { mockDirectiveResolver.setView(type, view); });
this._templateOverrides.forEach(
(template, type) => mockViewResolver.setInlineTemplate(type, template));
(template, type) => mockDirectiveResolver.setInlineTemplate(type, template));
this._animationOverrides.forEach(
(animationsEntry, type) => mockViewResolver.setAnimations(type, animationsEntry));
(animationsEntry, type) => mockDirectiveResolver.setAnimations(type, animationsEntry));
this._directiveOverrides.forEach((overrides, component) => {
overrides.forEach(
(to, from) => { mockViewResolver.overrideViewDirective(component, from, to); });
(to, from) => { mockDirectiveResolver.overrideViewDirective(component, from, to); });
});
this._bindingsOverrides.forEach(
(bindings, type) => mockDirectiveResolver.setProvidersOverride(type, bindings));

View File

@ -1,141 +0,0 @@
/**
* @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
*/
import {AnimationEntryMetadata, BaseException, Compiler, Injectable, Injector, Type, ViewMetadata, resolveForwardRef} from '@angular/core';
import {ViewResolver} from '../index';
import {Map} from '../src/facade/collection';
import {isArray, isBlank, isPresent, stringify} from '../src/facade/lang';
@Injectable()
export class MockViewResolver extends ViewResolver {
/** @internal */
_views = new Map<Type, ViewMetadata>();
/** @internal */
_inlineTemplates = new Map<Type, string>();
/** @internal */
_animations = new Map<Type, AnimationEntryMetadata[]>();
/** @internal */
_directiveOverrides = new Map<Type, Map<Type, Type>>();
constructor(private _injector: Injector) { super(); }
private get _compiler(): Compiler { return this._injector.get(Compiler); }
private _clearCacheFor(component: Type) { this._compiler.clearCacheFor(component); }
/**
* Overrides the {@link ViewMetadata} for a component.
*/
setView(component: Type, view: ViewMetadata): void {
this._views.set(component, view);
this._clearCacheFor(component);
}
/**
* Overrides the inline template for a component - other configuration remains unchanged.
*/
setInlineTemplate(component: Type, template: string): void {
this._inlineTemplates.set(component, template);
this._clearCacheFor(component);
}
setAnimations(component: Type, animations: AnimationEntryMetadata[]): void {
this._animations.set(component, animations);
this._clearCacheFor(component);
}
/**
* Overrides a directive from the component {@link ViewMetadata}.
*/
overrideViewDirective(component: Type, from: Type, to: Type): void {
var overrides = this._directiveOverrides.get(component);
if (isBlank(overrides)) {
overrides = new Map<Type, Type>();
this._directiveOverrides.set(component, overrides);
}
overrides.set(from, to);
this._clearCacheFor(component);
}
/**
* Returns the {@link ViewMetadata} for a component:
* - Set the {@link ViewMetadata} to the overridden view when it exists or fallback to the default
* `ViewResolver`,
* see `setView`.
* - Override the directives, see `overrideViewDirective`.
* - Override the @View definition, see `setInlineTemplate`.
*/
resolve(component: Type, throwIfNotFound = true): ViewMetadata {
var view = this._views.get(component);
if (isBlank(view)) {
view = super.resolve(component, throwIfNotFound);
if (!view) {
return null;
}
}
var directives: any[] /** TODO #9100 */ = [];
if (isPresent(view.directives)) {
flattenArray(view.directives, directives);
}
var animations = view.animations;
var templateUrl = view.templateUrl;
var overrides = this._directiveOverrides.get(component);
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;
}
if (isPresent(overrides) && isPresent(view.directives)) {
overrides.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(component)}`);
}
directives[srcIndex] = to;
});
}
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,
interpolation: view.interpolation
});
return view;
}
}
function flattenArray(tree: any[], out: Array<Type|any[]>): void {
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);
}
}
}