196 lines
8.7 KiB
HTML

<!DOCTYPE html>
<html lang="${LANG}">
<head>
<meta charset="UTF-8">
<title>Torrent Upload/Download Ratio Limiting</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>
"use strict";
const UseGlobalLimit = -2;
const NoLimit = -1;
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("save").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 hashesList = new URI().getData("hashes").split("|");
const origValues = new URI().getData("orig").split("|");
const values = {
ratioLimit: window.qBittorrent.Misc.friendlyFloat(origValues[0], 2),
seedingTimeLimit: parseInt(origValues[1], 10),
inactiveSeedingTimeLimit: parseInt(origValues[2], 10),
maxRatio: window.qBittorrent.Misc.friendlyFloat(origValues[3], 2),
maxSeedingTime: parseInt(origValues[4], 10),
maxInactiveSeedingTime: parseInt(origValues[5], 10)
};
// select default when orig values not passed. using double equals to compare string and int
if ((origValues[0] === "")
|| ((values.ratioLimit === UseGlobalLimit)
&& (values.seedingTimeLimit === UseGlobalLimit)
&& (values.inactiveSeedingTimeLimit === UseGlobalLimit))) {
// use default option
setSelectedRadioValue("shareLimit", "default");
}
else if ((values.maxRatio === NoLimit) && (values.maxSeedingTime === NoLimit) && (values.maxInactiveSeedingTime === NoLimit)) {
setSelectedRadioValue("shareLimit", "none");
// TODO set input boxes to *global* max ratio and seeding time
}
else {
setSelectedRadioValue("shareLimit", "custom");
if (values.ratioLimit >= 0) {
$("setRatio").checked = true;
$("ratio").value = values.ratioLimit;
}
if (values.seedingTimeLimit >= 0) {
$("setTotalMinutes").checked = true;
$("totalMinutes").value = values.seedingTimeLimit;
}
if (values.inactiveSeedingTimeLimit >= 0) {
$("setInactiveMinutes").checked = true;
$("inactiveMinutes").value = values.inactiveSeedingTimeLimit;
}
}
shareLimitChanged();
$("default").focus();
$("save").addEventListener("click", (e) => {
e.preventDefault();
e.stopPropagation();
if (!isFormValid())
return;
const shareLimit = getSelectedRadioValue("shareLimit");
let ratioLimitValue = 0.00;
let seedingTimeLimitValue = 0;
let inactiveSeedingTimeLimitValue = 0;
if (shareLimit === "default") {
ratioLimitValue = seedingTimeLimitValue = inactiveSeedingTimeLimitValue = UseGlobalLimit;
}
else if (shareLimit === "none") {
ratioLimitValue = seedingTimeLimitValue = inactiveSeedingTimeLimitValue = NoLimit;
}
else if (shareLimit === "custom") {
ratioLimitValue = $("setRatio").checked ? $("ratio").value : -1;
seedingTimeLimitValue = $("setTotalMinutes").checked ? $("totalMinutes").value : -1;
inactiveSeedingTimeLimitValue = $("setInactiveMinutes").checked ? $("inactiveMinutes").value : -1;
}
else {
return;
}
new Request({
url: "api/v2/torrents/setShareLimits",
method: "post",
data: {
hashes: hashesList.join("|"),
ratioLimit: ratioLimitValue,
seedingTimeLimit: seedingTimeLimitValue,
inactiveSeedingTimeLimit: inactiveSeedingTimeLimitValue
},
onComplete: function() {
window.parent.qBittorrent.Client.closeWindows();
}
}).send();
});
});
function getSelectedRadioValue(name) {
const radios = document.getElementsByName(name);
for (let i = 0; i < radios.length; ++i) {
const radio = radios[i];
if (radio.checked)
return radio.value;
}
}
function setSelectedRadioValue(name, value) {
const radios = document.getElementsByName(name);
for (let i = 0; i < radios.length; ++i) {
const radio = radios[i];
if (radio.value === value)
radio.checked = true;
}
}
function shareLimitChanged() {
const customShareLimit = getSelectedRadioValue("shareLimit") === "custom";
$("setRatio").disabled = !customShareLimit;
$("setTotalMinutes").disabled = !customShareLimit;
$("setInactiveMinutes").disabled = !customShareLimit;
enableInputBoxes();
$("save").disabled = !isFormValid();
}
function enableInputBoxes() {
$("ratio").disabled = $("setRatio").disabled || !$("setRatio").checked;
$("totalMinutes").disabled = $("setTotalMinutes").disabled || !$("setTotalMinutes").checked;
$("inactiveMinutes").disabled = $("setInactiveMinutes").disabled || !$("setInactiveMinutes").checked;
$("save").disabled = !isFormValid();
}
function isFormValid() {
return !((getSelectedRadioValue("shareLimit") === "custom") && !$("setRatio").checked
&& !$("setTotalMinutes").checked && !$("setInactiveMinutes").checked);
}
</script>
</head>
<body>
<div style="padding: 10px 10px 0px 10px;">
<input type="radio" name="shareLimit" id="default" value="default" onchange="shareLimitChanged()" checked style="margin-bottom: 5px;"><label for="default">Use global share limit</label><br>
<input type="radio" name="shareLimit" id="shareLimitNone" value="none" onchange="shareLimitChanged()" style="margin-bottom: 5px;"><label for="shareLimitNone">Set no share limit</label><br>
<input type="radio" name="shareLimit" id="shareLimitCustom" value="custom" onchange="shareLimitChanged()" style="margin-bottom: 5px;"><label for="shareLimitCustom">Set share limit to</label><br>
<div style="margin-left: 40px; margin-bottom: 5px;">
<input type="checkbox" id="setRatio" class="shareLimitInput" onclick="enableInputBoxes()">
<label id="ratioLabel" for="setRatio">ratio</label>
<input type="number" id="ratio" value="0.00" step=".01" min="0" max="9999" class="shareLimitInput" aria-labelledby="ratioLabel">
</div>
<div style="margin-left: 40px; margin-bottom: 5px;">
<input type="checkbox" id="setTotalMinutes" class="shareLimitInput" onclick="enableInputBoxes()">
<label id="totalMinutesLabel" for="setTotalMinutes">total minutes</label>
<input type="number" id="totalMinutes" value="0" step="1" min="0" max="525600" class="shareLimitInput" aria-labelledby="totalMinutesLabel">
</div>
<div style="margin-left: 40px; margin-bottom: 5px;">
<input type="checkbox" id="setInactiveMinutes" class="shareLimitInput" onclick="enableInputBoxes()">
<label id="inactiveMinutesLabel" for="setInactiveMinutes">inactive minutes</label>
<input type="number" id="inactiveMinutes" value="0" step="1" min="0" max="525600" class="shareLimitInput" aria-labelledby="inactiveMinutesLabel">
</div>
<div style="text-align: center; padding-top: 10px;">
<input type="button" value="Save" id="save">
</div>
</div>
</body>
</html>