OLD | NEW |
1 /* Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 * Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 * found in the LICENSE file. */ | 3 // found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * @fileoverview | 6 * @fileoverview |
7 * `cr-events` provides helpers for handling events in Chrome Polymer elements. | 7 * `cr-events` provides helpers for handling events in Chrome Polymer elements. |
8 * | 8 * |
9 * Example: | 9 * Example: |
10 * | 10 * |
11 * <cr-events id="events"></cr-events> | 11 * <cr-events id="events"></cr-events> |
12 * | 12 * |
13 * Usage: | 13 * Usage: |
14 * | 14 * |
15 * this.$.events.forward(this.$.element, ['change']); | 15 * this.$.events.forward(this.$.element, ['change']); |
16 * | 16 * |
17 * @element cr-events | 17 * @element cr-events |
18 */ | 18 */ |
19 Polymer({ | 19 Polymer({ |
20 /** | 20 /** |
21 * Sets up an element to forward events across the shadow boundary, for events | 21 * Sets up an element to forward events across the shadow boundary, for events |
22 * which normally stop at the root node (see http://goo.gl/WGMO9x). | 22 * which normally stop at the root node (see http://goo.gl/WGMO9x). |
23 * @param {!HTMLElement} element The element to forward events from. | 23 * @param {!HTMLElement} element The element to forward events from. |
24 * @param {!Array.<string>} events The events to forward. | 24 * @param {!Array.<string>} events The events to forward. |
25 */ | 25 */ |
26 forward: function(element, events) { | 26 forward: function(element, events) { |
27 for (var i = 0; i < events.length; i++) | 27 for (var i = 0; i < events.length; i++) |
28 element.addEventListener(events[i], this.forwardEvent_); | 28 element.addEventListener(events[i], this.forwardEvent_); |
29 }, | 29 }, |
30 | 30 |
31 | |
32 /** | 31 /** |
33 * Forwards events that don't automatically cross the shadow boundary | 32 * Forwards events that don't automatically cross the shadow boundary |
34 * if the event should bubble. | 33 * if the event should bubble. |
35 * @param {!Event} e The event to forward. | 34 * @param {!Event} e The event to forward. |
36 * @param {*} detail Data passed when initializing the event. | 35 * @param {*} detail Data passed when initializing the event. |
37 * @param {Node=} opt_sender Node that declared the handler. | 36 * @param {Node=} opt_sender Node that declared the handler. |
38 * @private | 37 * @private |
39 */ | 38 */ |
40 forwardEvent_: function(e, detail, opt_sender) { | 39 forwardEvent_: function(e, detail, opt_sender) { |
41 if (!e.bubbles) | 40 if (!e.bubbles) |
42 return; | 41 return; |
43 | 42 |
44 var node = e.path[e.path.length - 1]; | 43 var node = e.path[e.path.length - 1]; |
45 if (node instanceof ShadowRoot) { | 44 if (node instanceof ShadowRoot) { |
46 // Forward the event to the shadow host. | 45 // Forward the event to the shadow host. |
47 e.stopPropagation(); | 46 e.stopPropagation(); |
48 node.host.fire(e.type, detail, node.host, true, e.cancelable); | 47 node.host.fire(e.type, detail, node.host, true, e.cancelable); |
49 } | 48 } |
50 }, | 49 }, |
51 }); | 50 }); |
OLD | NEW |