Skip to content

Commit f4679de

Browse files
committed
Move EventManager to AppViewUtils.
Move listen calls out of renderer for Renderer deprecation. *BREAKING API CHANGE* Renderer marked deprecated. To listen to events use element.onEventName or element.on['customevent'] instead of injecting Renderer to call listen. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=138762470
1 parent 20dc210 commit f4679de

File tree

15 files changed

+81
-76
lines changed

15 files changed

+81
-76
lines changed

lib/platform/common_dom.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ library angular2.platform.common_dom;
55
export "package:angular2/src/core/render/api.dart" show sharedStylesHost;
66
export "package:angular2/src/platform/dom/dom_adapter.dart"
77
show DOM, setRootDomAdapter, DomAdapter;
8-
export "package:angular2/src/platform/dom/dom_renderer.dart" show DomRenderer;
98
export "package:angular2/src/platform/dom/dom_tokens.dart" show DOCUMENT;
109
export "package:angular2/src/platform/dom/events/dom_events.dart"
1110
show DomEventsPlugin;

lib/src/compiler/view_compiler/constants.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ class ViewConstructorVars {
7777
}
7878

7979
class ViewProperties {
80-
static var renderer = new o.ReadClassMemberExpr('renderer');
8180
static var projectableNodes = new o.ReadClassMemberExpr('projectableNodes');
8281
}
8382

lib/src/compiler/view_compiler/event_binder.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import '../template_ast.dart' show BoundEventAst, DirectiveAst;
44
import 'compile_binding.dart' show CompileBinding;
55
import 'compile_element.dart' show CompileElement;
66
import 'compile_method.dart' show CompileMethod;
7-
import 'constants.dart' show EventHandlerVars, ViewProperties;
7+
import 'constants.dart' show EventHandlerVars;
88
import 'expression_converter.dart' show convertCdStatementToIr;
99

