| OLD | NEW |
| 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 22 matching lines...) Expand all Loading... |
| 33 /** A specialized Stream available to [Element]s to enable event delegation. */ | 33 /** A specialized Stream available to [Element]s to enable event delegation. */ |
| 34 abstract class ElementStream<T extends Event> implements Stream<T> { | 34 abstract class ElementStream<T extends Event> implements Stream<T> { |
| 35 /** | 35 /** |
| 36 * Return a stream that only fires when the particular event fires for | 36 * Return a stream that only fires when the particular event fires for |
| 37 * elements matching the specified CSS selector. | 37 * elements matching the specified CSS selector. |
| 38 * | 38 * |
| 39 * This is the Dart equivalent to jQuery's | 39 * This is the Dart equivalent to jQuery's |
| 40 * [delegate](http://api.jquery.com/delegate/). | 40 * [delegate](http://api.jquery.com/delegate/). |
| 41 */ | 41 */ |
| 42 Stream<T> matches(String selector); | 42 Stream<T> matches(String selector); |
| 43 |
| 44 StreamSubscription<T> capture(void onData(T event)); |
| 43 } | 45 } |
| 44 | 46 |
| 45 /** | 47 /** |
| 46 * Adapter for exposing DOM Element events as streams, while also allowing | 48 * Adapter for exposing DOM Element events as streams, while also allowing |
| 47 * event delegation. | 49 * event delegation. |
| 48 */ | 50 */ |
| 49 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T> | 51 class _ElementEventStreamImpl<T extends Event> extends _EventStream<T> |
| 50 implements ElementStream<T> { | 52 implements ElementStream<T> { |
| 51 _ElementEventStreamImpl(target, eventType, useCapture) : | 53 _ElementEventStreamImpl(target, eventType, useCapture) : |
| 52 super(target, eventType, useCapture); | 54 super(target, eventType, useCapture); |
| 53 | 55 |
| 54 Stream<T> matches(String selector) => this.where( | 56 Stream<T> matches(String selector) => this.where( |
| 55 (event) => event.target.matchesWithAncestors(selector)).map((e) { | 57 (event) => event.target.matchesWithAncestors(selector)).map((e) { |
| 56 e._selector = selector; | 58 e._selector = selector; |
| 57 return e; | 59 return e; |
| 58 }); | 60 }); |
| 61 |
| 62 StreamSubscription<T> capture(void onData(T event)) => |
| 63 new _EventStreamSubscription<T>( |
| 64 this._target, this._eventType, onData, true); |
| 59 } | 65 } |
| 60 | 66 |
| 61 /** | 67 /** |
| 62 * Adapter for exposing events on a collection of DOM Elements as streams, | 68 * Adapter for exposing events on a collection of DOM Elements as streams, |
| 63 * while also allowing event delegation. | 69 * while also allowing event delegation. |
| 64 */ | 70 */ |
| 65 class _ElementListEventStreamImpl<T extends Event> extends Stream<T> | 71 class _ElementListEventStreamImpl<T extends Event> extends Stream<T> |
| 66 implements ElementStream<T> { | 72 implements ElementStream<T> { |
| 67 final _StreamPool _pool; | 73 final Iterable<Element> _targetList; |
| 68 Stream<T> _stream; | 74 final bool _useCapture; |
| 75 final String _eventType; |
| 69 | 76 |
| 70 _ElementListEventStreamImpl(targetList, eventType, useCapture) : | 77 _ElementListEventStreamImpl( |
| 71 _pool = new _StreamPool.broadcast() { | 78 this._targetList, this._eventType, this._useCapture); |
| 72 for (Element target in targetList) { | |
| 73 var stream = new _EventStream(target, eventType, useCapture); | |
| 74 _pool.add(stream); | |
| 75 } | |
| 76 _stream = _pool.stream; | |
| 77 } | |
| 78 | 79 |
| 79 Stream<T> matches(String selector) => this.where( | 80 Stream<T> matches(String selector) => this.where( |
| 80 (event) => event.target.matchesWithAncestors(selector)).map((e) { | 81 (event) => event.target.matchesWithAncestors(selector)).map((e) { |
| 81 e._selector = selector; | 82 e._selector = selector; |
| 82 return e; | 83 return e; |
| 83 }); | 84 }); |
| 84 | 85 |
| 85 // Delegate all regular Stream behavor to our wrapped Stream. | 86 // Delegate all regular Stream behavior to a wrapped Stream. |
| 86 StreamSubscription<T> listen(void onData(T event), | 87 StreamSubscription<T> listen(void onData(T event), |
| 87 { Function onError, | 88 { Function onError, |
| 88 void onDone(), | 89 void onDone(), |
| 89 bool cancelOnError}) => | 90 bool cancelOnError}) { |
| 90 _stream.listen(onData, onError: onError, onDone: onDone, | 91 var pool = new _StreamPool.broadcast(); |
| 92 for (var target in _targetList) { |
| 93 pool.add(new _EventStream(target, _eventType, _useCapture)); |
| 94 } |
| 95 return pool.stream.listen(onData, onError: onError, onDone: onDone, |
| 91 cancelOnError: cancelOnError); | 96 cancelOnError: cancelOnError); |
| 97 } |
| 98 |
| 99 StreamSubscription<T> capture(void onData(T event)) { |
| 100 var pool = new _StreamPool.broadcast(); |
| 101 for (var target in _targetList) { |
| 102 pool.add(new _EventStream(target, _eventType, true)); |
| 103 } |
| 104 return pool.stream.listen(onData); |
| 105 } |
| 106 |
| 92 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), | 107 Stream<T> asBroadcastStream({void onListen(StreamSubscription subscription), |
| 93 void onCancel(StreamSubscription subscription)}) | 108 void onCancel(StreamSubscription subscription)}) |
| 94 => _stream; | 109 => this; |
| 95 bool get isBroadcast => true; | 110 bool get isBroadcast => true; |
| 96 } | 111 } |
| 97 | 112 |
| 98 /** | 113 /** |
| 99 * A stream of custom events, which enables the user to "fire" (add) their own | 114 * A stream of custom events, which enables the user to "fire" (add) their own |
| 100 * custom events to a stream. | 115 * custom events to a stream. |
| 101 */ | 116 */ |
| 102 abstract class CustomStream<T extends Event> implements Stream<T> { | 117 abstract class CustomStream<T extends Event> implements Stream<T> { |
| 103 /** | 118 /** |
| 104 * Add the following custom event to the stream for dispatching to interested | 119 * Add the following custom event to the stream for dispatching to interested |
| (...skipping 277 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 | 397 |
| 383 ElementStream<T> _forElementList(ElementList e, | 398 ElementStream<T> _forElementList(ElementList e, |
| 384 {bool useCapture: false}) { | 399 {bool useCapture: false}) { |
| 385 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); | 400 return new _ElementListEventStreamImpl(e, _eventTypeGetter(e), useCapture); |
| 386 } | 401 } |
| 387 | 402 |
| 388 String getEventType(EventTarget target) { | 403 String getEventType(EventTarget target) { |
| 389 return _eventTypeGetter(target); | 404 return _eventTypeGetter(target); |
| 390 } | 405 } |
| 391 } | 406 } |
| OLD | NEW |