apply suggestions

This commit is contained in:
tehcneko 2025-04-12 14:06:39 +08:00
commit 4bc13937fb
2 changed files with 28 additions and 38 deletions

View file

@ -290,11 +290,6 @@
event.preventDefault(); event.preventDefault();
window.qBittorrent.Client.closeWindow(windowEl); window.qBittorrent.Client.closeWindow(windowEl);
}); });
// synchronize header scrolling to table body
$("bulkRenameFilesTableDiv").onscroll = function() {
const length = $(this).scrollLeft;
$("bulkRenameFilesTableFixedHeaderDiv").scrollLeft = length;
};
const handleTorrentFiles = (files, selectedRows) => { const handleTorrentFiles = (files, selectedRows) => {
const rows = files.map((file, index) => { const rows = files.map((file, index) => {

View file

@ -114,7 +114,7 @@ window.qBittorrent.DynamicTable ??= (() => {
}, },
setupCommonEvents: function() { setupCommonEvents: function() {
const tableFixedHeaderDiv = $(this.dynamicTableFixedHeaderDivId); const tableFixedHeaderDiv = document.getElementById(this.dynamicTableFixedHeaderDivId);
const tableElement = tableFixedHeaderDiv.querySelector("table"); const tableElement = tableFixedHeaderDiv.querySelector("table");
this.dynamicTableDiv.addEventListener("scroll", (e) => { this.dynamicTableDiv.addEventListener("scroll", (e) => {
@ -733,10 +733,9 @@ window.qBittorrent.DynamicTable ??= (() => {
selectAll: function() { selectAll: function() {
this.deselectAll(); this.deselectAll();
for (const tr of this.getTrs())
tr.classList.add("selected");
for (const row of this.getFilteredAndSortedRows()) for (const row of this.getFilteredAndSortedRows())
this.selectedRows.push(row.rowId); this.selectedRows.push(row.rowId);
this.setRowClass();
}, },
deselectAll: function() { deselectAll: function() {
@ -866,10 +865,10 @@ window.qBittorrent.DynamicTable ??= (() => {
const trs = [...this.getTrs()]; const trs = [...this.getTrs()];
for (let rowPos = 0; rowPos < rows.length; ++rowPos) { for (let rowPos = 0; rowPos < rows.length; ++rowPos) {
const rowId = rows[rowPos]["rowId"]; const rowId = rows[rowPos].rowId;
let tr_found = false; let tr_found = false;
for (let j = rowPos; j < trs.length; ++j) { for (let j = rowPos; j < trs.length; ++j) {
if (trs[j]["rowId"] === rowId) { if (trs[j].rowId === rowId) {
tr_found = true; tr_found = true;
if (rowPos === j) if (rowPos === j)
break; break;
@ -896,9 +895,6 @@ window.qBittorrent.DynamicTable ??= (() => {
trs.splice(rowPos, 0, tr); trs.splice(rowPos, 0, tr);
} }
// Update context menu
this.contextMenu?.addTarget(tr);
this.updateRow(tr, true); this.updateRow(tr, true);
} }
} }
@ -914,9 +910,6 @@ window.qBittorrent.DynamicTable ??= (() => {
// set the scrollable height // set the scrollable height
this.table.style.height = `${rows.length * this.rowHeight}px`; this.table.style.height = `${rows.length * this.rowHeight}px`;
// remove existing children
this.tableBody.replaceChildren();
// show extra 6 rows at top/bottom to reduce flickering // show extra 6 rows at top/bottom to reduce flickering
const extraRowCount = 6; const extraRowCount = 6;
// how many rows can be shown in the visible area // how many rows can be shown in the visible area
@ -937,17 +930,16 @@ window.qBittorrent.DynamicTable ??= (() => {
const position = i - startRow; const position = i - startRow;
// reuse existing elements // reuse existing elements
let element = this.cachedElements[position]; let element = this.cachedElements[position];
if (element !== undefined) { if (element !== undefined)
this.updateRowElement(element, row["rowId"], offset); this.updateRowElement(element, row.rowId, offset);
} else
else {
element = this.cachedElements[position] = this.createRowElement(row, offset); element = this.cachedElements[position] = this.createRowElement(row, offset);
// update context menu
this.contextMenu?.addTarget(element);
}
elements.push(element); elements.push(element);
} }
this.tableBody.append(...elements); this.tableBody.replaceChildren(...elements);
// update row classes
this.setRowClass();
// update visible rows // update visible rows
for (const row of this.tableBody.children) for (const row of this.tableBody.children)
@ -980,21 +972,22 @@ window.qBittorrent.DynamicTable ??= (() => {
tr.append(td); tr.append(td);
} }
this.updateRowElement(tr, row["rowId"], top); this.updateRowElement(tr, row.rowId, top);
// update context menu
this.contextMenu?.addTarget(tr);
return tr; return tr;
}, },
updateRowElement(tr, rowId, top) { updateRowElement(tr, rowId, top) {
tr.setAttribute("data-row-id", rowId); tr.dataset.rowId = rowId;
tr["rowId"] = rowId; tr.rowId = rowId;
tr.className = ""; tr.className = "";
if (this.useVirtualList) { if (this.useVirtualList) {
tr.style.position = "absolute"; tr.style.position = "absolute";
tr.style.top = `${top}px`; tr.style.top = `${top}px`;
if (this.selectedRows.contains(rowId))
tr.classList.add("selected");
} }
}, },
@ -2360,7 +2353,7 @@ window.qBittorrent.DynamicTable ??= (() => {
checkbox.type = "checkbox"; checkbox.type = "checkbox";
checkbox.className = "RenamingCB"; checkbox.className = "RenamingCB";
checkbox.addEventListener("click", (e) => { checkbox.addEventListener("click", (e) => {
const id = e.target.getAttribute("data-id"); const id = e.target.dataset.id;
const node = that.getNode(id); const node = that.getNode(id);
node.checked = e.target.checked ? 0 : 1; node.checked = e.target.checked ? 0 : 1;
node.full_data.checked = node.checked; node.full_data.checked = node.checked;
@ -2372,7 +2365,7 @@ window.qBittorrent.DynamicTable ??= (() => {
td.append(checkbox); td.append(checkbox);
} }
checkbox.id = `cbRename${id}`; checkbox.id = `cbRename${id}`;
checkbox.setAttribute("data-id", id); checkbox.dataset.id = id;
checkbox.checked = (node.checked === 0); checkbox.checked = (node.checked === 0);
checkbox.state = checkbox.checked ? "checked" : "unchecked"; checkbox.state = checkbox.checked ? "checked" : "unchecked";
}; };
@ -2569,11 +2562,13 @@ window.qBittorrent.DynamicTable ??= (() => {
}, },
setupCommonEvents: function() { setupCommonEvents: function() {
if (this.useVirtualList) { const headerDiv = document.getElementById("bulkRenameFilesTableFixedHeaderDiv");
this.dynamicTableDiv.addEventListener("scroll", (e) => { this.dynamicTableDiv.addEventListener("scroll", (e) => {
headerDiv.scrollLeft = this.dynamicTableDiv.scrollLeft;
// rerender on scroll
if (this.useVirtualList)
this.rerender(); this.rerender();
}); });
}
} }
}); });
@ -2807,7 +2802,7 @@ window.qBittorrent.DynamicTable ??= (() => {
collapseIcon.src = "images/go-down.svg"; collapseIcon.src = "images/go-down.svg";
collapseIcon.className = "filesTableCollapseIcon"; collapseIcon.className = "filesTableCollapseIcon";
collapseIcon.addEventListener("click", (e) => { collapseIcon.addEventListener("click", (e) => {
const id = collapseIcon.getAttribute("data-id"); const id = collapseIcon.dataset.id;
const node = that.getNode(id); const node = that.getNode(id);
if (node) { if (node) {
if (that.isCollapsed(node.rowId)) if (that.isCollapsed(node.rowId))
@ -2823,7 +2818,7 @@ window.qBittorrent.DynamicTable ??= (() => {
if (node.isFolder) { if (node.isFolder) {
collapseIcon.style.marginLeft = `${node.depth * 20}px`; collapseIcon.style.marginLeft = `${node.depth * 20}px`;
collapseIcon.style.display = "inline"; collapseIcon.style.display = "inline";
collapseIcon.setAttribute("data-id", id); collapseIcon.dataset.id = id;
collapseIcon.classList.toggle("rotate", that.isCollapsed(node.rowId)); collapseIcon.classList.toggle("rotate", that.isCollapsed(node.rowId));
} }
else { else {
@ -2865,7 +2860,7 @@ window.qBittorrent.DynamicTable ??= (() => {
const id = row.rowId; const id = row.rowId;
const value = Number(this.getRowValue(row)); const value = Number(this.getRowValue(row));
const progressBar = td.firstElementChild;; const progressBar = td.firstElementChild;
if (progressBar === null) { if (progressBar === null) {
td.append(new window.qBittorrent.ProgressBar.ProgressBar(value, { td.append(new window.qBittorrent.ProgressBar.ProgressBar(value, {
width: 80 width: 80