1010
class CompileEventListener {
@@ -93,11 +93,13 @@ class CompileEventListener {
9393
new o.ReadClassMemberExpr(_methodName)
9494
.callMethod(o.BuiltinMethod.bind, [o.THIS_EXPR])
9595
]);
96-
o.Expression listenExpr = ViewProperties.renderer.callMethod('listen', [
96+
97+
o.Expression listenExpr = new o.InvokeMemberMethodExpr('listen', [
9798
this.compileElement.renderNode,
9899
o.literal(this.eventName),
99100
eventListener
100101
]);
102+
101103
this
102104
.compileElement
103105
.view

lib/src/compiler/view_compiler/property_binder.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ void bindAndWriteToRenderer(List<BoundElementPropertyAst> boundProps,
137137
updateStmts.add(
138138
logBindingUpdateStmt(renderNode, boundProp.name, currValExpr));
139139
}
140-
updateStmts.add(new o.ReadClassMemberExpr('renderer').callMethod(
141-
renderMethod,
142-
[renderNode, o.literal(boundProp.name), renderValue]).toStmt());
140+
updateStmts.add(new o.InvokeMemberMethodExpr(
141+
'setProp', [renderNode, o.literal(boundProp.name), renderValue])
142+
.toStmt());
143143
break;
144144
case PropertyBindingType.Attribute:
145145
// For attributes convert value to a string.
@@ -408,7 +408,7 @@ void bindToUpdateMethod(
408408

409409
o.Statement logBindingUpdateStmt(
410410
o.Expression renderNode, String propName, o.Expression value) {
411-
return o.THIS_EXPR.prop('renderer').callMethod('setBindingDebugInfo', [
411+
return new o.InvokeMemberMethodExpr('setBindingDebugInfo', [
412412
renderNode,
413413
o.literal('ng-reflect-${propName}'),
414414
value.isBlank().conditional(o.NULL_EXPR, value.callMethod('toString', []))

lib/src/compiler/view_compiler/view_compiler_utils.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ const NAMESPACE_URIS = const {
1818

1919
// Template Anchor comment.
2020
const TEMPLATE_COMMENT_TEXT = 'template bindings={}';
21-
// RegExp to match anchor comment when logging bindings for debugging.
22-
var TEMPLATE_BINDINGS_EXP = new RegExp(r'^template bindings=(.*)$');
2321

2422
// Creates method parameters list for AppView set attribute calls.
2523
List<o.Expression> createSetAttributeParams(

lib/src/core/linker/app_view.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import 'element_injector.dart' show ElementInjector;
1616
import 'exceptions.dart' show ViewDestroyedException;
1717
import 'view_ref.dart' show ViewRefImpl;
1818
import 'view_type.dart' show ViewType;
19+
import 'dart:js_util' as js_util;
1920

2021
export 'package:angular2/src/core/change_detection/component_state.dart';
2122

@@ -463,6 +464,20 @@ abstract class AppView<T> {
463464
}
464465
DomRootRenderer.isDirty = true;
465466
}
467+
468+
Function listen(dynamic renderElement, String name, Function callback) {
469+
return appViewUtils.eventManager.addEventListener(renderElement, name,
470+
(Event event) {
471+
var result = callback(event);
472+
if (identical(result, false)) {
473+
event.preventDefault();
474+
}
475+
});
476+
}
477+
478+
void setProp(Element element, String name, Object value) {
479+
js_util.setProperty(element, name, value);
480+
}
466481
}
467482

468483
dynamic _findLastRenderNode(dynamic node) {

lib/src/core/linker/app_view_utils.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import 'package:angular2/src/core/security.dart' show SafeValue;
99
import 'package:angular2/src/core/security.dart';
1010
import 'package:angular2/src/facade/exceptions.dart' show BaseException;
1111
import 'package:angular2/src/facade/lang.dart' show looseIdentical;
12-
12+
import 'package:angular2/src/platform/dom/events/event_manager.dart'
13+
show EventManager;
1314
import 'exceptions.dart' show ExpressionChangedAfterItHasBeenCheckedException;
1415

1516
/// Function called when a view is destroyed.
@@ -24,6 +25,7 @@ AppViewUtils appViewUtils;
2425
class AppViewUtils {
2526
RootRenderer _renderer;
2627
String _appId;
28+
EventManager eventManager;
2729
static int _nextCompTypeId = 0;
2830

2931
/// Whether change detection should throw an exception when a change is
@@ -34,7 +36,8 @@ class AppViewUtils {
3436
static int _throwOnChangesCounter = 0;
3537
SanitizationService sanitizer;
3638

37-
AppViewUtils(this._renderer, @Inject(APP_ID) this._appId, this.sanitizer);
39+
AppViewUtils(this._renderer, @Inject(APP_ID) this._appId, this.sanitizer,
40+
this.eventManager);
3841

3942
/// Used by the generated code.
4043
RenderComponentType createRenderComponentType(

lib/src/core/render/api.dart

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ abstract class RenderDebugInfo {
9898
String get source;
9999
}
100100

101+
@Deprecated('Use dart:html')
101102
abstract class Renderer {
102103
dynamic selectRootElement(
103104
dynamic /* String | dynamic */ selectorOrNode, RenderDebugInfo debugInfo);
@@ -106,17 +107,12 @@ abstract class Renderer {
106107
void attachViewAfter(dynamic node, List<dynamic> viewRootNodes);
107108
void detachView(List<dynamic> viewRootNodes);
108109
void destroyView(dynamic hostElement, List<dynamic> viewAllNodes);
109-
Function listen(dynamic renderElement, String name, Function callback);
110110
void setElementProperty(
111111
dynamic renderElement, String propertyName, dynamic propertyValue);
112112
@Deprecated("Use dart:html Element attributes and setAttribute.")
113113
void setElementAttribute(
114114
dynamic renderElement, String attributeName, String attributeValue);
115115

116-
/// Used only in debug mode to serialize property changes to dom nodes as
117-
/// attributes.
118-
void setBindingDebugInfo(
119-
dynamic renderElement, String propertyName, String propertyValue);
120116
void setElementClass(dynamic renderElement, String className, bool isAdd);
121117
@Deprecated("Use dart:html Element.style instead")
122118
void setElementStyle(

lib/src/debug/debug_app_view.dart

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'dart:html';
2+
import 'dart:convert';
23

34
import 'package:angular2/src/core/change_detection/change_detection.dart'
45
show ChangeDetectionStrategy, ChangeDetectorState;
@@ -14,7 +15,12 @@ import 'package:angular2/src/core/render/api.dart' show RenderComponentType;
1415
import 'package:angular2/src/debug/debug_context.dart'
1516
show StaticNodeDebugInfo, DebugContext;
1617
import 'package:angular2/src/debug/debug_node.dart'
17-
show DebugElement, DebugNode, getDebugNode, indexDebugNode;
18+
show
19+
DebugElement,
20+
DebugNode,
21+
getDebugNode,
22+
indexDebugNode,
23+
DebugEventListener;
1824
import 'package:angular2/src/platform/dom/dom_renderer.dart'
1925
show DomRootRenderer;
2026

@@ -24,6 +30,10 @@ export 'package:angular2/src/debug/debug_context.dart'
2430

2531
WtfScopeFn _scope_check = wtfCreateScope('AppView#check(ascii id)');
2632

33+
// RegExp to match anchor comment when logging bindings for debugging.
34+
RegExp _TEMPLATE_BINDINGS_EXP = new RegExp(r'^template bindings=(.*)$');
35+
const _TEMPLATE_COMMENT_TEXT = 'template bindings={}';
36+
2737
class DebugAppView<T> extends AppView<T> {
2838
static bool profilingEnabled = false;
2939

@@ -142,6 +152,31 @@ class DebugAppView<T> extends AppView<T> {
142152
};
143153
}
144154

155+
Function listen(dynamic renderElement, String name, Function callback) {
156+
var debugEl = getDebugNode(renderElement);
157+
if (debugEl != null) {
158+
debugEl.listeners.add(new DebugEventListener(name, callback));
159+
}
160+
return super.listen(renderElement, name, callback);
161+
}
162+
163+
/// Used only in debug mode to serialize property changes to dom nodes as
164+
/// attributes.
165+
void setBindingDebugInfo(
166+
dynamic renderElement, String propertyName, String propertyValue) {
167+
if (renderElement is Comment) {
168+
var existingBindings = _TEMPLATE_BINDINGS_EXP
169+
.firstMatch(renderElement.text.replaceAll(new RegExp(r'\n'), ''));
170+
var parsedBindings = JSON.decode(existingBindings[1]);
171+
parsedBindings[propertyName] = propertyValue;
172+
String debugStr =
173+
const JsonEncoder.withIndent(' ').convert(parsedBindings);
174+
renderElement.text = _TEMPLATE_COMMENT_TEXT.replaceFirst('{}', debugStr);
175+
} else {
176+
setAttr(renderElement, propertyName, propertyValue);
177+
}
178+
}
179+
145180
/// Sets up current debug context to node so that failures can be associated
146181
/// with template source location and DebugElement.
147182
DebugContext dbg(num nodeIndex, num rowNum, num colNum) =>

lib/src/debug/debug_node.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import "package:angular2/src/core/zone/ng_zone.dart" show NgZone;
55

66
typedef bool Predicate<T>(T item);
77

8-
class EventListener {
8+
class DebugEventListener {
99
String name;
1010
Function callback;
11-
EventListener(this.name, this.callback);
11+
DebugEventListener(this.name, this.callback);
1212
}
1313

1414
class DebugNode {
1515
RenderDebugInfo _debugInfo;
1616
dynamic nativeNode;
17-
List<EventListener> listeners;
17+
List<DebugEventListener> listeners;
1818
DebugElement parent;
1919
DebugNode(dynamic nativeNode, DebugNode parent, this._debugInfo) {
2020
this.nativeNode = nativeNode;

0 commit comments

Comments
 (0)