forked from google/jsaction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsaction.js
36 lines (32 loc) · 1.28 KB
/
jsaction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* @fileoverview Public static API for using jsaction.
*/
goog.provide('jsaction');
/**
* Fires a custom event with an optional payload. Only intended to be consumed
* by jsaction itself. Supported in Firefox 6+, IE 9+, and all Chrome versions.
*
* TODO(user): Investigate polyfill options.
*
* @param {!Element} target The target element.
* @param {string} type The type of the action, e.g. 'submit'.
* @param {!Object.<string, *>=} opt_data An optional data payload.
*/
jsaction.fireCustomEvent = function(target, type, opt_data) {
// We don't use the CustomEvent constructor directly since it isn't supported
// in IE 9 or 10 and initCustomEvent below works just fine.
try {
var customEvent = document.createEvent('CustomEvent');
} catch (e) {
// TODO(user): Call directly into jsaction when events fail (FF4/5, Android
// Gingerbread).
return;
}
// We use '_type' for the event contract, which lives in a separate
// compilation unit, but also include the renamable keys so that event
// consumers can access the data directly, e.g. detail.type instead of
// detail['type'].
var detail = {'_type': type, type: type, data: opt_data};
customEvent.initCustomEvent(jsaction.EventType.CUSTOM, true, false, detail);
target.dispatchEvent(customEvent);
};