WebUI: migrate to fetch API

This is the final part of it.

PR #22072.
This commit is contained in:
Chocobo1
2024-12-29 15:44:28 +08:00
committed by GitHub
parent e740a42366
commit 9c0475ebfa
15 changed files with 626 additions and 551 deletions

View File

@@ -360,13 +360,12 @@ const initializeWindows = () => {
toggleSequentialDownloadFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/toggleSequentialDownload",
method: "post",
data: {
fetch("api/v2/torrents/toggleSequentialDownload", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -374,13 +373,12 @@ const initializeWindows = () => {
toggleFirstLastPiecePrioFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/toggleFirstLastPiecePrio",
method: "post",
data: {
fetch("api/v2/torrents/toggleFirstLastPiecePrio", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -388,14 +386,13 @@ const initializeWindows = () => {
setSuperSeedingFN = (val) => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/setSuperSeeding",
method: "post",
data: {
value: val,
hashes: hashes.join("|")
}
}).send();
fetch("api/v2/torrents/setSuperSeeding", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
value: val
})
});
updateMainData();
}
};
@@ -403,14 +400,13 @@ const initializeWindows = () => {
setForceStartFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/setForceStart",
method: "post",
data: {
value: "true",
hashes: hashes.join("|")
}
}).send();
fetch("api/v2/torrents/setForceStart", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
value: "true"
})
});
updateMainData();
}
};
@@ -494,22 +490,23 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/delete",
method: "post",
data: {
hashes: hashes.join("|"),
deleteFiles: forceDeleteFiles
},
onSuccess: () => {
fetch("api/v2/torrents/delete", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
deleteFiles: forceDeleteFiles
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
torrentsTable.deselectAll();
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -523,13 +520,12 @@ const initializeWindows = () => {
stopFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -537,13 +533,12 @@ const initializeWindows = () => {
startFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -565,20 +560,21 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/setAutoManagement",
method: "post",
data: {
hashes: hashes.join("|"),
enable: enableAutoTMM
},
onSuccess: () => {
fetch("api/v2/torrents/setAutoManagement", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
enable: enableAutoTMM
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to set Auto Torrent Management for the selected torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
},
onFailure: () => {
alert("QBT_TR(Unable to set Auto Torrent Management for the selected torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -596,19 +592,20 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/recheck",
method: "post",
data: {
hashes: hashes.join("|"),
},
onSuccess: () => {
fetch("api/v2/torrents/recheck", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to recheck torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
},
onFailure: () => {
alert("QBT_TR(Unable to recheck torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -616,13 +613,12 @@ const initializeWindows = () => {
reannounceFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/reannounce",
method: "post",
data: {
hashes: hashes.join("|"),
}
}).send();
fetch("api/v2/torrents/reannounce", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
});
updateMainData();
}
};
@@ -703,40 +699,42 @@ const initializeWindows = () => {
startVisibleTorrentsFN = () => {
const hashes = torrentsTable.getFilteredTorrentsHashes(selectedStatus, selectedCategory, selectedTag, selectedTracker);
if (hashes.length > 0) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
hashes: hashes.join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to start torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to start torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
};
stopVisibleTorrentsFN = () => {
const hashes = torrentsTable.getFilteredTorrentsHashes(selectedStatus, selectedCategory, selectedTag, selectedTracker);
if (hashes.length > 0) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
hashes: hashes.join("|")
},
onSuccess: () => {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to stop torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to stop torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
};
@@ -760,22 +758,23 @@ const initializeWindows = () => {
});
}
else {
new Request({
url: "api/v2/torrents/delete",
method: "post",
data: {
hashes: hashes.join("|"),
deleteFiles: false,
},
onSuccess: () => {
fetch("api/v2/torrents/delete", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
deleteFiles: false,
})
})
.then((response) => {
if (!response.ok) {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
return;
}
torrentsTable.deselectAll();
updateMainData();
updatePropertiesPanel();
},
onFailure: () => {
alert("QBT_TR(Unable to delete torrents.)QBT_TR[CONTEXT=HttpServer]");
}
}).send();
});
}
}
};
@@ -809,17 +808,19 @@ const initializeWindows = () => {
const categoryName = category_list.has(categoryHash)
? category_list.get(categoryHash).name
: "";
new Request({
url: "api/v2/torrents/setCategory",
method: "post",
data: {
hashes: hashes.join("|"),
category: categoryName
},
onSuccess: () => {
fetch("api/v2/torrents/setCategory", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
category: categoryName
})
})
.then((response) => {
if (!response.ok)
return;
updateMainData();
}
}).send();
});
};
createCategoryFN = () => {
@@ -879,18 +880,19 @@ const initializeWindows = () => {
};
removeCategoryFN = (categoryHash) => {
const categoryName = category_list.get(categoryHash).name;
new Request({
url: "api/v2/torrents/removeCategories",
method: "post",
data: {
categories: categoryName
},
onSuccess: () => {
fetch("api/v2/torrents/removeCategories", {
method: "POST",
body: new URLSearchParams({
categories: category_list.get(categoryHash).name
})
})
.then((response) => {
if (!response.ok)
return;
setCategoryFilter(CATEGORIES_ALL);
updateMainData();
}
}).send();
});
};
deleteUnusedCategoriesFN = () => {
@@ -899,18 +901,19 @@ const initializeWindows = () => {
if (torrentsTable.getFilteredTorrentsNumber("all", hash, TAGS_ALL, TRACKERS_ALL) === 0)
categories.push(category.name);
});
fetch("api/v2/torrents/removeCategories", {
method: "POST",
body: new URLSearchParams({
categories: categories.join("\n")
})
})
.then((response) => {
if (!response.ok)
return;
new Request({
url: "api/v2/torrents/removeCategories",
method: "post",
data: {
categories: categories.join("\n")
},
onSuccess: () => {
setCategoryFilter(CATEGORIES_ALL);
updateMainData();
}
}).send();
});
};
torrentAddTagsFN = () => {
@@ -939,27 +942,24 @@ const initializeWindows = () => {
if (hashes.length <= 0)
return;
const tagName = tagList.has(tagHash) ? tagList.get(tagHash).name : "";
new Request({
url: (isSet ? "api/v2/torrents/addTags" : "api/v2/torrents/removeTags"),
method: "post",
data: {
fetch((isSet ? "api/v2/torrents/addTags" : "api/v2/torrents/removeTags"), {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|"),
tags: tagName,
}
}).send();
tags: (tagList.get(tagHash)?.name || "")
})
});
};
torrentRemoveAllTagsFN = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: ("api/v2/torrents/removeTags"),
method: "post",
data: {
hashes: hashes.join("|"),
}
}).send();
fetch("api/v2/torrents/removeTags", {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
})
});
}
};
@@ -983,14 +983,12 @@ const initializeWindows = () => {
};
removeTagFN = (tagHash) => {
const tagName = tagList.get(tagHash).name;
new Request({
url: "api/v2/torrents/deleteTags",
method: "post",
data: {
tags: tagName
}
}).send();
fetch("api/v2/torrents/deleteTags", {
method: "POST",
body: new URLSearchParams({
tags: tagList.get(tagHash).name
})
});
setTagFilter(TAGS_ALL);
};
@@ -1000,13 +998,12 @@ const initializeWindows = () => {
if (torrentsTable.getFilteredTorrentsNumber("all", CATEGORIES_ALL, hash, TRACKERS_ALL) === 0)
tags.push(tag.name);
});
new Request({
url: "api/v2/torrents/deleteTags",
method: "post",
data: {
fetch("api/v2/torrents/deleteTags", {
method: "POST",
body: new URLSearchParams({
tags: tags.join(",")
}
}).send();
})
});
setTagFilter(TAGS_ALL);
};
@@ -1130,13 +1127,12 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Would you like to stop all torrents?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/torrents/stop",
method: "post",
data: {
fetch("api/v2/torrents/stop", {
method: "POST",
body: new URLSearchParams({
hashes: "all"
}
}).send();
})
});
updateMainData();
}
});
@@ -1146,13 +1142,12 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Would you like to start all torrents?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/torrents/start",
method: "post",
data: {
fetch("api/v2/torrents/start", {
method: "POST",
body: new URLSearchParams({
hashes: "all"
}
}).send();
})
});
updateMainData();
}
});
@@ -1165,13 +1160,12 @@ const initializeWindows = () => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
hashes.each((hash, index) => {
new Request({
url: "api/v2/torrents/" + item,
method: "post",
data: {
fetch(`api/v2/torrents/${item}`, {
method: "POST",
body: new URLSearchParams({
hashes: hash
}
}).send();
})
});
});
updateMainData();
}
@@ -1189,13 +1183,12 @@ const initializeWindows = () => {
setQueuePositionFN = (cmd) => {
const hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: "api/v2/torrents/" + cmd,
method: "post",
data: {
fetch(`api/v2/torrents/${cmd}`, {
method: "POST",
body: new URLSearchParams({
hashes: hashes.join("|")
}
}).send();
})
});
updateMainData();
}
};
@@ -1229,13 +1222,15 @@ const initializeWindows = () => {
e.preventDefault();
e.stopPropagation();
new Request({
url: "api/v2/auth/logout",
method: "post",
onSuccess: () => {
fetch("api/v2/auth/logout", {
method: "POST"
})
.then((response) => {
if (!response.ok)
return;
window.location.reload(true);
}
}).send();
});
});
addClickEvent("shutdown", (e) => {
@@ -1243,17 +1238,19 @@ const initializeWindows = () => {
e.stopPropagation();
if (confirm("QBT_TR(Are you sure you want to quit qBittorrent?)QBT_TR[CONTEXT=MainWindow]")) {
new Request({
url: "api/v2/app/shutdown",
method: "post",
onSuccess: () => {
fetch("api/v2/app/shutdown", {
method: "POST"
})
.then((response) => {
if (!response.ok)
return;
const shutdownMessage = "QBT_TR(%1 has been shutdown)QBT_TR[CONTEXT=HttpServer]".replace("%1", window.qBittorrent.Client.mainTitle());
document.write(`<!doctype html><html lang="${LANG}"><head> <meta charset="UTF-8"> <meta name="color-scheme" content="light dark"> <title>${shutdownMessage}</title> <style>* {font-family: Arial, Helvetica, sans-serif;}</style></head><body> <h1 style="text-align: center;">${shutdownMessage}</h1></body></html>`);
document.close();
window.stop();
window.qBittorrent.Client.stop();
}
}).send();
});
}
});