|
| 1 | +import { DashboardObject, TableauEvent } from '@tableau/extensions-api-types'; |
| 2 | +import * as React from 'react'; |
| 3 | +import * as ReactDOM from 'react-dom'; |
| 4 | + |
| 5 | +// Wrap everything in an anonymous function to avoid polluting the global namespace |
| 6 | +(async () => { |
| 7 | + interface IDashboardObjectVisibilityState { |
| 8 | + dashboardObjects: DashboardObject[]; |
| 9 | + visiblityOverrides: Map<number, boolean>; |
| 10 | + } |
| 11 | + |
| 12 | + class DashboardObjectVisibility extends React.Component<{}, IDashboardObjectVisibilityState> { |
| 13 | + public constructor(props = {}) { |
| 14 | + super(props); |
| 15 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 16 | + console.log(dashboard.objects); |
| 17 | + this.state = { dashboardObjects: dashboard.objects, visiblityOverrides: new Map() }; |
| 18 | + dashboard.addEventListener(tableau.TableauEventType.DashboardLayoutChanged, |
| 19 | + (event) => this.onDashboardLayoutChange(event)); |
| 20 | + } |
| 21 | + |
| 22 | + public static async initializeAndRender(): Promise<void> { |
| 23 | + // This is the entry point into the extension. It initializes the Tableau Extensions Api, and then |
| 24 | + // will create button elements to show/hide dashboard objects on the dashboard. |
| 25 | + console.log('Initializing extension API'); |
| 26 | + await tableau.extensions.initializeAsync(); |
| 27 | + |
| 28 | + ReactDOM.render(<DashboardObjectVisibility></DashboardObjectVisibility>, |
| 29 | + document.getElementById('dashboard-object-list')); |
| 30 | + } |
| 31 | + |
| 32 | + public render(): JSX.Element { |
| 33 | + return <> |
| 34 | + {this.state.dashboardObjects.map(dashboardObject => ( |
| 35 | + this.renderListItem(dashboardObject) |
| 36 | + ))} |
| 37 | + </>; |
| 38 | + } |
| 39 | + |
| 40 | + private renderListItem(dashboardObject: DashboardObject): JSX.Element { |
| 41 | + const isVisible = this.getCurrentIsVisible(dashboardObject); |
| 42 | + const buttonText = (isVisible ? 'Hide "' : 'Show "') + dashboardObject.name + '"'; |
| 43 | + |
| 44 | + return <li className='list-group-item list-group-item-primary'> |
| 45 | + <button onClick={() => this.showHideDashboardObject(dashboardObject)}>{buttonText}</button> |
| 46 | + </li>; |
| 47 | + } |
| 48 | + |
| 49 | + private getCurrentIsVisible(dashboardObject: DashboardObject): boolean { |
| 50 | + if (this.state.visiblityOverrides.has(dashboardObject.id)) { |
| 51 | + return this.state.visiblityOverrides.get(dashboardObject.id); |
| 52 | + } else { |
| 53 | + return dashboardObject.isVisible; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private onDashboardLayoutChange(event: TableauEvent): void { |
| 58 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 59 | + this.setState({ dashboardObjects: dashboard.objects, visiblityOverrides: new Map() }); |
| 60 | + } |
| 61 | + |
| 62 | + private showHideDashboardObject(dashboardObject: DashboardObject): void { |
| 63 | + const currentIsVisible = this.getCurrentIsVisible(dashboardObject); |
| 64 | + this.updateStateForVisiblityChange(dashboardObject, currentIsVisible); |
| 65 | + |
| 66 | + const dashboardObjectVisibilityMap = new Map(); |
| 67 | + const newDashboardObjectVisibilityType = currentIsVisible ? |
| 68 | + tableau.DashboardObjectVisibilityType.Hide : tableau.DashboardObjectVisibilityType.Show; |
| 69 | + |
| 70 | + dashboardObjectVisibilityMap.set(dashboardObject.id, newDashboardObjectVisibilityType); |
| 71 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 72 | + dashboard.setDashboardObjectVisibilityAsync(dashboardObjectVisibilityMap).then(() => { |
| 73 | + console.log('Done changing dashboard object visiblity'); |
| 74 | + }); |
| 75 | + } |
| 76 | + |
| 77 | + private updateStateForVisiblityChange(dashboardObject: DashboardObject, currentIsVisible: boolean): void { |
| 78 | + const id = dashboardObject.id; |
| 79 | + const newIsVisible = !currentIsVisible; |
| 80 | + this.setState(prevState => { |
| 81 | + const newVisiblityOverrides = new Map(prevState.visiblityOverrides); |
| 82 | + newVisiblityOverrides.set(id, newIsVisible); |
| 83 | + return { visiblityOverrides: newVisiblityOverrides }; |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + await DashboardObjectVisibility.initializeAndRender(); |
| 89 | +})(); |
0 commit comments