-
Notifications
You must be signed in to change notification settings - Fork 56
/
core_poi.js
executable file
·206 lines (186 loc) · 5.91 KB
/
core_poi.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import {OIL_CONFIG} from './core_constants.js';
import {getHubLocation, getHubOrigin, getPoiGroupName, isPoiActive} from './core_config.js';
import {getOrigin, registerMessageListener, removeMessageListener} from './core_utils.js';
import {logError, logInfo} from './core_log.js';
// Timeout after which promises should return
const TIMEOUT = 500;
// TODO really necessary?
let frameListenerRegistered = false;
// INTERNAL FUNCTIONS
export function addFrame(iframeUrl) {
let iframe = document.getElementById('oil-frame');
if (!iframe) {
logInfo('Creating iframe...');
iframe = document.createElement('iframe');
iframe.setAttribute('id', 'oil-frame');
iframe.setAttribute('title', 'oil-frame');
iframe.setAttribute('aria-hidden', 'true');
iframe.setAttribute('src', iframeUrl);
iframe.setAttribute('sandbox', 'allow-same-origin allow-scripts allow-forms allow-top-navigation');
iframe.style.width = '0';
iframe.style.height = '0';
iframe.style.border = '0';
iframe.style.border = 'none';
iframe.style.display = 'block';
document.body.appendChild(iframe);
} else {
logInfo('Found iframe');
}
return iframe;
}
/**
* Initializes the OIL iFrame
* @function
* @return Promise<any> as object {iframe:Element,config:{}}when iFrame is loaded
*/
export function init() {
return new Promise((resolve) => setTimeout(() => {
logInfo('Initializing Frame...');
if (isPoiActive()) {
let hubLocation = getHubLocation();
if (hubLocation) {
// setup iframe
let iframe = addFrame(hubLocation);
if (iframe && !iframe.onload) {
// Listen to message from child window after iFrame load
iframe.onload = () => resolve({iframe: iframe});
} else {
// if already loaded directly invoke
resolve({iframe: iframe});
}
} else {
logError(`Config for ${OIL_CONFIG.ATTR_HUB_ORIGIN} and ${OIL_CONFIG.ATTR_HUB_PATH} isnt set. No POI possible.`);
resolve(false);
}
} else {
logInfo('POI not active. Frame not initialized.');
resolve(false);
}
}));
}
/**
* Sent given event to hidden iframe
* @param eventName - event to sent
* @param origin - orgin url (aka parent)
* @param payload - payload to send
* @function
*/
export function sendEventToFrame(eventName, origin, payload = {}) {
logInfo('Send to Frame:', eventName, origin);
if (!isPoiActive()) {
return;
}
init().then((result) => {
let iframe = result.iframe;
let hubDomain = getHubOrigin(),
groupName = getPoiGroupName();
if (iframe && hubDomain) {
// tag::subscriber-postMessage[]
// see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage#Syntax
// MSIE needs Strings in postMessage
let message = JSON.stringify({event: eventName, origin: origin, group_name: groupName, payload: payload});
iframe.contentWindow.postMessage(message, hubDomain);
// end::subscriber-postMessage[]
}
});
}
/**
* Read configuration from hidden iframe
* @param origin - origin url (aka parent)
* @function
* @return Promise<any> with result of the
*/
function readConfigFromFrame(origin) {
return new Promise((resolve) => {
if (!isPoiActive()) {
resolve(false);
}
function handler(event) {
// tag::subscriber-receiveMessage[]
// only listen to our hub
let hubOrigin = getHubOrigin();
if (hubOrigin && hubOrigin.indexOf(event.origin) !== -1) {
let message = typeof event.data !== 'object' ? JSON.parse(event.data) : event.data;
logInfo('Message from hub received...', event.origin, message);
removeMessageListener(handler);
frameListenerRegistered = false;
resolve(message);
}
// end::subscriber-receiveMessage[]
}
// TODO why do we remove the listener, why do we use setTimeout? cant this be simpler?
// defer post to next tick
setTimeout(() => sendEventToFrame('oil-status-read', origin));
if (!frameListenerRegistered) {
// Listen to message from child window
registerMessageListener(handler);
frameListenerRegistered = true;
}
// add timeout for hub answer read
setTimeout(() => {
removeMessageListener(handler);
frameListenerRegistered = false;
resolve(false);
}, TIMEOUT);
});
}
// PUBLIC API
/**
* Read config for Power Opt IN from hidden iframe
* @function
* @return Promise<any> with result (is true if POI ist set, otherwise false)
*/
export function verifyPowerOptIn() {
return new Promise((resolve) => {
if (!isPoiActive()) {
resolve({power_opt_in: false});
} else {
init().then((result) => {
let iframe = result.iframe;
if (iframe) {
if (!iframe.onload) {
// Listen to message from child window after iFrame load
iframe.onload = () => readConfigFromFrame(getOrigin()).then((data) => {
if (!data) {
resolve({power_opt_in: false});
}
resolve(data);
});
} else {
// if already loaded directly invoke
readConfigFromFrame(getOrigin()).then((data) => {
if (!data) {
resolve({power_opt_in: false});
}
resolve(data);
});
}
} else {
logInfo('Could not initialize POI. Fallback to POI false.');
resolve({power_opt_in: false});
}
});
}
});
}
/**
* DeActivate Power Opt IN
* @function
* @return Promise when done
*/
// FIXME needs testing
export function deActivatePowerOptIn() {
logInfo('deActivatePowerOptIn');
if (!isPoiActive()) {
return new Promise((resolve) => {
resolve();
});
}
// init iFrame first
return new Promise((resolve) => init().then(() => {
// then deactivate
sendEventToFrame('oil-poi-delete', getOrigin(), {});
// defer answer to next tick
setTimeout(resolve);
}));
}