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 * @return {boolean} If any of the listeners called {@code preventDefault} 40 * during the dispatch this will return false. 41 */ 42 function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) { 43 var e = new Event(type, opt_bubbles, opt_cancelable); 44 return target.dispatchEvent(e); 45 } 46 47 return { 48 Event: Event, 49 dispatchSimpleEvent: dispatchSimpleEvent 50 }; 51 }); 52 53