2015-05-18 11:57:20 -07:00
|
|
|
import {Injectable} from 'angular2/di';
|
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-18 11:57:20 -07:00
|
|
|
import {
|
|
|
|
ViewDefinition,
|
|
|
|
ProtoViewDto,
|
2015-06-09 15:20:33 +02:00
|
|
|
ViewType,
|
2015-05-18 11:57:20 -07:00
|
|
|
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-06-05 11:10:54 -07:00
|
|
|
import {PropertySetterFactory} from '../view/property_setter_factory';
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-06-05 11:10:54 -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.
|
|
|
|
*/
|
|
|
|
export class DomCompiler extends RenderCompiler {
|
2015-03-23 14:10:55 -07:00
|
|
|
_templateLoader: TemplateLoader;
|
|
|
|
_stepFactory: CompileStepFactory;
|
2015-06-05 11:10:07 -07:00
|
|
|
_propertySetterFactory: PropertySetterFactory;
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
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-06-05 11:10:07 -07:00
|
|
|
this._propertySetterFactory = new PropertySetterFactory();
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
compile(template: ViewDefinition): Promise<ProtoViewDto> {
|
2015-03-23 14:10:55 -07:00
|
|
|
var tplPromise = this._templateLoader.load(template);
|
2015-05-18 11:57:20 -07:00
|
|
|
return PromiseWrapper.then(
|
2015-06-09 15:20:33 +02:00
|
|
|
tplPromise, (el) => this._compileTemplate(template, el, ViewType.COMPONENT), (e) => {
|
2015-06-01 11:07:14 +02:00
|
|
|
throw new BaseException(
|
|
|
|
`Failed to load the template for "${template.componentId}" : ${e}`);
|
2015-05-18 11:57:20 -07:00
|
|
|
});
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
compileHost(directiveMetadata: DirectiveMetadata): Promise<ProtoViewDto> {
|
2015-04-27 15:14:30 -07:00
|
|
|
var hostViewDef = new ViewDefinition({
|
|
|
|
componentId: directiveMetadata.id,
|
2015-05-18 11:57:20 -07:00
|
|
|
absUrl: null, template: null,
|
2015-04-27 15:14:30 -07:00
|
|
|
directives: [directiveMetadata]
|
|
|
|
});
|
|
|
|
var element = DOM.createElement(directiveMetadata.selector);
|
2015-06-09 15:20:33 +02:00
|
|
|
return this._compileTemplate(hostViewDef, element, ViewType.HOST);
|
2015-04-27 15:14:30 -07:00
|
|
|
}
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-05-18 11:57:20 -07:00
|
|
|
_compileTemplate(viewDef: ViewDefinition, tplElement,
|
2015-06-09 15:20:33 +02:00
|
|
|
protoViewType: ViewType): Promise<ProtoViewDto> {
|
2015-04-27 15:14:30 -07:00
|
|
|
var subTaskPromises = [];
|
|
|
|
var pipeline = new CompilePipeline(this._stepFactory.createSteps(viewDef, subTaskPromises));
|
2015-05-11 17:59:39 -07:00
|
|
|
var compileElements = pipeline.process(tplElement, protoViewType, viewDef.componentId);
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-06-05 11:10:07 -07:00
|
|
|
var protoView = compileElements[0].inheritedProtoView.build(this._propertySetterFactory);
|
2015-03-23 14:10:55 -07:00
|
|
|
|
|
|
|
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-05-18 11:57:20 -07:00
|
|
|
constructor(parser: Parser, shadowDomStrategy: ShadowDomStrategy,
|
|
|
|
templateLoader: TemplateLoader) {
|
2015-04-07 20:54:20 -07:00
|
|
|
super(new DefaultStepFactory(parser, shadowDomStrategy), templateLoader);
|
|
|
|
}
|
2015-04-09 21:20:11 +02:00
|
|
|
}
|