ashish-sdk
Version:
ThoughtSpot Embed SDK
97 lines • 3.45 kB
JavaScript
/**
* Copyright (c) 2022
*
* Embed ThoughtSpot search or a saved answer
*
* @summary Search embed
* @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
*/
import { DataSourceVisualMode, Param, Action } from '../types';
import { getQueryParamString } from '../utils';
import { TsEmbed } from './ts-embed';
export const HiddenActionItemByDefaultForSearchEmbed = [
Action.EditACopy,
Action.SaveAsView,
Action.UpdateTML,
Action.EditTML,
Action.AnswerDelete,
];
/**
* Embed ThoughtSpot search
*
* @Category Search Embed
*/
export class SearchEmbed extends TsEmbed {
constructor(domSelector, viewConfig) {
super(domSelector);
this.viewConfig = viewConfig;
}
/**
* Get the state of the data sources panel that the embedded
* ThoughtSpot search will be initialized with.
*/
getDataSourceMode() {
let dataSourceMode = DataSourceVisualMode.Expanded;
if (this.viewConfig.collapseDataSources === true) {
dataSourceMode = DataSourceVisualMode.Collapsed;
}
if (this.viewConfig.hideDataSources === true) {
dataSourceMode = DataSourceVisualMode.Hidden;
}
return dataSourceMode;
}
/**
* Construct the URL of the embedded ThoughtSpot search to be
* loaded in the iframe
* @param answerId The GUID of a saved answer
* @param dataSources A list of data source GUIDs
*/
getIFrameSrc(answerId, dataSources) {
var _a;
const { hideResults, enableSearchAssist, forceTable, searchOptions, } = this.viewConfig;
const answerPath = answerId ? `saved-answer/${answerId}` : 'answer';
const queryParams = this.getBaseQueryParams();
queryParams[Param.HideActions] = [
...((_a = queryParams[Param.HideActions]) !== null && _a !== void 0 ? _a : []),
...HiddenActionItemByDefaultForSearchEmbed,
];
if (dataSources && dataSources.length) {
queryParams[Param.DataSources] = JSON.stringify(dataSources);
}
if (searchOptions === null || searchOptions === void 0 ? void 0 : searchOptions.searchTokenString) {
queryParams[Param.searchTokenString] = encodeURIComponent(searchOptions.searchTokenString);
if (searchOptions.executeSearch) {
queryParams[Param.executeSearch] = true;
}
}
if (enableSearchAssist) {
queryParams[Param.EnableSearchAssist] = true;
}
if (hideResults) {
queryParams[Param.HideResult] = true;
}
if (forceTable) {
queryParams[Param.ForceTable] = true;
}
queryParams[Param.DataSourceMode] = this.getDataSourceMode();
queryParams[Param.UseLastSelectedDataSource] = false;
queryParams[Param.searchEmbed] = true;
let query = '';
const queryParamsString = getQueryParamString(queryParams, true);
if (queryParamsString) {
query = `?${queryParamsString}`;
}
return `${this.getEmbedBasePath(query)}/${answerPath}`;
}
/**
* Render the embedded ThoughtSpot search
*/
render() {
super.render();
const { answerId, dataSources } = this.viewConfig;
const src = this.getIFrameSrc(answerId, dataSources);
this.renderIFrame(src, this.viewConfig.frameParams);
return this;
}
}
//# sourceMappingURL=search.js.map