PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/@angular/compiler/src/view_compiler/property_binder.js

https://gitlab.com/btluis/Angular2_Ejemplo
JavaScript | 211 lines | 204 code | 0 blank | 7 comment | 15 complexity | 59ecebde811a825a9251a28d616c12f9 MD5 | raw file
  1. "use strict";
  2. var core_private_1 = require('../../core_private');
  3. var lang_1 = require('../facade/lang');
  4. var identifiers_1 = require('../identifiers');
  5. var o = require('../output/output_ast');
  6. var constants_1 = require('./constants');
  7. var template_ast_1 = require('../template_ast');
  8. var util_1 = require('../util');
  9. var expression_converter_1 = require('./expression_converter');
  10. var compile_binding_1 = require('./compile_binding');
  11. var core_1 = require('@angular/core');
  12. function createBindFieldExpr(exprIndex) {
  13. return o.THIS_EXPR.prop("_expr_" + exprIndex);
  14. }
  15. function createCurrValueExpr(exprIndex) {
  16. return o.variable("currVal_" + exprIndex); // fix syntax highlighting: `
  17. }
  18. function bind(view, currValExpr, fieldExpr, parsedExpression, context, actions, method) {
  19. var checkExpression = expression_converter_1.convertCdExpressionToIr(view, context, parsedExpression, constants_1.DetectChangesVars.valUnwrapper);
  20. if (lang_1.isBlank(checkExpression.expression)) {
  21. // e.g. an empty expression was given
  22. return;
  23. }
  24. // private is fine here as no child view will reference the cached value...
  25. view.fields.push(new o.ClassField(fieldExpr.name, null, [o.StmtModifier.Private]));
  26. view.createMethod.addStmt(o.THIS_EXPR.prop(fieldExpr.name).set(o.importExpr(identifiers_1.Identifiers.uninitialized)).toStmt());
  27. if (checkExpression.needsValueUnwrapper) {
  28. var initValueUnwrapperStmt = constants_1.DetectChangesVars.valUnwrapper.callMethod('reset', []).toStmt();
  29. method.addStmt(initValueUnwrapperStmt);
  30. }
  31. method.addStmt(currValExpr.set(checkExpression.expression).toDeclStmt(null, [o.StmtModifier.Final]));
  32. var condition = o.importExpr(identifiers_1.Identifiers.checkBinding).callFn([
  33. constants_1.DetectChangesVars.throwOnChange, fieldExpr, currValExpr
  34. ]);
  35. if (checkExpression.needsValueUnwrapper) {
  36. condition = constants_1.DetectChangesVars.valUnwrapper.prop('hasWrappedValue').or(condition);
  37. }
  38. method.addStmt(new o.IfStmt(condition, actions.concat([o.THIS_EXPR.prop(fieldExpr.name).set(currValExpr).toStmt()])));
  39. }
  40. function bindRenderText(boundText, compileNode, view) {
  41. var bindingIndex = view.bindings.length;
  42. view.bindings.push(new compile_binding_1.CompileBinding(compileNode, boundText));
  43. var currValExpr = createCurrValueExpr(bindingIndex);
  44. var valueField = createBindFieldExpr(bindingIndex);
  45. view.detectChangesRenderPropertiesMethod.resetDebugInfo(compileNode.nodeIndex, boundText);
  46. bind(view, currValExpr, valueField, boundText.value, view.componentContext, [o.THIS_EXPR.prop('renderer')
  47. .callMethod('setText', [compileNode.renderNode, currValExpr])
  48. .toStmt()], view.detectChangesRenderPropertiesMethod);
  49. }
  50. exports.bindRenderText = bindRenderText;
  51. function bindAndWriteToRenderer(boundProps, context, compileElement) {
  52. var view = compileElement.view;
  53. var renderNode = compileElement.renderNode;
  54. boundProps.forEach(function (boundProp) {
  55. var bindingIndex = view.bindings.length;
  56. view.bindings.push(new compile_binding_1.CompileBinding(compileElement, boundProp));
  57. view.detectChangesRenderPropertiesMethod.resetDebugInfo(compileElement.nodeIndex, boundProp);
  58. var fieldExpr = createBindFieldExpr(bindingIndex);
  59. var currValExpr = createCurrValueExpr(bindingIndex);
  60. var renderMethod;
  61. var oldRenderValue = sanitizedValue(boundProp, fieldExpr);
  62. var renderValue = sanitizedValue(boundProp, currValExpr);
  63. var updateStmts = [];
  64. switch (boundProp.type) {
  65. case template_ast_1.PropertyBindingType.Property:
  66. if (view.genConfig.logBindingUpdate) {
  67. updateStmts.push(logBindingUpdateStmt(renderNode, boundProp.name, renderValue));
  68. }
  69. updateStmts.push(o.THIS_EXPR.prop('renderer')
  70. .callMethod('setElementProperty', [renderNode, o.literal(boundProp.name), renderValue])
  71. .toStmt());
  72. break;
  73. case template_ast_1.PropertyBindingType.Attribute:
  74. renderValue =
  75. renderValue.isBlank().conditional(o.NULL_EXPR, renderValue.callMethod('toString', []));
  76. updateStmts.push(o.THIS_EXPR.prop('renderer')
  77. .callMethod('setElementAttribute', [renderNode, o.literal(boundProp.name), renderValue])
  78. .toStmt());
  79. break;
  80. case template_ast_1.PropertyBindingType.Class:
  81. updateStmts.push(o.THIS_EXPR.prop('renderer')
  82. .callMethod('setElementClass', [renderNode, o.literal(boundProp.name), renderValue])
  83. .toStmt());
  84. break;
  85. case template_ast_1.PropertyBindingType.Style:
  86. var strValue = renderValue.callMethod('toString', []);
  87. if (lang_1.isPresent(boundProp.unit)) {
  88. strValue = strValue.plus(o.literal(boundProp.unit));
  89. }
  90. renderValue = renderValue.isBlank().conditional(o.NULL_EXPR, strValue);
  91. updateStmts.push(o.THIS_EXPR.prop('renderer')
  92. .callMethod('setElementStyle', [renderNode, o.literal(boundProp.name), renderValue])
  93. .toStmt());
  94. break;
  95. case template_ast_1.PropertyBindingType.Animation:
  96. var animationName = boundProp.name;
  97. var animation = view.componentView.animations.get(animationName);
  98. if (!lang_1.isPresent(animation)) {
  99. throw new core_1.BaseException("Internal Error: couldn't find an animation entry for " + boundProp.name);
  100. }
  101. // it's important to normalize the void value as `void` explicitly
  102. // so that the styles data can be obtained from the stringmap
  103. var emptyStateValue = o.literal(core_private_1.EMPTY_STATE);
  104. // void => ...
  105. var oldRenderVar = o.variable('oldRenderVar');
  106. updateStmts.push(oldRenderVar.set(oldRenderValue).toDeclStmt());
  107. updateStmts.push(new o.IfStmt(oldRenderVar.equals(o.importExpr(identifiers_1.Identifiers.uninitialized)), [oldRenderVar.set(emptyStateValue).toStmt()]));
  108. // ... => void
  109. var newRenderVar = o.variable('newRenderVar');
  110. updateStmts.push(newRenderVar.set(renderValue).toDeclStmt());
  111. updateStmts.push(new o.IfStmt(newRenderVar.equals(o.importExpr(identifiers_1.Identifiers.uninitialized)), [newRenderVar.set(emptyStateValue).toStmt()]));
  112. updateStmts.push(animation.fnVariable.callFn([o.THIS_EXPR, renderNode, oldRenderVar, newRenderVar])
  113. .toStmt());
  114. view.detachMethod.addStmt(animation.fnVariable.callFn([o.THIS_EXPR, renderNode, oldRenderValue, emptyStateValue])
  115. .toStmt());
  116. break;
  117. }
  118. bind(view, currValExpr, fieldExpr, boundProp.value, context, updateStmts, view.detectChangesRenderPropertiesMethod);
  119. });
  120. }
  121. function sanitizedValue(boundProp, renderValue) {
  122. var enumValue;
  123. switch (boundProp.securityContext) {
  124. case core_private_1.SecurityContext.NONE:
  125. return renderValue; // No sanitization needed.
  126. case core_private_1.SecurityContext.HTML:
  127. enumValue = 'HTML';
  128. break;
  129. case core_private_1.SecurityContext.STYLE:
  130. enumValue = 'STYLE';
  131. break;
  132. case core_private_1.SecurityContext.SCRIPT:
  133. enumValue = 'SCRIPT';
  134. break;
  135. case core_private_1.SecurityContext.URL:
  136. enumValue = 'URL';
  137. break;
  138. case core_private_1.SecurityContext.RESOURCE_URL:
  139. enumValue = 'RESOURCE_URL';
  140. break;
  141. default:
  142. throw new Error("internal error, unexpected SecurityContext " + boundProp.securityContext + ".");
  143. }
  144. var ctx = constants_1.ViewProperties.viewUtils.prop('sanitizer');
  145. var args = [o.importExpr(identifiers_1.Identifiers.SecurityContext).prop(enumValue), renderValue];
  146. return ctx.callMethod('sanitize', args);
  147. }
  148. function bindRenderInputs(boundProps, compileElement) {
  149. bindAndWriteToRenderer(boundProps, compileElement.view.componentContext, compileElement);
  150. }
  151. exports.bindRenderInputs = bindRenderInputs;
  152. function bindDirectiveHostProps(directiveAst, directiveInstance, compileElement) {
  153. bindAndWriteToRenderer(directiveAst.hostProperties, directiveInstance, compileElement);
  154. }
  155. exports.bindDirectiveHostProps = bindDirectiveHostProps;
  156. function bindDirectiveInputs(directiveAst, directiveInstance, compileElement) {
  157. if (directiveAst.inputs.length === 0) {
  158. return;
  159. }
  160. var view = compileElement.view;
  161. var detectChangesInInputsMethod = view.detectChangesInInputsMethod;
  162. detectChangesInInputsMethod.resetDebugInfo(compileElement.nodeIndex, compileElement.sourceAst);
  163. var lifecycleHooks = directiveAst.directive.lifecycleHooks;
  164. var calcChangesMap = lifecycleHooks.indexOf(core_private_1.LifecycleHooks.OnChanges) !== -1;
  165. var isOnPushComp = directiveAst.directive.isComponent &&
  166. !core_private_1.isDefaultChangeDetectionStrategy(directiveAst.directive.changeDetection);
  167. if (calcChangesMap) {
  168. detectChangesInInputsMethod.addStmt(constants_1.DetectChangesVars.changes.set(o.NULL_EXPR).toStmt());
  169. }
  170. if (isOnPushComp) {
  171. detectChangesInInputsMethod.addStmt(constants_1.DetectChangesVars.changed.set(o.literal(false)).toStmt());
  172. }
  173. directiveAst.inputs.forEach(function (input) {
  174. var bindingIndex = view.bindings.length;
  175. view.bindings.push(new compile_binding_1.CompileBinding(compileElement, input));
  176. detectChangesInInputsMethod.resetDebugInfo(compileElement.nodeIndex, input);
  177. var fieldExpr = createBindFieldExpr(bindingIndex);
  178. var currValExpr = createCurrValueExpr(bindingIndex);
  179. var statements = [directiveInstance.prop(input.directiveName).set(currValExpr).toStmt()];
  180. if (calcChangesMap) {
  181. statements.push(new o.IfStmt(constants_1.DetectChangesVars.changes.identical(o.NULL_EXPR), [constants_1.DetectChangesVars.changes
  182. .set(o.literalMap([], new o.MapType(o.importType(identifiers_1.Identifiers.SimpleChange))))
  183. .toStmt()]));
  184. statements.push(constants_1.DetectChangesVars.changes.key(o.literal(input.directiveName))
  185. .set(o.importExpr(identifiers_1.Identifiers.SimpleChange).instantiate([fieldExpr, currValExpr]))
  186. .toStmt());
  187. }
  188. if (isOnPushComp) {
  189. statements.push(constants_1.DetectChangesVars.changed.set(o.literal(true)).toStmt());
  190. }
  191. if (view.genConfig.logBindingUpdate) {
  192. statements.push(logBindingUpdateStmt(compileElement.renderNode, input.directiveName, currValExpr));
  193. }
  194. bind(view, currValExpr, fieldExpr, input.value, view.componentContext, statements, detectChangesInInputsMethod);
  195. });
  196. if (isOnPushComp) {
  197. detectChangesInInputsMethod.addStmt(new o.IfStmt(constants_1.DetectChangesVars.changed, [
  198. compileElement.appElement.prop('componentView').callMethod('markAsCheckOnce', []).toStmt()
  199. ]));
  200. }
  201. }
  202. exports.bindDirectiveInputs = bindDirectiveInputs;
  203. function logBindingUpdateStmt(renderNode, propName, value) {
  204. return o.THIS_EXPR.prop('renderer')
  205. .callMethod('setBindingDebugInfo', [
  206. renderNode, o.literal("ng-reflect-" + util_1.camelCaseToDashCase(propName)),
  207. value.isBlank().conditional(o.NULL_EXPR, value.callMethod('toString', []))
  208. ])
  209. .toStmt();
  210. }
  211. //# sourceMappingURL=property_binder.js.map