All files / src/embed pinboard.ts

100% Statements 29/29
100% Branches 18/18
100% Functions 3/3
100% Lines 29/29

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                    3x 3x 3x                                     3x               5x           5x 5x 1x 1x   5x 1x   5x 1x   5x 1x     5x   5x                               5x 5x 5x 1x   5x 5x 3x     5x                         5x   5x 5x   5x      
/**
 * Copyright (c) 2021
 *
 * Embed pinboard or visualization
 * https://docs.thoughtspot.com/5.2/app-integrate/embedding-viz/embed-a-viz.html
 *
 * @summary Pinboard & viz embed
 * @author Ayon Ghosh <ayon.ghosh@thoughtspot.com>
 */
 
import { Action, Param, RuntimeFilter } from '../types';
import { getFilterQuery, getQueryParamString } from '../utils';
import { V1Embed, ViewConfig } from './base';
 
export interface PinboardViewConfig extends ViewConfig {
    fullHeight?: boolean;
    disabledActions?: Action[];
    disabledActionReason?: string;
    hiddenActions?: Action[];
    enableVizTransformations?: boolean;
}
 
export interface PinboardRenderOptions {
    pinboardId: string;
    vizId?: string;
    runtimeFilters?: RuntimeFilter[];
}
 
/**
 * Embed a ThoughtSpot pinboard or visualization
 */
export class PinboardEmbed extends V1Embed {
    protected viewConfig: PinboardViewConfig;
 
    /**
     * Construct a map of params to be passed on to the
     * embedded pinboard or viz
     */
    private getEmbedParams() {
        const params = {};
        const {
            disabledActions,
            disabledActionReason,
            hiddenActions,
            enableVizTransformations,
        } = this.viewConfig;
        if (disabledActions && disabledActions.length) {
            const disabledActionsString = disabledActions.join(',');
            params[Param.DisableActions] = disabledActionsString;
        }
        if (disabledActionReason) {
            params[Param.DisableActionReason] = disabledActionReason;
        }
        if (hiddenActions && hiddenActions.length) {
            params[Param.HideActions] = hiddenActions.join(',');
        }
        if (enableVizTransformations) {
            params[Param.EnableVizTransformations] = true;
        }
 
        const queryParams = getQueryParamString(params);
 
        return queryParams;
    }
 
    /**
     * Construct the URL of the embedded ThoughtSpot pinboard or viz
     * to be loaded within the iframe
     * @param pinboardId The GUID of the pinboard
     * @param vizId The optional GUID of a visualization within the pinboard
     * @param runtimeFilters A list of runtime filters to be applied to
     * the pinboard or viz on load
     */
    private getIFrameSrc(
        pinboardId: string,
        vizId?: string,
        runtimeFilters?: RuntimeFilter[],
    ) {
        const filterQuery = getFilterQuery(runtimeFilters || []);
        let url = `${this.getV1EmbedBasePath(filterQuery)}/viz/${pinboardId}`;
        if (vizId) {
            url = `${url}/${vizId}`;
        }
        const postHashQueryParams = this.getEmbedParams();
        if (postHashQueryParams) {
            url = `${url}?${postHashQueryParams}`;
        }
 
        return url;
    }
 
    /**
     * Render an embedded ThoughtSpot pinboard or viz
     * @param renderOptions An object specifying the pinboard id,
     * viz id and the runtime filters
     */
    public render({
        pinboardId,
        vizId,
        runtimeFilters,
    }: PinboardRenderOptions): PinboardEmbed {
        super.render();
 
        const src = this.getIFrameSrc(pinboardId, vizId, runtimeFilters);
        this.renderV1Embed(src);
 
        return this;
    }
}