Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
docs(EventHandler): Add EventHandler documentation.
Browse files Browse the repository at this point in the history
Add documentation to EventHandler. This change also makes fields and methods within EventHandler, which are not intended for public use, private.

Closes #764
  • Loading branch information
mvuksano authored and mhevery committed Mar 19, 2014
1 parent dd2e553 commit 4e4415e
Showing 1 changed file with 43 additions and 15 deletions.
58 changes: 43 additions & 15 deletions lib/core_dom/event_handler.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,73 @@ part of angular.core.dom;

typedef void EventFunction(event);

/**
* [EventHandler] is responsible for handling events bound using on-* syntax
* (i.e. `on-click="ctrl.doSomething();"`). The root of the application has an
* EventHandler attached as does every [NgComponent].
*
* Events bound within [NgComponent] are handled by EventHandler attached to
* their [ShadowRoot]. All other events are handled by EventHandler attached
* to the application root ([NgApp]).
*
* **Note**: The expressions are executed within the closest context.
*
* Example:
*
* <div foo>
* <button on-click="ctrl.say('Hello');">Button</button>;
* </div>
*
* @NgComponent(selector: '[foo]', publishAs: ctrl)
* class FooController {
* say(String something) => print(something);
* }
*
* When button is clicked, "Hello" will be printed in the console.
*/
@NgInjectableService()
class EventHandler {
dom.Node rootNode;
final Expando expando;
final ExceptionHandler exceptionHandler;
final listeners = <String, Function>{};
dom.Node _rootNode;
final Expando _expando;
final ExceptionHandler _exceptionHandler;
final _listeners = <String, Function>{};

EventHandler(this.rootNode, this.expando, this.exceptionHandler);
EventHandler(this._rootNode, this._expando, this._exceptionHandler);

/**
* Register an event. This makes sure that an event (of the specified name)
* which bubbles to this node, gets processed by this [EventHandler].
*/
void register(String eventName) {
listeners.putIfAbsent(eventName, () {
dom.EventListener eventListener = this.eventListener;
rootNode.on[eventName].listen(eventListener);
_listeners.putIfAbsent(eventName, () {
dom.EventListener eventListener = this._eventListener;
_rootNode.on[eventName].listen(eventListener);
return eventListener;
});
}

void eventListener(dom.Event event) {
void _eventListener(dom.Event event) {
dom.Node element = event.target;
while (element != null && element != rootNode) {
while (element != null && element != _rootNode) {
var expression;
if (element is dom.Element)
expression = (element as dom.Element).attributes[eventNameToAttrName(event.type)];
if (expression != null) {
try {
var scope = getScope(element);
var scope = _getScope(element);
if (scope != null) scope.eval(expression);
} catch (e, s) {
exceptionHandler(e, s);
_exceptionHandler(e, s);
}
}
element = element.parentNode;
}
}

Scope getScope(dom.Node element) {
Scope _getScope(dom.Node element) {
// var topElement = (rootNode is dom.ShadowRoot) ? rootNode.parentNode : rootNode;
while (element != rootNode.parentNode) {
ElementProbe probe = expando[element];
while (element != _rootNode.parentNode) {
ElementProbe probe = _expando[element];
if (probe != null) {
return probe.scope;
}
Expand Down

0 comments on commit 4e4415e

Please sign in to comment.