2016-06-23 09:47:54 -07:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2016-08-10 15:55:18 -07:00
|
|
|
|
2016-10-13 16:34:37 -07:00
|
|
|
import {CompileDirectiveMetadata, CompileIdentifierMetadata, CompileTokenMetadata} from '../compile_metadata';
|
2016-10-19 09:17:36 -07:00
|
|
|
import {createDiTokenExpression} from '../compiler_util/identifier_util';
|
2016-09-30 09:26:53 -07:00
|
|
|
import {isPresent} from '../facade/lang';
|
2016-08-24 17:39:49 -07:00
|
|
|
import {Identifiers, resolveIdentifier} from '../identifiers';
|
2016-01-06 14:13:44 -08:00
|
|
|
import * as o from '../output/output_ast';
|
2016-06-08 16:38:52 -07:00
|
|
|
|
2016-01-06 14:13:44 -08:00
|
|
|
import {CompileView} from './compile_view';
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
export function getPropertyInView(
|
|
|
|
property: o.Expression, callingView: CompileView, definedView: CompileView): o.Expression {
|
2016-04-22 15:33:32 -07:00
|
|
|
if (callingView === definedView) {
|
2016-01-06 14:13:44 -08:00
|
|
|
return property;
|
|
|
|
} else {
|
|
|
|
var viewProp: o.Expression = o.THIS_EXPR;
|
2016-04-22 15:33:32 -07:00
|
|
|
var currView: CompileView = callingView;
|
|
|
|
while (currView !== definedView && isPresent(currView.declarationElement.view)) {
|
|
|
|
currView = currView.declarationElement.view;
|
|
|
|
viewProp = viewProp.prop('parent');
|
|
|
|
}
|
|
|
|
if (currView !== definedView) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(
|
2016-04-22 15:33:32 -07:00
|
|
|
`Internal error: Could not calculate a property in a parent view: ${property}`);
|
2016-01-06 14:13:44 -08:00
|
|
|
}
|
2016-10-13 16:34:37 -07:00
|
|
|
return property.visitExpression(new _ReplaceViewTransformer(viewProp, definedView), null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class _ReplaceViewTransformer extends o.ExpressionTransformer {
|
|
|
|
constructor(private _viewExpr: o.Expression, private _view: CompileView) { super(); }
|
|
|
|
private _isThis(expr: o.Expression): boolean {
|
|
|
|
return expr instanceof o.ReadVarExpr && expr.builtin === o.BuiltinVar.This;
|
|
|
|
}
|
|
|
|
|
|
|
|
visitReadVarExpr(ast: o.ReadVarExpr, context: any): any {
|
|
|
|
return this._isThis(ast) ? this._viewExpr : ast;
|
|
|
|
}
|
|
|
|
visitReadPropExpr(ast: o.ReadPropExpr, context: any): any {
|
|
|
|
if (this._isThis(ast.receiver)) {
|
2016-01-06 14:13:44 -08:00
|
|
|
// Note: Don't cast for members of the AppView base class...
|
2016-10-13 16:34:37 -07:00
|
|
|
if (this._view.fields.some((field) => field.name == ast.name) ||
|
|
|
|
this._view.getters.some((field) => field.name == ast.name)) {
|
|
|
|
return this._viewExpr.cast(this._view.classType).prop(ast.name);
|
2016-01-06 14:13:44 -08:00
|
|
|
}
|
|
|
|
}
|
2016-10-13 16:34:37 -07:00
|
|
|
return super.visitReadPropExpr(ast, context);
|
2016-01-06 14:13:44 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
export function injectFromViewParentInjector(
|
|
|
|
token: CompileTokenMetadata, optional: boolean): o.Expression {
|
2016-04-14 12:35:24 -07:00
|
|
|
var args = [createDiTokenExpression(token)];
|
|
|
|
if (optional) {
|
|
|
|
args.push(o.NULL_EXPR);
|
|
|
|
}
|
|
|
|
return o.THIS_EXPR.prop('parentInjector').callMethod('get', args);
|
2016-01-06 14:13:44 -08:00
|
|
|
}
|
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
export function getViewFactoryName(
|
|
|
|
component: CompileDirectiveMetadata, embeddedTemplateIndex: number): string {
|
2016-01-06 14:13:44 -08:00
|
|
|
return `viewFactory_${component.type.name}${embeddedTemplateIndex}`;
|
|
|
|
}
|
|
|
|
|
2016-10-26 16:58:35 -07:00
|
|
|
export function getHandleEventMethodName(elementIndex: number): string {
|
|
|
|
return `handleEvent_${elementIndex}`;
|
|
|
|
}
|