-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdynamicparam.js
126 lines (118 loc) · 5.04 KB
/
dynamicparam.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
let dashboard, parameter, dataSource;
$(document).ready(function() {
tableau.extensions.initializeAsync({ 'configure': configure }).then(() => {
console.log(tableau.extensions.settings.getAll());
dashboard = tableau.extensions.dashboardContent.dashboard;
let configured = tableau.extensions.settings.get('configured');
if (configured != 'true') {
configure();
} else {
findDataSource(tableau.extensions.settings.get('selDataSource'));
document.body.style.backgroundColor = tableau.extensions.settings.get('bg');
}
});
});
// Pops open the configure page
function configure() {
const popupUrl = `${window.location.origin}/Dynamic-Parameter/config.html`
let payload = "";
tableau.extensions.ui.displayDialogAsync(popupUrl, payload, { height: 500, width: 600 }).then((closePayload) => {
console.log("Dialog was closed.");
console.log(closePayload);
document.body.style.backgroundColor = tableau.extensions.settings.get('bg');
findDataSource();
}).catch((error) => {
switch (error.errorCode) {
case tableau.ErrorCodes.DialogClosedByUser:
console.log("Dialog was closed by user.");
break;
default:
console.error(error.message);
}
});
}
// Gets data source based on name passed back from dialog
function findDataSource() {
console.log('Collecting data source.');
let wslist = [];
dashboard.worksheets.forEach(function(worksheet) {
worksheet.getDataSourcesAsync().then(function(datasources) {
datasources.forEach(function(datasource) {
dataSource = datasources.find(datasource => datasource.name === tableau.extensions.settings.get('selDataSource'))
});
if (wslist.indexOf(worksheet.name) == -1) {
wslist.push(worksheet.name);
}
if (wslist.length == dashboard.worksheets.length) {
console.log(dataSource);
findParameter()
}
});
});
}
function findParameter() {
tableau.extensions.dashboardContent.dashboard.getParametersAsync().then(params => {
parameter = params.find(param => param.name === tableau.extensions.settings.get('selParam'));
wsEvent();
});
}
function wsEvent() {
if (tableau.extensions.settings.get('dpRelevant') == 'true') {
let dataSheet = dashboard.worksheets.find(ws => ws.name == tableau.extensions.settings.get('selWorksheet'));
let unregisterHandlerFunction = dataSheet.addEventListener(tableau.TableauEventType.FilterChanged, getParamData);
}
getParamData();
}
// Gets the values from the selected field and populates the Dynamic Parameter
function getParamData() {
console.log('Populating parameter.');
let dataField = tableau.extensions.settings.get('selField');
let rel = tableau.extensions.settings.get('dpRelevant');
if (rel == 'false') {
console.log('Getting all dynamic parameter values.');
dataSource.getUnderlyingDataAsync().then(dataTable => {
fieldIndex = dataTable.columns.find(column => column.fieldName == dataField).index;
let list = [];
let options = "";
for (row of dataTable.data) {
list.push(row[fieldIndex].value);
};
list = list.filter(function(item, index, inputArray) {
return inputArray.indexOf(item) == index;
});
list.sort();
for (l of list) {
options += "<option value='" + l + "'>" + l + "</option>";
}
document.getElementById('dynamicparameter').innerHTML = options;
document.getElementById('dynamicparameter').value = parameter.currentValue.value;
// document.getElementById('dropdown').style.display = "block";
});
} else {
let dataSheet = dashboard.worksheets.find(ws => ws.name == tableau.extensions.settings.get('selWorksheet'));
dataSheet.getSummaryDataAsync().then(dataTable => {
fieldIndex = dataTable.columns.find(column => column.fieldName == dataField).index;
let list = [];
let options = "";
for (row of dataTable.data) {
list.push(row[fieldIndex].value);
};
list = list.filter(function(item, index, inputArray) {
return inputArray.indexOf(item) == index;
});
list.sort();
for (l of list) {
options += "<option value='" + l + "'>" + l + "</option>";
}
document.getElementById('dynamicparameter').innerHTML = options;
document.getElementById('dynamicparameter').value = parameter.currentValue.value;
// document.getElementById('dropdown').style.display = "block";
});
}
}
// Updates the parameter based on selection in Dynamic Parameter
function updateParam(arg) {
console.log('Updating parameter.');
parameter.changeValueAsync(arg);
getParamData();
}