function setupIntentsPanel() { $("#newIntent") .dialog({ autoOpen: false, title: "Create new intent", height: "auto", width: "auto", autoResize: true, resizable: false, modal: true, closeOnEscase: true, draggable: false, open: function() { $("input").val(""); $(".ui-state-error").hide(); $(this) .find("input:visible") .first() .focus(); $(this).dialog("option", "title", "Create new intent"); }, buttons: { "Create new intent": function() { var name = $("#newIntent #intentName") .val() .trim(); if (!canUpdateDB()) return; // check intent name if (!isValidEntityName(name)) { $("#newIntent .ui-state-error") .text("Invalid intent name") .show(); return; } createIntent(name); $(this).dialog("close"); }, Cancel: function() { $(this).dialog("close"); } } }) .keydown(function(event) { if (event.which === $.ui.keyCode.ENTER) return false; return true; }); if (canUpdateDB()) $("#createNewIntentButton") .button() .click(function() { $("#newIntent") .data("vp3IntentName", name) .dialog("open"); }); else $("#createNewIntentButton").hide(); setupIntentsTable(); } function setupIntentsTable() { setupNiceTable($("#intentssTable"), { headers: {0: {sorter: false}, 3: {sorter: "shortDate"}}, dblClick: function($tr) { getIntent($tr, "edit"); }, cmenuDelegate: ".intentMenu", cmenu: [ { title: "View", uiIcon: "ui-icon-document", action: function(event, ui) { setTimeout(function() { getIntent($niceTableCurrentTR, "view"); }, 0); } }, { title: "Edit", uiIcon: "ui-icon-pencil", disabled: !canUpdateDB(), action: function(event, ui) { setTimeout(function() { getIntent($niceTableCurrentTR, "edit"); }, 0); } }, {title: "---"}, { title: "Create new intent", uiIcon: "ui-icon-plus", disabled: !canUpdateDB(), action: function(event, ui) { setTimeout(function() { $("#newIntent") .data("vp3Action", "create") .data("vp3IntentName", name) .dialog("open"); }, 0); } }, { title: "Delete", uiIcon: "ui-icon-trash", disabled: !canUpdateDB(), action: function(event, ui) { setTimeout(function() { deleteIntent($niceTableCurrentTR); }, 0); } }, { title: "Rename", uiIcon: "ui-icon-grip-dotted-horizontal", disabled: true, action: function(event, ui) { setTimeout(function() { var name = $niceTableCurrentTR .find(".intentName") .text() .trim(); if (name) renameEntity(name, "intent"); }); } } ] }); } function getIntent($tr, action) { var name = $tr .find(".intentName") .text() .trim(); var url = getIntentUrl(name); switch (action) { case "view": window.open(url, name); break; case "edit": window.open(url + "/edit", name); break; } } function deleteIntent($tr) { var name = $tr .find(".intentName") .text() .trim(); if (!name) return; showDialog("Are you sure you want to delete intent '" + name + "'?", "Confirm Delete", "highlight", { Cancel: function() { $(this).dialog("close"); }, "Yes, Delete!": function() { $(this).dialog("close"); $.ajax({ type: "DELETE", url: formatEndpoint(getIntentUrl(name)), success: function(data, textStatus, jqXHR) { switch (data.severity) { case "information": $tr.remove(); showInfo(data.message); break; case "error": showError(data.message); break; case "warning": showWarning(data.message); break; } }, error: function(jqXHR, textStatus, errorThrown) { showError("Cannot delete intent - " + errorThrown); } }); } }); } function createIntent(intentName) { $.ajax({ type: "PUT", url: formatEndpoint(getIntentUrl(intentName)), processData: false, success: function(data, textStatus, jqXHR) { switch (data.severity) { case "information": var $firstRow = $(".intentRow:first"); $tr = $firstRow.clone().insertBefore($firstRow); $tr.find(".intentName").text(intentName); $tr.find(".updateTime").text(new Date(data.serverTime).toLocaleString()); $tr.find(".updateUser").text(data.serverUser); showInfo(data.message); break; case "error": showError(data.message); break; case "warning": showWarning(data.message); break; } }, error: function(jqXHR, textStatus, errorThrown) { showError("Cannot create intent - " + errorThrown); } }); }