WebUI: Globally expose loaded categories and tags

This allows iframes to access these values without having to re-fetch them.
This commit is contained in:
Thomas Piccirello 2025-06-27 22:43:15 -07:00
parent e525375248
commit 2f974fac7c
No known key found for this signature in database
4 changed files with 31 additions and 30 deletions

View file

@ -42,10 +42,17 @@ window.qBittorrent.Client ??= (() => {
showLogViewer: showLogViewer,
isShowSearchEngine: isShowSearchEngine,
isShowRssReader: isShowRssReader,
isShowLogViewer: isShowLogViewer
isShowLogViewer: isShowLogViewer,
categoryMap: categoryMap,
tagMap: tagMap
};
};
// Map<category: String, {savePath: String, torrents: Set}>
const categoryMap = new Map();
// Map<tag: String, torrents: Set>
const tagMap = new Map();
let cacheAllSettled;
const setup = () => {
// fetch various data and store it in memory
@ -146,9 +153,6 @@ const displayFullURLTrackerColumn = LocalPreferences.get("full_url_tracker_colum
const CATEGORIES_ALL = "b4af0e4c-e76d-4bac-a392-46cbc18d9655";
const CATEGORIES_UNCATEGORIZED = "e24bd469-ea22-404c-8e2e-a17c82f37ea0";
// Map<category: String, {savePath: String, torrents: Set}>
const categoryMap = new Map();
let selectedCategory = LocalPreferences.get("selected_category", CATEGORIES_ALL);
let setCategoryFilter = () => {};
@ -156,9 +160,6 @@ let setCategoryFilter = () => {};
const TAGS_ALL = "b4af0e4c-e76d-4bac-a392-46cbc18d9655";
const TAGS_UNTAGGED = "e24bd469-ea22-404c-8e2e-a17c82f37ea0";
// Map<tag: String, torrents: Set>
const tagMap = new Map();
let selectedTag = LocalPreferences.get("selected_tag", TAGS_ALL);
let setTagFilter = () => {};
@ -389,7 +390,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
return false;
let removed = false;
for (const data of categoryMap.values()) {
for (const data of window.qBittorrent.Client.categoryMap.values()) {
const deleteResult = data.torrents.delete(hash);
removed ||= deleteResult;
}
@ -407,12 +408,12 @@ window.addEventListener("DOMContentLoaded", (event) => {
return true;
}
let categoryData = categoryMap.get(category);
let categoryData = window.qBittorrent.Client.categoryMap.get(category);
if (categoryData === undefined) { // This should not happen
categoryData = {
torrents: new Set()
};
categoryMap.set(category, categoryData);
window.qBittorrent.Client.categoryMap.set(category, categoryData);
}
if (categoryData.torrents.has(hash))
@ -428,7 +429,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
return false;
let removed = false;
for (const torrents of tagMap.values()) {
for (const torrents of window.qBittorrent.Client.tagMap.values()) {
const deleteResult = torrents.delete(hash);
removed ||= deleteResult;
}
@ -448,10 +449,10 @@ window.addEventListener("DOMContentLoaded", (event) => {
const tags = torrent["tags"].split(", ");
let added = false;
for (const tag of tags) {
let torrents = tagMap.get(tag);
let torrents = window.qBittorrent.Client.tagMap.get(tag);
if (torrents === undefined) { // This should not happen
torrents = new Set();
tagMap.set(tag, torrents);
window.qBittorrent.Client.tagMap.set(tag, torrents);
}
if (!torrents.has(hash)) {
@ -550,7 +551,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
}
const sortedCategories = [];
for (const [category, categoryData] of categoryMap) {
for (const [category, categoryData] of window.qBittorrent.Client.categoryMap) {
sortedCategories.push({
categoryName: category,
categoryCount: categoryData.torrents.size,
@ -651,7 +652,7 @@ window.addEventListener("DOMContentLoaded", (event) => {
tagFilterList.appendChild(createLink(TAGS_UNTAGGED, "QBT_TR(Untagged)QBT_TR[CONTEXT=TagFilterModel]", untagged));
const sortedTags = [];
for (const [tag, torrents] of tagMap) {
for (const [tag, torrents] of window.qBittorrent.Client.tagMap) {
sortedTags.push({
tagName: tag,
tagSize: torrents.size
@ -815,8 +816,8 @@ window.addEventListener("DOMContentLoaded", (event) => {
updateTrackers = true;
updateTorrents = true;
torrentsTable.clear();
categoryMap.clear();
tagMap.clear();
window.qBittorrent.Client.categoryMap.clear();
window.qBittorrent.Client.tagMap.clear();
trackerMap.clear();
}
if (responseJSON["rid"])
@ -827,9 +828,9 @@ window.addEventListener("DOMContentLoaded", (event) => {
continue;
const responseData = responseJSON["categories"][responseName];
const categoryData = categoryMap.get(responseName);
const categoryData = window.qBittorrent.Client.categoryMap.get(responseName);
if (categoryData === undefined) {
categoryMap.set(responseName, {
window.qBittorrent.Client.categoryMap.set(responseName, {
savePath: responseData.savePath,
torrents: new Set()
});
@ -843,19 +844,19 @@ window.addEventListener("DOMContentLoaded", (event) => {
}
if (responseJSON["categories_removed"]) {
for (const category of responseJSON["categories_removed"])
categoryMap.delete(category);
window.qBittorrent.Client.categoryMap.delete(category);
updateCategories = true;
}
if (responseJSON["tags"]) {
for (const tag of responseJSON["tags"]) {
if (!tagMap.has(tag))
tagMap.set(tag, new Set());
if (!window.qBittorrent.Client.tagMap.has(tag))
window.qBittorrent.Client.tagMap.set(tag, new Set());
}
updateTags = true;
}
if (responseJSON["tags_removed"]) {
for (const tag of responseJSON["tags_removed"])
tagMap.delete(tag);
window.qBittorrent.Client.tagMap.delete(tag);
updateTags = true;
}
if (responseJSON["trackers"]) {
@ -945,11 +946,11 @@ window.addEventListener("DOMContentLoaded", (event) => {
if (updateCategories) {
updateCategoryList();
window.qBittorrent.TransferList.contextMenu.updateCategoriesSubMenu(categoryMap);
window.qBittorrent.TransferList.contextMenu.updateCategoriesSubMenu(window.qBittorrent.Client.categoryMap);
}
if (updateTags) {
updateTagList();
window.qBittorrent.TransferList.contextMenu.updateTagsSubMenu(tagMap);
window.qBittorrent.TransferList.contextMenu.updateTagsSubMenu(window.qBittorrent.Client.tagMap);
}
if (updateTrackers)
updateTrackerList();

View file

@ -449,7 +449,7 @@ window.qBittorrent.ContextMenu ??= (() => {
this.setEnabled("copyInfohash2", thereAreV2Hashes);
const contextTagList = document.getElementById("contextTagList");
for (const tag of tagMap.keys()) {
for (const tag of window.qBittorrent.Client.tagMap.keys()) {
const checkbox = contextTagList.querySelector(`a[href="#Tag/${tag}"] input[type="checkbox"]`);
const count = tagCount.get(tag);
const hasCount = (count !== undefined);
@ -459,7 +459,7 @@ window.qBittorrent.ContextMenu ??= (() => {
}
const contextCategoryList = document.getElementById("contextCategoryList");
for (const category of categoryMap.keys()) {
for (const category of window.qBittorrent.Client.categoryMap.keys()) {
const categoryIcon = contextCategoryList.querySelector(`a[href$="#Category/${category}"] img`);
const count = categoryCount.get(category);
const isEqual = ((count !== undefined) && (count === selectedRows.length));

View file

@ -1630,7 +1630,7 @@ window.qBittorrent.DynamicTable ??= (() => {
return false;
}
else {
const selectedCategory = categoryMap.get(category);
const selectedCategory = window.qBittorrent.Client.categoryMap.get(category);
if (selectedCategory !== undefined) {
const selectedCategoryName = `${category}/`;
const torrentCategoryName = `${row["full_data"].category}/`;

View file

@ -977,7 +977,7 @@ const initializeWindows = () => {
deleteUnusedCategoriesFN = () => {
const categories = [];
for (const category of categoryMap.keys()) {
for (const category of window.qBittorrent.Client.categoryMap.keys()) {
if (torrentsTable.getFilteredTorrentsNumber("all", category, TAGS_ALL, TRACKERS_ALL) === 0)
categories.push(category);
}
@ -1082,7 +1082,7 @@ const initializeWindows = () => {
deleteUnusedTagsFN = () => {
const tags = [];
for (const tag of tagMap.keys()) {
for (const tag of window.qBittorrent.Client.tagMap.keys()) {
if (torrentsTable.getFilteredTorrentsNumber("all", CATEGORIES_ALL, tag, TRACKERS_ALL) === 0)
tags.push(tag);
}