1<!DOCTYPE html>
2<!--
3Copyright (c) 2014 The Chromium Authors. All rights reserved.
4Use of this source code is governed by a BSD-style license that can be
5found in the LICENSE file.
6-->
7
8<link rel="import" href="/tracing/base/event_target.html">
9
10<script>
11'use strict';
12
13tr.exportTo('tr.b', function() {
14  var Event;
15  if (tr.isHeadless) {
16    /**
17     * Creates a new event to be used with tr.b.EventTarget or DOM EventTarget
18     * objects.
19     * @param {string} type The name of the event.
20     * @param {boolean=} opt_bubbles Whether the event bubbles.
21     *     Default is false.
22     * @param {boolean=} opt_preventable Whether the default action of the event
23     *     can be prevented.
24     * @constructor
25     * @extends {Event}
26     */
27    function HeadlessEvent(type, opt_bubbles, opt_preventable) {
28      this.type = type;
29      this.bubbles = (opt_bubbles !== undefined ?
30          !!opt_bubbles : false);
31      this.cancelable = (opt_preventable !== undefined ?
32          !!opt_preventable : false);
33
34      this.defaultPrevented = false;
35      this.cancelBubble = false;
36    };
37
38    HeadlessEvent.prototype = {
39      preventDefault: function() {
40        this.defaultPrevented = true;
41      },
42
43      stopPropagation: function() {
44        this.cancelBubble = true;
45      }
46    };
47    Event = HeadlessEvent;
48  } else {
49    /**
50     * Creates a new event to be used with tr.b.EventTarget or DOM EventTarget
51     * objects.
52     * @param {string} type The name of the event.
53     * @param {boolean=} opt_bubbles Whether the event bubbles.
54     *     Default is false.
55     * @param {boolean=} opt_preventable Whether the default action of the event
56     *     can be prevented.
57     * @constructor
58     * @extends {Event}
59     */
60    function TrEvent(type, opt_bubbles, opt_preventable) {
61      var e = tr.doc.createEvent('Event');
62      e.initEvent(type, !!opt_bubbles, !!opt_preventable);
63      e.__proto__ = global.Event.prototype;
64      return e;
65    };
66
67    TrEvent.prototype = {
68      __proto__: global.Event.prototype
69    };
70    Event = TrEvent;
71  }
72
73  /**
74   * Dispatches a simple event on an event target.
75   * @param {!EventTarget} target The event target to dispatch the event on.
76   * @param {string} type The type of the event.
77   * @param {boolean=} opt_bubbles Whether the event bubbles or not.
78   * @param {boolean=} opt_cancelable Whether the default action of the event
79   *     can be prevented.
80   * @return {boolean} If any of the listeners called {@code preventDefault}
81   *     during the dispatch this will return false.
82   */
83  function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) {
84    var e = new tr.b.Event(type, opt_bubbles, opt_cancelable);
85    return target.dispatchEvent(e);
86  }
87
88  return {
89    Event: Event,
90    dispatchSimpleEvent: dispatchSimpleEvent
91  };
92});
93</script>
94