✏️ JS code clean and refractor

This commit is contained in:
SebClem 2021-01-08 20:30:21 +01:00
parent f7ebd6a9e0
commit fce90f0b6a

View File

@ -4,7 +4,7 @@ var last_manu_back = "";
var last_auto_back = "";
const default_toast_timeout = 10000;
let loadingModal = null;
let loadingModal;
let nextcloud_setting_modal;
let backup_setting_modal;
document.addEventListener('DOMContentLoaded', function () {
@ -29,8 +29,9 @@ function updateDynamicListeners() {
let id = this.getAttribute('data-id');
console.log(id);
});
$('.manual-back-list').unbind();
$('.manual-back-list').click(function () {
let manual_back_list = $('.manual-back-list');
manual_back_list.unbind();
manual_back_list.click(function () {
let id = this.getAttribute('data-id');
let name = this.getAttribute('data-name');
manualBackup(id, name);
@ -50,8 +51,9 @@ function updateLocalSnaps() {
return;
last_local_snap = JSON.stringify(data);
needUpdate = true;
$('#local_snaps').empty();
$('#local_snaps').html(data);
let local_snaps = $('#local_snaps');
local_snaps.empty();
local_snaps.html(data);
}).always(() => {
updateManuBackup(needUpdate);
});
@ -64,8 +66,9 @@ function updateManuBackup(prevUpdate) {
return;
last_manu_back = JSON.stringify(data);
needUpdate = true;
$('#manual_backups').empty();
$('#manual_backups').html(data);
let manual_backups = $('#manual_backups')
manual_backups.empty();
manual_backups.html(data);
}).always(() => {
updateAutoBackup(prevUpdate || needUpdate);
@ -79,8 +82,9 @@ function updateAutoBackup(prevUpdate) {
return;
needUpdate = true;
last_auto_back = JSON.stringify(data);
$('#auto_backups').empty();
$('#auto_backups').html(data);
let auto_backups = $('#auto_backups')
auto_backups.empty();
auto_backups.html(data);
}).always(() => {
if (prevUpdate || needUpdate)
@ -92,43 +96,46 @@ function update_status() {
$.get('./api/status', (data) => {
if (JSON.stringify(data) !== last_status) {
last_status = JSON.stringify(data);
let buttons = $('#btn-backup-now, #btn-clean-now');
switch (data.status) {
case "error":
printStatus('Error', data.message);
$('#btn-backup-now, #btn-clean-now').removeClass("disabled");
buttons.removeClass("disabled");
break;
case "idle":
printStatus('Idle', "Waiting for next backup.");
$('#btn-backup-now, #btn-clean-now').removeClass("disabled");
buttons.removeClass("disabled");
break;
case "download":
printStatusWithBar('Downloading Snapshot', data.progress);
$('#btn-backup-now, #btn-clean-now').addClass("disabled");
buttons.addClass("disabled");
break;
case "download-b":
printStatusWithBar('Downloading Backup', data.progress);
$('#btn-backup-now, #btn-clean-now').addClass("disabled");
buttons.addClass("disabled");
break;
case "upload":
printStatusWithBar('Uploading Snapshot', data.progress);
$('#btn-backup-now, #btn-clean-now').addClass("disabled");
buttons.addClass("disabled");
break;
case "upload-b":
printStatusWithBar('Uploading Snapshot', data.progress);
$('#btn-backup-now, #btn-clean-now').addClass("disabled");
buttons.addClass("disabled");
break;
case "creating":
printStatusWithBar('Creating Snapshot', data.progress);
$('#btn-backup-now, #btn-clean-now').addClass("disabled");
buttons.addClass("disabled");
break;
}
if (data.last_backup != null) {
if ($('#last_back_status').html() != data.last_backup)
$('#last_back_status').html(data.last_backup);
let last_back_status = $('#last_back_status');
if (last_back_status.html() !== data.last_backup)
last_back_status.html(data.last_backup);
}
if (data.next_backup != null) {
if ($('#next_back_status').html() != data.next_backup)
$('#next_back_status').html(data.next_backup);
let next_back_status = $('#next_back_status');
if (next_back_status.html() !== data.next_backup)
next_back_status.html(data.next_backup);
}
}
@ -138,11 +145,13 @@ function update_status() {
function printStatus(status, secondLine) {
$('#status').empty();
$('#status').html(status);
$('#status-second-line').empty();
$('#status-second-line').removeClass('text-center');
$('#status-second-line').html(secondLine);
let status_jq = $('#status');
status_jq.empty();
status_jq.html(status);
let status_s_l_jq = $('#status-second-line');
status_s_l_jq.empty();
status_s_l_jq.removeClass('text-center');
status_s_l_jq.html(secondLine);
$('#progress').addClass("invisible");
}
@ -168,27 +177,17 @@ function printStatusWithBar(status, progress) {
}
function listeners() {
$('#save-nextcloud-settings').click(sendNextcloudSettings);
$('#trigger-nextcloud-settings').click(getNextcloudSettings);
$('#trigger-backup-settings').click(getBackupSettings);
$('#btn-backup-now').click(backupNow);
$('#btn-clean-now').click(cleanNow);
$('#trigger-backup-settings').click(getBackupSettings);
$('#cron-drop-settings').change(updateDropVisibility);
$('#save-backup-settings').click(sendBackupSettings);
$('#local-snap-keep').on('input', function () {
$('#local-snap-keep-read').val($(this).val());
});
$('#backup-snap-keep').on('input', function () {
$('#backup-snap-keep-read').val($(this).val());
});
$('#trigger-nextcloud-settings').click(getNextcloudSettings);
$('#save-nextcloud-settings').click(sendNextcloudSettings);
$('#ssl').change(function () {
let div = $('#self_signed').parent().parent();
if ($('#ssl').is(':checked'))
div.removeClass("invisible")
else
@ -199,7 +198,7 @@ function listeners() {
function restore(id) {
loadingModal.show();
$.post('./api/restore', { path: id })
.done((data) => {
.done(() => {
console.log("Restore cmd send !");
create_toast("success", "Command send !", default_toast_timeout);
})
@ -227,22 +226,23 @@ function sendNextcloudSettings() {
back_dir: back_dir,
self_signed: self_signed
})
.done((data) => {
.done(() => {
console.log('Saved');
$('#nextcloud_settings_message').parent().addClass("d-none");
create_toast("success", "Nextcloud settings saved !", default_toast_timeout);
0
})
.fail((data) => {
if (data.status == 406) {
let nextcloud_settings_message = $('#nextcloud_settings_message')
if (data.status === 406) {
console.log(data.responseJSON.message);
$('#nextcloud_settings_message').html(data.responseJSON.message);
nextcloud_settings_message.html(data.responseJSON.message);
} else {
$('#nextcloud_settings_message').html("Invalid Settings.");
nextcloud_settings_message.html("Invalid Settings.");
}
$('#nextcloud_settings_message').parent().removeClass("d-none");
nextcloud_settings_message.parent().removeClass("d-none");
nextcloud_setting_modal.show();
create_toast("error", "Invalid Nextcloud settings !", default_toast_timeout);
console.log('Fail');
@ -252,8 +252,8 @@ function sendNextcloudSettings() {
}
function manualBackup(id, name) {
$.post('./api/manual-backup?id=' + id + '&name=' + name)
.done((data) => {
$.post(`./api/manual-backup?id=${id}&name=${name}`)
.done(() => {
console.log("manual bk cmd send !");
create_toast("success", "Command send !", default_toast_timeout);
})
@ -261,19 +261,19 @@ function manualBackup(id, name) {
console.log(error);
create_toast("error", "Can't send command !", default_toast_timeout);
})
}
function getNextcloudSettings() {
loadingModal.show();
$.get('./api/nextcloud-settings', (data) => {
$('#ssl').prop("checked", data.ssl == "true");
if (data.ssl == "true") {
let div = $('#self_signed').parent().parent();
$('#ssl').prop("checked", data.ssl === "true");
let sef_signed_jq = $('#self_signed')
if (data.ssl === "true") {
let div = sef_signed_jq.parent().parent();
div.removeClass("invisible");
}
$('#self_signed').prop('checked', data.self_signed == "true")
sef_signed_jq.prop('checked', data.self_signed === "true")
$('#hostname').val(data.host);
$('#username').val(data.username);
$('#password').val(data.password);
@ -316,7 +316,7 @@ function cleanNow() {
function getBackupSettings() {
loadingModal.show();
$.get('./api/backup-settings', (data) => {
if (JSON.stringify(data) == "{}") {
if (JSON.stringify(data) === "{}") {
data = {
cron_base: "0",
cron_hour: "00:00",
@ -345,16 +345,15 @@ function getBackupSettings() {
$('#cron-drop-day').val(data.settings.cron_weekday);
let folder_html = ""
for (let index in data.folders) {
let thisFolder = data.folders[index];
for (let thisFolder of data.folders) {
//TODO Bug ici ?
let exclude = data.settings.exclude_folder.includes(thisFolder.slug);
folder_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input addons-box" type="checkbox" id="${thisFolder.slug}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisFolder.slug}">${thisFolder.name}</label></div></li>`
}
$("#folders-div").html(folder_html);
let addons_html = ""
for (let index in data.addonList) {
let thisAddon = data.addonList[index];
for (let thisAddon of data.addonList) {
let exclude = data.settings.exclude_addon.includes(thisAddon.slug);
addons_html += `<li class="list-group-item"><div class="form-check"><input class="form-check-input addons-box" type="checkbox" id="${thisAddon.slug}" ${exclude ? "" : "checked"}><label class="form-label mb-0" for="${thisAddon.slug}">${thisAddon.name}</label></div></li>`
}
@ -370,28 +369,30 @@ function getBackupSettings() {
function updateDropVisibility() {
let cronBase = $("#cron-drop-settings").val();
let timepicker = $('#timepicker');
let cron_drop_day = $('#cron-drop-day');
let cron_drop_day_mount = $('#cron-drop-day-month');
switch (cronBase) {
case "3":
$('#timepicker').parent().parent().removeClass("d-none");
$('#cron-drop-day').parent().parent().addClass("d-none");
$('#cron-drop-day-month').parent().parent().removeClass("d-none");
timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().removeClass("d-none");
break;
case "2":
$('#timepicker').parent().parent().removeClass("d-none");
$('#cron-drop-day').parent().parent().removeClass("d-none");
$('#cron-drop-day-month').parent().parent().addClass("d-none");
timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().removeClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none");
break;
case "1":
$('#timepicker').parent().parent().removeClass("d-none");
$('#cron-drop-day').parent().parent().addClass("d-none");
$('#cron-drop-day-month').parent().parent().addClass("d-none");
timepicker.parent().parent().removeClass("d-none");
cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none");
break;
case "0":
$('#timepicker').parent().parent().addClass("d-none");
$('#cron-drop-day').parent().parent().addClass("d-none");
$('#cron-drop-day-month').parent().parent().addClass("d-none");
timepicker.parent().parent().addClass("d-none");
cron_drop_day.parent().parent().addClass("d-none");
cron_drop_day_mount.parent().parent().addClass("d-none");
break;
}
}
@ -406,7 +407,7 @@ function sendBackupSettings() {
let auto_clean_local_keep = $("#local-snap-keep").val();
let auto_clean_backup_keep = $("#backup-snap-keep").val();
let name_template = $('#name-template').val();
//TODO Bug ici ?
let excluded_folders_nodes = document.querySelectorAll('.folders-box:not(:checked)');
let exclude_folder = [""];
for (let i of excluded_folders_nodes) {