|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Wrap everything in an anonymous function to avoid polluting the global namespace |
| 4 | +(function () { |
| 5 | + $(document).ready(function () { |
| 6 | + tableau.extensions.initializeAsync().then(function () { |
| 7 | + // Since dataSource info is attached to the worksheet, we will perform |
| 8 | + // one async call per worksheet to get every dataSource used in this |
| 9 | + // dashboard. This demonstrates the use of Promise.all to combine |
| 10 | + // promises together and wait for each of them to resolve. |
| 11 | + let dataSourceFetchPromises = []; |
| 12 | + |
| 13 | + // Maps dataSource id to dataSource so we can keep track of unique dataSources. |
| 14 | + let dashboardDataSources = {}; |
| 15 | + |
| 16 | + // To get dataSource info, first get the dashboard. |
| 17 | + const dashboard = tableau.extensions.dashboardContent.dashboard; |
| 18 | + |
| 19 | + // Then loop through each worksheet and get its dataSources, save promise for later. |
| 20 | + dashboard.worksheets.forEach(function (worksheet) { |
| 21 | + dataSourceFetchPromises.push(worksheet.getDataSourcesAsync()); |
| 22 | + }); |
| 23 | + |
| 24 | + Promise.all(dataSourceFetchPromises).then(function (fetchResults) { |
| 25 | + fetchResults.forEach(function (dataSourcesForWorksheet) { |
| 26 | + dataSourcesForWorksheet.forEach(function (dataSource) { |
| 27 | + if (!dashboardDataSources[dataSource.id]) { // We've already seen it, skip it. |
| 28 | + dashboardDataSources[dataSource.id] = dataSource; |
| 29 | + } |
| 30 | + }); |
| 31 | + }); |
| 32 | + |
| 33 | + buildDataSourcesTable(dashboardDataSources); |
| 34 | + |
| 35 | + // This just modifies the UI by removing the loading banner and showing the dataSources table. |
| 36 | + $('#loading').addClass('hidden'); |
| 37 | + $('#dataSourcesTable').removeClass('hidden').addClass('show'); |
| 38 | + }); |
| 39 | + }, function (err) { |
| 40 | + // Something went wrong in initialization. |
| 41 | + console.log('Error while Initializing: ' + err.toString()); |
| 42 | + }); |
| 43 | + }); |
| 44 | + |
| 45 | + // Refreshes the given dataSource. |
| 46 | + function refreshDataSource (dataSource) { |
| 47 | + dataSource.refreshAsync().then(function () { |
| 48 | + console.log(dataSource.name + ': Refreshed Successfully'); |
| 49 | + }); |
| 50 | + } |
| 51 | + |
| 52 | + // Displays a modal dialog with more details about the given dataSource. |
| 53 | + function showModal (dataSource) { |
| 54 | + var modal = $('#infoModal'); |
| 55 | + |
| 56 | + $('#nameDetail').text(dataSource.name); |
| 57 | + $('#idDetail').text(dataSource.id); |
| 58 | + $('#typeDetail').text((dataSource.isExtract) ? 'Extract' : 'Live'); |
| 59 | + |
| 60 | + // Loop through every field in the dataSource and concat it to a string. |
| 61 | + var fieldNamesStr = ''; |
| 62 | + dataSource.fields.forEach(function (field) { |
| 63 | + fieldNamesStr += field.name + ', '; |
| 64 | + }); |
| 65 | + |
| 66 | + // Slice off the last ", " for formatting. |
| 67 | + $('#fieldsDetail').text(fieldNamesStr.slice(0, -2)); |
| 68 | + |
| 69 | + dataSource.getConnectionSummariesAsync().then(function (connectionSummaries) { |
| 70 | + // Loop through each connection summary and list the connection's |
| 71 | + // name and type in the info field |
| 72 | + var connectionsStr = ''; |
| 73 | + connectionSummaries.forEach(function (summary) { |
| 74 | + connectionsStr += summary.name + ': ' + summary.type + ', '; |
| 75 | + }); |
| 76 | + |
| 77 | + // Slice of the last ", " for formatting. |
| 78 | + $('#connectionsDetail').text(connectionsStr.slice(0, -2)); |
| 79 | + }); |
| 80 | + |
| 81 | + dataSource.getActiveTablesAsync().then(function (activeTables) { |
| 82 | + // Loop through each table that was used in creating this datasource |
| 83 | + var tableStr = ''; |
| 84 | + activeTables.forEach(function (table) { |
| 85 | + tableStr += table.name + ', '; |
| 86 | + }); |
| 87 | + |
| 88 | + // Slice of the last ", " for formatting. |
| 89 | + $('#activeTablesDetail').text(tableStr.slice(0, -2)); |
| 90 | + }); |
| 91 | + |
| 92 | + modal.modal('show'); |
| 93 | + } |
| 94 | + |
| 95 | + // Constructs UI that displays all the dataSources in this dashboard |
| 96 | + // given a mapping from dataSourceId to dataSource objects. |
| 97 | + function buildDataSourcesTable (dataSources) { |
| 98 | + // Clear the table first. |
| 99 | + $('#dataSourcesTable > tbody tr').remove(); |
| 100 | + const dataSourcesTable = $('#dataSourcesTable > tbody')[0]; |
| 101 | + |
| 102 | + // Add an entry to the dataSources table for each dataSource. |
| 103 | + for (let dataSourceId in dataSources) { |
| 104 | + const dataSource = dataSources[dataSourceId]; |
| 105 | + |
| 106 | + let newRow = dataSourcesTable.insertRow(dataSourcesTable.rows.length); |
| 107 | + let nameCell = newRow.insertCell(0); |
| 108 | + let refreshCell = newRow.insertCell(1); |
| 109 | + let infoCell = newRow.insertCell(2); |
| 110 | + |
| 111 | + let refreshButton = document.createElement('button'); |
| 112 | + refreshButton.innerHTML = ('Refresh Now'); |
| 113 | + refreshButton.type = 'button'; |
| 114 | + refreshButton.className = 'btn btn-primary'; |
| 115 | + refreshButton.addEventListener('click', function () { refreshDataSource(dataSource); }); |
| 116 | + |
| 117 | + let infoSpan = document.createElement('span'); |
| 118 | + infoSpan.className = 'glyphicon glyphicon-info-sign'; |
| 119 | + infoSpan.addEventListener('click', function () { showModal(dataSource); }); |
| 120 | + |
| 121 | + nameCell.innerHTML = dataSource.name; |
| 122 | + refreshCell.appendChild(refreshButton); |
| 123 | + infoCell.appendChild(infoSpan); |
| 124 | + } |
| 125 | + } |
| 126 | +})(); |
0 commit comments