-
Notifications
You must be signed in to change notification settings - Fork 30
/
search-bar.tsx
171 lines (159 loc) · 4.82 KB
/
search-bar.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import { Param, ViewConfig } from '../types';
import { getQueryParamString } from '../utils';
import { TsEmbed } from './ts-embed';
import { SearchOptions } from './search';
/**
* @group Embed components
*/
export interface SearchBarViewConfig
extends Omit<
ViewConfig,
| 'runtimeFilters'
| 'showAlerts'
| 'dataPanelV2'
| 'hiddenHomepageModules'
| 'hiddenHomeLeftNavItems'
| 'hiddenTabs'
| 'visibleTabs'
| 'reorderedHomepageModules'
> {
/**
* The array of data source GUIDs to set on load.
* Only a single data source is supported currently.
*
* @deprecated Use `dataSource` instead
*
* @version: SDK: 1.1.0 | ThoughtSpot: 8.1.1-sw
*
* @example
* ```js
* const embed = new SearchBarEmbed('#tsEmbed', {
* ... // other options
* dataSources:['id-2345','id-2345'],
* })
* ```
*/
dataSources?: string[];
/**
* Pass the ID of the source to be selected.
*
* @version: SDK: 1.19.0, ThoughtSpot 9.0.0.cl, 9.0.1.sw
*
* @example
* ```js
* const embed = new SearchBarEmbed('#tsEmbed', {
* ... // other options
* dataSource:'id-2345',
* })
* ```
*/
dataSource?: string;
/**
* Boolean to define if the last selected data source should be used
*
* @version: SDK: 1.24.0, ThoughtSpot 9.5.0.cl, 9.5.0.sw
*
* @example
* ```js
* const embed = new SearchBarEmbed('#tsEmbed', {
* ... // other options
* useLastSelectedSources:false,
* })
* ```
*/
useLastSelectedSources?: boolean;
/**
* Configuration for search options.
* Includes the following properties:
*
* `searchTokenString`: Search tokens to pass in the query.
*
* `executeSearch`: Boolean to define if the search should be executed or not.
* If it is executed, the focus is placed on the results.
* If it’s not executed, the focus is placed at the end of
* the token string in the search bar.
*
* @version: SDK: 1.2.0 | ThoughtSpot: 9.4.0.sw
*
* @example
* ```js
* const embed = new SearchBarEmbed('#tsEmbed', {
* ... // other options
* searchOptions: {
* searchTokenString: '[quantity purchased] [region]',
* executeSearch: true,
* }
* })
* ```
*/
searchOptions?: SearchOptions;
}
/**
* Embed ThoughtSpot search bar
*
* @version: SDK: 1.18.0 | ThoughtSpot: 8.10.0.cl, 9.0.1-sw
* @group Embed components
*/
export class SearchBarEmbed extends TsEmbed {
/**
* The view configuration for the embedded ThoughtSpot search bar.
*/
protected viewConfig: SearchBarViewConfig;
protected embedComponentType = 'SearchBarEmbed';
constructor(domSelector: string, viewConfig: SearchBarViewConfig) {
super(domSelector);
this.viewConfig = viewConfig;
}
/**
* Construct the URL of the embedded ThoughtSpot search to be
* loaded in the iframe
*
* @param dataSources A list of data source GUIDs
*/
private getIFrameSrc() {
const {
searchOptions,
dataSource,
dataSources,
useLastSelectedSources = false,
} = this.viewConfig;
const path = 'search-bar-embed';
const queryParams = this.getBaseQueryParams();
queryParams[Param.HideActions] = [...(queryParams[Param.HideActions] ?? [])];
if (dataSources && dataSources.length) {
queryParams[Param.DataSources] = JSON.stringify(dataSources);
}
if (dataSource) {
queryParams[Param.DataSources] = `["${dataSource}"]`;
}
if (searchOptions?.searchTokenString) {
queryParams[Param.searchTokenString] = encodeURIComponent(
searchOptions.searchTokenString,
);
if (searchOptions.executeSearch) {
queryParams[Param.executeSearch] = true;
}
}
queryParams[Param.UseLastSelectedDataSource] = useLastSelectedSources;
if (dataSource || dataSources) {
queryParams[Param.UseLastSelectedDataSource] = false;
}
queryParams[Param.searchEmbed] = true;
let query = '';
const queryParamsString = getQueryParamString(queryParams, true);
if (queryParamsString) {
query = `?${queryParamsString}`;
}
const tsPostHashParams = this.getThoughtSpotPostUrlParams();
return `${this.getEmbedBasePath(query)}/embed/${path}${tsPostHashParams}`;
}
/**
* Render the embedded ThoughtSpot search
*/
public async render(): Promise<SearchBarEmbed> {
super.render();
const src = this.getIFrameSrc();
await this.renderIFrame(src);
return this;
}
}