2015-02-24 09:32:44 -05:00
|
|
|
import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection';
|
2015-03-30 09:45:03 -04:00
|
|
|
import {Type, isPresent, BaseException, stringify, isBlank} from 'angular2/src/facade/lang';
|
2015-02-24 09:32:44 -05:00
|
|
|
|
2015-04-28 21:17:00 -04:00
|
|
|
import {View} from 'angular2/src/core/annotations_impl/view';
|
2015-02-24 09:32:44 -05:00
|
|
|
import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
|
2015-02-24 09:32:44 -05:00
|
|
|
export class MockTemplateResolver extends TemplateResolver {
|
2015-05-15 19:42:52 -04:00
|
|
|
_views: Map<Type, View>;
|
2015-03-30 09:45:03 -04:00
|
|
|
_inlineTemplates: Map<Type, string>;
|
2015-05-15 19:42:52 -04:00
|
|
|
_viewCache: Map<Type, View>;
|
2015-05-20 20:19:46 -04:00
|
|
|
_directiveOverrides: Map<Type, Map<Type, Type>>;
|
2015-02-24 09:32:44 -05:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2015-05-15 19:42:52 -04:00
|
|
|
this._views = MapWrapper.create();
|
2015-03-30 09:45:03 -04:00
|
|
|
this._inlineTemplates = MapWrapper.create();
|
2015-05-15 19:42:52 -04:00
|
|
|
this._viewCache = MapWrapper.create();
|
2015-03-30 09:45:03 -04:00
|
|
|
this._directiveOverrides = MapWrapper.create();
|
2015-02-24 09:32:44 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:45:03 -04:00
|
|
|
/**
|
2015-04-17 16:01:07 -04:00
|
|
|
* Overrides the {@link View} 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-04-09 15:20:11 -04:00
|
|
|
setView(component: Type, view: View): void {
|
2015-03-30 09:45:03 -04:00
|
|
|
this._checkOverrideable(component);
|
2015-05-15 19:42:52 -04:00
|
|
|
MapWrapper.set(this._views, 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);
|
|
|
|
MapWrapper.set(this._inlineTemplates, component, template);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-17 16:01:07 -04:00
|
|
|
* Overrides a directive from the component {@link View}.
|
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);
|
|
|
|
|
|
|
|
var overrides = MapWrapper.get(this._directiveOverrides, component);
|
|
|
|
|
|
|
|
if (isBlank(overrides)) {
|
|
|
|
overrides = MapWrapper.create();
|
|
|
|
MapWrapper.set(this._directiveOverrides, component, overrides);
|
|
|
|
}
|
|
|
|
|
|
|
|
MapWrapper.set(overrides, from, to);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-17 16:01:07 -04:00
|
|
|
* Returns the {@link View} for a component:
|
2015-05-15 19:42:52 -04:00
|
|
|
* - Set the {@link View} to the overridden view when it exists or fallback to the default
|
2015-05-20 20:19:46 -04:00
|
|
|
* `TemplateResolver`,
|
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-04-09 15:20:11 -04:00
|
|
|
resolve(component: Type): View {
|
2015-05-15 19:42:52 -04:00
|
|
|
var view = MapWrapper.get(this._viewCache, component);
|
2015-04-09 15:20:11 -04:00
|
|
|
if (isPresent(view)) return view;
|
2015-02-24 09:32:44 -05:00
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
view = MapWrapper.get(this._views, 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-03-30 09:45:03 -04:00
|
|
|
var overrides = MapWrapper.get(this._directiveOverrides, component);
|
|
|
|
|
|
|
|
if (isPresent(overrides) && isPresent(directives)) {
|
2015-04-09 15:20:11 -04:00
|
|
|
directives = ListWrapper.clone(view.directives);
|
2015-03-30 09:45:03 -04:00
|
|
|
MapWrapper.forEach(overrides, (to, from) => {
|
|
|
|
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-05-20 20:19:46 -04:00
|
|
|
view = new View(
|
|
|
|
{template: view.template, templateUrl: view.templateUrl, directives: directives});
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
var inlineTemplate = MapWrapper.get(this._inlineTemplates, component);
|
|
|
|
if (isPresent(inlineTemplate)) {
|
2015-05-20 20:19:46 -04:00
|
|
|
view = new View({template: inlineTemplate, templateUrl: null, directives: view.directives});
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
|
|
|
|
2015-05-15 19:42:52 -04:00
|
|
|
MapWrapper.set(this._viewCache, component, view);
|
2015-04-09 15:20:11 -04:00
|
|
|
return view;
|
2015-03-30 09:45:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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-05-15 19:42:52 -04:00
|
|
|
var cached = MapWrapper.get(this._viewCache, 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
|
|
|
}
|
|
|
|
}
|