forked from looker-open-source/embed-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.ts
569 lines (530 loc) · 18.8 KB
/
demo.ts
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
/*
MIT License
Copyright (c) 2022 Looker Data Sciences, Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
// IDs for content to demonstrate are configured in demo_config.ts
import type {
LookerEmbedLook,
LookerEmbedDashboard,
LookerEmbedExplore,
SessionStatus,
} from '../src/index'
import { LookerEmbedSDK } from '../src/index'
import type { RuntimeConfig } from './demo_config'
import {
getConfiguration,
updateConfiguration,
loadConfiguration,
resetConfiguration,
} from './demo_config'
let currentDashboard: LookerEmbedDashboard | undefined
let currentLook: LookerEmbedLook | undefined
let currentExplore: LookerEmbedExplore | undefined
const initializeRunAllButton = () => {
// Add a listener to the "Run All" button and send 'xxxx:run' messages when clicked
const runAllButton = document.querySelector('#run-all')
if (runAllButton) {
runAllButton.addEventListener('click', () => {
if (currentDashboard) {
currentDashboard.run()
}
if (currentLook) {
currentLook.run()
}
if (currentExplore) {
currentExplore.run()
}
})
}
}
/**
* Set up the dashboard after the SDK connects
*/
const setupDashboard = (dashboard: LookerEmbedDashboard) => {
currentDashboard = dashboard
// Add a listener to the dashboard's "Run" button and send a 'dashboard:run' message when clicked
const runButton = document.querySelector('#run-dashboard')
if (runButton) {
runButton.addEventListener('click', () => dashboard.run())
}
// Add a listener to the dashboard's "Send session token" button and send a 'session:token' message when clicked
const stopButton = document.querySelector('#stop-dashboard')
if (stopButton) {
stopButton.addEventListener('click', () => dashboard.stop())
}
// Add a listener to the dashboard's "Edit" button and send a 'dashboard:edit' message when clicked
const editButton = document.querySelector('#edit-dashboard')
if (editButton) {
editButton.addEventListener('click', () => dashboard.edit())
}
// Add a listener to the state selector and update the dashboard filters when changed
const stateFilter = document.querySelector('#state')
if (stateFilter) {
stateFilter.addEventListener('change', (event) => {
dashboard.updateFilters({
'State / Region': (event.target as HTMLSelectElement).value,
})
})
}
}
/**
* Set up the look after the SDK connects.
*/
const setupLook = (look: LookerEmbedLook) => {
currentLook = look
// Add a listener to the look's "Run" button and send a 'look:run' message when clicked
const runButton = document.querySelector('#run-look')
if (runButton) {
runButton.addEventListener('click', () => look.run())
}
// Add a listener to the state selector and update the look filters when changed
const stateFilter = document.querySelector('#state')
if (stateFilter) {
stateFilter.addEventListener('change', (event) => {
look.updateFilters({
'users.state': (event.target as HTMLSelectElement).value,
})
})
}
}
/**
* Set up the explore after the SDK connects.
*/
const setupExplore = (explore: LookerEmbedExplore) => {
currentExplore = explore
// Add a listener to the explore's "Run" button and send a 'explore:run' message when clicked
const runButton = document.querySelector('#run-explore')
if (runButton) {
runButton.addEventListener('click', () => explore.run())
}
// Add a listener to the state selector and update the look filters when changed
const stateFilter = document.querySelector('#state')
if (stateFilter) {
stateFilter.addEventListener('change', (event) => {
explore.updateFilters({
'users.state': (event.target as HTMLSelectElement).value,
})
})
}
}
/**
* Update the status for each embedded element
*/
const updateStatus = (selector: string, state: string) => {
const stateElement = document.querySelector(selector)
if (stateElement) {
stateElement.textContent = state
}
}
/**
* A canceller callback can prevent the default behavior of links on a dashboard.
* In this instance, if the click will navigate to a new window, the navigation is
* cancelled.
*/
const preventNavigation = (event: any): any => {
const { preventNavigation } = getConfiguration()
if (preventNavigation) {
updateStatus('#dashboard-state', `${event.label} clicked`)
return { cancel: !event.modal }
}
return {}
}
/**
* Initialize the show dashboard configuration checkbox.
*/
const initializeShowDashboardCheckbox = () => {
const cb = document.getElementById('showDashboard') as HTMLInputElement
if (cb) {
const { dashboardId, showDashboard } = getConfiguration()
if (dashboardId) {
cb.checked = showDashboard
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.showDashboard = event.target.checked
updateConfiguration(runtimeConfig)
renderDashboard(runtimeConfig)
})
} else {
currentDashboard = undefined
cb.parentElement!.style.display = 'none'
}
}
}
/**
* Initialize the show look configuration checkbox.
*/
const initializeShowLookCheckbox = () => {
const cb = document.getElementById('showLook') as HTMLInputElement
if (cb) {
const { lookId, showLook } = getConfiguration()
if (lookId) {
cb.checked = showLook
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.showLook = event.target.checked
updateConfiguration(runtimeConfig)
renderLook(runtimeConfig)
})
} else {
currentLook = undefined
cb.parentElement!.style.display = 'none'
}
}
}
/**
* Initialize the show explore configuration checkbox.
*/
const initializeShowExploreCheckbox = () => {
const cb = document.getElementById('showExplore') as HTMLInputElement
if (cb) {
const { exploreId, showExplore } = getConfiguration()
if (exploreId) {
cb.checked = showExplore
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.showExplore = event.target.checked
updateConfiguration(runtimeConfig)
renderExplore(runtimeConfig)
})
} else {
currentExplore = undefined
cb.parentElement!.style.display = 'none'
}
}
}
/**
* Initialize the show extension configuration checkbox.
*/
const initializeShowExtensionCheckbox = () => {
const cb = document.getElementById('showExtension') as HTMLInputElement
if (cb) {
const { extensionId, showExtension } = getConfiguration()
if (extensionId) {
cb.checked = showExtension
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.showExtension = event.target.checked
updateConfiguration(runtimeConfig)
renderExtension(runtimeConfig)
})
} else {
cb.parentElement!.style.display = 'none'
}
}
}
/**
* Initialize the use cookieless configuration checkbox.
*/
const initializeUseCookielessCheckbox = () => {
const cb = document.getElementById('useCookieless') as HTMLInputElement
if (cb) {
const { useCookieless } = getConfiguration()
cb.checked = useCookieless
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.useCookieless = event.target.checked
updateConfiguration(runtimeConfig)
location.reload()
})
}
}
/**
* Initialize the prevent navigation configuration checkbox.
*/
const initializePreventNavigationCheckbox = () => {
const cb = document.getElementById('preventNavigation') as HTMLInputElement
if (cb) {
const { preventNavigation } = getConfiguration()
cb.checked = preventNavigation
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.preventNavigation = event.target.checked
updateConfiguration(runtimeConfig)
})
}
}
/**
* Initialize the use dynamic heights configuration checkbox.
*/
const initializeUseDynamicHeightsCheckbox = () => {
const cb = document.getElementById('useDynamicHeights') as HTMLInputElement
if (cb) {
const { useDynamicHeights } = getConfiguration()
cb.checked = useDynamicHeights
cb.addEventListener('change', (event: any) => {
const runtimeConfig = getConfiguration()
runtimeConfig.useDynamicHeights = event.target.checked
updateConfiguration(runtimeConfig)
location.reload()
})
}
}
/**
* Initialize the reset configuration button.
*/
const initializeResetConfigButton = () => {
const b = document.getElementById('reset-config') as HTMLInputElement
if (b) {
b.addEventListener('click', () => {
resetConfiguration()
location.reload()
})
}
}
/**
* Initialize controls.
*/
const initializeControls = () => {
initializeRunAllButton()
initializeShowDashboardCheckbox()
initializeShowLookCheckbox()
initializeShowExploreCheckbox()
initializeShowExtensionCheckbox()
initializePreventNavigationCheckbox()
initializeUseCookielessCheckbox()
initializeUseDynamicHeightsCheckbox()
initializeResetConfigButton()
}
/**
* Monitor cookieless embed session status. A simple implementation
* that displays a message.
*/
const processSessionStatus = (event: SessionStatus, selector: string) => {
const { expired, interrupted } = event
if (expired) {
updateStatus(selector, 'Session has expired')
} else if (interrupted) {
updateStatus(selector, 'Session has been interrupted')
}
}
/**
* Render a dashboard using the Embed SDK. When active this sets up listeners
* for events that can be sent by the Looker embedded UI.
*/
const renderDashboard = (runtimeConfig: RuntimeConfig) => {
if (runtimeConfig.showDashboard) {
document.querySelector<HTMLDivElement>('#demo-dashboard')!.style.display =
''
LookerEmbedSDK.createDashboardWithId(runtimeConfig.dashboardId)
// When true scrolls the top of the IFRAME into view
.withDialogScroll(runtimeConfig.useDynamicHeights)
// When true updates the IFRAME height to reflect the height of the
// dashboard
.withDynamicIFrameHeight(runtimeConfig.useDynamicHeights)
// When true monitors the scroll position of the hosting window
// and sends it to the Looker IFRAME. The Looker IFRAME uses the
// information to position dialogs correctly.
.withScrollMonitor(runtimeConfig.useDynamicHeights)
// Allow fullscreen tile visualizations
.withAllowAttr('fullscreen')
// Append to the #dashboard element
.appendTo('#dashboard')
// Listen to messages to display progress
.on('dashboard:loaded', () => updateStatus('#dashboard-state', 'Loaded'))
.on('dashboard:run:start', () =>
updateStatus('#dashboard-state', 'Running')
)
.on('dashboard:run:complete', () =>
updateStatus('#dashboard-state', 'Done')
)
// Listen to messages that change dashboard
.on('dashboard:edit:start', () =>
updateStatus('#dashboard-state', 'Editing')
)
.on('dashboard:edit:cancel', () =>
updateStatus('#dashboard-state', 'Editing cancelled')
)
.on('dashboard:save:complete', () =>
updateStatus('#dashboard-state', 'Saved')
)
.on('dashboard:delete:complete', () =>
updateStatus('#dashboard-state', 'Deleted')
)
.on('session:status', (event: SessionStatus) => {
processSessionStatus(event, '#dashboard-state')
})
// Listen to messages to prevent the user from navigating away
.on('drillmenu:click', preventNavigation)
.on('drillmodal:explore', preventNavigation)
.on('dashboard:tile:explore', preventNavigation)
.on('dashboard:tile:view', preventNavigation)
// Give the embedded content a class for styling purposes
.withClassName('looker-embed')
// Set the initial filters
.withFilters({ 'State / Region': 'California' })
// Finalize the build
.build()
// Connect to Looker
.connect()
// Finish up setup
.then(setupDashboard)
// Log if something went wrong
.catch((error: Error) => {
updateStatus('#dashboard-state', 'Connection error')
console.error('Connection error', error)
})
} else {
document.querySelector<HTMLDivElement>('#dashboard')!.innerHTML = ''
document.querySelector<HTMLDivElement>('#demo-dashboard')!.style.display =
'none'
}
}
/**
* Render a look using the Embed SDK. When active this sets up listeners
* for events that can be sent by the Looker embedded UI.
*/
const renderLook = (runtimeConfig: RuntimeConfig) => {
// Create an embedded Look
if (runtimeConfig.showLook) {
document.querySelector<HTMLDivElement>('#demo-look')!.style.display = ''
LookerEmbedSDK.createLookWithId(runtimeConfig.lookId)
// Append to the #look element
.appendTo('#look')
// Listen to messages to display progress
.on('look:ready', () => updateStatus('#look-state', 'Loaded'))
.on('look:run:start', () => updateStatus('#look-state', 'Running'))
.on('look:run:complete', () => updateStatus('#look-state', 'Done'))
// Listen to messages that change Look
.on('look:save:complete', () => updateStatus('#look-state', 'Saved'))
.on('look:delete:complete', () => updateStatus('#look-state', 'Deleted'))
.on('session:status', (event: SessionStatus) => {
processSessionStatus(event, '#look-state')
})
// Give the embedded content a class for styling purposes
.withClassName('looker-embed')
// Set the initial filters
.withFilters({ 'users.state': 'California' })
// Finalize the build
.build()
// Connect to Looker
.connect()
// Finish up setup
.then(setupLook)
// Log if something went wrong
.catch((error: Error) => {
updateStatus('#look-state', 'Connection error')
console.error('Connection error', error)
})
} else {
document.querySelector<HTMLDivElement>('#look')!.innerHTML = ''
document.querySelector<HTMLDivElement>('#demo-look')!.style.display = 'none'
}
}
/**
* Render an explore using the Embed SDK. When active this sets up listeners
* for events that can be sent by the Looker embedded UI.
*/
const renderExplore = (runtimeConfig: RuntimeConfig) => {
// Create an embedded Explore
if (runtimeConfig.showExplore) {
document.querySelector<HTMLDivElement>('#demo-explore')!.style.display = ''
LookerEmbedSDK.createExploreWithId(runtimeConfig.exploreId)
// Append to the #explore element
.appendTo('#explore')
// Listen to messages to display progress
.on('explore:ready', () => updateStatus('#explore-state', 'Loaded'))
.on('explore:run:start', () => updateStatus('#explore-state', 'Running'))
.on('explore:run:complete', () => updateStatus('#explore-state', 'Done'))
.on('session:status', (event: SessionStatus) => {
processSessionStatus(event, '#explore-state')
})
// Give the embedded content a class for styling purposes
.withClassName('looker-embed')
// Set the initial filters
.withFilters({ 'users.state': 'California' })
// Finalize the build
.build()
// Connect to Looker
.connect()
// Finish up setup
.then(setupExplore)
// Log if something went wrong
.catch((error: Error) => {
updateStatus('#explore-state', 'Connection error')
console.error('Connection error', error)
})
} else {
document.querySelector<HTMLDivElement>('#explore')!.innerHTML = ''
document.querySelector<HTMLDivElement>('#demo-explore')!.style.display =
'none'
}
}
/**
* Render an extension using the Embed SDK. When active this sets up listeners
* for events that can be sent by the Looker embedded UI.
*/
const renderExtension = (runtimeConfig: RuntimeConfig) => {
// Create an embedded extension (Requires Looker 7.12 and extension framework)
if (runtimeConfig.showExtension) {
document.querySelector<HTMLDivElement>('#demo-extension')!.style.display =
''
LookerEmbedSDK.createExtensionWithId(runtimeConfig.extensionId)
// Append to the #extension element
.appendTo('#extension')
.on('session:status', (event: SessionStatus) => {
processSessionStatus(event, '#extension-state')
})
// Give the embedded content a class for styling purposes
.withClassName('looker-embed')
// Finalize the build
.build()
// Connect to Looker
.connect()
// Log if something went wrong
.catch((error: Error) => {
updateStatus('#extension-state', 'Connection error')
console.error('Connection error', error)
})
} else {
document.querySelector<HTMLDivElement>('#extension')!.innerHTML = ''
document.querySelector<HTMLDivElement>('#demo-extension')!.style.display =
'none'
}
}
/**
* Initialize the SDK. lookerHost is the address of the Looker instance. It is configured in
* democonfig.ts. lookerHost needs to be set for messages to be exchanged from the host
* document to the embedded content. The auth endpoint is documented in README.md.
*/
const initializeEmbedSdk = (runtimeConfig: RuntimeConfig) => {
if (runtimeConfig.useCookieless) {
// Use cookieless embed
LookerEmbedSDK.initCookieless(
runtimeConfig.lookerHost,
'/acquire-embed-session',
'/generate-embed-tokens'
)
} else {
// Use SSO embed
LookerEmbedSDK.init(runtimeConfig.lookerHost, '/auth')
}
}
/**
* Event listener to create embedded content. Waits until DOM is loaded so that
* all the parent elements are present.
*/
document.addEventListener('DOMContentLoaded', function () {
loadConfiguration()
initializeControls()
const runtimeConfig = getConfiguration()
initializeEmbedSdk(runtimeConfig)
renderDashboard(runtimeConfig)
renderLook(runtimeConfig)
renderExplore(runtimeConfig)
renderExtension(runtimeConfig)
})