Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WebUI: Replace getElements & getChildren #22220

Merged
merged 3 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/webui/www/private/scripts/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,9 @@ window.addEventListener("DOMContentLoaded", () => {
const categoryList = document.getElementById("categoryFilterList");
if (!categoryList)
return;
categoryList.getChildren().each(c => c.destroy());

for (const category of categoryList.querySelectorAll("li"))
Chocobo1 marked this conversation as resolved.
Show resolved Hide resolved
category.destroy();

const categoryItemTemplate = document.getElementById("categoryFilterItem");

Expand Down Expand Up @@ -611,7 +613,8 @@ window.addEventListener("DOMContentLoaded", () => {
if (tagFilterList === null)
return;

tagFilterList.getChildren().each(c => c.destroy());
for (const tag of tagFilterList.querySelectorAll("li"))
tag.destroy();

const tagItemTemplate = document.getElementById("tagFilterItem");

Expand Down Expand Up @@ -664,7 +667,8 @@ window.addEventListener("DOMContentLoaded", () => {
if (trackerFilterList === null)
return;

trackerFilterList.getChildren().each(c => c.destroy());
for (const tracker of trackerFilterList.querySelectorAll("li"))
tracker.destroy();

const trackerItemTemplate = document.getElementById("trackerFilterItem");

Expand Down
7 changes: 4 additions & 3 deletions src/webui/www/private/scripts/contextmenu.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,8 @@ window.qBittorrent.ContextMenu ??= (() => {

updateCategoriesSubMenu(categories) {
const contextCategoryList = $("contextCategoryList");
contextCategoryList.getChildren().each(c => c.destroy());
for (const category of contextCategoryList.querySelectorAll("li"))
category.destroy();

const createMenuItem = (text, imgURL, clickFn) => {
const anchor = document.createElement("a");
Expand Down Expand Up @@ -633,15 +634,15 @@ window.qBittorrent.ContextMenu ??= (() => {
class SearchPluginsTableContextMenu extends ContextMenu {
updateMenuItems() {
const enabledColumnIndex = (text) => {
const columns = $("searchPluginsTableFixedHeaderRow").getChildren("th");
const columns = document.querySelectorAll("#searchPluginsTableFixedHeaderRow th");
for (let i = 0; i < columns.length; ++i) {
if (columns[i].textContent === "Enabled")
return i;
Chocobo1 marked this conversation as resolved.
Show resolved Hide resolved
}
};

this.showItem("Enabled");
this.setItemChecked("Enabled", (this.options.element.getChildren("td")[enabledColumnIndex()].textContent === "Yes"));
this.setItemChecked("Enabled", (this.options.element.children[enabledColumnIndex()].textContent === "Yes"));

this.showItem("Uninstall");
}
Expand Down
91 changes: 43 additions & 48 deletions src/webui/www/private/scripts/dynamicTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ window.qBittorrent.DynamicTable ??= (() => {
this.dynamicTableDivId = dynamicTableDivId;
this.dynamicTableFixedHeaderDivId = dynamicTableFixedHeaderDivId;
this.dynamicTableDiv = document.getElementById(dynamicTableDivId);
this.fixedTableHeader = $(dynamicTableFixedHeaderDivId).getElements("tr")[0];
this.hiddenTableHeader = $(dynamicTableDivId).getElements("tr")[0];
this.tableBody = $(dynamicTableDivId).getElements("tbody")[0];
this.fixedTableHeader = document.querySelector(`#${dynamicTableFixedHeaderDivId} thead tr`);
this.hiddenTableHeader = this.dynamicTableDiv.querySelector(`thead tr`);
this.tableBody = this.dynamicTableDiv.querySelector(`tbody`);
this.rows = new Map();
this.selectedRows = [];
this.columns = [];
Expand Down Expand Up @@ -330,10 +330,7 @@ window.qBittorrent.DynamicTable ??= (() => {
}
}.bind(this);

const ths = this.fixedTableHeader.getElements("th");

for (let i = 0; i < ths.length; ++i) {
const th = ths[i];
for (const th of this.getRowCells(this.fixedTableHeader)) {
th.addEventListener("mousemove", mouseMoveFn);
th.addEventListener("mouseout", mouseOutFn);
th.addEventListener("touchend", onTouch, { passive: true });
Expand Down Expand Up @@ -382,8 +379,8 @@ window.qBittorrent.DynamicTable ??= (() => {
context.font = window.getComputedStyle(bodyColumn, null).getPropertyValue("font");

const longestTd = { value: "", width: 0 };
for (const tr of this.tableBody.querySelectorAll("tr")) {
const tds = tr.querySelectorAll("td");
for (const tr of this.getTrs()) {
const tds = this.getRowCells(tr);
const td = tds[columnIndex];

const buffer = column.calculateBuffer(tr.rowId);
Expand Down Expand Up @@ -606,8 +603,7 @@ window.qBittorrent.DynamicTable ??= (() => {
},

updateHeader: function(header) {
const ths = header.getElements("th");

const ths = this.getRowCells(header);
for (let i = 0; i < ths.length; ++i) {
const th = ths[i];
th._this = this;
Expand Down Expand Up @@ -708,10 +704,7 @@ window.qBittorrent.DynamicTable ??= (() => {

selectAll: function() {
this.deselectAll();

const trs = this.tableBody.getElements("tr");
for (let i = 0; i < trs.length; ++i) {
const tr = trs[i];
for (const tr of this.getTrs()) {
this.selectedRows.push(tr.rowId);
tr.classList.add("selected");
}
Expand Down Expand Up @@ -741,31 +734,30 @@ window.qBittorrent.DynamicTable ??= (() => {
}

let select = false;
const that = this;
this.tableBody.getElements("tr").each((tr) => {
for (const tr of this.getTrs()) {
if ((tr.rowId === rowId1) || (tr.rowId === rowId2)) {
select = !select;
that.selectedRows.push(tr.rowId);
this.selectedRows.push(tr.rowId);
}
else if (select) {
that.selectedRows.push(tr.rowId);
this.selectedRows.push(tr.rowId);
}
});
}
this.setRowClass();
this.onSelectedRowChanged();
},

reselectRows: function(rowIds) {
this.deselectAll();
this.selectedRows = rowIds.slice();
this.tableBody.getElements("tr").each((tr) => {
for (const tr of this.getTrs()) {
if (rowIds.includes(tr.rowId))
tr.classList.add("selected");
});
}
},

setRowClass: function() {
for (const tr of this.tableBody.querySelectorAll("tr"))
for (const tr of this.getTrs())
tr.classList.toggle("selected", this.isRowSelected(tr.rowId));
},

Expand Down Expand Up @@ -827,10 +819,9 @@ window.qBittorrent.DynamicTable ??= (() => {
},

getTrByRowId: function(rowId) {
const trs = this.tableBody.getElements("tr");
for (let i = 0; i < trs.length; ++i) {
if (trs[i].rowId === rowId)
return trs[i];
for (const tr of this.getTrs()) {
Chocobo1 marked this conversation as resolved.
Show resolved Hide resolved
if (tr.rowId === rowId)
return tr;
}
return null;
},
Expand All @@ -845,7 +836,7 @@ window.qBittorrent.DynamicTable ??= (() => {
}
}

const trs = this.tableBody.getElements("tr");
const trs = [...this.getTrs()];

for (let rowPos = 0; rowPos < rows.length; ++rowPos) {
const rowId = rows[rowPos]["rowId"];
Expand Down Expand Up @@ -927,9 +918,8 @@ window.qBittorrent.DynamicTable ??= (() => {
clear: function() {
this.deselectAll();
this.rows.clear();
const trs = this.tableBody.getElements("tr");
while (trs.length > 0)
trs.pop().destroy();
for (const tr of this.getTrs())
tr.destroy();
},

selectedRowsIds: function() {
Expand All @@ -953,7 +943,11 @@ window.qBittorrent.DynamicTable ??= (() => {
},

selectNextRow: function() {
const visibleRows = $(this.dynamicTableDivId).getElements("tbody tr").filter(e => e.style.display !== "none");
const visibleRows = [];
for (const tr of this.getTrs()) {
if (!tr.classList.contains("invisible") && (tr.style.display !== "none"))
visibleRows.push(tr);
}
Chocobo1 marked this conversation as resolved.
Show resolved Hide resolved
const selectedRowId = this.getSelectedRowId();

let selectedIndex = -1;
Expand All @@ -975,7 +969,11 @@ window.qBittorrent.DynamicTable ??= (() => {
},

selectPreviousRow: function() {
const visibleRows = $(this.dynamicTableDivId).getElements("tbody tr").filter(e => e.style.display !== "none");
const visibleRows = [];
for (const tr of this.getTrs()) {
if (!tr.classList.contains("invisible") && (tr.style.display !== "none"))
visibleRows.push(tr);
}
Chocobo1 marked this conversation as resolved.
Show resolved Hide resolved
const selectedRowId = this.getSelectedRowId();

let selectedIndex = -1;
Expand Down Expand Up @@ -1249,8 +1247,8 @@ window.qBittorrent.DynamicTable ??= (() => {
if ((progressFormatted === 100.0) && (progress !== 1.0))
progressFormatted = 99.9;

if (td.getChildren("div").length > 0) {
const div = td.getChildren("div")[0];
const div = td.firstElementChild;
if (div !== null) {
if (td.resized) {
td.resized = false;
div.setWidth(progressColumnWidth - 5);
Expand All @@ -1270,14 +1268,13 @@ window.qBittorrent.DynamicTable ??= (() => {
this.columns["progress"].staticWidth = 100;
this.columns["progress"].onResize = function(columnName) {
const pos = this.getColumnPos(columnName);
const trs = this.tableBody.getElements("tr");
progressColumnWidth = -1;
for (let i = 0; i < trs.length; ++i) {
const td = trs[i].getElements("td")[pos];
for (const tr of this.getTrs()) {
const td = this.getRowCells(tr)[pos];
if (progressColumnWidth < 0)
progressColumnWidth = td.offsetWidth;
td.resized = true;
this.columns[columnName].updateTd(td, this.rows.get(trs[i].rowId));
this.columns[columnName].updateTd(td, this.getRow(tr.rowId));
}
}.bind(this);

Expand Down Expand Up @@ -2312,11 +2309,10 @@ window.qBittorrent.DynamicTable ??= (() => {
},

reselectRows: function(rowIds) {
const that = this;
this.deselectAll();
this.tableBody.getElements("tr").each((tr) => {
for (const tr of this.getTrs()) {
if (rowIds.includes(tr.rowId)) {
const node = that.getNode(tr.rowId);
const node = this.getNode(tr.rowId);
node.checked = 0;
node.full_data.checked = 0;

Expand All @@ -2325,8 +2321,7 @@ window.qBittorrent.DynamicTable ??= (() => {
checkbox.indeterminate = false;
checkbox.checked = true;
}
});

}
this.updateGlobalCheckbox();
},

Expand Down Expand Up @@ -2838,7 +2833,7 @@ window.qBittorrent.DynamicTable ??= (() => {
const row = this.rows.get(tr.rowId);
const data = row[fullUpdate ? "full_data" : "data"];

const tds = tr.getElements("td");
const tds = this.getRowCells(tr);
for (let i = 0; i < this.columns.length; ++i) {
if (Object.hasOwn(data, this.columns[i].dataProperties[0]))
this.columns[i].updateTd(tds[i], row);
Expand Down Expand Up @@ -2975,7 +2970,7 @@ window.qBittorrent.DynamicTable ??= (() => {
const data = row[fullUpdate ? "full_data" : "data"];
tr.classList.toggle("unreadArticle", !row.full_data.isRead);

const tds = tr.getElements("td");
const tds = this.getRowCells(tr);
for (let i = 0; i < this.columns.length; ++i) {
if (Object.hasOwn(data, this.columns[i].dataProperties[0]))
this.columns[i].updateTd(tds[i], row);
Expand Down Expand Up @@ -3251,7 +3246,7 @@ window.qBittorrent.DynamicTable ??= (() => {
tr.classList.add("articleTableArticle");
}

const tds = tr.getElements("td");
const tds = this.getRowCells(tr);
for (let i = 0; i < this.columns.length; ++i) {
if (Object.hasOwn(data, this.columns[i].dataProperties[0]))
this.columns[i].updateTd(tds[i], row);
Expand All @@ -3266,7 +3261,7 @@ window.qBittorrent.DynamicTable ??= (() => {
filterText: "",

filteredLength: function() {
return this.tableBody.getElements("tr").length;
return this.tableBody.rows.length;
},

initColumns: function() {
Expand Down
2 changes: 1 addition & 1 deletion src/webui/www/private/scripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ window.qBittorrent.Search ??= (() => {
};

const numSearchTabs = () => {
return $("searchTabs").getElements("li").length;
return document.querySelectorAll("#searchTabs li").length;
};

const getSearchIdFromTab = (tab) => {
Expand Down
8 changes: 6 additions & 2 deletions src/webui/www/private/views/preferences.html
Original file line number Diff line number Diff line change
Expand Up @@ -2102,7 +2102,9 @@

// Advanced Tab
const updateNetworkInterfaces = (default_iface, default_iface_name) => {
$("networkInterface").getChildren().each(c => c.destroy());
for (const option of document.querySelectorAll("#networkInterface option"))
option.destroy();

fetch("api/v2/app/networkInterfaceList", {
method: "GET",
cache: "no-store"
Expand Down Expand Up @@ -2130,7 +2132,9 @@
};

const updateInterfaceAddresses = (iface, default_addr) => {
$("optionalIPAddressToBind").getChildren().each(c => c.destroy());
for (const option of document.querySelectorAll("#optionalIPAddressToBind option"))
option.destroy();

const url = new URL("api/v2/app/networkInterfaceAddressList", window.location);
url.search = new URLSearchParams({
iface: iface
Expand Down
10 changes: 8 additions & 2 deletions src/webui/www/private/views/rss.html
Original file line number Diff line number Diff line change
Expand Up @@ -417,13 +417,19 @@
});
});

$("rssDetailsView").getChildren().each(c => c.destroy());
clearDetails();
rssArticleTable.updateTable(false);
};

const clearDetails = () => {
for (const element of [...document.getElementById("rssDetailsView").children])
element.destroy();
};

const showDetails = (feedUid, articleID) => {
markArticleAsRead(pathByFeedId.get(feedUid), articleID);
$("rssDetailsView").getChildren().each(c => c.destroy());
clearDetails();

const article = feedData[feedUid].filter((article) => article.id === articleID)[0];
if (article) {
$("rssDetailsView").append((() => {
Expand Down
Loading