Skip to content

Commit

Permalink
feat(embedded): add optional dashboard ui configuration (#19031)
Browse files Browse the repository at this point in the history
* feat: add optional dashboard ui configuration

* change all flags to boolean

* update README and lint
  • Loading branch information
Lily Kuang authored Mar 11, 2022
1 parent 7524e1e commit 124cb0d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions superset-embedded-sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ embedDashboard({
supersetDomain: "https://superset.example.com",
mountPoint: document.getElementById("my-superset-container"), // any html element that can contain an iframe
fetchGuestToken: () => fetchGuestTokenFromBackend(),
dashboardUiConfig: { hideTitle: true }, // dashboard UI config: hideTitle, hideTab, hideChartControls (optional)
});
```

Expand Down
2 changes: 1 addition & 1 deletion superset-embedded-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@superset-ui/embedded-sdk",
"version": "0.1.0-alpha.3",
"version": "0.1.0-alpha.4",
"description": "SDK for embedding resources from Superset into your own application",
"access": "public",
"keywords": [
Expand Down
28 changes: 27 additions & 1 deletion superset-embedded-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import { Switchboard } from '@superset-ui/switchboard';
*/
export type GuestTokenFetchFn = () => Promise<string>;

export type UiConfigType = {
hideTitle?: boolean
hideTab?: boolean
hideChartControls?: boolean
}

export type EmbedDashboardParams = {
/** The id provided by the embed configuration UI in Superset */
id: string
Expand All @@ -38,6 +44,8 @@ export type EmbedDashboardParams = {
mountPoint: HTMLElement
/** A function to fetch a guest token from the Host App's backend server */
fetchGuestToken: GuestTokenFetchFn
/** The dashboard UI config: hideTitle, hideTab, hideChartControls **/
dashboardUiConfig?: UiConfigType
/** Are we in debug mode? */
debug?: boolean
}
Expand All @@ -59,6 +67,7 @@ export async function embedDashboard({
supersetDomain,
mountPoint,
fetchGuestToken,
dashboardUiConfig,
debug = false
}: EmbedDashboardParams): Promise<EmbeddedDashboard> {
function log(...info: unknown[]) {
Expand All @@ -69,9 +78,26 @@ export async function embedDashboard({

log('embedding');

function calculateConfig() {
let configNumber = 0
if(dashboardUiConfig) {
if(dashboardUiConfig.hideTitle) {
configNumber += 1
}
if(dashboardUiConfig.hideTab) {
configNumber += 2
}
if(dashboardUiConfig.hideChartControls) {
configNumber += 8
}
}
return configNumber
}

async function mountIframe(): Promise<Switchboard> {
return new Promise(resolve => {
const iframe = document.createElement('iframe');
const dashboardConfig = dashboardUiConfig ? `?uiConfig=${calculateConfig()}` : ""

// setup the iframe's sandbox configuration
iframe.sandbox.add("allow-same-origin"); // needed for postMessage to work
Expand Down Expand Up @@ -103,7 +129,7 @@ export async function embedDashboard({
resolve(new Switchboard({ port: ourPort, name: 'superset-embedded-sdk', debug }));
});

iframe.src = `${supersetDomain}/dashboard/${id}/embedded`;
iframe.src = `${supersetDomain}/dashboard/${id}/embedded${dashboardConfig}`;
mountPoint.replaceChildren(iframe);
log('placed the iframe')
});
Expand Down

0 comments on commit 124cb0d

Please sign in to comment.