From 7fe69dcf386d3db8364bc8483464f87ec2791b6b Mon Sep 17 00:00:00 2001 From: Shreyas Zare Date: Sun, 9 Oct 2022 15:45:59 +0530 Subject: [PATCH] apps.js: updated implementation to avoid refreshing apps list for all actions. --- DnsServerCore/www/js/apps.js | 177 +++++++++++++++++++---------------- 1 file changed, 96 insertions(+), 81 deletions(-) diff --git a/DnsServerCore/www/js/apps.js b/DnsServerCore/www/js/apps.js index e62df3c4..3521420c 100644 --- a/DnsServerCore/www/js/apps.js +++ b/DnsServerCore/www/js/apps.js @@ -31,59 +31,7 @@ function refreshApps() { var tableHtmlRows = ""; for (var i = 0; i < apps.length; i++) { - var id = Math.floor(Math.random() * 10000); - var name = apps[i].name; - var version = apps[i].version; - var updateVersion = apps[i].updateVersion; - var updateUrl = apps[i].updateUrl; - var updateAvailable = apps[i].updateAvailable; - - var dnsAppsTable = null; - - //dnsApps - if (apps[i].dnsApps.length > 0) { - dnsAppsTable = ""; - - for (var j = 0; j < apps[i].dnsApps.length; j++) { - var labels = ""; - var description = null; - - if (apps[i].dnsApps[j].isAppRecordRequestHandler) { - labels += "APP Record"; - description = "

" + htmlEncode(apps[i].dnsApps[j].description).replace(/\n/g, "
") + "

" + (apps[i].dnsApps[j].recordDataTemplate == null ? "" : "
Record Data Template
" + htmlEncode(apps[i].dnsApps[j].recordDataTemplate) + "
"); - } - - if (apps[i].dnsApps[j].isRequestController) - labels += "Access Control"; - - if (apps[i].dnsApps[j].isAuthoritativeRequestHandler) - labels += "Authoritative"; - - if (apps[i].dnsApps[j].isQueryLogger) - labels += "Query Logs"; - - if (labels == "") - labels = "Generic"; - - if (description == null) - description = htmlEncode(apps[i].dnsApps[j].description).replace(/\n/g, "
"); - - dnsAppsTable += ""; - } - - dnsAppsTable += "
Class PathDescription
" + htmlEncode(apps[i].dnsApps[j].classPath) + "
" + labels + "
" + description + "
" - } - - tableHtmlRows += "
" + htmlEncode(name) + "
Version " + htmlEncode(version) + " Update " + htmlEncode(updateVersion) + "
"; - - if (dnsAppsTable != null) - tableHtmlRows += dnsAppsTable; - - tableHtmlRows += ""; - tableHtmlRows += ""; - tableHtmlRows += ""; - tableHtmlRows += ""; - tableHtmlRows += ""; + tableHtmlRows += getAppRowHtml(apps[i]); } $("#tableAppsBody").html(tableHtmlRows); @@ -107,6 +55,68 @@ function refreshApps() { }); } +function getAppRowId(appName) { + return appName.replace(/ /g, ""); +} + +function getAppRowHtml(app) { + var name = app.name; + var version = app.version; + var updateVersion = app.updateVersion; + var updateUrl = app.updateUrl; + var updateAvailable = app.updateAvailable; + + var dnsAppsTable = null; + + //dnsApps + if (app.dnsApps.length > 0) { + dnsAppsTable = ""; + + for (var j = 0; j < app.dnsApps.length; j++) { + var labels = ""; + var description = null; + + if (app.dnsApps[j].isAppRecordRequestHandler) { + labels += "APP Record"; + description = "

" + htmlEncode(app.dnsApps[j].description).replace(/\n/g, "
") + "

" + (app.dnsApps[j].recordDataTemplate == null ? "" : "
Record Data Template
" + htmlEncode(app.dnsApps[j].recordDataTemplate) + "
"); + } + + if (app.dnsApps[j].isRequestController) + labels += "Access Control"; + + if (app.dnsApps[j].isAuthoritativeRequestHandler) + labels += "Authoritative"; + + if (app.dnsApps[j].isQueryLogger) + labels += "Query Logs"; + + if (labels == "") + labels = "Generic"; + + if (description == null) + description = htmlEncode(app.dnsApps[j].description).replace(/\n/g, "
"); + + dnsAppsTable += ""; + } + + dnsAppsTable += "
Class PathDescription
" + htmlEncode(app.dnsApps[j].classPath) + "
" + labels + "
" + description + "
" + } + + var id = getAppRowId(name); + var tableHtmlRow = "
" + htmlEncode(name) + "
Version " + htmlEncode(version) + " Update " + htmlEncode(updateVersion) + "
"; + + if (dnsAppsTable != null) + tableHtmlRow += dnsAppsTable; + + tableHtmlRow += ""; + tableHtmlRow += ""; + tableHtmlRow += ""; + tableHtmlRow += ""; + tableHtmlRow += ""; + + return tableHtmlRow +} + function showStoreAppsModal() { var divStoreAppsAlert = $("#divStoreAppsAlert"); var divStoreAppsLoader = $("#divStoreAppsLoader"); @@ -204,9 +214,11 @@ function installStoreApp(objBtn, appName, url) { var id = btn.attr("data-id"); $("#btnStoreAppUninstall" + id).show(); - refreshApps(); + var tableHtmlRow = getAppRowHtml(responseJSON.response.installedApp); + $("#tableAppsBody").prepend(tableHtmlRow); + updateAppsFooterCount(); - showAlert("success", "Store App Installed!", "DNS application was installed successfully from DNS App Store.", divStoreAppsAlert); + showAlert("success", "Store App Installed!", "DNS application '" + appName + "' was installed successfully from DNS App Store.", divStoreAppsAlert); }, error: function () { btn.button('reset'); @@ -235,21 +247,17 @@ function updateStoreApp(objBtn, appName, url, isModal) { btn.button('reset'); btn.hide(); - var id = btn.attr("data-id"); - if (isModal) { + var id = btn.attr("data-id"); $("#spanStoreAppUpdateVersion" + id).hide(); $("#spanStoreAppDisplayVersion" + id).text($("#spanStoreAppUpdateVersion" + id).text().replace(/Update/g, "Version")); - - refreshApps(); - } - else { - $("#btnAppsStoreUpdate" + id).hide(); - $("#trAppUpdateVersion" + id).hide(); - $("#trAppVersion" + id).text($("#trAppUpdateVersion" + id).text().replace(/Update/g, "Version")); } - showAlert("success", "Store App Updated!", "DNS application was updated successfully from DNS App Store.", divStoreAppsAlert); + var tableHtmlRow = getAppRowHtml(responseJSON.response.updatedApp); + var id = getAppRowId(responseJSON.response.updatedApp.name); + $("#trApp" + id).replaceWith(tableHtmlRow); + + showAlert("success", "Store App Updated!", "DNS application '" + appName + "' was updated successfully from DNS App Store.", divStoreAppsAlert); }, error: function () { btn.button('reset'); @@ -282,9 +290,11 @@ function uninstallStoreApp(objBtn, appName) { $("#btnStoreAppUpdate" + id).hide(); $("#spanStoreAppVersion" + id).attr("class", "label label-primary"); - refreshApps(); + var id = getAppRowId(appName); + $("#trApp" + id).remove(); + updateAppsFooterCount(); - showAlert("success", "Store App Uninstalled!", "DNS application was uninstalled successfully.", divStoreAppsAlert); + showAlert("success", "Store App Uninstalled!", "DNS application '" + appName + "' was uninstalled successfully.", divStoreAppsAlert); }, error: function () { btn.button('reset'); @@ -327,9 +337,11 @@ function installApp() { success: function (responseJSON) { $("#modalInstallApp").modal("hide"); - refreshApps(); + var tableHtmlRow = getAppRowHtml(responseJSON.response.installedApp); + $("#tableAppsBody").prepend(tableHtmlRow); + updateAppsFooterCount(); - showAlert("success", "App Installed!", "DNS application was installed successfully."); + showAlert("success", "App Installed!", "DNS application '" + appName + "' was installed successfully."); }, error: function () { btn.button('reset'); @@ -365,9 +377,11 @@ function updateApp() { success: function (responseJSON) { $("#modalUpdateApp").modal("hide"); - refreshApps(); + var tableHtmlRow = getAppRowHtml(responseJSON.response.updatedApp); + var id = getAppRowId(responseJSON.response.updatedApp.name); + $("#trApp" + id).replaceWith(tableHtmlRow); - showAlert("success", "App Updated!", "DNS application was updated successfully."); + showAlert("success", "App Updated!", "DNS application '" + appName + "' was updated successfully."); }, error: function () { btn.button('reset'); @@ -385,7 +399,6 @@ function uninstallApp(objBtn, appName) { return; var btn = $(objBtn); - btn.button('loading'); HTTPRequest({ @@ -393,15 +406,9 @@ function uninstallApp(objBtn, appName) { success: function (responseJSON) { var id = btn.attr("data-id"); $("#trApp" + id).remove(); + updateAppsFooterCount(); - var totalApps = $('#tableApps >tbody >tr').length; - - if (totalApps > 0) - $("#tableAppsFooter").html("Total Apps: " + totalApps + ""); - else - $("#tableAppsFooter").html("No App Found"); - - showAlert("success", "App Uninstalled!", "DNS application was uninstalled successfully."); + showAlert("success", "App Uninstalled!", "DNS application '" + appName + "' was uninstalled successfully."); }, error: function () { btn.button('reset'); @@ -412,6 +419,14 @@ function uninstallApp(objBtn, appName) { }); } +function updateAppsFooterCount() { + var totalApps = $('#tableApps >tbody >tr').length; + if (totalApps > 0) + $("#tableAppsFooter").html("Total Apps: " + totalApps + ""); + else + $("#tableAppsFooter").html("No App Found"); +} + function showAppConfigModal(objBtn, appName) { var btn = $(objBtn); @@ -459,7 +474,7 @@ function saveAppConfig() { success: function (responseJSON) { $("#modalAppConfig").modal("hide"); - showAlert("success", "App Config Saved!", "The DNS application config was saved and reloaded successfully."); + showAlert("success", "App Config Saved!", "The DNS application '" + appName + "' config was saved and reloaded successfully."); }, error: function () { btn.button('reset');