2015-08-03 14:10:07 -07:00
|
|
|
import {ListWrapper} from 'angular2/src/facade/collection';
|
|
|
|
import {BaseException, Json} from 'angular2/src/facade/lang';
|
|
|
|
import {CodegenNameUtil} from './codegen_name_util';
|
2015-08-12 16:26:21 -07:00
|
|
|
import {codify, combineGeneratedStrings, rawString} from './codegen_facade';
|
2015-08-03 14:10:07 -07:00
|
|
|
import {ProtoRecord, RecordType} from './proto_record';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class responsible for providing change detection logic for chagne detector classes.
|
|
|
|
*/
|
|
|
|
export class CodegenLogicUtil {
|
|
|
|
constructor(private _names: CodegenNameUtil, private _utilName: string) {}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a statement which updates the local variable representing `protoRec` with the current
|
2015-08-12 16:26:21 -07:00
|
|
|
* value of the record. Used by property bindings.
|
2015-08-03 14:10:07 -07:00
|
|
|
*/
|
2015-08-12 16:26:21 -07:00
|
|
|
genPropertyBindingEvalValue(protoRec: ProtoRecord): string {
|
|
|
|
return this.genEvalValue(protoRec, idx => this._names.getLocalName(idx),
|
|
|
|
this._names.getLocalsAccessorName());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a statement which updates the local variable representing `protoRec` with the current
|
|
|
|
* value of the record. Used by event bindings.
|
|
|
|
*/
|
|
|
|
genEventBindingEvalValue(eventRecord: any, protoRec: ProtoRecord): string {
|
|
|
|
return this.genEvalValue(protoRec, idx => this._names.getEventLocalName(eventRecord, idx),
|
|
|
|
"locals");
|
|
|
|
}
|
|
|
|
|
|
|
|
private genEvalValue(protoRec: ProtoRecord, getLocalName: Function,
|
|
|
|
localsAccessor: string): string {
|
2015-08-03 14:10:07 -07:00
|
|
|
var context = (protoRec.contextIndex == -1) ?
|
|
|
|
this._names.getDirectiveName(protoRec.directiveIndex) :
|
2015-08-12 16:26:21 -07:00
|
|
|
getLocalName(protoRec.contextIndex);
|
|
|
|
var argString = ListWrapper.map(protoRec.args, (arg) => getLocalName(arg)).join(", ");
|
2015-08-03 14:10:07 -07:00
|
|
|
|
|
|
|
var rhs: string;
|
|
|
|
switch (protoRec.mode) {
|
|
|
|
case RecordType.SELF:
|
|
|
|
rhs = context;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.CONST:
|
|
|
|
rhs = codify(protoRec.funcOrValue);
|
|
|
|
break;
|
|
|
|
|
2015-08-12 16:26:21 -07:00
|
|
|
case RecordType.PROPERTY_READ:
|
2015-08-03 14:10:07 -07:00
|
|
|
rhs = `${context}.${protoRec.name}`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.SAFE_PROPERTY:
|
|
|
|
rhs = `${this._utilName}.isValueBlank(${context}) ? null : ${context}.${protoRec.name}`;
|
|
|
|
break;
|
|
|
|
|
2015-08-12 16:26:21 -07:00
|
|
|
case RecordType.PROPERTY_WRITE:
|
|
|
|
rhs = `${context}.${protoRec.name} = ${getLocalName(protoRec.args[0])}`;
|
|
|
|
break;
|
|
|
|
|
2015-08-03 14:10:07 -07:00
|
|
|
case RecordType.LOCAL:
|
2015-08-12 16:26:21 -07:00
|
|
|
rhs = `${localsAccessor}.get(${rawString(protoRec.name)})`;
|
2015-08-03 14:10:07 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.INVOKE_METHOD:
|
|
|
|
rhs = `${context}.${protoRec.name}(${argString})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.SAFE_INVOKE_METHOD:
|
|
|
|
rhs =
|
|
|
|
`${this._utilName}.isValueBlank(${context}) ? null : ${context}.${protoRec.name}(${argString})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.INVOKE_CLOSURE:
|
|
|
|
rhs = `${context}(${argString})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.PRIMITIVE_OP:
|
|
|
|
rhs = `${this._utilName}.${protoRec.name}(${argString})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.COLLECTION_LITERAL:
|
|
|
|
rhs = `${this._utilName}.${protoRec.name}(${argString})`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.INTERPOLATE:
|
|
|
|
rhs = this._genInterpolation(protoRec);
|
|
|
|
break;
|
|
|
|
|
2015-08-12 16:26:21 -07:00
|
|
|
case RecordType.KEYED_READ:
|
|
|
|
rhs = `${context}[${getLocalName(protoRec.args[0])}]`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.KEYED_WRITE:
|
|
|
|
rhs = `${context}[${getLocalName(protoRec.args[0])}] = ${getLocalName(protoRec.args[1])}`;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RecordType.CHAIN:
|
|
|
|
rhs = 'null';
|
2015-08-03 14:10:07 -07:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw new BaseException(`Unknown operation ${protoRec.mode}`);
|
|
|
|
}
|
2015-08-12 16:26:21 -07:00
|
|
|
return `${getLocalName(protoRec.selfIndex)} = ${rhs};`;
|
2015-08-03 14:10:07 -07:00
|
|
|
}
|
|
|
|
|
2015-08-12 16:26:21 -07:00
|
|
|
|
2015-08-03 14:10:07 -07:00
|
|
|
_genInterpolation(protoRec: ProtoRecord): string {
|
|
|
|
var iVals = [];
|
|
|
|
for (var i = 0; i < protoRec.args.length; ++i) {
|
|
|
|
iVals.push(codify(protoRec.fixedArgs[i]));
|
|
|
|
iVals.push(`${this._utilName}.s(${this._names.getLocalName(protoRec.args[i])})`);
|
|
|
|
}
|
|
|
|
iVals.push(codify(protoRec.fixedArgs[protoRec.args.length]));
|
|
|
|
return combineGeneratedStrings(iVals);
|
|
|
|
}
|
|
|
|
}
|