166 lines
7.0 KiB
HTML

<!DOCTYPE html>
<html lang="${LANG}">
<head>
<meta charset="UTF-8">
<title>New Category</title>
<link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
<script src="scripts/lib/MooTools-Core-1.6.0-compat-compressed.js"></script>
<script src="scripts/lib/MooTools-More-1.6.0-compat-compressed.js"></script>
<script src="scripts/misc.js?locale=${LANG}&v=${CACHEID}"></script>
<script src="scripts/pathAutofill.js?v=${CACHEID}"></script>
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("categoryNameButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeWindows();
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeWindows();
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
const uriAction = window.qBittorrent.Misc.safeTrim(new URI().getData("action"));
const uriHashes = window.qBittorrent.Misc.safeTrim(new URI().getData("hashes"));
const uriCategoryName = window.qBittorrent.Misc.safeTrim(new URI().getData("categoryName"));
const uriSavePath = window.qBittorrent.Misc.safeTrim(new URI().getData("savePath"));
if (uriAction === "edit") {
if (!uriCategoryName)
return;
$("categoryName").disabled = true;
$("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName);
$("savePath").value = window.qBittorrent.Misc.escapeHtml(uriSavePath);
$("savePath").focus();
}
else if (uriAction === "createSubcategory") {
$("categoryName").value = window.qBittorrent.Misc.escapeHtml(uriCategoryName);
$("categoryName").focus();
}
else {
$("categoryName").focus();
}
$("categoryNameButton").addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
const savePath = $("savePath").value.trim();
const categoryName = $("categoryName").value.trim();
const verifyCategoryName = function(name) {
if ((name === null) || (name === ""))
return false;
if (name.match("^([^\\\\\\/]|[^\\\\\\/]([^\\\\\\/]|\\/(?=[^\\/]))*[^\\\\\\/])$") === null) {
alert("Invalid category name:\nPlease do not use any special characters in the category name.");
return false;
}
return true;
};
switch (uriAction) {
case "set":
if ((uriHashes === "") || !verifyCategoryName(categoryName))
return;
new Request({
url: "api/v2/torrents/createCategory",
method: "post",
data: {
category: categoryName,
savePath: savePath
},
onSuccess: function() {
new Request({
url: "api/v2/torrents/setCategory",
method: "post",
data: {
hashes: uriHashes,
category: categoryName
},
onSuccess: function() {
window.parent.updateMainData();
window.parent.qBittorrent.Client.closeWindows();
},
onFailure: function() {
alert("Unable to set category");
}
}).send();
},
onFailure: function() {
alert("Unable to create category " + window.qBittorrent.Misc.escapeHtml(categoryName));
}
}).send();
break;
case "create":
case "createSubcategory":
if (!verifyCategoryName(categoryName))
return;
new Request({
url: "api/v2/torrents/createCategory",
method: "post",
data: {
category: categoryName,
savePath: savePath
},
onSuccess: function() {
window.parent.updateMainData();
window.parent.qBittorrent.Client.closeWindows();
},
onFailure: function() {
alert("Unable to create category");
}
}).send();
break;
case "edit":
new Request({
url: "api/v2/torrents/editCategory",
method: "post",
data: {
category: uriCategoryName, // category name can't be changed
savePath: savePath
},
onSuccess: function() {
window.parent.updateMainData();
window.parent.qBittorrent.Client.closeWindows();
},
onFailure: function() {
alert("Unable to edit category");
}
}).send();
break;
}
});
});
window.qBittorrent.pathAutofill.attachPathAutofill();
</script>
</head>
<body>
<div style="padding: 10px 10px 0px 10px;">
<label for="categoryName" style="font-weight: bold;">Category:</label>
<input type="text" id="categoryName" style="width: 99%;">
<label for="savePath" style="font-weight: bold;">Save path:</label>
<input type="text" id="savePath" class="pathDirectory" style="width: 99%;">
<div style="text-align: center; padding-top: 10px;">
<input type="button" value="Add" id="categoryNameButton">
</div>
</div>
</body>
</html>