blob: c023ce81b1e48634ff404605b86b9a732442fb48 [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright (c) 2014 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.
-->
<link rel="import" href="/base/event_target.html">
<script>
'use strict';
tr.exportTo('tr.b', function() {
var Event;
if (tr.isHeadless) {
/**
* Creates a new event to be used with tr.b.EventTarget or DOM EventTarget
* objects.
* @param {string} type The name of the event.
* @param {boolean=} opt_bubbles Whether the event bubbles.
* Default is false.
* @param {boolean=} opt_preventable Whether the default action of the event
* can be prevented.
* @constructor
* @extends {Event}
*/
function HeadlessEvent(type, opt_bubbles, opt_preventable) {
this.type = type;
this.bubbles = (opt_bubbles !== undefined ?
!!opt_bubbles : false);
this.cancelable = (opt_preventable !== undefined ?
!!opt_preventable : false);
this.defaultPrevented = false;
this.cancelBubble = false;
};
HeadlessEvent.prototype = {
preventDefault: function() {
this.defaultPrevented = true;
},
stopPropagation: function() {
this.cancelBubble = true;
}
};
Event = HeadlessEvent;
} else {
/**
* Creates a new event to be used with tr.b.EventTarget or DOM EventTarget
* objects.
* @param {string} type The name of the event.
* @param {boolean=} opt_bubbles Whether the event bubbles.
* Default is false.
* @param {boolean=} opt_preventable Whether the default action of the event
* can be prevented.
* @constructor
* @extends {Event}
*/
function TrEvent(type, opt_bubbles, opt_preventable) {
var e = tr.doc.createEvent('Event');
e.initEvent(type, !!opt_bubbles, !!opt_preventable);
e.__proto__ = global.Event.prototype;
return e;
};
TrEvent.prototype = {
__proto__: global.Event.prototype
};
Event = TrEvent;
}
/**
* Dispatches a simple event on an event target.
* @param {!EventTarget} target The event target to dispatch the event on.
* @param {string} type The type of the event.
* @param {boolean=} opt_bubbles Whether the event bubbles or not.
* @param {boolean=} opt_cancelable Whether the default action of the event
* can be prevented.
* @return {boolean} If any of the listeners called {@code preventDefault}
* during the dispatch this will return false.
*/
function dispatchSimpleEvent(target, type, opt_bubbles, opt_cancelable) {
var e = new tr.b.Event(type, opt_bubbles, opt_cancelable);
return target.dispatchEvent(e);
}
return {
Event: Event,
dispatchSimpleEvent: dispatchSimpleEvent
};
});
</script>