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-09 15:20:11 -04:00
|
|
|
import {View} from 'angular2/src/core/annotations/view';
|
2015-02-24 09:32:44 -05:00
|
|
|
import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver';
|
|
|
|
|
|
|
|
export class MockTemplateResolver extends TemplateResolver {
|
2015-04-09 15:20:11 -04:00
|
|
|
_templates: Map<Type, View>;
|
2015-03-30 09:45:03 -04:00
|
|
|
_inlineTemplates: Map<Type, string>;
|
2015-04-09 15:20:11 -04:00
|
|
|
_templateCache: Map<Type, View>;
|
2015-03-30 09:45:03 -04:00
|
|
|
_directiveOverrides: Map<Type, Type>;
|
2015-02-24 09:32:44 -05:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
2015-03-30 09:45:03 -04:00
|
|
|
this._templates = MapWrapper.create();
|
|
|
|
this._inlineTemplates = MapWrapper.create();
|
|
|
|
this._templateCache = MapWrapper.create();
|
|
|
|
this._directiveOverrides = MapWrapper.create();
|
2015-02-24 09:32:44 -05:00
|
|
|
}
|
|
|
|
|
2015-03-30 09:45:03 -04:00
|
|
|
/**
|
2015-04-09 15:20:11 -04:00
|
|
|
* Overrides the [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-04-09 15:20:11 -04:00
|
|
|
MapWrapper.set(this._templates, 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-09 15:20:11 -04:00
|
|
|
* Overrides a directive from the component [View].
|
2015-03-30 09:45:03 -04:00
|
|
|
*
|
|
|
|
* @param {Type} component
|
|
|
|
* @param {Type} from
|
|
|
|
* @param {Type} to
|
|
|
|
*/
|
|
|
|
overrideTemplateDirective(component: Type, from: Type, to: Type): void {
|
|
|
|
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-09 15:20:11 -04:00
|
|
|
* Returns the [View] for a component:
|
|
|
|
* - Set the [View] to the overridden template when it exists or fallback to the default
|
|
|
|
* [TemplateResolver], see [setView]
|
2015-03-30 09:45:03 -04:00
|
|
|
* - Override the directives, see [overrideTemplateDirective]
|
2015-04-09 15:20:11 -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 {
|
|
|
|
var view = MapWrapper.get(this._templateCache, component);
|
|
|
|
if (isPresent(view)) return view;
|
2015-02-24 09:32:44 -05:00
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
view = MapWrapper.get(this._templates, component);
|
|
|
|
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) {
|
|
|
|
throw new BaseException(`Overriden directive ${stringify(from)} not found in the template of ${stringify(component)}`);
|
|
|
|
}
|
|
|
|
directives[srcIndex] = to;
|
|
|
|
});
|
2015-04-09 15:20:11 -04:00
|
|
|
view = new View({
|
|
|
|
template: view.template,
|
|
|
|
templateUrl: view.templateUrl,
|
2015-03-30 09:45:03 -04:00
|
|
|
directives: directives,
|
2015-04-09 15:20:11 -04:00
|
|
|
formatters: view.formatters,
|
|
|
|
source: view.source,
|
|
|
|
locale: view.locale,
|
|
|
|
device: view.device
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
var inlineTemplate = MapWrapper.get(this._inlineTemplates, component);
|
|
|
|
if (isPresent(inlineTemplate)) {
|
2015-04-09 15:20:11 -04:00
|
|
|
view = new View({
|
|
|
|
template: inlineTemplate,
|
|
|
|
templateUrl: null,
|
|
|
|
directives: view.directives,
|
|
|
|
formatters: view.formatters,
|
|
|
|
source: view.source,
|
|
|
|
locale: view.locale,
|
|
|
|
device: view.device
|
2015-03-30 09:45:03 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-09 15:20:11 -04:00
|
|
|
MapWrapper.set(this._templateCache, component, view);
|
|
|
|
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 {
|
|
|
|
var cached = MapWrapper.get(this._templateCache, component);
|
|
|
|
|
|
|
|
if (isPresent(cached)) {
|
|
|
|
throw new BaseException(`The component ${stringify(component)} has already been compiled, its configuration can not be changed`);
|
|
|
|
}
|
2015-02-24 09:32:44 -05:00
|
|
|
}
|
|
|
|
}
|