Jamie Gennis | 66a3768 | 2013-07-15 18:29:18 -0700 | [diff] [blame^] | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | 'use strict'; |
| 6 | |
| 7 | base.require('base.event_target'); |
| 8 | |
| 9 | base.exportTo('base', function() { |
| 10 | /** |
| 11 | * Creates a new event to be used with base.EventTarget or DOM EventTarget |
| 12 | * objects. |
| 13 | * @param {string} type The name of the event. |
| 14 | * @param {boolean=} opt_bubbles Whether the event bubbles. |
| 15 | * Default is false. |
| 16 | * @param {boolean=} opt_preventable Whether the default action of the event |
| 17 | * can be prevented. |
| 18 | * @constructor |
| 19 | * @extends {Event} |
| 20 | */ |
| 21 | function Event(type, opt_bubbles, opt_preventable) { |
| 22 | var e = base.doc.createEvent('Event'); |
| 23 | e.initEvent(type, !!opt_bubbles, !!opt_preventable); |
| 24 | e.__proto__ = global.Event.prototype; |
| 25 | return e; |
| 26 | }; |
| 27 | |
| 28 | Event.prototype = { |
| 29 | __proto__: global.Event.prototype |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * Dispatches a simple event on an event target. |
| 34 | * @param {!EventTarget} target The event target to dispatch the event on. |
| 35 | * @param {string} type The type of the event. |
| 36 | * @param {boolean=} opt_bubbles Whether the event bubbles or not. |
| 37 | * @param {boolean=} opt_cancelable Whether the default action of the event |
| 38 | * can be prevented. |
| 39 | * @param {boolean=} opt_bubbles Whether the event bubbles or not. |
| 40 | * @param {boolean=} opt_cancelable Whether the default action of the event |
| 41 | * can be prevented. |
| 42 | * @return {boolean} If any of the listeners called {@code preventDefault} |
| 43 | * during the dispatch this will return false. |
| 44 | */ |
| 45 | function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) { |
| 46 | var e = new Event(type, opt_bubbles, opt_cancelable); |
| 47 | return target.dispatchEvent(e); |
| 48 | } |
| 49 | |
| 50 | return { |
| 51 | Event: Event, |
| 52 | dispatchSimpleEvent: dispatchSimpleEvent |
| 53 | }; |
| 54 | }); |
| 55 | |