|
1 | | -import {Type} from 'facade/lang'; |
2 | | -import {Promise} from 'facade/async'; |
3 | | -import {Element} from 'facade/dom'; |
4 | | -//import {ProtoView} from './view'; |
| 1 | +import {Type, FIELD, isBlank, isPresent} from 'facade/lang'; |
| 2 | +import {Promise, PromiseWrapper} from 'facade/async'; |
| 3 | +import {List, ListWrapper} from 'facade/collection'; |
| 4 | +import {DOM, Element} from 'facade/dom'; |
| 5 | + |
| 6 | +import {Parser} from 'change_detection/parser/parser'; |
| 7 | +import {ClosureMap} from 'change_detection/parser/closure_map'; |
| 8 | + |
| 9 | +import {Reflector} from './reflector'; |
| 10 | +import {ProtoView} from './view'; |
| 11 | +import {CompilePipeline} from './pipeline/compile_pipeline'; |
| 12 | +import {CompileElement} from './pipeline/compile_element'; |
| 13 | +import {createDefaultSteps} from './pipeline/default_steps'; |
5 | 14 | import {TemplateLoader} from './template_loader'; |
6 | | -import {FIELD} from 'facade/lang'; |
| 15 | +import {AnnotatedType} from './annotated_type'; |
7 | 16 |
|
| 17 | +/** |
| 18 | + * The compiler loads and translates the html templates of components into |
| 19 | + * nested ProtoViews. To decompose its functionality it uses |
| 20 | + * the CompilePipeline and the CompileSteps. |
| 21 | + */ |
8 | 22 | export class Compiler { |
9 | | - |
10 | | - @FIELD('final _templateLoader:TemplateLoader') |
11 | | - constructor(templateLoader:TemplateLoader) { |
| 23 | + constructor(templateLoader:TemplateLoader, reflector: Reflector, parser:Parser, closureMap:ClosureMap) { |
12 | 24 | this._templateLoader = templateLoader; |
| 25 | + this._reflector = reflector; |
| 26 | + this._parser = parser; |
| 27 | + this._closureMap = closureMap; |
13 | 28 | } |
14 | 29 |
|
15 | | - /** |
16 | | - * # Why promise? |
17 | | - * - compilation will load templates. Instantiating views before templates are loaded will |
18 | | - * complicate the Directive code. BENEFIT: view instantiation become synchrnous. |
19 | | - * # Why result that is independent of injector? |
20 | | - * - don't know about injector in deserialization |
21 | | - * - compile does not need the injector, only the ViewFactory does |
22 | | - */ |
23 | | - compile(component:Type, element:Element/* = null*/):Promise/*<ProtoView>*/ { |
24 | | - return null; |
| 30 | + createSteps(component:AnnotatedType):List<CompileStep> { |
| 31 | + var directives = component.annotation.template.directives; |
| 32 | + var annotatedDirectives = ListWrapper.create(); |
| 33 | + for (var i=0; i<directives.length; i++) { |
| 34 | + ListWrapper.push(annotatedDirectives, this._reflector.annotatedType(directives[i])); |
| 35 | + } |
| 36 | + return createDefaultSteps(this._parser, this._closureMap, annotatedDirectives); |
25 | 37 | } |
26 | 38 |
|
| 39 | + compile(component:Type, templateRoot:Element = null):Promise<ProtoView> { |
| 40 | + // TODO load all components transitively from the cache first |
| 41 | + var cache = null; |
| 42 | + return PromiseWrapper.resolve(this._compileAllCached( |
| 43 | + this._reflector.annotatedType(component), |
| 44 | + cache, |
| 45 | + templateRoot) |
| 46 | + ); |
| 47 | + } |
27 | 48 |
|
| 49 | + _compileAllCached(component:AnnotatedType, cache, templateRoot:Element = null):ProtoView { |
| 50 | + if (isBlank(templateRoot)) { |
| 51 | + // TODO: read out the cache if templateRoot = null. Could contain: |
| 52 | + // - templateRoot string |
| 53 | + // - precompiled template |
| 54 | + // - ProtoView |
| 55 | + templateRoot = DOM.createTemplate(component.annotation.template.inline); |
| 56 | + } |
| 57 | + var pipeline = new CompilePipeline(this.createSteps(component)); |
| 58 | + var compileElements = pipeline.process(templateRoot); |
| 59 | + var rootProtoView = compileElements[0].inheritedProtoView; |
| 60 | + // TODO: put the rootProtoView into the cache to support recursive templates! |
| 61 | + |
| 62 | + for (var i=0; i<compileElements.length; i++) { |
| 63 | + var ce = compileElements[i]; |
| 64 | + if (isPresent(ce.componentDirective)) { |
| 65 | + ce.inheritedElementBinder.nestedProtoView = this._compileAllCached(ce.componentDirective, cache, null); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + return rootProtoView; |
| 70 | + } |
28 | 71 | } |
0 commit comments