|
1 | 1 | import {Map, MapWrapper, ListWrapper} from 'angular2/src/facade/collection'; |
2 | | -import {Type, isPresent} from 'angular2/src/facade/lang'; |
| 2 | +import {Type, isPresent, BaseException, stringify, isBlank} from 'angular2/src/facade/lang'; |
3 | 3 |
|
4 | 4 | import {Template} from 'angular2/src/core/annotations/template'; |
5 | 5 | import {TemplateResolver} from 'angular2/src/core/compiler/template_resolver'; |
6 | 6 |
|
7 | 7 | export class MockTemplateResolver extends TemplateResolver { |
8 | | - _cmpTemplates: Map; |
| 8 | + _templates: Map<Type, Template>; |
| 9 | + _inlineTemplates: Map<Type, string>; |
| 10 | + _templateCache: Map<Type, Template>; |
| 11 | + _directiveOverrides: Map<Type, Type>; |
9 | 12 |
|
10 | 13 | constructor() { |
11 | 14 | super(); |
12 | | - this._cmpTemplates = MapWrapper.create(); |
| 15 | + this._templates = MapWrapper.create(); |
| 16 | + this._inlineTemplates = MapWrapper.create(); |
| 17 | + this._templateCache = MapWrapper.create(); |
| 18 | + this._directiveOverrides = MapWrapper.create(); |
13 | 19 | } |
14 | 20 |
|
15 | | - setTemplate(component: Type, template: Template) { |
16 | | - MapWrapper.set(this._cmpTemplates, component, template); |
| 21 | + /** |
| 22 | + * Overrides the [Template] for a component. |
| 23 | + * |
| 24 | + * @param {Type} component |
| 25 | + * @param {Template} template |
| 26 | + */ |
| 27 | + setTemplate(component: Type, template: Template): void { |
| 28 | + this._checkOverrideable(component); |
| 29 | + MapWrapper.set(this._templates, component, template); |
17 | 30 | } |
18 | 31 |
|
| 32 | + /** |
| 33 | + * Overrides the inline template for a component - other configuration remains unchanged. |
| 34 | + * |
| 35 | + * @param {Type} component |
| 36 | + * @param {string} template |
| 37 | + */ |
| 38 | + setInlineTemplate(component: Type, template: string): void { |
| 39 | + this._checkOverrideable(component); |
| 40 | + MapWrapper.set(this._inlineTemplates, component, template); |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * Overrides a directive from the component [Template]. |
| 45 | + * |
| 46 | + * @param {Type} component |
| 47 | + * @param {Type} from |
| 48 | + * @param {Type} to |
| 49 | + */ |
| 50 | + overrideTemplateDirective(component: Type, from: Type, to: Type): void { |
| 51 | + this._checkOverrideable(component); |
| 52 | + |
| 53 | + var overrides = MapWrapper.get(this._directiveOverrides, component); |
| 54 | + |
| 55 | + if (isBlank(overrides)) { |
| 56 | + overrides = MapWrapper.create(); |
| 57 | + MapWrapper.set(this._directiveOverrides, component, overrides); |
| 58 | + } |
| 59 | + |
| 60 | + MapWrapper.set(overrides, from, to); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Returns the [Template] for a component: |
| 65 | + * - Set the [Template] to the overridden template when it exists or fallback to the default |
| 66 | + * [TemplateResolver], see [setTemplate] |
| 67 | + * - Override the directives, see [overrideTemplateDirective] |
| 68 | + * - Override the template definition, see [setInlineTemplate] |
| 69 | + * |
| 70 | + * @param component |
| 71 | + * @returns {Template} |
| 72 | + */ |
19 | 73 | resolve(component: Type): Template { |
20 | | - var override = MapWrapper.get(this._cmpTemplates, component); |
| 74 | + var template = MapWrapper.get(this._templateCache, component); |
| 75 | + if (isPresent(template)) return template; |
21 | 76 |
|
22 | | - if (isPresent(override)) { |
23 | | - return override; |
| 77 | + template = MapWrapper.get(this._templates, component); |
| 78 | + if (isBlank(template)) { |
| 79 | + template = super.resolve(component); |
24 | 80 | } |
25 | 81 |
|
26 | | - return super.resolve(component); |
| 82 | + var directives = template.directives; |
| 83 | + var overrides = MapWrapper.get(this._directiveOverrides, component); |
| 84 | + |
| 85 | + if (isPresent(overrides) && isPresent(directives)) { |
| 86 | + directives = ListWrapper.clone(template.directives); |
| 87 | + MapWrapper.forEach(overrides, (to, from) => { |
| 88 | + var srcIndex = directives.indexOf(from); |
| 89 | + if (srcIndex == -1) { |
| 90 | + throw new BaseException(`Overriden directive ${stringify(from)} not found in the template of ${stringify(component)}`); |
| 91 | + } |
| 92 | + directives[srcIndex] = to; |
| 93 | + }); |
| 94 | + template = new Template({ |
| 95 | + inline: template.inline, |
| 96 | + url: template.url, |
| 97 | + directives: directives, |
| 98 | + formatters: template.formatters, |
| 99 | + source: template.source, |
| 100 | + locale: template.locale, |
| 101 | + device: template.device, |
| 102 | + }); |
| 103 | + } |
| 104 | + |
| 105 | + var inlineTemplate = MapWrapper.get(this._inlineTemplates, component); |
| 106 | + if (isPresent(inlineTemplate)) { |
| 107 | + template = new Template({ |
| 108 | + inline: inlineTemplate, |
| 109 | + url: null, |
| 110 | + directives: template.directives, |
| 111 | + formatters: template.formatters, |
| 112 | + source: template.source, |
| 113 | + locale: template.locale, |
| 114 | + device: template.device, |
| 115 | + }); |
| 116 | + } |
| 117 | + |
| 118 | + MapWrapper.set(this._templateCache, component, template); |
| 119 | + return template; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Once a component has been compiled, the ProtoView is stored in the compiler cache. |
| 124 | + * |
| 125 | + * Then it should not be possible to override the component configuration after the component |
| 126 | + * has been compiled. |
| 127 | + * |
| 128 | + * @param {Type} component |
| 129 | + */ |
| 130 | + _checkOverrideable(component: Type): void { |
| 131 | + var cached = MapWrapper.get(this._templateCache, component); |
| 132 | + |
| 133 | + if (isPresent(cached)) { |
| 134 | + throw new BaseException(`The component ${stringify(component)} has already been compiled, its configuration can not be changed`); |
| 135 | + } |
27 | 136 | } |
28 | 137 | } |
0 commit comments