WebUI: Support managing web seeds

Closes #8475.
PR #21055.
This commit is contained in:
Thomas Piccirello
2024-09-28 00:37:36 -07:00
committed by GitHub
parent a23f45cc70
commit 81def39d8c
8 changed files with 304 additions and 87 deletions

View File

@@ -36,55 +36,7 @@ window.qBittorrent.PropWebseeds ??= (() => {
};
};
const webseedsDynTable = new Class({
initialize: function() {},
setup: function(table) {
this.table = $(table);
this.rows = new Hash();
},
removeRow: function(url) {
if (this.rows.has(url)) {
this.rows.get(url).destroy();
this.rows.erase(url);
return true;
}
return false;
},
removeAllRows: function() {
this.rows.each((tr, url) => {
this.removeRow(url);
});
},
updateRow: function(tr, row) {
const tds = tr.getElements("td");
for (let i = 0; i < row.length; ++i)
tds[i].textContent = row[i];
return true;
},
insertRow: function(row) {
const url = row[0];
if (this.rows.has(url)) {
const tableRow = this.rows.get(url);
this.updateRow(tableRow, row);
return;
}
// this.removeRow(id);
const tr = new Element("tr");
this.rows.set(url, tr);
for (let i = 0; i < row.length; ++i) {
const td = document.createElement("td");
td.textContent = row[i];
tr.appendChild(td);
}
tr.injectInside(this.table);
},
});
const torrentWebseedsTable = new window.qBittorrent.DynamicTable.TorrentWebseedsTable();
let current_hash = "";
@@ -97,41 +49,41 @@ window.qBittorrent.PropWebseeds ??= (() => {
}
const new_hash = torrentsTable.getCurrentTorrentID();
if (new_hash === "") {
wsTable.removeAllRows();
torrentWebseedsTable.clear();
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
return;
}
if (new_hash !== current_hash) {
wsTable.removeAllRows();
torrentWebseedsTable.clear();
current_hash = new_hash;
}
const url = new URI("api/v2/torrents/webseeds?hash=" + current_hash);
new Request.JSON({
url: url,
url: new URI("api/v2/torrents/webseeds").setData("hash", current_hash),
method: "get",
noCache: true,
onFailure: function() {
$("error_div").textContent = "QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]";
onComplete: function() {
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(20000);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
},
onSuccess: function(webseeds) {
$("error_div").textContent = "";
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
torrentWebseedsTable.clear();
if (webseeds) {
// Update WebSeeds data
webseeds.each((webseed) => {
const row = [];
row.length = 1;
row[0] = webseed.url;
wsTable.insertRow(row);
torrentWebseedsTable.updateRowData({
rowId: webseed.url,
url: webseed.url,
});
});
}
else {
wsTable.removeAllRows();
}
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
torrentWebseedsTable.updateTable(false);
if (selectedWebseeds.length > 0)
torrentWebseedsTable.reselectRows(selectedWebseeds);
}
}).send();
};
@@ -142,8 +94,123 @@ window.qBittorrent.PropWebseeds ??= (() => {
loadWebSeedsData();
};
const wsTable = new webseedsDynTable();
wsTable.setup($("webseedsTable"));
const torrentWebseedsContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
targets: "#torrentWebseedsTableDiv",
menu: "torrentWebseedsMenu",
actions: {
AddWebSeeds: function(element, ref) {
addWebseedFN();
},
EditWebSeed: function(element, ref) {
// only allow editing of one row
element.firstChild.click();
editWebSeedFN(element);
},
RemoveWebSeed: function(element, ref) {
removeWebSeedFN(element);
}
},
offsets: {
x: -15,
y: 2
},
onShow: function() {
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
if (selectedWebseeds.length === 0) {
this.hideItem("EditWebSeed");
this.hideItem("RemoveWebSeed");
this.hideItem("CopyWebseedUrl");
}
else {
if (selectedWebseeds.length === 1)
this.showItem("EditWebSeed");
else
this.hideItem("EditWebSeed");
this.showItem("RemoveWebSeed");
this.showItem("CopyWebseedUrl");
}
}
});
const addWebseedFN = function() {
if (current_hash.length === 0)
return;
new MochaUI.Window({
id: "webseedsPage",
title: "QBT_TR(Add web seeds)QBT_TR[CONTEXT=HttpServer]",
loadMethod: "iframe",
contentURL: "addwebseeds.html?hash=" + current_hash,
scrollbars: true,
resizable: false,
maximizable: false,
closable: true,
paddingVertical: 0,
paddingHorizontal: 0,
width: 500,
height: 250,
onCloseComplete: function() {
updateData();
}
});
};
const editWebSeedFN = function(element) {
if (current_hash.length === 0)
return;
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
if (selectedWebseeds.length > 1)
return;
const webseedUrl = selectedWebseeds[0];
new MochaUI.Window({
id: "webseedsPage",
title: "QBT_TR(Web seed editing)QBT_TR[CONTEXT=PropertiesWidget]",
loadMethod: "iframe",
contentURL: "editwebseed.html?hash=" + current_hash + "&url=" + encodeURIComponent(webseedUrl),
scrollbars: true,
resizable: false,
maximizable: false,
closable: true,
paddingVertical: 0,
paddingHorizontal: 0,
width: 500,
height: 150,
onCloseComplete: function() {
updateData();
}
});
};
const removeWebSeedFN = function(element) {
if (current_hash.length === 0)
return;
const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
new Request({
url: "api/v2/torrents/removeWebSeeds",
method: "post",
data: {
hash: current_hash,
urls: selectedWebseeds.map(webseed => encodeURIComponent(webseed)).join("|")
},
onSuccess: function() {
updateData();
}
}).send();
};
new ClipboardJS("#CopyWebseedUrl", {
text: function(trigger) {
return torrentWebseedsTable.selectedRowsIds().join("\n");
}
});
torrentWebseedsTable.setup("torrentWebseedsTableDiv", "torrentWebseedsTableFixedHeaderDiv", torrentWebseedsContextMenu);
return exports();
})();