UNPKG

angular2

Version:

Angular 2 - a web framework for modern web apps

255 lines (254 loc) 8.12 kB
import { isPresent } from 'angular2/src/facade/lang'; /** * A segment of text within the template. */ export class TextAst { constructor(value, ngContentIndex, sourceSpan) { this.value = value; this.ngContentIndex = ngContentIndex; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitText(this, context); } } /** * A bound expression within the text of a template. */ export class BoundTextAst { constructor(value, ngContentIndex, sourceSpan) { this.value = value; this.ngContentIndex = ngContentIndex; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitBoundText(this, context); } } /** * A plain attribute on an element. */ export class AttrAst { constructor(name, value, sourceSpan) { this.name = name; this.value = value; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitAttr(this, context); } } /** * A binding for an element property (e.g. `[property]="expression"`). */ export class BoundElementPropertyAst { constructor(name, type, value, unit, sourceSpan) { this.name = name; this.type = type; this.value = value; this.unit = unit; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitElementProperty(this, context); } } /** * A binding for an element event (e.g. `(event)="handler()"`). */ export class BoundEventAst { constructor(name, target, handler, sourceSpan) { this.name = name; this.target = target; this.handler = handler; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitEvent(this, context); } get fullName() { if (isPresent(this.target)) { return `${this.target}:${this.name}`; } else { return this.name; } } } /** * A reference declaration on an element (e.g. `let someName="expression"`). */ export class ReferenceAst { constructor(name, value, sourceSpan) { this.name = name; this.value = value; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitReference(this, context); } } /** * A variable declaration on a <template> (e.g. `var-someName="someLocalName"`). */ export class VariableAst { constructor(name, value, sourceSpan) { this.name = name; this.value = value; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitVariable(this, context); } } /** * An element declaration in a template. */ export class ElementAst { constructor(name, attrs, inputs, outputs, references, directives, providers, hasViewContainer, children, ngContentIndex, sourceSpan) { this.name = name; this.attrs = attrs; this.inputs = inputs; this.outputs = outputs; this.references = references; this.directives = directives; this.providers = providers; this.hasViewContainer = hasViewContainer; this.children = children; this.ngContentIndex = ngContentIndex; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitElement(this, context); } /** * Get the component associated with this element, if any. */ getComponent() { for (var i = 0; i < this.directives.length; i++) { var dirAst = this.directives[i]; if (dirAst.directive.isComponent) { return dirAst.directive; } } return null; } } /** * A `<template>` element included in an Angular template. */ export class EmbeddedTemplateAst { constructor(attrs, outputs, references, variables, directives, providers, hasViewContainer, children, ngContentIndex, sourceSpan) { this.attrs = attrs; this.outputs = outputs; this.references = references; this.variables = variables; this.directives = directives; this.providers = providers; this.hasViewContainer = hasViewContainer; this.children = children; this.ngContentIndex = ngContentIndex; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitEmbeddedTemplate(this, context); } } /** * A directive property with a bound value (e.g. `*ngIf="condition"). */ export class BoundDirectivePropertyAst { constructor(directiveName, templateName, value, sourceSpan) { this.directiveName = directiveName; this.templateName = templateName; this.value = value; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitDirectiveProperty(this, context); } } /** * A directive declared on an element. */ export class DirectiveAst { constructor(directive, inputs, hostProperties, hostEvents, sourceSpan) { this.directive = directive; this.inputs = inputs; this.hostProperties = hostProperties; this.hostEvents = hostEvents; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitDirective(this, context); } } /** * A provider declared on an element */ export class ProviderAst { constructor(token, multiProvider, eager, providers, providerType, sourceSpan) { this.token = token; this.multiProvider = multiProvider; this.eager = eager; this.providers = providers; this.providerType = providerType; this.sourceSpan = sourceSpan; } visit(visitor, context) { // No visit method in the visitor for now... return null; } } export var ProviderAstType; (function (ProviderAstType) { ProviderAstType[ProviderAstType["PublicService"] = 0] = "PublicService"; ProviderAstType[ProviderAstType["PrivateService"] = 1] = "PrivateService"; ProviderAstType[ProviderAstType["Component"] = 2] = "Component"; ProviderAstType[ProviderAstType["Directive"] = 3] = "Directive"; ProviderAstType[ProviderAstType["Builtin"] = 4] = "Builtin"; })(ProviderAstType || (ProviderAstType = {})); /** * Position where content is to be projected (instance of `<ng-content>` in a template). */ export class NgContentAst { constructor(index, ngContentIndex, sourceSpan) { this.index = index; this.ngContentIndex = ngContentIndex; this.sourceSpan = sourceSpan; } visit(visitor, context) { return visitor.visitNgContent(this, context); } } /** * Enumeration of types of property bindings. */ export var PropertyBindingType; (function (PropertyBindingType) { /** * A normal binding to a property (e.g. `[property]="expression"`). */ PropertyBindingType[PropertyBindingType["Property"] = 0] = "Property"; /** * A binding to an element attribute (e.g. `[attr.name]="expression"`). */ PropertyBindingType[PropertyBindingType["Attribute"] = 1] = "Attribute"; /** * A binding to a CSS class (e.g. `[class.name]="condition"`). */ PropertyBindingType[PropertyBindingType["Class"] = 2] = "Class"; /** * A binding to a style rule (e.g. `[style.rule]="expression"`). */ PropertyBindingType[PropertyBindingType["Style"] = 3] = "Style"; })(PropertyBindingType || (PropertyBindingType = {})); /** * Visit every node in a list of {@link TemplateAst}s with the given {@link TemplateAstVisitor}. */ export function templateVisitAll(visitor, asts, context = null) { var result = []; asts.forEach(ast => { var astResult = ast.visit(visitor, context); if (isPresent(astResult)) { result.push(astResult); } }); return result; }