UNPKG

ashish-sdk

Version:
277 lines (276 loc) 8.84 kB
/** * Copyright (c) 2022 * * Base classes * * @summary Base classes * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com> */ import { DOMSelector, HostEvent, EmbedEvent, MessageCallback, Action, RuntimeFilter, EmbedConfig, Plugin } from '../types'; export interface LayoutConfig { } /** * Embedded iFrame configuration */ export interface FrameParams { /** * The width of the iFrame (unit is pixels if numeric). */ width?: number | string; /** * The height of the iFrame (unit is pixels if numeric). */ height?: number | string; } /** * The configuration object for an embedded view. */ export interface ViewConfig { /** * @hidden */ layoutConfig?: LayoutConfig; /** * The <b>width</b> and <b>height</b> dimensions to render an embedded object inside your app. Specify the values in pixels or percentage. */ frameParams?: FrameParams; /** * @hidden */ theme?: string; /** * @hidden */ styleSheet__unstable?: string; /** * The list of actions to disable from the primary menu, more menu * (...), and the contextual menu. */ disabledActions?: Action[]; /** * The tooltip to display for disabled actions. */ disabledActionReason?: string; /** * The list of actions to hide from the primary menu, more menu * (...), and the contextual menu. */ hiddenActions?: Action[]; /** * The list of actions to display from the primary menu, more menu * (...), and the contextual menu. * @version 1.6.0 or later */ visibleActions?: Action[]; /** * The list of runtime filters to apply to a search answer, * visualization, or Liveboard. */ runtimeFilters?: RuntimeFilter[]; /** * This is an object (key/val) of override flags which will be applied * to the internal embedded object. This can be used to add any * URL flag. * @version 1.8.0 */ additionalFlags?: { [key: string]: string | number | boolean; }; /** * Provide a list of plugins, the plugins are executed in the order * provided below, * * @version alpha */ plugins?: Plugin[]; } /** * Base class for embedding v2 experience * Note: the v2 version of ThoughtSpot Blink is built on the new stack: * React+GraphQL */ export declare class TsEmbed { /** * The DOM node where the ThoughtSpot app is to be embedded. */ private el; /** * A reference to the iframe within which the ThoughtSpot app * will be rendered. */ protected iFrame: HTMLIFrameElement; protected viewConfig: ViewConfig; protected embedConfig: EmbedConfig; /** * The ThoughtSpot hostname or IP address */ protected thoughtSpotHost: string; protected thoughtSpotV2Base: string; /** * A map of event handlers for particular message types triggered * by the embedded app; multiple event handlers can be registered * against a particular message type. */ private eventHandlerMap; /** * A flag that is set to true post render. */ private isRendered; /** * A flag to mark if an error has occurred. */ private isError; /** * Should we encode URL Query Params using base64 encoding which thoughtspot * will generate for embedding. This provides additional security to * thoughtspot clusters against Cross site scripting attacks. * @default false */ private shouldEncodeUrlQueryParams; constructor(domSelector: DOMSelector, viewConfig?: ViewConfig); private registerPlugins; /** * Gets a reference to the root DOM node where * the embedded content will appear. * @param domSelector */ private getDOMNode; /** * Throws error encountered during initialization. */ private throwInitError; /** * Handles errors within the SDK * @param error The error message or object */ protected handleError(error: string | Record<string, unknown>): void; /** * Extracts the type field from the event payload * @param event The window message event */ private getEventType; /** * Extracts the port field from the event payload * @param event The window message event * @returns */ private getEventPort; /** * fix for ts7.sep.cl * will be removed for ts7.oct.cl * @hidden */ private formatEventData; /** * Adds a global event listener to window for "message" events. * ThoughtSpot detects if a particular event is targeted to this * embed instance through an identifier contained in the payload, * and executes the registered callbacks accordingly. */ private subscribeToEvents; /** * Constructs the base URL string to load the ThoughtSpot app. */ protected getEmbedBasePath(query: string): string; /** * Common query params set for all the embed modes. * @returns queryParams */ protected getBaseQueryParams(): {}; /** * Constructs the base URL string to load v1 of the ThoughtSpot app. * This is used for embedding Liveboards, visualizations, and full application. * @param queryString The query string to append to the URL. * @param isAppEmbed A Boolean parameter to specify if you are embedding * the full application. */ protected getV1EmbedBasePath(queryString: string, showPrimaryNavbar?: boolean, disableProfileAndHelp?: boolean, isAppEmbed?: boolean): string; /** * Renders the embedded ThoughtSpot app in an iframe and sets up * event listeners. * @param url * @param frameOptions */ protected renderIFrame(url: string, frameOptions: FrameParams): void; /** * Sets the height of the iframe * @param height The height in pixels */ protected setIFrameHeight(height: number): void; /** * Executes all registered event handlers for a particular event type * @param eventType The event type * @param data The payload invoked with the event handler * @param eventPort The event Port for a specific MessageChannel */ protected executeCallbacks(eventType: EmbedEvent, data: any, eventPort?: MessagePort | void): void; /** * Returns the ThoughtSpot hostname or IP address. */ protected getThoughtSpotHost(): string; /** * Gets the v1 event type (if applicable) for the EmbedEvent type * @param eventType The v2 event type * @returns The corresponding v1 event type if one exists * or else the v2 event type itself */ protected getCompatibleEventType(eventType: EmbedEvent): EmbedEvent; /** * Calculates the iframe center for the current visible viewPort * of iframe using Scroll position of Host App, offsetTop for iframe * in Host app. ViewPort height of the tab. * @returns iframe Center in visible viewport, * Iframe height, * View port height. */ protected getIframeCenter(): { iframeCenter: number; iframeScrolled: number; iframeHeight: number; viewPortHeight: number; iframeVisibleViewPort: number; }; /** * Registers an event listener to trigger an alert when the ThoughtSpot app * sends an event of a particular message type to the host application. * * @param messageType The message type * @param callback A callback function */ on(messageType: EmbedEvent, callback: MessageCallback): typeof TsEmbed.prototype; /** * Triggers an event on specific Port registered against * for the EmbedEvent * @param eventType The message type * @param data The payload to send */ private triggerEventOnPort; /** * Triggers an event to the embedded app * @param messageType The event type * @param data The payload to send with the message */ trigger(messageType: HostEvent, data: any): typeof TsEmbed.prototype; /** * Marks the ThoughtSpot object to have been rendered * Needs to be overridden by subclasses to do the actual * rendering of the iframe. * @param args */ render(): TsEmbed; } /** * Base class for embedding v1 experience * Note: The v1 version of ThoughtSpot Blink works on the AngularJS stack * which is currently under migration to v2 */ export declare class V1Embed extends TsEmbed { protected viewConfig: ViewConfig; constructor(domSelector: DOMSelector, viewConfig: ViewConfig); /** * Render the app in an iframe and set up event handlers * @param iframeSrc */ protected renderV1Embed(iframeSrc: string): void; on(messageType: EmbedEvent, callback: MessageCallback): typeof TsEmbed.prototype; }