From 00d0725ce10694c04463278f940f651042c3d2d4 Mon Sep 17 00:00:00 2001 From: skomerko <168652295+skomerko@users.noreply.github.com> Date: Tue, 3 Dec 2024 11:06:53 +0100 Subject: [PATCH] WebUI: Use classlist.toggle() whenever possible --- src/webui/www/private/scripts/contextmenu.js | 5 +--- src/webui/www/private/scripts/dynamicTable.js | 27 +++++-------------- src/webui/www/private/scripts/prop-files.js | 10 ++----- 3 files changed, 9 insertions(+), 33 deletions(-) diff --git a/src/webui/www/private/scripts/contextmenu.js b/src/webui/www/private/scripts/contextmenu.js index 368f048d1..d86c7e50a 100644 --- a/src/webui/www/private/scripts/contextmenu.js +++ b/src/webui/www/private/scripts/contextmenu.js @@ -415,10 +415,7 @@ window.qBittorrent.ContextMenu ??= (() => { const show_seq_dl = (all_are_seq_dl || !there_are_seq_dl); const show_f_l_piece_prio = (all_are_f_l_piece_prio || !there_are_f_l_piece_prio); - if (!show_seq_dl && show_f_l_piece_prio) - this.menu.getElement("a[href$=firstLastPiecePrio]").parentNode.classList.add("separator"); - else - this.menu.getElement("a[href$=firstLastPiecePrio]").parentNode.classList.remove("separator"); + this.menu.getElement("a[href$=firstLastPiecePrio]").parentNode.classList.toggle("separator", !show_seq_dl && show_f_l_piece_prio); if (show_seq_dl) this.showItem("sequentialDownload"); diff --git a/src/webui/www/private/scripts/dynamicTable.js b/src/webui/www/private/scripts/dynamicTable.js index 4122ffe44..ae347c789 100644 --- a/src/webui/www/private/scripts/dynamicTable.js +++ b/src/webui/www/private/scripts/dynamicTable.js @@ -598,10 +598,7 @@ window.qBittorrent.DynamicTable ??= (() => { th.setAttribute("style", "width: " + this.columns[i].width + "px;" + this.columns[i].style); th.columnName = this.columns[i].name; th.classList.add("column_" + th.columnName); - if ((this.columns[i].visible === "0") || this.columns[i].force_hide) - th.classList.add("invisible"); - else - th.classList.remove("invisible"); + th.classList.toggle("invisible", (this.columns[i].visible === "0") || this.columns[i].force_hide); } }, @@ -676,10 +673,7 @@ window.qBittorrent.DynamicTable ??= (() => { const colElem = getCol(this.dynamicTableFixedHeaderDivId, newColumn); if (colElem !== null) { colElem.classList.add("sorted"); - if (isReverse) - colElem.classList.add("reverse"); - else - colElem.classList.remove("reverse"); + colElem.classList.toggle("reverse", isReverse); } const oldColElem = getCol(this.dynamicTableFixedHeaderDivId, oldColumn); if (oldColElem !== null) { @@ -711,8 +705,7 @@ window.qBittorrent.DynamicTable ??= (() => { for (let i = 0; i < trs.length; ++i) { const tr = trs[i]; this.selectedRows.push(tr.rowId); - if (!tr.classList.contains("selected")) - tr.classList.add("selected"); + tr.classList.add("selected"); } }, @@ -764,13 +757,8 @@ window.qBittorrent.DynamicTable ??= (() => { }, setRowClass: function() { - const that = this; - this.tableBody.getElements("tr").each((tr) => { - if (that.isRowSelected(tr.rowId)) - tr.classList.add("selected"); - else - tr.classList.remove("selected"); - }); + for (const tr of this.tableBody.querySelectorAll("tr")) + tr.classList.toggle("selected", this.isRowSelected(tr.rowId)); }, onSelectedRowChanged: () => {}, @@ -2987,10 +2975,7 @@ window.qBittorrent.DynamicTable ??= (() => { updateRow: function(tr, fullUpdate) { const row = this.rows.get(tr.rowId); const data = row[fullUpdate ? "full_data" : "data"]; - if (!row.full_data.isRead) - tr.classList.add("unreadArticle"); - else - tr.classList.remove("unreadArticle"); + tr.classList.toggle("unreadArticle", !row.full_data.isRead); const tds = tr.getElements("td"); for (let i = 0; i < this.columns.length; ++i) { diff --git a/src/webui/www/private/scripts/prop-files.js b/src/webui/www/private/scripts/prop-files.js index 3ca24619f..0edf84392 100644 --- a/src/webui/www/private/scripts/prop-files.js +++ b/src/webui/www/private/scripts/prop-files.js @@ -668,10 +668,7 @@ window.qBittorrent.PropFiles ??= (() => { if (span === null) return; const rowElem = span.parentElement.parentElement; - if (shouldHide) - rowElem.classList.add("invisible"); - else - rowElem.classList.remove("invisible"); + rowElem.classList.toggle("invisible", shouldHide); }; /** @@ -689,10 +686,7 @@ window.qBittorrent.PropFiles ??= (() => { // rotate the collapse icon const collapseIcon = td.getElementsByClassName("filesTableCollapseIcon")[0]; - if (isCollapsed) - collapseIcon.classList.add("rotate"); - else - collapseIcon.classList.remove("rotate"); + collapseIcon.classList.toggle("rotate", isCollapsed); }; const _isCollapsed = (node) => {