-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinmydata-base.js
More file actions
385 lines (325 loc) · 12.7 KB
/
Copy pathinmydata-base.js
File metadata and controls
385 lines (325 loc) · 12.7 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/**
* Base class for inmydata custom elements.
* Provides common functionality for handling attributes, rendering iframes, and message handling.
*/
export default class InmydataBase extends HTMLElement {
constructor() {
super();
// Attach a shadow DOM to the element
this.attachShadow({ mode: 'open' });
// Convert the class name to kebab-case (as per the component name) for use in error messages
this.componentName = this.constructor.name.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
this.componentId = this.uuidv4();
}
uuidv4() {
return "10000000-1000-4000-8000-100000000000".replace(/[018]/g, c =>
(+c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> +c / 4).toString(16)
);
}
/**
* Called when the element is added to the DOM.
* Sets up message handling and renders the element.
*/
connectedCallback() {
this.handleMessages();
//delay rendering until another component has logged in ('showLogin = false' means don't render yet). See listenForLoggedIn() for more details.
if (this.getCommonAttributes().showLogin === true)
{
this._render();
}
else
{
this.renderPlaceholder();
this.listenForLoggedIn();
}
}
/**
* Called when an observed attribute is changed.
* Re-renders the element.
*/
attributeChangedCallback(name) {
if (this._rendered) this.render();
}
/**
* Retrieves common attributes from the element.
* @returns {Object} An object containing common attributes.
*/
getCommonAttributes() {
const renderAttribute = this.getAttribute('render')?.toLowerCase();
const render = renderAttribute === 'false' || renderAttribute === false ? false : true;
return {
tenant: this.getAttribute('tenant'),
domain: this.getAttribute('domain') || 'inmydata.com',
width: this.getAttribute('width') || '100%',
height: this.getAttribute('height') || '100%',
render: renderAttribute ? render : true,
suppressDrilldown: this.getAttribute('suppress-drilldown')|| false,
demo: this.getAttribute('demo') || false,
showLogin: this.getAttribute('show-login') === "false" ? false : true,
modalDialog: this.getAttribute('modal-dialog') === "true" ? true : false
};
}
static getCommonObservedAttributes() {
return ['tenant','render','suppress-drilldown'];
}
/**
* Validates that the tenant attribute is provided.
* @param {string} tenant - The tenant attribute value.
* @throws {TypeError} If the tenant attribute is not provided.
*/
validateTenant(tenant) {
if (!tenant) {
throw new TypeError('You must provide a value for the tenant attribute');
}
}
/**
* Creates a URL for the iframe source.
* @param {string} path - The path for the URL.
* @returns {URL} The constructed URL.
*/
createUrl(path) {
const { tenant, domain, suppressDrilldown, demo } = this.getCommonAttributes();
let url;
if (domain.toLowerCase().indexOf("localhost") == 0)
url = new URL(path,`https://${domain}`);
else {
this.validateTenant(tenant);
url = new URL(path, `https://${tenant}.${domain}`);
}
if (demo){
//automatically log the user in as the demo user, for the demo tenant only
url.searchParams.append('demo', demo);
}
url.searchParams.append('WebComponentID', this.componentId);
if (suppressDrilldown) {
url.searchParams.append('SuppressDrilldown', suppressDrilldown);
}
return url;
}
/**
* Renders an iframe with the specified URL and title.
* @param {URL} url - The URL for the iframe source.
* @param {string} title - The title for the iframe.
*/
renderIframe(url, title) {
const { width, height, modalDialog } = this.getCommonAttributes();
const dialogOpen = modalDialog ? `<dialog>` : '';
const dialogClose = modalDialog ? `</dialog>` : '';
const closeButton = modalDialog ? `<span role="button" aria-label="Close dialog" class="close-button">X</span>` : '';
this.shadowRoot.innerHTML = `
<style>
:host {
${this._getHostStyle(width,height)}
}
iframe {
width: ${width};
height: ${height};
border: none;
margin: 0;
padding: 0;
}
dialog {
width: 90%;
height: 90%;
padding:10px;
overflow: hidden;
display: flex;
flex-direction: column;
}
.close-button {
display: flex;
flex-direction: row-reverse;
cursor: pointer;
}
</style>
${dialogOpen}
${closeButton}
<iframe
src="${url.toString()}"
title="${title}"
allow="microphone"
></iframe>
${dialogClose}
`;
if (modalDialog) {
this.dialog = this.shadowRoot.querySelector('dialog');
//add an event listener to close the dialog when the escape key is pressed
const closeOnEscape = (event) => {
if (event.key === 'Escape' || event.code === 'Escape') {
this.dialog.close();
this.emit('inmydata.dialog.close');
}
};
//add an event listener to close the dialog when the escape key is pressed
window.addEventListener('keydown', closeOnEscape);
this.shadowRoot.querySelector('.close-button').addEventListener('click', (e) => {
this.dialog.close();
this.emit('inmydata.dialog.close');
});
this.dialog.close();
}
}
/**
* Sets up message handling for the element.
* Listens for messages from the iframe and emits custom events.
*/
handleMessages() {
window.addEventListener(
"message",
this.boundMessageListener,
false,
);
}
boundMessageListener = this.messageListener.bind(this);
messageListener(event) {
const attributes = this.getCommonAttributes();
const { tenant, domain } = attributes;
// Validate the origin of the message
if (event.origin.indexOf(`https://${tenant}.${domain}`) !== 0) {
//this.error("INVALID ORIGIN: " + event.origin);
return;
}
// Parse the message and emit a custom event
let messageParts = event.data.split(':');
if (messageParts.length < 2 || messageParts[0].indexOf("inmydata.") != 0) {
//ensure only inmydata messages are emitted
return;
}
let data = JSON.parse(event.data.replace(messageParts[0] + ':', ''));
let eventName = messageParts[0];
this.emit(eventName, data);
}
/**
* Emits a custom event.
* @param {string} type - The event type.
* @param {Object} detail - Any details to pass along with the event.
* @returns {boolean} Whether the event was canceled.
*/
emit(type, detail = {}) {
// Create a new event
let event = new CustomEvent(type.toLowerCase(), {
bubbles: true,
cancelable: true,
detail: detail
});
// Dispatch the event
return this.dispatchEvent(event);
}
postMessage(message) {
const attributes = this.getCommonAttributes();
const { tenant, domain } = attributes;
const iframe = this.shadowRoot.querySelector('iframe');
iframe.contentWindow.postMessage(message, `https://${tenant}.${domain}`);
}
refresh() {
const iframe = this.shadowRoot.querySelector('iframe');
iframe.src = iframe.src;
}
/**
* Throws an error with a formatted message.
* @param {string} message - The error message.
* @throws {Error} The formatted error.
*/
error(message) {
throw new Error(`[${this.componentName}]: ${message}`);
}
listenForLoggedIn(){
//listen for 'logged in' events on other components and automatically refresh this component to removed the login prompt
window.addEventListener("load", () => {
var thisName = this.tagName.toLowerCase();
var visId = this.getAttribute('vis-id');
var dashId = this.getAttribute('dashboard-id');
var visComponents, dashComponents;
if (thisName != 'inmydata-vis'){
visComponents = document.querySelectorAll('inmydata-vis'); //other vis components
}
else{
visComponents = document.querySelectorAll('inmydata-vis[vis-id^="' + visId + '"]'); //other vis components
}
if (thisName != 'inmydata-dashboard'){
dashComponents = document.querySelectorAll('inmydata-dashboard'); //other dashboard components
}
else{
dashComponents = document.querySelectorAll('inmydata-dashboard[dashboard-id^="' + dashId + '"]'); //other dashboard components
}
if (thisName != 'inmydata-copilot')
{
//only listen for other copilots if this isn't a copilot itself - we're not likely to have two copilots, and they can't be individually identified
let copilot = document.querySelector('inmydata-copilot'); //other copilot components
if (copilot) copilot.addEventListener('inmydata.loggedin', (event) => {
// this.refresh();
this._render();
});
}
if (thisName != 'inmydata-insights'){
//only listen for other insights if this isn't an insights itself - we're not likely to have two insights, and they can't be individually identified
let insight = document.querySelector('inmydata-insights'); //other insight components
if (insight) insight.addEventListener('inmydata.loggedin', (event) => {
// this.refresh();
this._render();
});
}
visComponents.forEach((vis) => {
vis.addEventListener('inmydata.loggedin', (event) => {
// this.refresh();
this._render();
});
});
dashComponents.forEach((dash) => {
dash.addEventListener('inmydata.loggedin', (event) => {
// this.refresh();
this._render();
});
});
});
}
_getHostStyle(width,height) {
return `
display: block;
width: ${width};
height: ${height};
display: flex;
justify-content: center;
align-items: center;
font-family: Arial, sans-serif;
`;
}
_render() {
this.removePlaceholder();
this.render();
this._rendered = true;
}
renderPlaceholder(){
const { width, height } = this.getCommonAttributes();
this.shadowRoot.innerHTML = `
<style>
:host {
${this._getHostStyle(width,height)}
}
.inmydata-placeholder {
height:1em;
margin:auto;
}
</style>
<div class="inmydata-placeholder">
Waiting for login...
</div>
`;
}
removePlaceholder(){
this.shadowRoot.innerHTML = ``;
}
openModal(){
this.dialog?.showModal();
}
closeModal(){
this.dialog?.close();
}
dispose() {
window.removeEventListener(
"message",
this.boundMessageListener,
false,
);
}
}