This repository has been archived by the owner on Mar 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
events.js
84 lines (77 loc) · 2.97 KB
/
events.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Utility methods for o-ads events. Methods defined here are added to the utils object not the utils.event object.
* @author Origami Advertising, origami.advertising@ft.com
* @module utils/events
* @see utils
*/
// Creates a timestamp in the browser's performance entry buffer
// for later use
export const perfMark = name => {
/* istanbul ignore next */
const performance = window.LUX || window.performance || window.msPerformance || window.webkitPerformance || window.mozPerformance;
if (performance && performance.mark) {
performance.mark(name);
}
};
/**
* Broadscasts an o-ads event
* @param {string} name The name of the event
* @param {object} data The data to send as event detail
* @param {HTMLElement} target The element to attach the event listener to
*/
export function broadcast(eventName, data, target) {
/* istanbul ignore next: ignore the final fallback as hard trigger */
target = target || document.body || document.documentElement;
eventName = `oAds.${eventName}`;
const opts = {
bubbles: true,
cancelable: true,
detail: data
};
const markName = typeof data === 'object' && 'pos' in data && 'name' in data ? [eventName, data.pos, data.name, data.size.length ? data.size.toString() : ''].join('__') : eventName;
perfMark(markName);
target.dispatchEvent(new CustomEvent(eventName, opts));
}
/**
* Sets an event listener for an oAds event
* @param {string} name The name of the event
* @param {function} callback The function to execute on the event
* @param {HTMLElement} target The element to attach the event listener to
*/
export function on(name, callback, target) {
name = `oAds.${name}`;
/* istanbul ignore next: ignore the final fallback as hard trigger */
target = target || document.body || document.documentElement;
target.addEventListener(name, callback);
}
/**
* Removes an event listener for an oAds event
* @param {string} name The name of the event
* @param {function} callback The function on the event to be removed
* @param {HTMLElement} target The element the event listener is attached to
*/
export function off(name, callback, target) {
name = `oAds.${name}`;
/* istanbul ignore next: ignore the final fallback as hard trigger */
target = target || document.body || document.documentElement;
target.removeEventListener(name, callback);
}
/**
* Sets a one time event listener for an oAds event
* @param {string} name The name of the event
* @param {function} callback The function to execute on the event
* @param {HTMLElement} target The element to attach the event listener to
*/
export function once(name, callback, target) {
const handler = function(event) {
/* istanbul ignore next: ignore the final fallback as hard trigger */
const targ = event.target || event.srcElement;
targ.removeEventListener(name = `oAds.${name}`, callback);
if (callback) {
callback(event);
// we set callback to null so if for some reason the listener isn't removed the callback will still only be called once
callback = null;
}
};
on(name, handler, target);
}