|
| 1 | +import { DashboardLayoutChange, DashboardLayoutChangedEvent, DashboardObject, TableauEvent } from '@tableau/extensions-api-types'; |
| 2 | + |
| 3 | +// Wrap everything in an anonymous function to avoid polluting the global namespace |
| 4 | +(async () => { |
| 5 | + class DashboardLayout { |
| 6 | + public dashboardObjects: DashboardObject[]; |
| 7 | + private self: DashboardLayout; |
| 8 | + |
| 9 | + // Avoid globals. |
| 10 | + constructor(private _$: JQueryStatic) {} |
| 11 | + |
| 12 | + /** |
| 13 | + * Initializes the extension |
| 14 | + */ |
| 15 | + public async initialize() { |
| 16 | + console.log('Waiting for DOM ready'); |
| 17 | + await this._$.ready; |
| 18 | + console.log('Initializing extension API'); |
| 19 | + await tableau.extensions.initializeAsync(); |
| 20 | + |
| 21 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 22 | + this.dashboardObjects = dashboard.objects; |
| 23 | + console.log(this.dashboardObjects); |
| 24 | + |
| 25 | + // enabling dashboard event button |
| 26 | + this._$('#dashboard-event-btn').prop('disabled', false); |
| 27 | + this._$('#dashboard-event-btn').click(this.onEventButtonClick.bind(this)); |
| 28 | + } |
| 29 | + |
| 30 | + // When changes are made to the dashboard we get all the details for each of the |
| 31 | + // dashboard objects that were changed and compare it with their previous values. |
| 32 | + // The dashboardLayoutChangeDetails property is a map of dashboard obj3ct ids to |
| 33 | + // an array of dashboard layout changes. |
| 34 | + // Dashboard layout change events are invoked when dashboard objects are resized, |
| 35 | + // repositioned, added, and more. See DashboardLayoutChange in the API documentation |
| 36 | + // for all possible actions. |
| 37 | + // Extension reloads when worksheets are added / removed. |
| 38 | + private onDashboardLayoutChange(event: TableauEvent) { |
| 39 | + console.log(event); |
| 40 | + const dashboardEvent = event as DashboardLayoutChangedEvent; |
| 41 | + const dashboardEventDetails = dashboardEvent.dashboardLayoutChangeDetails; |
| 42 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 43 | + |
| 44 | + // updating dashboard objects and storing the previous dashboard objects for referrence. |
| 45 | + const oldDashboardObjects = this.dashboardObjects; |
| 46 | + this.dashboardObjects = dashboard.objects; |
| 47 | + |
| 48 | + // An empty dashboard layout change event may be invoked when loading an extension from the manifest. |
| 49 | + // In this case we ignore it and return. |
| 50 | + if (dashboardEventDetails === undefined || dashboardEventDetails.size === 0) { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + // Emptying previous content from the UI's change list. |
| 55 | + this._$('#dashboard-layout-change-list').empty(); |
| 56 | + |
| 57 | + // Updating UI's change list to display information on the current dashboard event. |
| 58 | + dashboardEventDetails.forEach((changesMade: DashboardLayoutChange[], dashboardObjectId: number) => { |
| 59 | + // getting dashboard object from its id |
| 60 | + const dashboardObject = dashboard.getDashboardObjectById(dashboardObjectId); |
| 61 | + |
| 62 | + // building a div for the changes made to this dashboard object. |
| 63 | + const changesDiv = this._$('<div>'); |
| 64 | + |
| 65 | + // checking if this dashboard object was added as part of the event. |
| 66 | + if (changesMade.includes(tableau.DashboardLayoutChange.Added)) { |
| 67 | + const toAppend = this._$('<h6/>'); |
| 68 | + toAppend.text(`Dashboard Object ${dashboardObjectId} added: "${dashboardObject.name}"`); |
| 69 | + changesDiv.append(toAppend); |
| 70 | + this._$('#dashboard-layout-change-list').append(changesDiv); |
| 71 | + return; |
| 72 | + } |
| 73 | + |
| 74 | + // getting old dashboard object before event to compare it with the current one. |
| 75 | + const oldDashboardObject = oldDashboardObjects.find(o => o.id === dashboardObjectId); |
| 76 | + |
| 77 | + // checking if this dashboard object was removed as part of the event. |
| 78 | + if (changesMade.includes(tableau.DashboardLayoutChange.Removed)) { |
| 79 | + const toAppend = this._$('<h6/>'); |
| 80 | + toAppend.text(`Dashboard Object ${dashboardObjectId} removed: "${oldDashboardObject.name}"`); |
| 81 | + changesDiv.append(toAppend); |
| 82 | + this._$('#dashboard-layout-change-list').append(changesDiv); |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + // the following dashboard changes are not mutually exclusive, so we list them together. |
| 87 | + const h6 = this._$('<h6/>'); |
| 88 | + h6.text(`Dashboard Object ${dashboardObjectId}: "${dashboardObject.name}"`); |
| 89 | + changesDiv.append(h6); |
| 90 | + const ul = this._$('<ul/>'); |
| 91 | + |
| 92 | + // checking if the dashboard object had changes to its floating state. |
| 93 | + if (changesMade.includes(tableau.DashboardLayoutChange.IsFloatingChanged)) { |
| 94 | + const li = this._$('<li/>'); |
| 95 | + li.text(`Floating is now ${dashboardObject.isFloating}, was ${oldDashboardObject.isFloating}`); |
| 96 | + ul.append(li); |
| 97 | + } |
| 98 | + |
| 99 | + // checking if the dashbaord object had changes to its visibility. |
| 100 | + if (changesMade.includes(tableau.DashboardLayoutChange.IsVisibleChanged)) { |
| 101 | + const li = this._$('<li/>'); |
| 102 | + li.text(`Visibility is now ${dashboardObject.isVisible}, was ${oldDashboardObject.isVisible}`); |
| 103 | + ul.append(li); |
| 104 | + } |
| 105 | + |
| 106 | + // checking if the dashboard object was repositioned. |
| 107 | + if (changesMade.includes(tableau.DashboardLayoutChange.PositionChanged)) { |
| 108 | + const li = this._$('<li/>'); |
| 109 | + const newPos = dashboardObject.position; |
| 110 | + const oldPos = oldDashboardObject.position; |
| 111 | + li.text(`Position is now (${newPos.x},${newPos.y}), was (${oldPos.x},${oldPos.y})`); |
| 112 | + ul.append(li); |
| 113 | + } |
| 114 | + |
| 115 | + // checking if the dashboard object was resized. |
| 116 | + if (changesMade.includes(tableau.DashboardLayoutChange.SizeChanged)) { |
| 117 | + const li = this._$('<li/>'); |
| 118 | + const newSize = dashboardObject.size; |
| 119 | + const oldSize = oldDashboardObject.size; |
| 120 | + li.text(`Size is now ${newSize.width}x${newSize.height}, was ${oldSize.width}x${oldSize.height}`); |
| 121 | + ul.append(li); |
| 122 | + } |
| 123 | + |
| 124 | + // checking if the dashboard object was renamed. |
| 125 | + if (changesMade.includes(tableau.DashboardLayoutChange.NameChanged)) { |
| 126 | + const li = this._$('<li/>'); |
| 127 | + li.text(`Name is now "${dashboardObject.name}", was "${oldDashboardObject.name}"`); |
| 128 | + ul.append(li); |
| 129 | + } |
| 130 | + |
| 131 | + changesDiv.append(ul); |
| 132 | + this._$('#dashboard-layout-change-list').append(changesDiv); |
| 133 | + }); |
| 134 | + } |
| 135 | + |
| 136 | + // This function adds a dashboard event if there is not one already, and removes it if there is. |
| 137 | + private onEventButtonClick() { |
| 138 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 139 | + if ($('#dashboard-event-btn').text() === 'Add Dashboard Event') { |
| 140 | + dashboard.addEventListener(tableau.TableauEventType.DashboardLayoutChanged, |
| 141 | + (event) => this.onDashboardLayoutChange(event)); |
| 142 | + $('#dashboard-event-btn').text('Remove Dashboard Event'); |
| 143 | + } else { |
| 144 | + dashboard.removeEventListener(tableau.TableauEventType.DashboardLayoutChanged, |
| 145 | + (event) => this.onDashboardLayoutChange(event)); |
| 146 | + $('#dashboard-layout-change-list').empty(); |
| 147 | + $('#dashboard-event-btn').text('Add Dashboard Event'); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + console.log('Initializing DashboardLayout extension.'); |
| 153 | + await new DashboardLayout($).initialize(); |
| 154 | +})(); |
0 commit comments