|
| 1 | +/* |
| 2 | +
|
| 3 | + MIT License |
| 4 | +
|
| 5 | + Copyright (c) 2025 Looker Data Sciences, Inc. |
| 6 | +
|
| 7 | + Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | + of this software and associated documentation files (the "Software"), to deal |
| 9 | + in the Software without restriction, including without limitation the rights |
| 10 | + to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | + copies of the Software, and to permit persons to whom the Software is |
| 12 | + furnished to do so, subject to the following conditions: |
| 13 | +
|
| 14 | + The above copyright notice and this permission notice shall be included in all |
| 15 | + copies or substantial portions of the Software. |
| 16 | +
|
| 17 | + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 20 | + AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | + OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | + SOFTWARE. |
| 24 | +
|
| 25 | + */ |
| 26 | + |
| 27 | +import type { ILookerConnection, ILookerEmbedSDK } from '../src/index' |
| 28 | +import { getEmbedSDK } from '../src/index' |
| 29 | +import type { RuntimeConfig } from './demo_config' |
| 30 | +import { getConfiguration, loadConfiguration } from './demo_config' |
| 31 | + |
| 32 | +let embedConnection: ILookerConnection |
| 33 | + |
| 34 | +/** |
| 35 | + * Save the embed connection. This provides access to the undelying |
| 36 | + * functionality of each embed content type. |
| 37 | + */ |
| 38 | +const embedConnected = (connection: ILookerConnection) => { |
| 39 | + embedConnection = connection |
| 40 | + updateStatus('') |
| 41 | +} |
| 42 | + |
| 43 | +/** |
| 44 | + * Update the status for each embedded element |
| 45 | + */ |
| 46 | +const updateStatus = (status: string) => { |
| 47 | + const statusElement = document.querySelector('#embed-status') |
| 48 | + if (statusElement) { |
| 49 | + if (status) { |
| 50 | + statusElement.textContent = status |
| 51 | + } else { |
| 52 | + statusElement.innerHTML = ' ' |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +/** |
| 58 | + * Initialize controls. |
| 59 | + */ |
| 60 | +const initializeControls = () => { |
| 61 | + const runButton = document.querySelector('#close-merge') |
| 62 | + if (runButton) { |
| 63 | + runButton.addEventListener('click', () => { |
| 64 | + window.close() |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * A canceller callback that prevents the default behavior of edit merge query. |
| 71 | + * The default behavior is for the edit merge query page to be opened in a top |
| 72 | + * level window. |
| 73 | + */ |
| 74 | +const openMergeQuery = (event: any): any => { |
| 75 | + window.open(`/merge_edit?merge_url=${encodeURI(event.url)}`) |
| 76 | + updateStatus('Merge query edit opened in a new window') |
| 77 | + return { cancel: true } |
| 78 | +} |
| 79 | + |
| 80 | +/** |
| 81 | + * The merge edit url is stored in history state |
| 82 | + */ |
| 83 | +const getEmbedMergeEditUrl = () => { |
| 84 | + const embedUrl = decodeURI(history.state?.merge_url || '') |
| 85 | + return embedUrl || '/embed/preload' |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Render the embedded merge edit page. |
| 90 | + */ |
| 91 | +const createEmbed = (sdk: ILookerEmbedSDK) => { |
| 92 | + const abortController = new AbortController() |
| 93 | + const signal = abortController.signal |
| 94 | + let timeoutId: any = setTimeout(() => { |
| 95 | + abortController.abort( |
| 96 | + `Connection attempt timed out. Please check that ${location.origin} has been allow listed` |
| 97 | + ) |
| 98 | + timeoutId = undefined |
| 99 | + }, 60000) |
| 100 | + sdk |
| 101 | + .createWithUrl(getEmbedMergeEditUrl()) |
| 102 | + .appendTo('#embed-container') |
| 103 | + // // Open merge query in its own window |
| 104 | + .on('dashboard:tile:merge', openMergeQuery) |
| 105 | + .on('session:expired', () => updateStatus('Session Expired')) |
| 106 | + .withClassName('looker-embed') |
| 107 | + .build() |
| 108 | + // Connect to Looker |
| 109 | + .connect({ signal, waitUntilLoaded: true }) |
| 110 | + // Finish up setup |
| 111 | + .then((connection) => { |
| 112 | + if (timeoutId) { |
| 113 | + clearTimeout(timeoutId) |
| 114 | + timeoutId = undefined |
| 115 | + } |
| 116 | + embedConnected(connection) |
| 117 | + }) |
| 118 | + // Log if something went wrong |
| 119 | + .catch((error: any) => { |
| 120 | + updateStatus(typeof error === 'string' ? error : 'Connection error') |
| 121 | + console.error('Connection error', error) |
| 122 | + }) |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Initialize the SDK. lookerHost is the address of the Looker instance. It is configured in |
| 127 | + * democonfig.ts. lookerHost needs to be set for messages to be exchanged from the host |
| 128 | + * document to the embedded content. The auth endpoint is documented in README.md. |
| 129 | + */ |
| 130 | +const initializeEmbedSdk = (runtimeConfig: RuntimeConfig) => { |
| 131 | + const sdk: ILookerEmbedSDK = getEmbedSDK() |
| 132 | + if (runtimeConfig.embedType === 'cookieless') { |
| 133 | + // Use cookieless embed |
| 134 | + sdk.initCookieless( |
| 135 | + runtimeConfig.lookerHost, |
| 136 | + `${runtimeConfig.proxyPath}/acquire-embed-session`, |
| 137 | + `${runtimeConfig.proxyPath}/generate-embed-tokens` |
| 138 | + ) |
| 139 | + } else if (runtimeConfig.embedType === 'private') { |
| 140 | + // Use private embedding |
| 141 | + sdk.init(runtimeConfig.lookerHost) |
| 142 | + } else { |
| 143 | + // Use SSO embed |
| 144 | + sdk.init(runtimeConfig.lookerHost, `${runtimeConfig.proxyPath}/auth`) |
| 145 | + } |
| 146 | + // Now preload the embed |
| 147 | + createEmbed(sdk) |
| 148 | +} |
| 149 | + |
| 150 | +/** |
| 151 | + * Event listener to create embedded content. Waits until DOM is loaded so that |
| 152 | + * all the parent elements are present. |
| 153 | + */ |
| 154 | +document.addEventListener('DOMContentLoaded', function () { |
| 155 | + // Hide merge url from end user |
| 156 | + const searchParams = new URLSearchParams(location.search) |
| 157 | + if (searchParams.has('merge_url')) { |
| 158 | + history.replaceState( |
| 159 | + { merge_url: searchParams.get('merge_url') }, |
| 160 | + '', |
| 161 | + location.pathname |
| 162 | + ) |
| 163 | + } |
| 164 | + loadConfiguration() |
| 165 | + initializeControls() |
| 166 | + const runtimeConfig = getConfiguration() |
| 167 | + initializeEmbedSdk(runtimeConfig) |
| 168 | +}) |
0 commit comments