Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(510)

Side by Side Diff: tools/dom/src/EventStreamProvider.dart

Issue 26722002: Add "matchingTarget" function to Event, which is the Element that matched the CSS value selector. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of html; 5 part of html;
6 6
7 /** 7 /**
8 * Adapter for exposing DOM events as Dart streams. 8 * Adapter for exposing DOM events as Dart streams.
9 */ 9 */
10 class _EventStream<T extends Event> extends Stream<T> { 10 class _EventStream<T extends Event> extends Stream<T> {
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
45 /** 45 /**
46 * Adapter for exposing DOM Element events as streams, while also allowing 46 * Adapter for exposing DOM Element events as streams, while also allowing
47 * event delegation. 47 * event delegation.
48 */ 48 */
49 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T> 49 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T>
50 implements ElementStream<T> { 50 implements ElementStream<T> {
51 _ElementEventStreamImpl(target, eventType, useCapture) : 51 _ElementEventStreamImpl(target, eventType, useCapture) :
52 super(target, eventType, useCapture); 52 super(target, eventType, useCapture);
53 53
54 Stream<T> matches(String selector) => 54 Stream<T> matches(String selector) =>
55 this.where((event) => event.target.matchesWithAncestors(selector)); 55 new _MatchingStream(
56 this.where((event) => event.target.matchesWithAncestors(selector)),
57 selector);
56 } 58 }
57 59
58 /** 60 /**
59 * Adapter for exposing events on a collection of DOM Elements as streams, 61 * Adapter for exposing events on a collection of DOM Elements as streams,
60 * while also allowing event delegation. 62 * while also allowing event delegation.
61 */ 63 */
62 class _ElementListEventStreamImpl<T extends Event> extends Stream<T> 64 class _ElementListEventStreamImpl<T extends Event> extends Stream<T>
63 implements ElementStream<T> { 65 implements ElementStream<T> {
64 final _StreamPool _pool; 66 final _StreamPool _pool;
65 Stream<T> _stream; 67 Stream<T> _stream;
66 68
67 _ElementListEventStreamImpl(targetList, eventType, useCapture) : 69 _ElementListEventStreamImpl(targetList, eventType, useCapture) :
68 _pool = new _StreamPool.broadcast() { 70 _pool = new _StreamPool.broadcast() {
69 for (Element target in targetList) { 71 for (Element target in targetList) {
70 var stream = new _EventStream(target, eventType, useCapture); 72 var stream = new _EventStream(target, eventType, useCapture);
71 _pool.add(stream); 73 _pool.add(stream);
72 } 74 }
73 _stream = _pool.stream; 75 _stream = _pool.stream;
74 } 76 }
75 77
76 Stream<T> matches(String selector) => 78 Stream<T> matches(String selector) =>
blois 2013/10/09 22:25:44 how about => this.where((event) => event.target.ma
Emily Fortuna 2013/10/09 22:39:18 Good point. fixed.
77 this.where((event) => event.target.matchesWithAncestors(selector)); 79 new _MatchingStream(
80 this.where((event) => event.target.matchesWithAncestors(selector)),
81 selector);
78 82
79 // Delegate all regular Stream behavor to our wrapped Stream. 83 // Delegate all regular Stream behavor to our wrapped Stream.
80 StreamSubscription<T> listen(void onData(T event), 84 StreamSubscription<T> listen(void onData(T event),
81 { void onError(error), 85 { void onError(error),
82 void onDone(), 86 void onDone(),
83 bool cancelOnError}) => 87 bool cancelOnError}) =>
84 _stream.listen(onData, onError: onError, onDone: onDone, 88 _stream.listen(onData, onError: onError, onDone: onDone,
85 cancelOnError: cancelOnError); 89 cancelOnError: cancelOnError);
86 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), 90 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription),
87 void onCancel(StreamSubscription subscription)}) 91 void onCancel(StreamSubscription subscription)})
88 => _stream; 92 => _stream;
89 bool get isBroadcast => true; 93 bool get isBroadcast => true;
90 } 94 }
91 95
92 /** 96 /**
97 * A stream that keeps track of what CSS selectors it used to collect element
98 * events for event delegation.
99 */
100 class _MatchingStream<T extends Event> extends Stream<T> {
101 Stream<T> _stream;
102 String selector;
103
104 _MatchingStream(Stream this._stream, String this.selector);
105
106 // Delegate all regular Stream behavor to our wrapped Stream.
107 StreamSubscription<T> listen(void onData(T event),
108 { void onError(error),
109 void onDone(),
110 bool cancelOnError}) =>
111 _stream.listen((e) { e._selector = selector; return onData(e); },
112 onError: onError,
113 onDone: onDone,
114 cancelOnError: cancelOnError);
115 }
116
117 /**
93 * A stream of custom events, which enables the user to "fire" (add) their own 118 * A stream of custom events, which enables the user to "fire" (add) their own
94 * custom events to a stream. 119 * custom events to a stream.
95 */ 120 */
96 abstract class CustomStream<T extends Event> implements Stream<T> { 121 abstract class CustomStream<T extends Event> implements Stream<T> {
97 /** 122 /**
98 * Add the following custom event to the stream for dispatching to interested 123 * Add the following custom event to the stream for dispatching to interested
99 * listeners. 124 * listeners.
100 */ 125 */
101 void add(T event); 126 void add(T event);
102 } 127 }
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 407
383 ElementStream<T> _forElementList(ElementList e, 408 ElementStream<T> _forElementList(ElementList e,
384 {bool useCapture: false}) { 409 {bool useCapture: false}) {
385 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); 410 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture);
386 } 411 }
387 412
388 String getEventType(EventTarget target) { 413 String getEventType(EventTarget target) {
389 return _eventTypeGetter(target); 414 return _eventTypeGetter(target);
390 } 415 }
391 } 416 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698