-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
87 lines (77 loc) · 2.83 KB
/
app.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
//Validation check for fields
function validateFields() {
var validates = Array.from(document.getElementsByClassName('validable'));
validates.forEach(function (input) {
input.classList.add("is-invalid");
input.addEventListener('input', function (e) {
if (e.target.value) {
input.classList.remove("is-invalid");
} else {
input.classList.add("is-invalid");
}
})
})
}
function renderError(msg) {
$('#error-msg').text(msg);
$('#error').fadeIn();
}
function hideError() {
$('#error-msg').text(null)
$('#error').fadeOut();
}
function validateCredentials(success) {
var deploymentId = document.getElementById('deployment-id').value;
var adminKey = document.getElementById('admin-key').value;
var req = new XMLHttpRequest();
req.open('POST',config.base_url + "/events/validate");
req.setRequestHeader('Content-Type','application/json; charset=utf-8');
req.send(JSON.stringify({ deploymentId: deploymentId, adminKey: adminKey }));
req.onload = function (){
if(req.status >= 200 && req.status < 300){
success();
}
else {
renderError(req.response);
}
};
}
function formIsValid() {
return document.getElementById('deployment-id').value && document.getElementById('admin-key').value;
}
function refillDataIfExists() {
if(tableau.phase && tableau.phase === 'interactive'){
if(tableau.connectionData){
var data = JSON.parse(tableau.connectionData);
var toBeCheckedFields = {deploymentId: "deployment-id", workbook: "workbook-name", userMetadata: "custom-metadata-fields"}
for (var key in toBeCheckedFields) {
if(data[key]) {
var field = document.getElementById(toBeCheckedFields[key]);
field.value = data[key].toString();
field.classList.remove("is-invalid");
}
}
}
} else {
setTimeout(refillDataIfExists, 250)
}
}
$(document).ready(function () {
$("#submitButton").click(function () {
if(formIsValid()){
validateCredentials(function () {
hideError();
tableau.connectionName = "Connector";
tableau.connectionData = JSON.stringify({
deploymentId: document.getElementById('deployment-id').value,
adminKey: document.getElementById('admin-key').value,
workbook: document.getElementById('workbook-name').value,
userMetadata: document.getElementById('custom-metadata-fields').value.split(',').map(function(val) { return val.trim(); })
});
tableau.submit();
});
}
});
validateFields();
refillDataIfExists();
});