All files / src/embed base.ts

69.57% Statements 48/69
53.85% Branches 21/39
54.17% Functions 13/24
70.59% Lines 48/68

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318                  3x           3x               3x 3x             3x             3x 3x                                         3x                                                                     13x 13x   13x 13x                 13x                                     13x                         7x                           6x 6x 6x 5x     6x                   13x     13x       13x         13x 13x 13x 13x 13x 13x 13x             13x   13x                       13x 13x             7x             12x                                                                                       13x             3x       6x 6x               6x     6x   6x     6x   6x   6x   6x   6x                          
/**
 * Copyright (c) 2021
 *
 * Base classes
 *
 * @summary Base classes
 * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
 */
 
import {
    getThoughtSpotHost,
    URL_MAX_LENGTH,
    DEFAULT_EMBED_WIDTH,
    DEFAULT_EMBED_HEIGHT,
} from '../config';
import {
    DOMSelector,
    EmbedConfig,
    EventType,
    EventTypeV1,
    GenericCallbackFn,
    MessageCallback,
} from '../types';
import { id } from '../utils';
import {
    getCurrentData,
    initialize,
    subscribeToAlerts,
    subscribeToData,
} from '../v1/api';
 
let config = {} as EmbedConfig;
 
/**
 * Initialize the ThoughtSpot embed settings globally
 * @param embedConfig The configuration object containing ThoughtSpot host,
 * authentication mecheanism etc.
 */
export const init = (embedConfig: EmbedConfig): void => {
    config = embedConfig;
};
 
// eslint-disable-next-line @typescript-eslint/no-empty-interface
export interface LayoutConfig {}
export interface FrameParams {
    width?: number;
    height?: number;
}
 
export interface ViewConfig {
    layoutConfig?: LayoutConfig;
    frameParams?: FrameParams;
    theme?: string;
    // eslint-disable-next-line camelcase
    styleSheet__unstable?: string;
}
 
/**
 * Base class for embedding v2 experience
 */
export class TsEmbed {
    /**
     * The identifier for the instance
     */
    private id: string;
 
    /**
     * The DOM node where the ThoughtSpot app is to be embedded
     */
    private el: Element;
 
    /**
     * A reference to the iframe within which the ThoughtSpot app
     * will be rendered
     */
    private iFrame: HTMLIFrameElement;
 
    /**
     * A map of event handlers for particular message types triggered
     * by the embdedded app; multiple event handlers can be registered
     * against a particular message type
     */
    private eventHandlerMap: Map<string, MessageCallback[]>;
 
    /**
     * The ThoughtSpot host name or IP address
     */
    private thoughtSpotHost: string;
 
    /**
     * A flag that is set to true post render
     */
    private isRendered: boolean;
 
    constructor(domSelector: DOMSelector) {
        this.el = this.getDOMNode(domSelector);
        this.id = id();
        // TODO: handle error
        this.thoughtSpotHost = getThoughtSpotHost(config);
        this.eventHandlerMap = new Map();
    }
 
    /**
     * Get a reference to the root DOM node where
     * the embedded content will appear
     * @param domSelector
     */
    private getDOMNode(domSelector: DOMSelector) {
        return typeof domSelector === 'string'
            ? document.querySelector(domSelector)
            : domSelector;
    }
 
    /**
     * Throw error encountered during initialization
     */
    private throwInitError() {
        throw new Error('You need to init the ThoughtSpot SDK module first');
    }
 
    /**
     * Add an global event listener to window for "message" events
     * We detect if a particular event is targeted to this particular
     * embed instance through an identifier contained in the payload,
     * and execute the registere callbacks accordingly
     */
    private subscribeToEvents() {
        window.addEventListener('message', (event) => {
            const eventType = event.data?.type;
            const embedId = event.data?.embedId;
            if (embedId === this.getId()) {
                this.executeCallbacks(eventType, event.data);
            }
        });
    }
 
    /**
     * Construct the base URL string to load the ThoughtSpot app
     */
    protected getEmbedBasePath(): string {
        return `${this.thoughtSpotHost}/v2/#/embed/${this.getId()}`;
    }
 
    /**
     * Construct the base URL string to load v1 of the ThoughtSpot app
     * This is used for pinboards, visualizations and full app embedding
     * @param queryString Query string to append to the URL
     * @param isAppEmbed A Boolean parameter to specify if we're embedding
     * the full app
     */
    protected getV1EmbedBasePath(
        queryString: string,
        isAppEmbed = false,
    ): string {
        const queryStringFrag = queryString ? `&${queryString}` : '';
        let path = `${this.thoughtSpotHost}/?embedApp=true${queryStringFrag}#`;
        if (!isAppEmbed) {
            path = `${path}/embed`;
        }
 
        return path;
    }
 
