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-06-24 10:43:36 +02:00
|
|
|
import {ViewLoader} from 'angular2/src/render/dom/compiler/view_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-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-06-24 10:43:36 +02:00
|
|
|
constructor(public _stepFactory: CompileStepFactory, public _viewLoader: ViewLoader) { super(); }
|
2015-03-23 14:10:55 -07:00
|
|
|
|
2015-06-15 15:57:42 +02:00
|
|
|
compile(view: ViewDefinition): Promise<ProtoViewDto> {
|
2015-06-24 10:43:36 +02:00
|
|
|
var tplPromise = this._viewLoader.load(view);
|
2015-05-18 11:57:20 -07:00
|
|
|
return PromiseWrapper.then(
|
2015-06-15 15:57:42 +02:00
|
|
|
tplPromise, (el) => this._compileTemplate(view, el, ViewType.COMPONENT), (e) => {
|
|
|
|
throw new BaseException(`Failed to load the template for "${view.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-06-10 14:40:24 +02:00
|
|
|
templateAbsUrl: null, template: null,
|
|
|
|
styles: null,
|
|
|
|
styleAbsUrls: 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-06-15 15:57:42 +02:00
|
|
|
var pipeline = new CompilePipeline(this._stepFactory.createSteps(viewDef));
|
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-15 15:57:42 +02:00
|
|
|
return PromiseWrapper.resolve(compileElements[0].inheritedProtoView.build());
|
2015-03-23 14:10:55 -07:00
|
|
|
}
|
2015-04-07 20:54:20 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
@Injectable()
|
2015-05-06 10:17:38 -07:00
|
|
|
export class DefaultDomCompiler extends DomCompiler {
|
2015-06-24 10:43:36 +02:00
|
|
|
constructor(parser: Parser, shadowDomStrategy: ShadowDomStrategy, viewLoader: ViewLoader) {
|
|
|
|
super(new DefaultStepFactory(parser, shadowDomStrategy), viewLoader);
|
2015-04-07 20:54:20 -07:00
|
|
|
}
|
2015-04-09 21:20:11 +02:00
|
|
|
}
|