Skip to content

Commit 4e4415e

Browse files
mvuksanomhevery
authored andcommitted
docs(EventHandler): Add EventHandler documentation.
Add documentation to EventHandler. This change also makes fields and methods within EventHandler, which are not intended for public use, private. Closes dart-archive#764
1 parent dd2e553 commit 4e4415e

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

lib/core_dom/event_handler.dart

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,73 @@ part of angular.core.dom;
22

33
typedef void EventFunction(event);
44

5+
/**
6+
* [EventHandler] is responsible for handling events bound using on-* syntax
7+
* (i.e. `on-click="ctrl.doSomething();"`). The root of the application has an
8+
* EventHandler attached as does every [NgComponent].
9+
*
10+
* Events bound within [NgComponent] are handled by EventHandler attached to
11+
* their [ShadowRoot]. All other events are handled by EventHandler attached
12+
* to the application root ([NgApp]).
13+
*
14+
* **Note**: The expressions are executed within the closest context.
15+
*
16+
* Example:
17+
*
18+
* <div foo>
19+
* <button on-click="ctrl.say('Hello');">Button</button>;
20+
* </div>
21+
*
22+
* @NgComponent(selector: '[foo]', publishAs: ctrl)
23+
* class FooController {
24+
* say(String something) => print(something);
25+
* }
26+
*
27+
* When button is clicked, "Hello" will be printed in the console.
28+
*/
529
@NgInjectableService()
630
class EventHandler {
7-
dom.Node rootNode;
8-
final Expando expando;
9-
final ExceptionHandler exceptionHandler;
10-
final listeners = <String, Function>{};
31+
dom.Node _rootNode;
32+
final Expando _expando;
33+
final ExceptionHandler _exceptionHandler;
34+
final _listeners = <String, Function>{};
1135

12-
EventHandler(this.rootNode, this.expando, this.exceptionHandler);
36+
EventHandler(this._rootNode, this._expando, this._exceptionHandler);
1337

38+
/**
39+
* Register an event. This makes sure that an event (of the specified name)
40+
* which bubbles to this node, gets processed by this [EventHandler].
41+
*/
1442
void register(String eventName) {
15-
listeners.putIfAbsent(eventName, () {
16-
dom.EventListener eventListener = this.eventListener;
17-
rootNode.on[eventName].listen(eventListener);
43+
_listeners.putIfAbsent(eventName, () {
44+
dom.EventListener eventListener = this._eventListener;
45+
_rootNode.on[eventName].listen(eventListener);
1846
return eventListener;
1947
});
2048
}
2149

22-
void eventListener(dom.Event event) {
50+
void _eventListener(dom.Event event) {
2351
dom.Node element = event.target;
24-
while (element != null && element != rootNode) {
52+
while (element != null && element != _rootNode) {
2553
var expression;
2654
if (element is dom.Element)
2755
expression = (element as dom.Element).attributes[eventNameToAttrName(event.type)];
2856
if (expression != null) {
2957
try {
30-
var scope = getScope(element);
58+
var scope = _getScope(element);
3159
if (scope != null) scope.eval(expression);
3260
} catch (e, s) {
33-
exceptionHandler(e, s);
61+
_exceptionHandler(e, s);
3462
}
3563
}
3664
element = element.parentNode;
3765
}
3866
}
3967

40-
Scope getScope(dom.Node element) {
68+
Scope _getScope(dom.Node element) {
4169
// var topElement = (rootNode is dom.ShadowRoot) ? rootNode.parentNode : rootNode;
42-
while (element != rootNode.parentNode) {
43-
ElementProbe probe = expando[element];
70+
while (element != _rootNode.parentNode) {
71+
ElementProbe probe = _expando[element];
4472
if (probe != null) {
4573
return probe.scope;
4674
}

0 commit comments

Comments
 (0)