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