ashish-sdk
Version:
ThoughtSpot Embed SDK
150 lines • 4.66 kB
JavaScript
/**
* Copyright (c) 2022
*
* Full application embedding
* https://developers.thoughtspot.com/docs/?pageid=full-embed
*
* @summary Full app embed
* @module
* @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
*/
import { getFilterQuery, getQueryParamString } from '../utils';
import { Param } from '../types';
import { V1Embed } from './ts-embed';
/**
* Pages within the ThoughtSpot app that can be embedded.
*/
// eslint-disable-next-line no-shadow
export var Page;
(function (Page) {
/**
* Home page
*/
Page["Home"] = "home";
/**
* Search page
*/
Page["Search"] = "search";
/**
* Saved answers listing page
*/
Page["Answers"] = "answers";
/**
* Liveboards listing page
*/
Page["Liveboards"] = "liveboards";
/**
* @hidden
*/
Page["Pinboards"] = "pinboards";
/**
* Data management page
*/
Page["Data"] = "data";
})(Page || (Page = {}));
/**
* Embeds full ThoughtSpot experience in a host application.
* @Category App Embed
*/
export class AppEmbed extends V1Embed {
// eslint-disable-next-line no-useless-constructor
constructor(domSelector, viewConfig) {
super(domSelector, viewConfig);
}
/**
* Constructs a map of parameters to be passed on to the
* embedded Liveboard or visualization.
*/
getEmbedParams() {
const params = this.getBaseQueryParams();
const { tag, hideObjects } = this.viewConfig;
if (tag) {
params[Param.Tag] = tag;
}
if (hideObjects && hideObjects.length) {
params[Param.HideObjects] = JSON.stringify(hideObjects);
}
const queryParams = getQueryParamString(params, true);
return queryParams;
}
/**
* Constructs the URL of the ThoughtSpot app page to be rendered.
* @param pageId The ID of the page to be embedded.
*/
getIFrameSrc(pageId, runtimeFilters) {
const filterQuery = getFilterQuery(runtimeFilters || []);
const queryParams = this.getEmbedParams();
const queryString = [filterQuery, queryParams]
.filter(Boolean)
.join('&');
const url = `${this.getV1EmbedBasePath(queryString, this.viewConfig.showPrimaryNavbar, this.viewConfig.disableProfileAndHelp, true)}/${pageId}`;
return url;
}
/**
* Gets the ThoughtSpot route of the page for a particular page ID.
* @param pageId The identifier for a page in the ThoughtSpot app.
*/
getPageRoute(pageId) {
switch (pageId) {
case Page.Search:
return 'answer';
case Page.Answers:
return 'answers';
case Page.Liveboards:
return 'pinboards';
case Page.Pinboards:
return 'pinboards';
case Page.Data:
return 'data/tables';
case Page.Home:
default:
return 'home';
}
}
/**
* Formats the path provided by the user.
* @param path The URL path.
* @returns The URL path that the embedded app understands.
*/
formatPath(path) {
if (!path) {
return null;
}
// remove leading slash
if (path.indexOf('/') === 0) {
return path.substring(1);
}
return path;
}
/**
* Navigate to particular page for app embed. eg:answers/pinboards/home
* This is used for embedding answers, pinboards, visualizations and full application only.
* @param path The string, set to iframe src and navigate to new page
* eg: appEmbed.navigateToPage('pinboards')
*/
navigateToPage(path) {
if (this.iFrame) {
const iframeSrc = this.iFrame.src;
const embedPath = '#/embed';
const currentPath = iframeSrc.includes(embedPath) ? embedPath : '#';
this.iFrame.src = `${iframeSrc.split(currentPath)[0]}${currentPath}/${path.replace(/^\/?#?\//, '')}`;
}
else {
console.log('Please call render before invoking this method');
}
}
/**
* Renders the embedded application pages in the ThoughtSpot app.
* @param renderOptions An object containing the page ID
* to be embedded.
*/
render() {
super.render();
const { pageId, runtimeFilters, path } = this.viewConfig;
const pageRoute = this.formatPath(path) || this.getPageRoute(pageId);
const src = this.getIFrameSrc(pageRoute, runtimeFilters);
this.renderV1Embed(src);
return this;
}
}
//# sourceMappingURL=app.js.map