From 0ea35c54a347bbfddf97f0b61982416f27ee52b4 Mon Sep 17 00:00:00 2001 From: skomerko <168652295+skomerko@users.noreply.github.com> Date: Fri, 13 Sep 2024 16:28:02 +0200 Subject: [PATCH] WebUI: Improve torrent deletion * Added 'Confirm when deleting torrents' option to the WebUI * Confirm deletion dialog now uses MUI.Modal PR #21289. Closes #18345. --- src/webui/api/appcontroller.cpp | 5 + src/webui/www/private/css/Window.css | 2 +- src/webui/www/private/scripts/mocha-init.js | 288 ++++++++++++++++++--------- src/webui/www/private/views/preferences.html | 6 + 4 files changed, 202 insertions(+), 99 deletions(-) diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 2d216ff92..2b3fc7e2d 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -127,6 +127,8 @@ void AppController::preferencesAction() // Language data[u"locale"_s] = pref->getLocale(); data[u"performance_warning"_s] = session->isPerformanceWarningEnabled(); + // Transfer List + data[u"confirm_torrent_deletion"_s] = pref->confirmTorrentDeletion(); // Log file data[u"file_log_enabled"_s] = app()->isFileLoggerEnabled(); data[u"file_log_path"_s] = app()->fileLoggerPath().toString(); @@ -504,6 +506,9 @@ void AppController::setPreferencesAction() } if (hasKey(u"performance_warning"_s)) session->setPerformanceWarningEnabled(it.value().toBool()); + // Transfer List + if (hasKey(u"confirm_torrent_deletion"_s)) + pref->setConfirmTorrentDeletion(it.value().toBool()); // Log file if (hasKey(u"file_log_enabled"_s)) app()->setFileLoggerEnabled(it.value().toBool()); diff --git a/src/webui/www/private/css/Window.css b/src/webui/www/private/css/Window.css index a5b1694e2..2149edfdb 100644 --- a/src/webui/www/private/css/Window.css +++ b/src/webui/www/private/css/Window.css @@ -205,7 +205,7 @@ div.mochaToolbarWrapper.bottom { ---------------------------------------------------------------- */ #modalOverlay { - background: #000; + background: hsla(0deg 0 0 / 30%); display: none; left: 0; opacity: 0; diff --git a/src/webui/www/private/scripts/mocha-init.js b/src/webui/www/private/scripts/mocha-init.js index 7e3c68fcb..fcf613c41 100644 --- a/src/webui/www/private/scripts/mocha-init.js +++ b/src/webui/www/private/scripts/mocha-init.js @@ -38,6 +38,53 @@ ----------------------------------------------------------------- */ "use strict"; +window.qBittorrent ??= {}; +window.qBittorrent.Dialog ??= (() => { + const exports = () => { + return { + baseModalOptions: baseModalOptions + }; + }; + + const deepFreeze = (obj) => { + // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze#examples + + const keys = Reflect.ownKeys(obj); + for (const key of keys) { + const value = obj[key]; + if ((value && (typeof value === "object")) || (typeof value === "function")) + deepFreeze(value); + } + Object.freeze(obj); + }; + + const baseModalOptions = Object.assign(Object.create(null), { + addClass: "modalDialog", + collapsible: false, + cornerRadius: 5, + draggable: true, + footerHeight: 20, + icon: "images/qbittorrent-tray.svg", + loadMethod: "xhr", + maximizable: false, + method: "post", + minimizable: false, + padding: { + top: 15, + right: 10, + bottom: 15, + left: 5 + }, + resizable: true, + width: 480 + }); + + deepFreeze(baseModalOptions); + + return exports(); +})(); +Object.freeze(window.qBittorrent.Dialog); + const LocalPreferences = new window.qBittorrent.LocalPreferences.LocalPreferencesClass(); let saveWindowSize = function() {}; @@ -396,32 +443,43 @@ const initializeWindows = function() { deleteFN = function(forceDeleteFiles = false) { const hashes = torrentsTable.selectedRowsIds(); if (hashes.length > 0) { - window.qBittorrent.Client.closeWindow("confirmDeletionPage"); - - new MochaUI.Window({ - id: "confirmDeletionPage", - icon: "images/qbittorrent-tray.svg", - title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", - data: { - hashes: hashes, - forceDeleteFiles: forceDeleteFiles - }, - loadMethod: "xhr", - contentURL: "views/confirmdeletion.html", - maximizable: false, - collapsible: false, - padding: { - left: 5, - right: 10, - top: 15, - bottom: 15 - }, - width: 480, - onContentLoaded: function(w) { - MochaUI.resizeWindow(w, { centered: true }); - MochaUI.centerWindow(w); - } - }); + if (window.qBittorrent.Cache.preferences.get().confirm_torrent_deletion) { + new MochaUI.Modal({ + ...window.qBittorrent.Dialog.baseModalOptions, + id: "confirmDeletionPage", + title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", + data: { + hashes: hashes, + forceDeleteFiles: forceDeleteFiles + }, + contentURL: "views/confirmdeletion.html", + onContentLoaded: function(w) { + MochaUI.resizeWindow(w, { centered: true }); + MochaUI.centerWindow(w); + }, + onCloseComplete: function() { + // make sure overlay is properly hidden upon modal closing + document.getElementById("modalOverlay").style.display = "none"; + } + }); + } + else { + new Request({ + url: "api/v2/torrents/delete", + method: "post", + data: { + hashes: hashes.join("|"), + deleteFiles: forceDeleteFiles + }, + onSuccess: function() { + torrentsTable.deselectAll(); + updateMainData(); + }, + onFailure: function() { + alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]"); + } + }).send(); + } } }; @@ -745,29 +803,40 @@ const initializeWindows = function() { deleteTorrentsByCategoryFN = function(categoryHash) { const hashes = torrentsTable.getFilteredTorrentsHashes("all", categoryHash, TAGS_ALL, TRACKERS_ALL); if (hashes.length > 0) { - window.qBittorrent.Client.closeWindow("confirmDeletionPage"); - - new MochaUI.Window({ - id: "confirmDeletionPage", - icon: "images/qbittorrent-tray.svg", - title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", - data: { hashes: hashes }, - loadMethod: "xhr", - contentURL: "views/confirmdeletion.html", - maximizable: false, - collapsible: false, - padding: { - left: 5, - right: 10, - top: 15, - bottom: 15 - }, - width: 480, - onContentLoaded: function(w) { - MochaUI.resizeWindow(w, { centered: true }); - MochaUI.centerWindow(w); - } - }); + if (window.qBittorrent.Cache.preferences.get().confirm_torrent_deletion) { + new MochaUI.Modal({ + ...window.qBittorrent.Dialog.baseModalOptions, + id: "confirmDeletionPage", + title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", + data: { hashes: hashes }, + contentURL: "views/confirmdeletion.html", + onContentLoaded: function(w) { + MochaUI.resizeWindow(w, { centered: true }); + MochaUI.centerWindow(w); + }, + onCloseComplete: function() { + // make sure overlay is properly hidden upon modal closing + document.getElementById("modalOverlay").style.display = "none"; + } + }); + } + else { + new Request({ + url: "api/v2/torrents/delete", + method: "post", + data: { + hashes: hashes.join("|"), + deleteFiles: false, + }, + onSuccess: function() { + torrentsTable.deselectAll(); + updateMainData(); + }, + onFailure: function() { + alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]"); + } + }).send(); + } } }; @@ -899,29 +968,40 @@ const initializeWindows = function() { deleteTorrentsByTagFN = function(tagHash) { const hashes = torrentsTable.getFilteredTorrentsHashes("all", CATEGORIES_ALL, tagHash, TRACKERS_ALL); if (hashes.length > 0) { - window.qBittorrent.Client.closeWindow("confirmDeletionPage"); - - new MochaUI.Window({ - id: "confirmDeletionPage", - icon: "images/qbittorrent-tray.svg", - title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", - data: { hashes: hashes }, - loadMethod: "xhr", - contentURL: "views/confirmdeletion.html", - maximizable: false, - collapsible: false, - padding: { - left: 5, - right: 10, - top: 15, - bottom: 15 - }, - width: 480, - onContentLoaded: function(w) { - MochaUI.resizeWindow(w, { centered: true }); - MochaUI.centerWindow(w); - } - }); + if (window.qBittorrent.Cache.preferences.get().confirm_torrent_deletion) { + new MochaUI.Modal({ + ...window.qBittorrent.Dialog.baseModalOptions, + id: "confirmDeletionPage", + title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", + data: { hashes: hashes }, + contentURL: "views/confirmdeletion.html", + onContentLoaded: function(w) { + MochaUI.resizeWindow(w, { centered: true }); + MochaUI.centerWindow(w); + }, + onCloseComplete: function() { + // make sure overlay is properly hidden upon modal closing + document.getElementById("modalOverlay").style.display = "none"; + } + }); + } + else { + new Request({ + url: "api/v2/torrents/delete", + method: "post", + data: { + hashes: hashes.join("|"), + deleteFiles: false, + }, + onSuccess: function() { + torrentsTable.deselectAll(); + updateMainData(); + }, + onFailure: function() { + alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]"); + } + }).send(); + } } }; @@ -1013,32 +1093,44 @@ const initializeWindows = function() { } if (hashes.length > 0) { - window.qBittorrent.Client.closeWindow("confirmDeletionPage"); - - new MochaUI.Window({ - id: "confirmDeletionPage", - icon: "images/qbittorrent-tray.svg", - title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", - data: { - hashes: hashes, - filterList: "tracker" - }, - loadMethod: "xhr", - contentURL: "views/confirmdeletion.html", - maximizable: false, - collapsible: false, - padding: { - left: 5, - right: 10, - top: 15, - bottom: 15 - }, - width: 480, - onContentLoaded: function(w) { - MochaUI.resizeWindow(w, { centered: true }); - MochaUI.centerWindow(w); - } - }); + if (window.qBittorrent.Cache.preferences.get().confirm_torrent_deletion) { + new MochaUI.Modal({ + ...window.qBittorrent.Dialog.baseModalOptions, + id: "confirmDeletionPage", + title: "QBT_TR(Remove torrent(s))QBT_TR[CONTEXT=confirmDeletionDlg]", + data: { + hashes: hashes, + filterList: "tracker" + }, + contentURL: "views/confirmdeletion.html", + onContentLoaded: function(w) { + MochaUI.resizeWindow(w, { centered: true }); + MochaUI.centerWindow(w); + }, + onCloseComplete: function() { + // make sure overlay is properly hidden upon modal closing + document.getElementById("modalOverlay").style.display = "none"; + } + }); + } + else { + new Request({ + url: "api/v2/torrents/delete", + method: "post", + data: { + hashes: hashes.join("|"), + deleteFiles: false, + }, + onSuccess: function() { + torrentsTable.deselectAll(); + setTrackerFilter(TRACKERS_ALL); + updateMainData(); + }, + onFailure: function() { + alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]"); + }, + }).send(); + } } }; diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index 2e4b8695a..cb7766320 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -9,6 +9,10 @@