|
| 1 | +import { MarksSelectedEvent, TableauEvent, Worksheet } from '@tableau/extensions-api-types'; |
| 2 | + |
| 3 | +// Wrap everything in an anonymous function to avoid polluting the global namespace |
| 4 | +(async () => { |
| 5 | + class Annotation { |
| 6 | + private worksheets: Worksheet[]; |
| 7 | + private currentWorksheet: Worksheet; |
| 8 | + private self: Annotation; |
| 9 | + |
| 10 | + // Avoid globals. |
| 11 | + constructor(private _$: JQueryStatic) {} |
| 12 | + |
| 13 | + /** |
| 14 | + * Initializes the extension |
| 15 | + */ |
| 16 | + public async initialize() { |
| 17 | + console.log('Waiting for DOM ready'); |
| 18 | + await this._$.ready; |
| 19 | + console.log('Initializing extension API'); |
| 20 | + await tableau.extensions.initializeAsync(); |
| 21 | + |
| 22 | + this.worksheets = tableau.extensions.dashboardContent.dashboard.worksheets; |
| 23 | + if (this.worksheets.length === 0) { |
| 24 | + return; |
| 25 | + } |
| 26 | + // populating selection menu with worksheets |
| 27 | + this.worksheets.forEach((worksheet, index) => { |
| 28 | + const menuOption = this._$('<option>').val(index).text(worksheet.name); |
| 29 | + this._$('#worksheet-selection').append(menuOption); |
| 30 | + }); |
| 31 | + // selecting the first worksheet by default |
| 32 | + this.currentWorksheet = this.worksheets[0]; |
| 33 | + this.currentWorksheet.addEventListener(tableau.TableauEventType.MarkSelectionChanged, this.onMarksSelectedEvent); |
| 34 | + // adding functionality to selection menu |
| 35 | + this._$('#worksheet-selection').on('click', this.onMenuSelection.bind(this)); |
| 36 | + this._$('#worksheet-selection').prop('disabled', false); |
| 37 | + } |
| 38 | + |
| 39 | + // Upon selecting marks, the worksheet will generate annotations replacing the previous ones |
| 40 | + private async onMarksSelectedEvent(event: TableauEvent) { |
| 41 | + const markSelectedEvent = event as MarksSelectedEvent; |
| 42 | + const worksheet = markSelectedEvent.worksheet; |
| 43 | + const marksCollection = await markSelectedEvent.getMarksAsync(); |
| 44 | + // In most cases the marksCollection will have a single data table |
| 45 | + const dataTable = marksCollection.data[0]; |
| 46 | + const marksInfo = dataTable.marksInfo; |
| 47 | + // returning if no marks were selected |
| 48 | + if (marksInfo.length === 0) { |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + // clearing the current annotations |
| 53 | + const annotations = await worksheet.getAnnotationsAsync(); |
| 54 | + for (const annotation of annotations) { |
| 55 | + await worksheet.removeAnnotationAsync(annotation); |
| 56 | + } |
| 57 | + |
| 58 | + // adding annotations for each of the selected marks |
| 59 | + marksInfo.forEach(async (markInfo, rowIndex) => { |
| 60 | + // getting data values corresponding to each markInfo |
| 61 | + const rowData = dataTable.data[rowIndex]; |
| 62 | + // building annotation text |
| 63 | + let annotationText = ''; |
| 64 | + dataTable.columns.forEach((column, colIndex) => { |
| 65 | + annotationText += `${column.fieldName}: ${rowData[colIndex].formattedValue}\n`; |
| 66 | + }); |
| 67 | + await worksheet.annotateMarkAsync(markInfo, annotationText); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + // This function will clear annotations and start listening for marks on the newly selected worksheet. |
| 72 | + private async onMenuSelection() { |
| 73 | + const selectedWorksheet = this.worksheets[this._$('#worksheet-selection option:selected').val() as number]; |
| 74 | + if (this.currentWorksheet === selectedWorksheet) { |
| 75 | + return; |
| 76 | + } |
| 77 | + // deactivating current worksheet and clearing annotations |
| 78 | + this.currentWorksheet.removeEventListener( |
| 79 | + tableau.TableauEventType.MarkSelectionChanged, |
| 80 | + this.onMarksSelectedEvent, |
| 81 | + ); |
| 82 | + const annotations = await this.currentWorksheet.getAnnotationsAsync(); |
| 83 | + for (const annotation of annotations) { |
| 84 | + await this.currentWorksheet.removeAnnotationAsync(annotation); |
| 85 | + } |
| 86 | + // activating selected worksheet |
| 87 | + selectedWorksheet.addEventListener(tableau.TableauEventType.MarkSelectionChanged, this.onMarksSelectedEvent); |
| 88 | + this.currentWorksheet = selectedWorksheet; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + console.log('Initializing Annotation extension.'); |
| 93 | + await new Annotation($).initialize(); |
| 94 | +})(); |
0 commit comments