2015-11-06 20:34:07 -05:00
|
|
|
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {Type, isPresent, stringify, isBlank} from 'angular2/src/facade/lang';
|
|
|
|
import {BaseException, WrappedException} from 'angular2/src/facade/exceptions';
|
2015-02-24 09:32:44 -05:00
|
|
|
|
2015-08-14 13:03:45 -04:00
|
|
|
import {ViewMetadata} from '../core/metadata';
|
2015-10-02 10:37:23 -04:00
|
|
|
import {ViewResolver} from 'angular2/src/core/linker/view_resolver';
|
2015-02-24 09:32:44 -05:00
|
|
|
|
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 */
|
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
|
|
|
*
|
|
|
|
* @param {Type} component
|
2015-04-09 15:20:11 -04:00
|
|
|
* @param {ViewDefinition} view
|
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.
|
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {string} template
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-08-14 13:03:45 -04:00
|
|
|
* Overrides a directive from the component {@link ViewMetadata}.
|
2015-03-30 09:45:03 -04:00
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {Type} from
|
|
|
|
* @param {Type} to
|
|
|
|
*/
|
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
|
|
|
*
|
|
|
|
* @param component
|
2015-04-09 15:20:11 -04:00
|
|
|
* @returns {ViewDefinition}
|
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
|
|
|
}
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
var directives = view.directives;
|
2015-06-17 19:21:40 -04:00
|
|
|
var overrides = this._directiveOverrides.get(component);
|
2015-03-30 09:45:03 -04:00
|
|
|
|
|
|
|
if (isPresent(overrides) && isPresent(directives)) {
|
2015-04-09 15:20:11 -04:00
|
|
|
directives = ListWrapper.clone(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;
|
|
|
|
});
|
2015-08-14 13:03:45 -04:00
|
|
|
view = new ViewMetadata(
|
2015-05-20 20:19:46 -04:00
|
|
|
{template: view.template, templateUrl: view.templateUrl, directives: directives});
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
|
|
|
|
2015-06-17 19:21:40 -04:00
|
|
|
var inlineTemplate = this._inlineTemplates.get(component);
|
2015-03-30 09:45:03 -04:00
|
|
|
if (isPresent(inlineTemplate)) {
|
2015-08-14 13:03:45 -04:00
|
|
|
view = new ViewMetadata(
|
|
|
|
{template: inlineTemplate, templateUrl: null, directives: view.directives});
|
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.
|
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
*/
|
|
|
|
_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
|
|
|
}
|
|
|
|
}
|