2015-05-04 11:11:22 -07:00
|
|
|
import {Injectable} from 'angular2/src/di/annotations_impl';
|
2015-04-07 20:54:20 -07:00
|
|
|
|
2015-03-23 14:10:55 -07:00
|
|
|
import {PromiseWrapper, Promise} from 'angular2/src/facade/async';
|
2015-05-06 10:17:38 -07:00
|
|
|
import {BaseException, isPresent} from 'angular2/src/facade/lang';
|
2015-04-27 15:14:30 -07:00
|
|
|
import {DOM} from 'angular2/src/dom/dom_adapter';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-05-06 10:17:38 -07:00
|
|
|
import {ViewDefinition, ProtoViewDto, DirectiveMetadata, RenderCompiler, RenderProtoViewRef} from '../../api';
|
2015-03-23 14:10:55 -07:00
|
|
|
import {CompilePipeline} from './compile_pipeline';
|
2015-04-02 14:40:49 -07:00
|
|
|
import {TemplateLoader} from 'angular2/src/render/dom/compiler/template_loader';
|
2015-04-07 20:54:20 -07:00
|
|
|
import {CompileStepFactory, DefaultStepFactory} from './compile_step_factory';
|
|
|
|
import {Parser} from 'angular2/change_detection';
|
|
|
|
import {ShadowDomStrategy} from '../shadow_dom/shadow_dom_strategy';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-04-02 14:40:49 -07:00
|
|
|
/**
|
|
|
|
* The compiler loads and translates the html templates of components into
|
|
|
|
* nested ProtoViews. To decompose its functionality it uses
|
|
|
|
* the CompilePipeline and the CompileSteps.
|
|
|
|
*/
|
2015-05-06 10:17:38 -07:00
|
|
|
export class DomCompiler extends RenderCompiler {
|
2015-03-23 14:10:55 -07:00
|
|
|
_templateLoader: TemplateLoader;
|
|
|
|
_stepFactory: CompileStepFactory;
|
|
|
|
|
|
|
|
constructor(stepFactory: CompileStepFactory, templateLoader: TemplateLoader) {
|
2015-05-06 10:17:38 -07:00
|
|
|
super();
|
2015-03-23 14:10:55 -07:00
|
|
|
this._templateLoader = templateLoader;
|
|
|
|
this._stepFactory = stepFactory;
|
|
|
|
}
|
|
|
|
|
2015-04-09 21:20:11 +02:00
|
|
|
compile(template: ViewDefinition):Promise<ProtoViewDto> {
|
2015-03-23 14:10:55 -07:00
|
|
|
var tplPromise = this._templateLoader.load(template);
|
|
|
|
return PromiseWrapper.then(tplPromise,
|
|
|
|
(el) => this._compileTemplate(template, el),
|
|
|
|
(_) => { throw new BaseException(`Failed to load the template "${template.componentId}"`); }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-27 15:14:30 -07:00
|
|
|
compileHost(directiveMetadata: DirectiveMetadata):Promise<ProtoViewDto> {
|
|
|
|
var hostViewDef = new ViewDefinition({
|
|
|
|
componentId: directiveMetadata.id,
|
|
|
|
absUrl: null,
|
|
|
|
template: null,
|
|
|
|
directives: [directiveMetadata]
|
|
|
|
});
|
|
|
|
var element = DOM.createElement(directiveMetadata.selector);
|
|
|
|
return this._compileTemplate(hostViewDef, element);
|
|
|
|
}
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-04-27 15:14:30 -07:00
|
|
|
_compileTemplate(viewDef: ViewDefinition, tplElement):Promise<ProtoViewDto> {
|
|
|
|
var subTaskPromises = [];
|
|
|
|
var pipeline = new CompilePipeline(this._stepFactory.createSteps(viewDef, subTaskPromises));
|
|
|
|
var compileElements = pipeline.process(tplElement, viewDef.componentId);
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
var protoView = compileElements[0].inheritedProtoView.build();
|
|
|
|
|
|
|
|
if (subTaskPromises.length > 0) {
|
|
|
|
return PromiseWrapper.all(subTaskPromises).then((_) => protoView);
|
|
|
|
} else {
|
|
|
|
return PromiseWrapper.resolve(protoView);
|
|
|
|
}
|
|
|
|
}
|
2015-04-07 20:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2015-05-06 10:17:38 -07:00
|
|
|
export class DefaultDomCompiler extends DomCompiler {
|
2015-04-07 20:54:20 -07:00
|
|
|
constructor(parser:Parser, shadowDomStrategy:ShadowDomStrategy, templateLoader: TemplateLoader) {
|
|
|
|
super(new DefaultStepFactory(parser, shadowDomStrategy), templateLoader);
|
|
|
|
}
|
2015-04-09 21:20:11 +02:00
|
|
|
}
|