2015-08-25 15:36:02 -07:00
|
|
|
import {AST} from 'angular2/src/core/change_detection/change_detection';
|
|
|
|
import {isPresent} from 'angular2/src/core/facade/lang';
|
|
|
|
import {DirectiveMetadata} from './api';
|
|
|
|
|
|
|
|
export interface TemplateAst {
|
|
|
|
sourceInfo: string;
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any;
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class TextAst implements TemplateAst {
|
|
|
|
constructor(public value: string, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitText(this, context); }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BoundTextAst implements TemplateAst {
|
|
|
|
constructor(public value: AST, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitBoundText(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class AttrAst implements TemplateAst {
|
|
|
|
constructor(public name: string, public value: string, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any { return visitor.visitAttr(this, context); }
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
export class BoundElementPropertyAst implements TemplateAst {
|
|
|
|
constructor(public name: string, public type: PropertyBindingType, public value: AST,
|
|
|
|
public unit: string, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitElementProperty(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BoundEventAst implements TemplateAst {
|
2015-08-27 16:29:02 -07:00
|
|
|
constructor(public name: string, public target: string, public handler: AST,
|
|
|
|
public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitEvent(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class VariableAst implements TemplateAst {
|
|
|
|
constructor(public name: string, public value: string, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitVariable(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class ElementAst implements TemplateAst {
|
2015-08-27 16:29:02 -07:00
|
|
|
constructor(public attrs: AttrAst[], public properties: BoundElementPropertyAst[],
|
2015-08-25 15:36:02 -07:00
|
|
|
public events: BoundEventAst[], public vars: VariableAst[],
|
2015-08-27 16:29:02 -07:00
|
|
|
public directives: DirectiveAst[], public children: TemplateAst[],
|
2015-08-25 15:36:02 -07:00
|
|
|
public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitElement(this, context);
|
|
|
|
}
|
|
|
|
|
|
|
|
isBound(): boolean {
|
|
|
|
return (this.properties.length > 0 || this.events.length > 0 || this.vars.length > 0 ||
|
|
|
|
this.directives.length > 0);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class EmbeddedTemplateAst implements TemplateAst {
|
2015-08-27 16:29:02 -07:00
|
|
|
constructor(public attrs: AttrAst[], public vars: VariableAst[],
|
|
|
|
public directives: DirectiveAst[], public children: TemplateAst[],
|
|
|
|
public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitEmbeddedTemplate(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
export class BoundDirectivePropertyAst implements TemplateAst {
|
|
|
|
constructor(public directiveName: string, public templateName: string, public value: AST,
|
|
|
|
public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitDirectiveProperty(this, context);
|
|
|
|
}
|
2015-08-27 16:29:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
export class DirectiveAst implements TemplateAst {
|
|
|
|
constructor(public directive: DirectiveMetadata, public properties: BoundDirectivePropertyAst[],
|
|
|
|
public hostProperties: BoundElementPropertyAst[], public hostEvents: BoundEventAst[],
|
|
|
|
public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitDirective(this, context);
|
|
|
|
}
|
2015-08-27 16:29:02 -07:00
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export class NgContentAst implements TemplateAst {
|
|
|
|
constructor(public select: string, public sourceInfo: string) {}
|
2015-09-01 16:24:23 -07:00
|
|
|
visit(visitor: TemplateAstVisitor, context: any): any {
|
|
|
|
return visitor.visitNgContent(this, context);
|
|
|
|
}
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
2015-08-27 16:29:02 -07:00
|
|
|
export enum PropertyBindingType {
|
|
|
|
Property,
|
|
|
|
Attribute,
|
|
|
|
Class,
|
|
|
|
Style
|
|
|
|
}
|
|
|
|
|
2015-08-25 15:36:02 -07:00
|
|
|
export interface TemplateAstVisitor {
|
2015-09-01 16:24:23 -07:00
|
|
|
visitNgContent(ast: NgContentAst, context: any): any;
|
|
|
|
visitEmbeddedTemplate(ast: EmbeddedTemplateAst, context: any): any;
|
|
|
|
visitElement(ast: ElementAst, context: any): any;
|
|
|
|
visitVariable(ast: VariableAst, context: any): any;
|
|
|
|
visitEvent(ast: BoundEventAst, context: any): any;
|
|
|
|
visitElementProperty(ast: BoundElementPropertyAst, context: any): any;
|
|
|
|
visitAttr(ast: AttrAst, context: any): any;
|
|
|
|
visitBoundText(ast: BoundTextAst, context: any): any;
|
|
|
|
visitText(ast: TextAst, context: any): any;
|
|
|
|
visitDirective(ast: DirectiveAst, context: any): any;
|
|
|
|
visitDirectiveProperty(ast: BoundDirectivePropertyAst, context: any): any;
|
2015-08-25 15:36:02 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-09-01 16:24:23 -07:00
|
|
|
export function templateVisitAll(visitor: TemplateAstVisitor, asts: TemplateAst[],
|
|
|
|
context: any = null): any[] {
|
2015-08-25 15:36:02 -07:00
|
|
|
var result = [];
|
|
|
|
asts.forEach(ast => {
|
2015-09-01 16:24:23 -07:00
|
|
|
var astResult = ast.visit(visitor, context);
|
2015-08-25 15:36:02 -07:00
|
|
|
if (isPresent(astResult)) {
|
|
|
|
result.push(astResult);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return result;
|
|
|
|
}
|