const MDCSelect = mdc.select.MDCSelect;
const MDCSelectFoundation = mdc.select.MDCSelectFoundation;
const MDCSnackbar = mdc.snackbar.MDCSnackbar;
const MDCSnackbarFoundation = mdc.snackbar.MDCSnackbarFoundation;
//var gallery = studioUrl = studioKey = studioSecret = null;
var ga = null;
var encryptedSecret = false;
var messages = new MDCSnackbar(document.querySelector('.mdc-snackbar'));
var postJsEventWithCallback = window.Alteryx.Gui.Utils.Functions.postJsEventWithCallback;
var validateUrl = function(url) {
url = url.trim();
var httpTest = new RegExp("^(http|https)://", "i");
if (!httpTest.test(url)) {
url = "http://" + url;
}
if (url.charAt(url.length - 1) == "/") {
url = url.substr(0, url.length - 1);
}
var apiTest = new RegExp("\/api\/v1", "i");
if (!apiTest.test(url)){
url = url + "/api/v1";
}
return url;
}
var atxEncrypt = function(unencyrpted, callback) {
postJsEventWithCallback(
'Encrypt',
{text: unencyrpted},
function(encryptedString) {
callback(encryptedString);
});
}
var atxDecrypt = function(encrypted, callback) {
postJsEventWithCallback(
'Decrypt',
{text: encrypted},
function(decryptedString) {
callback(decryptedString);
});
}
var updateSecret = function(text) {
$('#StudioSecret').val(text);
}
var login = function(callback) {
var studioUrl = $('#StudioUrl').val();
var studioKey = $('#StudioKey').val()
var studioSecret = $('#StudioSecret').val();
atxDecrypt(studioSecret, function(decryptedSecret) {
ga = new Gallery(studioUrl, studioKey, decryptedSecret);
callback();
});
}
var getApps = function(gallery, callback) {
gallery.getSubscriptionWorkflows(function(workflows) {
callback(workflows);
}, function(err) {
console.log(err);
var messageObj = {
message: "Login Error: " + err.responseJSON.message,
timeout: 5000,
multiline: true
}
messages.show(messageObj);
});
}
var setWorkflowUI = function(gallery, id, callback) {
gallery.getAppQuestions(id, function(q) {
$('#workflowInterface').html('');
for (var i = 0; i < q.length; i++) {
var question = q[i];
$('#workflowInterface').append(getQuestionUI(question, i));
if (question.type == "QuestionListBox" && question.multiple == "False") {
var root = document.getElementById(i+"select");
var nativeSelect = root.querySelector('.mdc-select__native-control');
var select = new mdc.select.MDCSelect(root);
} else if (question.type == "QuestionRadioGroup") {
mdc.radio.MDCRadio.attachTo(document.getElementById(i+'radio'));
} else if (question.type == "QuestionDate") {
const input = document.getElementById(i);
const picker = new MaterialDatetimePicker()
.on('submit', (val) => {
input.value = val.format("DD/MM/YYYY");
$('#'+i).parent().addClass('mdc-text-field--upgraded');
$('#'+i).next().addClass('mdc-floating-label--float-above');
});
input.addEventListener('focus', () => picker.open());
mdc.textField.MDCTextField.attachTo(document.getElementById(i+'text'));
} else {
if(document.getElementById(i+'text')) {
mdc.textField.MDCTextField.attachTo(document.getElementById(i+'text'));
}
}
}
callback();
}, function(err) {
console.log(err);
var messageObj = {
message: "App UI Error: " + err.responseJSON.message,
timeout: 5000,
multiline: true
}
messages.show(messageObj);
});
}
var getQuestionUI = function(question, id) {
var html = "";
if (question.type == "QuestionListBox" && question.multiple == "False") {
var options = question.items;
html += `
`+question.description+`
`
} else if (question.type == "QuestionRadioGroup") {
//Radio selection
html += `
`
} else if (question.type == "QuestionNumericUpDown") {
var upgraded = float = value = "";
if (question.value && question.value != "") {
upgraded = " mdc-text-field--upgraded";
float = " mdc-floating-label--float-above";
value = ' value="'+question.value+'"'
}
html += `
`
} else if (question.type == "QuestionTextBox") {
//text box
var type = upgraded = float = value = "";
var desc = question.description;
desc = desc.toLowerCase();
if (desc.indexOf('password') > -1 || desc.indexOf('secret') > -1 ) {
type = "password";
} else {
type = "text";
}
if (question.value && question.value != "") {
upgraded = " mdc-text-field--upgraded";
float = " mdc-floating-label--float-above";
value = ' value="'+question.value+'"'
}
html += `
`
} else if (question.type == "QuestionDate") {
//Date selector
var upgraded = float = value = "";
if (question.value && question.value != "") {
upgraded = " mdc-text-field--upgraded";
float = " mdc-floating-label--float-above";
value = ' value="'+question.value+'"'
}
html += `
`
} else if (question.type == "QuestionBooleanGroup") {
var value = "";
if (question.value && question.value == "True") {
value = ' checked';
}
html += `
`
} else if (question.type == "QuestionListBox" && question.multiple == "True") {
html += `
`
for (var i = 0; i < question.items.length; i++) {
var item = question.items[i];
html += `
`
}
html += `
`
} else {
//Catch all text box
var upgraded = float = value = "";
if (question.value && question.value != "") {
upgraded = " mdc-text-field--upgraded";
float = " mdc-floating-label--float-above";
value = ' value="'+question.value+'"'
}
html += `
`
}
return html;
}
var setAlteryxConfig = function(studioUrl, studioKey, studioSecret, workflows, responses, annotation) {
window.Alteryx.JsEvent(JSON.stringify({
Event: 'GetConfiguration',
Configuration: {
Configuration: {
StudioUrl: studioUrl,
StudioKey: studioKey,
StudioSecret: studioSecret,
Workflow: workflows,
Responses: responses
},
Annotation: annotation
}
}));
}
window.Alteryx.Gui = {
SetConfiguration: function (currentToolConfiguration) {
// Grab the values from the incoming tool configuration, and set the UI
if (currentToolConfiguration && currentToolConfiguration.IsFirstConfig === false) {
var studioUrl = currentToolConfiguration.Configuration.Configuration.StudioUrl;
var studioKey = currentToolConfiguration.Configuration.Configuration.StudioKey;
var studioSecret = currentToolConfiguration.Configuration.Configuration.StudioSecret;
var workflow = currentToolConfiguration.Configuration.Configuration.Workflow;
var signin = true;
var uiConfig = currentToolConfiguration.Configuration.Configuration.Responses;
if (studioUrl && studioUrl != "") {
$('#StudioUrl').parent().addClass('mdc-text-field--upgraded');
$('#StudioUrl').next().addClass('mdc-floating-label--float-above');
$('#StudioUrl').val(studioUrl);
} else {
signin = false;
}
if (studioKey && studioKey != "") {
$('#StudioKey').parent().addClass('mdc-text-field--upgraded');
$('#StudioKey').next().addClass('mdc-floating-label--float-above');
$('#StudioKey').val(studioKey);
} else {
signin = false;
}
if (studioSecret && studioSecret != "") {
$('#StudioSecret').parent().addClass('mdc-text-field--upgraded');
$('#StudioSecret').next().addClass('mdc-floating-label--float-above');
$('#StudioSecret').val(studioSecret);
$('#encryptedSecret').val('true');
} else {
signin = false;
}
if (signin) {
$($('._mdc-stepper__step')[0]).addClass('_mdc-stepper__step--done');
$($('._mdc-stepper__step')[1]).addClass('_mdc-stepper__step--active');
login(function() {
getApps(ga, function(apps) {
for (var i = 0; i < apps.length; i++) {
var app = apps[i];
var option = "";
$('#workflows').append(option);
}
if (workflow && workflow != "") {
$('#workflows option[value='+workflow+']').attr('selected','selected');
$('#workflows').next().addClass('mdc-floating-label--float-above');
setWorkflowUI(ga, $('#workflows').find(":selected").val(), function() {
if (uiConfig && uiConfig != "") {
atxDecrypt(uiConfig, function(config) {
var responses = JSON.parse(config);
for (var i = 0; i < responses.length; i++) {
var response = responses[i];
var tag = $('[data-name="'+response.name+'"]')[0];
if ($(tag).is("div")) {
var checkboxes = JSON.parse(response.value);
for (var j=0; j"+app.metaInfo.name+"";
$('#workflows').append(option);
}
});
});
});
let drawer = new mdc.drawer.MDCTemporaryDrawer(document.querySelector('.mdc-drawer--temporary'));
document.querySelector('.menu').addEventListener('click', () => drawer.open = true);
var root = document.getElementById('app-select');
var nativeSelect = root.querySelector('.mdc-select__native-control');
var select = new mdc.select.MDCSelect(root);
root.addEventListener('change', function() {
//console.log($('#workflows').find(":selected").val());
setWorkflowUI(ga, $('#workflows').find(":selected").val(), function() {
});
});
$(document).on("click", "._mdc-stepper__step", function(args) {
// if the title or label was clicked, toggle the active state
if(args.target.classList.contains('_mdc-stepper__step__title') || args.target.classList.contains('_mdc-stepper__step__label')){
$(this).toggleClass("_mdc-stepper__step--active");
}
});
});