UNPKG

@thoughtspot/visual-embed-sdk

Version:
95 lines 4.15 kB
import { HostEvent } from '../../types'; import { processTrigger as processTriggerService } from '../../utils/processTrigger'; import { getEmbedConfig } from '../embedConfig'; import { UIPassthroughEvent, } from './contracts'; export class HostEventClient { constructor(iFrame) { this.iFrame = iFrame; } /** * A wrapper over process trigger to * @param {HostEvent} message Host event to send * @param {any} data Data to send with the host event * @returns {Promise<any>} - the response from the process trigger */ async processTrigger(message, data) { if (!this.iFrame) { throw new Error('Iframe element is not set'); } const thoughtspotHost = getEmbedConfig().thoughtSpotHost; return processTriggerService(this.iFrame, message, thoughtspotHost, data); } async handleHostEventWithParam(apiName, parameters) { var _a, _b, _c, _d; const response = (_b = (_a = (await this.triggerUIPassthroughApi(apiName, parameters))) === null || _a === void 0 ? void 0 : _a.filter) === null || _b === void 0 ? void 0 : _b.call(_a, (r) => r.error || r.value)[0]; if (!response) { const error = `No answer found${parameters.vizId ? ` for vizId: ${parameters.vizId}` : ''}.`; // eslint-disable-next-line no-throw-literal throw { error }; } const errors = response.error || ((_c = response.value) === null || _c === void 0 ? void 0 : _c.errors) || ((_d = response.value) === null || _d === void 0 ? void 0 : _d.error); if (errors) { // eslint-disable-next-line no-throw-literal throw { error: response.error }; } return { ...response.value }; } async hostEventFallback(hostEvent, data) { return this.processTrigger(hostEvent, data); } /** * Setter for the iframe element used for host events * @param {HTMLIFrameElement} iFrame - the iframe element to set */ setIframeElement(iFrame) { this.iFrame = iFrame; } async triggerUIPassthroughApi(apiName, parameters) { const res = await this.processTrigger(HostEvent.UIPassthrough, { type: apiName, parameters, }); return res; } async handlePinEvent(payload) { var _a, _b; if (!payload || !('newVizName' in payload)) { return this.hostEventFallback(HostEvent.Pin, payload); } const formattedPayload = { ...payload, pinboardId: (_a = payload.liveboardId) !== null && _a !== void 0 ? _a : payload.pinboardId, newPinboardName: (_b = payload.newLiveboardName) !== null && _b !== void 0 ? _b : payload.newPinboardName, }; const data = await this.handleHostEventWithParam(UIPassthroughEvent.PinAnswerToLiveboard, formattedPayload); return { ...data, liveboardId: data.pinboardId, }; } async handleSaveAnswerEvent(payload) { var _a, _b, _c, _d; if (!payload || !('name' in payload) || !('description' in payload)) { // Save is the fallback for SaveAnswer return this.hostEventFallback(HostEvent.Save, payload); } const data = await this.handleHostEventWithParam(UIPassthroughEvent.SaveAnswer, payload); return { ...data, answerId: (_d = (_c = (_b = (_a = data === null || data === void 0 ? void 0 : data.saveResponse) === null || _a === void 0 ? void 0 : _a.data) === null || _b === void 0 ? void 0 : _b.Answer__save) === null || _c === void 0 ? void 0 : _c.answer) === null || _d === void 0 ? void 0 : _d.id, }; } async triggerHostEvent(hostEvent, payload) { switch (hostEvent) { case HostEvent.Pin: return this.handlePinEvent(payload); case HostEvent.SaveAnswer: return this.handleSaveAnswerEvent(payload); default: return this.hostEventFallback(hostEvent, payload); } } } //# sourceMappingURL=host-event-client.js.map