From 2b39419795166233273eb6688621f66266ddd10d Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Wed, 14 Feb 2018 17:42:49 -0800 Subject: [PATCH] keeping AYS DRY and hooking up restore/delete for content archives --- .../resources/web/content/js/content.js | 165 +++++++++++++----- .../resources/web/js/domain-server.js | 19 +- domain-server/resources/web/js/shared.js | 11 ++ .../resources/web/settings/js/settings.js | 78 ++++----- 4 files changed, 175 insertions(+), 98 deletions(-) diff --git a/domain-server/resources/web/content/js/content.js b/domain-server/resources/web/content/js/content.js index 4e2c27bf54..34af9262a2 100644 --- a/domain-server/resources/web/content/js/content.js +++ b/domain-server/resources/web/content/js/content.js @@ -7,7 +7,7 @@ $(document).ready(function(){ // construct the HTML needed for the settings backup panel var html = "
"; - html += "Upload a Content Archive (.zip) or entity file (.json, .json.gz) to replace the content of this domain."; + html += "Upload a content archive (.zip) or entity file (.json, .json.gz) to replace the content of this domain."; html += "
Note: Your domain content will be replaced by the content you upload, but the existing backup files of your domain's content will not immediately be changed.
"; html += ""; @@ -33,39 +33,36 @@ $(document).ready(function(){ $('body').on('click', '#' + RESTORE_SETTINGS_UPLOAD_ID, function(e){ e.preventDefault(); - swal({ - title: "Are you sure?", - text: "Your domain content will be replaced by the uploaded Content Archive or entity file", - type: "warning", - showCancelButton: true, - closeOnConfirm: false - }, - function () { - var files = $('#' + RESTORE_SETTINGS_FILE_ID).prop('files'); + swalAreYouSure( + "Your domain content will be replaced by the uploaded Content Archive or entity file", + "Restore content", + function() { + var files = $('#' + RESTORE_SETTINGS_FILE_ID).prop('files'); - var fileFormData = new FormData(); - fileFormData.append('restore-file', files[0]); + var fileFormData = new FormData(); + fileFormData.append('restore-file', files[0]); - showSpinnerAlert("Restoring Content"); + showSpinnerAlert("Restoring Content"); - $.ajax({ - url: '/content/upload', - type: 'POST', - cache: false, - processData: false, - contentType: false, - data: fileFormData - }).done(function(data, textStatus, jqXHR) { - swal.close(); - showRestartModal(); - }).fail(function(jqXHR, textStatus, errorThrown) { - showErrorMessage( - "Error", - "There was a problem restoring domain content.\n" - + "Please ensure that the content archive or entity file is valid and try again." - ); - }); - }); + $.ajax({ + url: '/content/upload', + type: 'POST', + cache: false, + processData: false, + contentType: false, + data: fileFormData + }).done(function(data, textStatus, jqXHR) { + swal.close(); + showRestartModal(); + }).fail(function(jqXHR, textStatus, errorThrown) { + showErrorMessage( + "Error", + "There was a problem restoring domain content.\n" + + "Please ensure that the content archive or entity file is valid and try again." + ); + }); + } + ); }); var GENERATE_ARCHIVE_BUTTON_ID = 'generate-archive-button'; @@ -104,6 +101,10 @@ $(document).ready(function(){ $('#' + Settings.CONTENT_ARCHIVES_PANEL_ID + ' .panel-body').html(html); } + var BACKUP_RESTORE_LINK_CLASS = 'restore-backup'; + var BACKUP_DOWNLOAD_LINK_CLASS = 'download-backup'; + var BACKUP_DELETE_LINK_CLASS = 'delete-backup'; + function reloadLatestBackups() { // make a GET request to get backup information to populate the table $.get('/api/backups', function(data) { @@ -117,12 +118,15 @@ $(document).ready(function(){ // populate the backups tables with the backups function createBackupTableRow(backup) { - return "" + backup.name + "" + return "" + + "" + backup.name + "" + moment(backup.createdAtMillis).format('lll') + "" + "" - + ""; + + "
"; } var automaticRows = ""; @@ -172,6 +176,79 @@ $(document).ready(function(){ }); } + // handle click in table to restore a given content backup + $('body').on('click', '.' + BACKUP_RESTORE_LINK_CLASS, function(e){ + // stop the default behaviour + e.preventDefault(); + + // grab the name of this backup so we can show it in alerts + var backupName = $(this).closest('tr').attr('data-backup-name'); + + // grab the ID of this backup in case we need to send a POST + var backupID = $(this).closest('tr').attr('data-backup-id'); + + // make sure the user knows what is about to happen + swalAreYouSure( + "Your domain content will be replaced by the content archive " + backupName, + "Restore content", + function() { + // show a spinner while we send off our request + showSpinnerAlert("Restoring Content Archive " + backupName); + + // setup an AJAX POST to request content restore + $.post('/api/backups/recover/' + backupID).done(function(data, textStatus, jqXHR) { + swal.close(); + showRestartModal(); + }).fail(function(jqXHR, textStatus, errorThrown) { + showErrorMessage( + "Error", + "There was a problem restoring domain content.\n" + + "If the problem persists, the content archive may be corrupted." + ); + }); + } + ) + }); + + // handle click in table to delete a given content backup + $('body').on('click', '.' + BACKUP_DELETE_LINK_CLASS, function(e){ + // stop the default behaviour + e.preventDefault(); + + // grab the name of this backup so we can show it in alerts + var backupName = $(this).closest('tr').attr('data-backup-name'); + + // grab the ID of this backup in case we need to send the DELETE request + var backupID = $(this).closest('tr').attr('data-backup-id'); + + // make sure the user knows what is about to happen + swalAreYouSure( + "The content archive " + backupName + " will be deleted and will no longer be available for restore or download from this page.", + "Delete content archive", + function() { + // show a spinner while we send off our request + showSpinnerAlert("Deleting content archive " + backupName); + + // setup an AJAX DELETE to request content archive delete + $.ajax({ + url: '/api/backups/' + backupID, + type: 'DELETE' + }).done(function(data, textStatus, jqXHR) { + swal.close(); + }).fail(function(jqXHR, textStatus, errorThrown) { + showErrorMessage( + "Error", + "There was an unexpected error deleting the content archive" + ); + }).always(function(){ + // reload the list of content archives in case we deleted a backup + // or it's no longer an available backup for some other reason + reloadContentArchives(); + }); + } + ) + }); + // handle click on automatic content archive settings link $('body').on('click', '#' + AUTO_ARCHIVES_SETTINGS_LINK_ID, function(e) { if (Settings.pendingChanges > 0) { @@ -181,18 +258,14 @@ $(document).ready(function(){ var settingsLink = $(this).attr('href'); - swal({ - title: "Are you sure?", - text: "You have pending changes to content settings that have not been saved. They will be lost if you leave the page to manage automatic content archive intervals.", - type: "warning", - showCancelButton: true, - confirmButtonText: "Proceed without Saving", - closeOnConfirm: true - }, - function () { - // user wants to drop their changes, switch pages - window.location = settingsLink; - }); + swalAreYouSure( + "You have pending changes to content settings that have not been saved. They will be lost if you leave the page to manage automatic content archive intervals.", + "Proceed without Saving", + function() { + // user wants to drop their changes, switch pages + window.location = settingsLink; + } + ); } }); diff --git a/domain-server/resources/web/js/domain-server.js b/domain-server/resources/web/js/domain-server.js index d3b20d40bb..2c12e2683a 100644 --- a/domain-server/resources/web/js/domain-server.js +++ b/domain-server/resources/web/js/domain-server.js @@ -39,16 +39,15 @@ $(document).ready(function(){ }).parent().addClass('active'); $('body').on('click', '#restart-server', function(e) { - swal( { - title: "Are you sure?", - text: "This will restart your domain server, causing your domain to be briefly offline.", - type: "warning", - html: true, - showCancelButton: true - }, function() { - $.get("/restart"); - showRestartModal(); - }); + swalAreYouSure( + "This will restart your domain server, causing your domain to be briefly offline.", + "Restart", + function() { + swal.close(); + $.get("/restart"); + showRestartModal(); + } + ) return false; }); diff --git a/domain-server/resources/web/js/shared.js b/domain-server/resources/web/js/shared.js index 040d8959e7..84bba4de56 100644 --- a/domain-server/resources/web/js/shared.js +++ b/domain-server/resources/web/js/shared.js @@ -98,6 +98,17 @@ var DOMAIN_ID_TYPE_TEMP = 1; var DOMAIN_ID_TYPE_FULL = 2; var DOMAIN_ID_TYPE_UNKNOWN = 3; +function swalAreYouSure(text, confirmButtonText, callback) { + swal({ + title: "Are you sure?", + text: text, + type: "warning", + showCancelButton: true, + confirmButtonText: confirmButtonText, + closeOnConfirm: false + }, callback); +} + function domainIDIsSet() { if (typeof Settings.data.values.metaverse !== 'undefined' && typeof Settings.data.values.metaverse.id !== 'undefined') { diff --git a/domain-server/resources/web/settings/js/settings.js b/domain-server/resources/web/settings/js/settings.js index b73337ef2d..e67ea43158 100644 --- a/domain-server/resources/web/settings/js/settings.js +++ b/domain-server/resources/web/settings/js/settings.js @@ -94,20 +94,17 @@ $(document).ready(function(){ var password = formJSON["security"]["http_password"]; if ((password == sha256_digest("")) && (username == undefined || (username && username.length != 0))) { - swal({ - title: "Are you sure?", - text: "You have entered a blank password with a non-blank username. Are you sure you want to require a blank password?", - type: "warning", - showCancelButton: true, - confirmButtonColor: "#5cb85c", - confirmButtonText: "Yes!", - closeOnConfirm: true - }, - function () { + swalAreYouSure( + "You have entered a blank password with a non-blank username. Are you sure you want to require a blank password?", + "Use blank password", + function() { + swal.close(); + formJSON["security"]["http_password"] = ""; postSettings(formJSON); - }); + } + ); return; } @@ -1033,41 +1030,38 @@ $(document).ready(function(){ $('body').on('click', '#' + RESTORE_SETTINGS_UPLOAD_ID, function(e){ e.preventDefault(); - swal({ - title: "Are you sure?", - text: "Your domain settings will be replaced by the uploaded settings", - type: "warning", - showCancelButton: true, - closeOnConfirm: false - }, - function() { - var files = $('#' + RESTORE_SETTINGS_FILE_ID).prop('files'); + swalAreYouSure( + "Your domain settings will be replaced by the uploaded settings", + "Restore settings", + function() { + var files = $('#' + RESTORE_SETTINGS_FILE_ID).prop('files'); - var fileFormData = new FormData(); - fileFormData.append('restore-file', files[0]); + var fileFormData = new FormData(); + fileFormData.append('restore-file', files[0]); - showSpinnerAlert("Restoring Settings"); + showSpinnerAlert("Restoring Settings"); - $.ajax({ - url: '/settings/restore', - type: 'POST', - processData: false, - contentType: false, - dataType: 'json', - data: fileFormData - }).done(function(data, textStatus, jqXHR) { - swal.close(); - showRestartModal(); - }).fail(function(jqXHR, textStatus, errorThrown) { - showErrorMessage( - "Error", - "There was a problem restoring domain settings.\n" - + "Please ensure that your current domain settings are valid and try again." - ); + $.ajax({ + url: '/settings/restore', + type: 'POST', + processData: false, + contentType: false, + dataType: 'json', + data: fileFormData + }).done(function(data, textStatus, jqXHR) { + swal.close(); + showRestartModal(); + }).fail(function(jqXHR, textStatus, errorThrown) { + showErrorMessage( + "Error", + "There was a problem restoring domain settings.\n" + + "Please ensure that your current domain settings are valid and try again." + ); - reloadSettings(); - }); - }); + reloadSettings(); + }); + } + ); }); $('body').on('change', '#' + RESTORE_SETTINGS_FILE_ID, function() {