ashish-sdk
Version:
ThoughtSpot Embed SDK
115 lines • 4.5 kB
JavaScript
/**
* Copyright (c) 2022
*
* Embed a ThoughtSpot Liveboard or visualization
* https://developers.thoughtspot.com/docs/?pageid=embed-pinboard
* https://developers.thoughtspot.com/docs/?pageid=embed-a-viz
*
* @summary Liveboard & visualization embed
* @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
*/
import { ERROR_MESSAGE } from '../errors';
import { EmbedEvent, Param, } from '../types';
import { getFilterQuery, getQueryParamString } from '../utils';
import { V1Embed } from './ts-embed';
/**
* Embed a ThoughtSpot Liveboard or visualization
* @Category Liveboards and Charts
*/
export class LiveboardEmbed extends V1Embed {
// eslint-disable-next-line no-useless-constructor
constructor(domSelector, viewConfig) {
super(domSelector, viewConfig);
this.defaultHeight = 500;
/**
* Set the iframe height as per the computed height received
* from the ThoughtSpot app.
* @param data The event payload
*/
this.updateIFrameHeight = (data) => {
this.setIFrameHeight(Math.max(data.data, this.defaultHeight));
};
this.embedIframeCenter = (data, responder) => {
const obj = this.getIframeCenter();
responder({ type: EmbedEvent.EmbedIframeCenter, data: obj });
};
this.setIframeHeightForNonEmbedLiveboard = (data) => {
if (!data.data.currentPath.startsWith('/embed/viz/')) {
this.setIFrameHeight(this.defaultHeight);
}
};
}
/**
* Construct a map of params to be passed on to the
* embedded Liveboard or visualization.
*/
getEmbedParams() {
const params = this.getBaseQueryParams();
const { enableVizTransformations, fullHeight, defaultHeight, } = this.viewConfig;
const preventLiveboardFilterRemoval = this.viewConfig.preventLiveboardFilterRemoval ||
this.viewConfig.preventPinboardFilterRemoval;
if (fullHeight === true) {
params[Param.fullHeight] = true;
}
if (defaultHeight) {
this.defaultHeight = defaultHeight;
}
if (enableVizTransformations !== undefined) {
params[Param.EnableVizTransformations] = enableVizTransformations.toString();
}
if (preventLiveboardFilterRemoval) {
params[Param.preventLiveboardFilterRemoval] = true;
}
params[Param.livedBoardEmbed] = true;
const queryParams = getQueryParamString(params, true);
return queryParams;
}
/**
* Construct the URL of the embedded ThoughtSpot Liveboard or visualization
* to be loaded within the iframe.
* @param liveboardId The GUID of the Liveboard.
* @param vizId The optional GUID of a visualization within the Liveboard.
* @param runtimeFilters A list of runtime filters to be applied to
* the Liveboard or visualization on load.
*/
getIFrameSrc(liveboardId, vizId, runtimeFilters) {
const filterQuery = getFilterQuery(runtimeFilters || []);
const queryParams = this.getEmbedParams();
const queryString = [filterQuery, queryParams]
.filter(Boolean)
.join('&');
let url = `${this.getV1EmbedBasePath(queryString, true, false, false)}/viz/${liveboardId}`;
if (vizId) {
url = `${url}/${vizId}`;
}
return url;
}
/**
* Render an embedded ThoughtSpot Liveboard or visualization
* @param renderOptions An object specifying the Liveboard ID,
* visualization ID and the runtime filters.
*/
render() {
var _a;
const { vizId, runtimeFilters } = this.viewConfig;
const liveboardId = (_a = this.viewConfig.liveboardId) !== null && _a !== void 0 ? _a : this.viewConfig.pinboardId;
if (!liveboardId) {
this.handleError(ERROR_MESSAGE.LIVEBOARD_VIZ_ID_VALIDATION);
}
if (this.viewConfig.fullHeight === true) {
this.on(EmbedEvent.RouteChange, this.setIframeHeightForNonEmbedLiveboard);
this.on(EmbedEvent.EmbedHeight, this.updateIFrameHeight);
this.on(EmbedEvent.EmbedIframeCenter, this.embedIframeCenter);
}
super.render();
const src = this.getIFrameSrc(liveboardId, vizId, runtimeFilters);
this.renderV1Embed(src);
return this;
}
}
/**
* @hidden
*/
export class PinboardEmbed extends LiveboardEmbed {
}
//# sourceMappingURL=liveboard.js.map