Skip to content

Commit

Permalink
WebUI: use native function for selecting elements
Browse files Browse the repository at this point in the history
PR #22179.
  • Loading branch information
Chocobo1 authored Jan 20, 2025
1 parent 1ee8403 commit f8c4834
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/webui/www/private/rename_files.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
bulkRenameFilesTable.setup("bulkRenameFilesTableDiv", "bulkRenameFilesTableFixedHeaderDiv", bulkRenameFilesContextMenu);

// Inject checkbox into the first column of the table header
const tableHeaders = $$("#bulkRenameFilesTableFixedHeaderDiv .dynamicTableHeader th");
const tableHeaders = document.querySelectorAll("#bulkRenameFilesTableFixedHeaderDiv .dynamicTableHeader th");
if (tableHeaders.length > 0) {
const checkboxHeader = document.createElement("input");
checkboxHeader.type = "checkbox";
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 @@ -67,7 +67,9 @@ window.qBittorrent.ContextMenu ??= (() => {

// option diffs menu
this.menu = $(this.options.menu);
this.targets = $$(this.options.targets);
this.targets = (this.options.targets.length > 0)
? Array.from(document.querySelectorAll(this.options.targets))
: [];

// fx
this.fx = new Fx.Tween(this.menu, {
Expand Down Expand Up @@ -197,9 +199,8 @@ window.qBittorrent.ContextMenu ??= (() => {
// get things started
startListener() {
/* all elements */
this.targets.each((el) => {
for (const el of this.targets)
this.setupEventListeners(el);
}, this);

/* menu items */
this.menu.addEventListener("click", (e) => {
Expand Down
24 changes: 6 additions & 18 deletions src/webui/www/private/scripts/dynamicTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ window.qBittorrent.DynamicTable ??= (() => {

setSortedColumnIcon: function(newColumn, oldColumn, isReverse) {
const getCol = (headerDivId, colName) => {
const colElem = $$(`#${headerDivId} .column_${colName}`);
const colElem = document.querySelectorAll(`#${headerDivId} .column_${colName}`);
if (colElem.length === 1)
return colElem[0];
return null;
Expand Down Expand Up @@ -2140,7 +2140,7 @@ window.qBittorrent.DynamicTable ??= (() => {
*/
toggleGlobalCheckbox: function() {
const checkbox = $("rootMultiRename_cb");
const checkboxes = $$("input.RenamingCB");
const checkboxes = document.querySelectorAll("input.RenamingCB");

for (let i = 0; i < checkboxes.length; ++i) {
const node = this.getNode(i);
Expand Down Expand Up @@ -2180,22 +2180,10 @@ window.qBittorrent.DynamicTable ??= (() => {

updateGlobalCheckbox: () => {
const checkbox = $("rootMultiRename_cb");
const checkboxes = $$("input.RenamingCB");
const isAllChecked = () => {
for (let i = 0; i < checkboxes.length; ++i) {
if (!checkboxes[i].checked)
return false;
}
return true;
};
const isAllUnchecked = () => {
for (let i = 0; i < checkboxes.length; ++i) {
if (checkboxes[i].checked)
return false;
}
return true;
};
if (isAllChecked()) {
const checkboxes = document.querySelectorAll("input.RenamingCB");
const isAllChecked = Array.prototype.every.call(checkboxes, (checkbox => checkbox.checked));
const isAllUnchecked = (() => Array.prototype.every.call(checkboxes, (checkbox => !checkbox.checked)));
if (isAllChecked) {
checkbox.state = "checked";
checkbox.indeterminate = false;
checkbox.checked = true;
Expand Down
4 changes: 2 additions & 2 deletions src/webui/www/private/scripts/mocha-init.js
Original file line number Diff line number Diff line change
Expand Up @@ -1313,10 +1313,10 @@ const initializeWindows = () => {
});

// Deactivate menu header links
$$("a.returnFalse").each((el) => {
for (const el of document.querySelectorAll("a.returnFalse")) {
el.addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
});
});
}
};
20 changes: 3 additions & 17 deletions src/webui/www/private/scripts/prop-files.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,23 +283,9 @@ window.qBittorrent.PropFiles ??= (() => {
checkbox.indeterminate = true;
};

const isAllCheckboxesChecked = () => {
const checkboxes = $$("input.DownloadedCB");
for (let i = 0; i < checkboxes.length; ++i) {
if (!checkboxes[i].checked)
return false;
}
return true;
};
const isAllCheckboxesChecked = () => Array.prototype.every.call(document.querySelectorAll("input.DownloadedCB"), (checkbox => checkbox.checked));

const isAllCheckboxesUnchecked = () => {
const checkboxes = $$("input.DownloadedCB");
for (let i = 0; i < checkboxes.length; ++i) {
if (checkboxes[i].checked)
return false;
}
return true;
};
const isAllCheckboxesUnchecked = () => Array.prototype.every.call(document.querySelectorAll("input.DownloadedCB"), (checkbox => !checkbox.checked));

const setFilePriority = (ids, fileIds, priority) => {
if (current_hash === "")
Expand Down Expand Up @@ -630,7 +616,7 @@ window.qBittorrent.PropFiles ??= (() => {

torrentFilesTable.setup("torrentFilesTableDiv", "torrentFilesTableFixedHeaderDiv", torrentFilesContextMenu);
// inject checkbox into table header
const tableHeaders = $$("#torrentFilesTableFixedHeaderDiv .dynamicTableHeader th");
const tableHeaders = document.querySelectorAll("#torrentFilesTableFixedHeaderDiv .dynamicTableHeader th");
if (tableHeaders.length > 0) {
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
Expand Down
12 changes: 6 additions & 6 deletions src/webui/www/private/views/aboutToolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,32 @@
MochaUI.initializeTabs("aboutTabs");

$("aboutAboutLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutAboutContent").classList.remove("invisible");
});

$("aboutAuthorLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutAuthorContent").classList.remove("invisible");
});

$("aboutSpecialThanksLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutSpecialThanksContent").classList.remove("invisible");
});

$("aboutTranslatorsLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutTranslatorsContent").classList.remove("invisible");
});

$("aboutLicenseLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutLicenseContent").classList.remove("invisible");
});

$("aboutSoftwareUsedLink").addEventListener("click", () => {
$$(".aboutTabContent").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".aboutTabContent"), (tab => tab.classList.add("invisible")));
$("aboutSoftwareUsedContent").classList.remove("invisible");
});
})();
Expand Down
16 changes: 8 additions & 8 deletions src/webui/www/private/views/preferencesToolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,35 +37,35 @@
MochaUI.initializeTabs("preferencesTabs");

$("PrefBehaviorLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("BehaviorTab").classList.remove("invisible");
});
$("PrefDownloadsLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("DownloadsTab").classList.remove("invisible");
});
$("PrefConnectionLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("ConnectionTab").classList.remove("invisible");
});
$("PrefSpeedLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("SpeedTab").classList.remove("invisible");
});
$("PrefBittorrentLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("BittorrentTab").classList.remove("invisible");
});
$("PrefRSSLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("RSSTab").classList.remove("invisible");
});
$("PrefWebUILink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("WebUITab").classList.remove("invisible");
});
$("PrefAdvancedLink").addEventListener("click", (e) => {
$$(".PrefTab").forEach(tab => { tab.classList.add("invisible"); });
Array.prototype.forEach.call(document.querySelectorAll(".PrefTab"), (tab => tab.classList.add("invisible")));
$("AdvancedTab").classList.remove("invisible");
});
})();
Expand Down

0 comments on commit f8c4834

Please sign in to comment.