    /**
     * Renders the embedded ThoughtSpot app in an iframe and sets up
     * event listeners
     * @param url
     * @param frameOptions
     */
    protected renderIFrame(url: string, frameOptions: FrameParams): void {
        Iif (!this.thoughtSpotHost) {
            this.throwInitError();
        }
        Iif (url.length > URL_MAX_LENGTH) {
            // warn: URL too long
        }
 
        this.executeCallbacks(EventType.RenderInit, {
            data: {
                timestamp: Date.now(),
            },
        });
        this.iFrame = document.createElement('iframe');
        this.iFrame.src = url;
        this.iFrame.width = `${frameOptions?.width || DEFAULT_EMBED_WIDTH}`;
        this.iFrame.height = `${frameOptions?.height || DEFAULT_EMBED_HEIGHT}`;
        this.iFrame.style.border = '0';
        this.iFrame.name = 'ThoughtSpot Embedded Analytics';
        this.iFrame.addEventListener('load', () =>
            this.executeCallbacks(EventType.Load, {
                data: {
                    timestamp: Date.now(),
                },
            }),
        );
        this.el.appendChild(this.iFrame);
 
        this.subscribeToEvents();
    }
 
    /**
     * Execute all registered event handlers for a particular event type
     * @param eventType The event type
     * @param data The payload the event handler will be invoked with
     */
    protected executeCallbacks(
        eventType: EventType | EventTypeV1,
        data: any,
    ): void {
        const callbacks = this.eventHandlerMap.get(eventType) || [];
        callbacks.forEach((callback) => callback(data));
    }
 
    /**
     * Return the identifier for this instance
     */
    public getId(): string {
        return this.id;
    }
 
    /**
     * Return the ThoughtSpot host name or IP address
     */
    public getThoughtSpotHost(): string {
        return this.thoughtSpotHost;
    }
 
    /**
     * Register an event listener to be triggered when we receive
     * an event of a particular message type from the embedded app
     * @param messageType The message type
     * @param callback A callback function
     */
    public on(
        messageType: string,
        callback: MessageCallback,
    ): typeof TsEmbed.prototype {
        if (this.isRendered) {
            throw new Error(
                'Please register event handlers before calling render',
            );
        }
 
        const callbacks = this.eventHandlerMap.get(messageType) || [];
        callbacks.push(callback);
        this.eventHandlerMap.set(messageType, callbacks);
 
        return this;
    }
 
    /**
     * Trigger a message event to the embedded app
     * @param messageType The message type
     * @param data The payload to send with the message
     */
    public trigger(messageType: string, data: any): typeof TsEmbed.prototype {
        this.iFrame.contentWindow.postMessage(
            {
                type: messageType,
                data,
            },
            this.thoughtSpotHost,
        );
 
        return this;
    }
 
    public render(...args: any[]): void {
        this.isRendered = true;
    }
}
 
/**
 * Base class for embedding legacy v1 experience
 */
export class V1Embed extends TsEmbed {
    protected viewConfig: ViewConfig;
 
    constructor(domSelector: DOMSelector, viewConfig: ViewConfig) {
        super(domSelector);
        this.viewConfig = viewConfig;
    }
 
    /**
     * Render the app in an iframe and set up event handlers
     * @param iframeSrc
     */
    protected renderV1Embed(iframeSrc: string): void {
        this.renderIFrame(iframeSrc, this.viewConfig.frameParams);
 
        // Set up event handlers using v1 API
        const onInit = (isInitialized: boolean) =>
            this.executeCallbacks(EventType.Init, isInitialized);
        const onAuthExpire = () =>
            this.executeCallbacks(EventTypeV1.AuthExpire, null);
 
        initialize(onInit, onAuthExpire, this.getThoughtSpotHost());
 
        const onAlert = (data: any) =>
            this.executeCallbacks(EventTypeV1.Alert, data);
        subscribeToAlerts(this.getThoughtSpotHost(), onAlert);
 
        const onData = (data: any) =>
            this.executeCallbacks(EventTypeV1.Data, data);
        subscribeToData(onData);
    }
 
    /**
     * Fetch the current pinboard or answer data from the
     * embedded app asynchronously
     * @param callback A function to be executed with the
     * fetched as an argument
     */
    public getCurrentData(callback: GenericCallbackFn): void {
        getCurrentData(callback);
    }
}