"use strict"; let gHandlersXslt = null; let gVariablesXslt = null; let gEgo = "all"; let gPersonality = ""; let gParent = ""; let gCustomerId = null; let gHandlertemplates = []; let gDeployments = []; // ajax error handler $.ajaxSetup({ error: function(xhr, status, error) { showError("An AJAX error occured: " + status + "\nError: " + error + "\nError detail: " + xhr.responseText); var errtext = "data:text/html;base64," + window.btoa(xhr.responseText); window.open(errtext, "_blank"); } }); doLogin(() => { // analyze URL gCustomerId = $.url("?customerid"); gEgo = decodeURIComponent($.url("?ego")); if (!gEgo || gEgo === "null") gEgo = "all"; else if (gEgo.charAt(0) === '"') gEgo = gEgo.substr(1, gEgo.length - 2); gPersonality = $.url(1); // Check permissions if (!gPermissions.has("/toolbox/golem")) { loginFailed("Permission /toolbox/golem not found"); return; } if (gPersonality) { // check access level to personality if (!canViewDB()) { loginFailed("Access to db " + gPersonality + " not allowed"); return; } loadXsltUi(`/${gPersonality}/info`, "/html/agent.xslt", () => { setupAgentUi(); }); } else { loadXsltUi(`/xml`, "/html/personalities.xslt", () => { setupPersonalitiesUi(); }); } }); function loadXsltUi(dataPath, xsltPath, callback) { $.when($.get(formatEndpoint(dataPath)), $.get(xsltPath)).then( function(data, styleSheet) { let newDoc = xslTransform(data[0], styleSheet[0]); document.body.outerHTML = newDoc.body.outerHTML; gParent = $("#parentName").text(); callback(); }, function(s) { showError("An AJAX error occurred: " + s.statusText); } ); } function setupAgentUi() { if (gParent && canViewDB(gParent)) { $.get( // or loadXmlAsync formatEndpoint("/" + gParent + "/handlers", "tag=Templates"), data => { let templates = utilSelectNodes("/handlers/handler", data, data.documentElement); gHandlertemplates = templates.map(t => t.getAttribute("name")); } ); } $("#agentTabs") .tabs({ create: function(event, ui) { activatePanel(ui.panel); }, activate: function(event, ui) { activatePanel(ui.newPanel); } }) .delegate("span.ui-icon-close", "click", function() { var panelId = $(this) .closest("li") .remove() .attr("aria-controls"); $("#" + panelId).remove(); $("#agentTabs").tabs("option", "active", 0); }); // setup theme switcher $("#themeswitcher") .themeswitcher({loadTheme: "smoothness"}) .hover( function() { $(this).fadeTo(1, 1); }, // make this an easter egg function() { $(this).fadeTo(1, 0); } ) .fadeTo(750, 0); if (!gPersonality.includes("-")) { $("#searchBox").show(); } $("#searchInput") .focus(function(e) { e.target.select(); $(e.target).one("mouseup", function(e1) { e1.preventDefault(); }); }) .keydown(function(e1) { if (e1.which == 13) { performSearch($(this).val()); } }); $("#searchButton") .button({icon: "ui-icon-search", showLabel: false}) .click(function() { performSearch($("#searchInput").val()); }); $("#copyPersonalityButton") .button() .click(function() { $("#copyPersonalityButton").prop("disabled", true); let to = $("#copyPersonalityName") .val() .trim(); if (to && !canCreateDB(to)) { showError("You have no permission to create " + to); return; } let parent = gParent ? $("#copyPersonalityParent") .val() .trim() : null; if (parent && !canCreateDB(parent)) { showError("You have no permission to create " + parent); return; } let descr = $("#copyPersonalityDescr") .val() .trim(); if (!to) return; let params = "to=" + to + (parent ? "&parent=" + parent : "") + (gParent && $("#copyPersonalityParentCopy")[0].checked ? "&parentfrom=" + gParent : "") + (descr ? "&todescr=" + descr : ""); $.get(formatEndpoint("/" + gPersonality + "/copy", params), data => { $("#copyPersonalityButton").prop("disabled", false); if (data.severity === "error") showError(data.message); else showInfo(data.message); }); }); let dateExt = formatDateToMMDDYY(new Date()); let personalityExt = "-s" + dateExt; let $copyPersonalityDescr = $("#copyPersonalityDescr"); $copyPersonalityDescr.val($copyPersonalityDescr.val() + " - staging " + dateExt); $("#copyPersonalityName").val(gPersonality + personalityExt); if (gParent) { $("#copyPersonalityParent") .val(gParent + "-" + gPersonality + personalityExt) .on("input", e => { $("#copyParentCopy").text(e.target.value); $("#copyPersonalityParentCopy")[0].checked = !!e.target.value && e.target.value !== gParent; }); $("#copyParentCopy").text(gParent + "-" + gPersonality + personalityExt); } $("#createPersonalityButton") .button() .click(function() { var name = $("#newPersonalityName") .val() .trim(); var descr = $("#newPersonalityDescr") .val() .trim(); var parent = $("#newPersonalityParent") .val() .trim(); if (!name || name.indexOf(" ") !== -1 || !descr) { showError("Invalid field value", "Field validation"); return; } if (parent && !canCreateDB(name)) { showError("You have no permission to create " + name); return; } $("#createPersonalityButton").prop("disabled", true); $.ajax({ url: formatEndpoint("/" + name + "/create"), type: "POST", data: $.param({description: descr, parent: parent}), success: function(data, textStatus) { $("#createPersonalityButton").prop("disabled", false); showInfo(data, "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#createPersonalityButton").prop("disabled", false); showError(textStatus + ": " + errorThrown, "Error"); } }); }); $("#newEgoButton") .button() .click(function() { if (!gEgo || gEgo == "all" || gEgo == "base") return; var newEgoName = $("#newEgoName").val(); if (!newEgoName || newEgoName.indexOf(" ") !== -1) { showError("Invalid field value", "Field validation"); return; } $("#newEgoButton").prop("disabled", true); $.ajax({ url: formatEndpoint(getPersonalityUrl() + "/transform", "op=copyego&fromego=" + gEgo + "&toego=" + newEgoName), type: "POST", success: function(data, textStatus) { $("#newEgoButton").prop("disabled", false); showInfo(data, "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#newEgoButton").prop("disabled", false); showError("Cannot create new ego: " + errorThrown, "Error"); } }); }); $("#sendVarsToParseButton") .button() .click(function() { $("#sendVarsToParseButton").prop("disabled", true); $.ajax({ url: formatEndpoint(getPersonalityUrl() + "/submitvarstoparseserver"), type: "POST", success: function(data, textStatus) { $("#sendVarsToParseButton").prop("disabled", false); showInfo(data, "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#sendVarsToParseButton").prop("disabled", false); showError("Cannot update vars in parse server: " + errorThrown, "Error"); } }); }); $("#sendVarsToParseButton_label").tooltip({ content: '' }); $("#parseAllButton") .button() .click(function() { $("#parseAllButton").prop("disabled", true); $.ajax({ url: formatEndpoint(getPersonalityUrl() + "/parseall", gEgo && gEgo != "all" ? "ego=" + gEgo : undefined), type: "POST", success: function(data, textStatus) { $("#parseAllButton").prop("disabled", false); showInfo(data, "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#parseAllButton").prop("disabled", false); showError("Cannot send all handlers to parse server: " + errorThrown, "Error"); } }); }); $("#parseAllButton_label").tooltip({ content: '' }); $("#vfReport") .button() .click(function() { let vfGateway = $("#vfGateway").val(); let vfKey = encodeURIComponent($("#vfKey").val()); let vfSecret = encodeURIComponent($("#vfSecret").val()); if (!gEgo || gEgo === "all" || gEgo === "base") return; if (!(!vfGateway && !vfKey && !vfSecret) && (!vfGateway || !vfKey || !vfSecret)) return; $("#vfReport").prop("disabled", true); $.ajax({ url: formatEndpoint( getPersonalityUrl() + "/voicefilereport", "ego=" + gEgo + "&gateway=" + vfGateway + "&deploymentKey=" + vfKey + "&deploymentSecret=" + vfSecret ), type: "POST", success: function(data, textStatus) { $("#vfReport").prop("disabled", false); showInfo(data, "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#vfReport").prop("disabled", false); showError("Cannot submit voice report: " + errorThrown, "Error"); } }); }); $("#vfReport_label").tooltip({ content: '' }); $("#deletePersonalityButton") .button() .click(function() { $("#deletePersonalityButton").prop("disabled", true); let pName = $("#perToDeleteName").val(); if (!pName) return; if (!canDeleteDB(pName)) { showError("You have no permission to delete " + pName); return; } $.ajax({ url: formatEndpoint("/" + pName), type: "DELETE", success: function(data, textStatus) { $("#deletePersonalityButton").prop("disabled", false); showInfo("Personality " + pName + " deleted", "Success"); }, error: function(jqXHR, textStatus, errorThrown) { $("#deletePersonalityButton").prop("disabled", false); showError("Cannot delete personality: " + errorThrown, "Error"); } }); }); let $deploySelect = $("#deploySelect"); $("#deployButton") .button() .click(function() { $("#deployButton") .prop("disabled", true) .text("Deploying..."); let deployName = $deploySelect.val(); if (!canDeployCustomerId(deployName)) { showError("You have no permission to deploy " + deployName); return; } $.ajax({ url: formatEndpoint("/deploy", "customerid=" + deployName), type: "GET", success: (data, textStatus) => { $("#deployButton") .prop("disabled", false) .text("Deploy"); switch (data.severity) { case "info": showInfo(data.message, "Success"); break; case "error": showError(data.message, "Deployment failed"); break; } }, error: (jqXHR, textStatus, errorThrown) => { $("#deployButton") .prop("disabled", false) .text("Deploy"); showError("Cannot deploy to " + deployName + ", " + errorThrown, "Deployment failed"); } }); }); gPermissions.forEach(v => { if (v.startsWith("/bot/deploy/") && v !== "/bot/deploy/*") gDeployments.push(v.substr("/bot/deploy/".length)); }); if (gDeployments.length) { gDeployments.forEach(d => $deploySelect.append($("