Chromium Code Reviews| Index: ui/webui/resources/cr_elements/cr_events/cr-events.js |
| diff --git a/ui/webui/resources/cr_elements/cr_events/cr-events.js b/ui/webui/resources/cr_elements/cr_events/cr-events.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7355a17ccc5ee19f3eac57cbff69f4ae1d2009d0 |
| --- /dev/null |
| +++ b/ui/webui/resources/cr_elements/cr_events/cr-events.js |
| @@ -0,0 +1,50 @@ |
| +/* Copyright 2015 The Chromium Authors. All rights reserved. |
| + * Use of this source code is governed by a BSD-style license that can be |
| + * found in the LICENSE file. */ |
| + |
| +/** |
| + * @fileoverview |
| + * `cr-events` provides helpers for handling events in Chrome Polymer elements. |
| + * |
| + * Example: |
| + * |
| + * <cr-events id="events"></cr-events> |
| + * |
| + * Usage: |
| + * |
| + * this.$.events.forward(this.$.element, ['change']); |
| + * |
| + * @element cr-events |
| + */ |
| +Polymer({ |
| + /** |
| + * Sets up an element to forward events across the shadow boundary, for events |
| + * which normally stop at the root node (see http://goo.gl/WGMO9x). |
| + * @param {!HTMLElement} element The element to forward events from. |
| + * @param {!Array.<string>} events The events to forward. |
| + */ |
| + forward: function(element, events) { |
| + for (var i = 0; i < events.length; i++) |
| + element.addEventListener(events[i], this.forwardEvent_); |
| + }, |
| + |
| + /** |
|
James Hawkins
2015/02/09 22:24:34
nit: Double-blank lines above JSDoc blocks (though
michaelpg
2015/02/09 22:40:04
Hmm I haven't seen that style in other web ui, whe
James Hawkins
2015/02/09 22:46:23
I forgot this isn't in the JS style guide, but the
michaelpg
2015/02/09 23:08:31
Done.
Dan Beam
2015/02/10 00:23:38
fwiw: I'd discourage this for consistency with oth
|
| + * Forwards events that don't automatically cross the shadow boundary |
| + * if the event should bubble. |
| + * @param {!Event} e The event to forward. |
| + * @param {*} detail Data passed when initializing the event. |
| + * @param {Node=} sender Node that declared the handler. |
| + * @private |
| + */ |
| + forwardEvent_: function(e, detail, sender) { |
| + if (!e.bubbles) |
| + return; |
| + |
| + var node = e.path[e.path.length - 1]; |
| + if (node instanceof ShadowRoot) { |
| + // Forward the event to the shadow host. |
| + e.stopPropagation(); |
| + node.host.fire(e.type, detail, node.host, true, e.cancelable); |
| + } |
| + }, |
| +}); |