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-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-08-02 15:53:34 -07:00
|
|
|
import {createDiTokenExpression} from '../util';
|
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}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function createFlatArray(expressions: o.Expression[]): o.Expression {
|
2016-10-07 17:36:08 -07:00
|
|
|
var lastNonArrayExpressions: o.Expression[] = [];
|
2016-01-06 14:13:44 -08:00
|
|
|
var result: o.Expression = o.literalArr([]);
|
|
|
|
for (var i = 0; i < expressions.length; i++) {
|
|
|
|
var expr = expressions[i];
|
|
|
|
if (expr.type instanceof o.ArrayType) {
|
|
|
|
if (lastNonArrayExpressions.length > 0) {
|
|
|
|
result =
|
|
|
|
result.callMethod(o.BuiltinMethod.ConcatArray, [o.literalArr(lastNonArrayExpressions)]);
|
|
|
|
lastNonArrayExpressions = [];
|
|
|
|
}
|
|
|
|
result = result.callMethod(o.BuiltinMethod.ConcatArray, [expr]);
|
|
|
|
} else {
|
|
|
|
lastNonArrayExpressions.push(expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lastNonArrayExpressions.length > 0) {
|
|
|
|
result =
|
|
|
|
result.callMethod(o.BuiltinMethod.ConcatArray, [o.literalArr(lastNonArrayExpressions)]);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2016-04-22 15:33:32 -07:00
|
|
|
|
2016-06-08 16:38:52 -07:00
|
|
|
export function createPureProxy(
|
|
|
|
fn: o.Expression, argCount: number, pureProxyProp: o.ReadPropExpr, view: CompileView) {
|
2016-04-30 16:13:03 -07:00
|
|
|
view.fields.push(new o.ClassField(pureProxyProp.name, null));
|
2016-04-22 15:33:32 -07:00
|
|
|
var pureProxyId =
|
|
|
|
argCount < Identifiers.pureProxies.length ? Identifiers.pureProxies[argCount] : null;
|
2016-09-30 09:26:53 -07:00
|
|
|
if (!pureProxyId) {
|
2016-08-25 00:50:16 -07:00
|
|
|
throw new Error(`Unsupported number of argument for pure functions: ${argCount}`);
|
2016-04-22 15:33:32 -07:00
|
|
|
}
|
2016-08-24 17:39:49 -07:00
|
|
|
view.createMethod.addStmt(o.THIS_EXPR.prop(pureProxyProp.name)
|
|
|
|
.set(o.importExpr(resolveIdentifier(pureProxyId)).callFn([fn]))
|
|
|
|
.toStmt());
|
2016-04-22 15:33:32 -07:00
|
|
|
}
|