WebUI: use native event listeners for keyboard events

PR #21924.
This commit is contained in:
Chocobo1 2024-12-02 13:41:19 +08:00 committed by GitHub
parent e8d8de8f19
commit 2d1c4fc809
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
24 changed files with 273 additions and 366 deletions

View File

@ -12,21 +12,16 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const hash = new URI().getData("hash");
if (!hash)
return;

View File

@ -13,19 +13,14 @@
"use strict";
window.addEventListener("DOMContentLoaded", () => {
new Keyboard({
defaultEventType: "keydown",
events: {
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Escape":
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
break;
}
}).activate();
});
$("trackersUrls").focus();
$("addTrackersButton").addEventListener("click", (e) => {

View File

@ -13,22 +13,17 @@
"use strict";
window.addEventListener("DOMContentLoaded", () => {
new Keyboard({
defaultEventType: "keydown",
events: {
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Escape":
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
break;
}
}).activate();
});
$("urls").focus();
$("addWebSeedsButton").addEvent("click", (e) => {
$("addWebSeedsButton").addEventListener("click", (e) => {
e.stopPropagation();
const hash = new URI().getData("hash");
new Request({

View File

@ -18,11 +18,11 @@
$("confirmDeleteTrackerText").textContent = "QBT_TR(Are you sure you want to remove tracker %1 from all torrents?)QBT_TR[CONTEXT=TrackersFilterWidget]".replace("%1", `"${host}"`);
$("cancelBtn").focus();
$("cancelBtn").addEvent("click", (e) => {
$("cancelBtn").addEventListener("click", (e) => {
e.stopPropagation();
window.parent.qBittorrent.Client.closeFrameWindow(window);
});
$("confirmBtn").addEvent("click", (e) => {
$("confirmBtn").addEventListener("click", (e) => {
e.stopPropagation();
const cmd = "api/v2/torrents/removeTrackers";
new Request({

View File

@ -33,6 +33,19 @@
<script>
"use strict";
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("applyButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const hashes = new URI().getData("hashes").split("|");
const setDlLimit = () => {
const limit = Number($("dllimitUpdatevalue").value) * 1024;
@ -64,24 +77,6 @@
}
};
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("applyButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
$("dllimitUpdatevalue").focus();
MochaUI.addDlLimitSlider(hashes);

View File

@ -13,25 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
event.preventDefault();
$("submitButton").click();
},
"Escape": (event) => {
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
},
"Esc": (event) => {
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("submitButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const currentUrl = new URI().getData("url");
$("url").value = currentUrl;

View File

@ -13,23 +13,18 @@
"use strict";
window.addEventListener("DOMContentLoaded", () => {
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("editTrackerButton").click();
break;
case "Escape":
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
break;
}
}).activate();
});
const currentUrl = new URI().getData("url");
if (!currentUrl)

View File

@ -13,29 +13,24 @@
"use strict";
window.addEventListener("DOMContentLoaded", () => {
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("editWebSeedButton").click();
break;
case "Escape":
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
break;
}
}).activate();
});
const origUrl = new URI().getData("url");
$("url").value = decodeURIComponent(origUrl);
$("url").focus();
$("editWebSeedButton").addEvent("click", (e) => {
$("editWebSeedButton").addEventListener("click", (e) => {
e.stopPropagation();
const hash = new URI().getData("hash");
new Request({

View File

@ -14,25 +14,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("categoryNameButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("categoryNameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
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"));

View File

@ -13,24 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
$("submitButton").click();
event.preventDefault();
},
"Escape": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("submitButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
$("feedURL").focus();
const path = new URI().getData("path");
$("submitButton").addEventListener("click", (e) => {

View File

@ -14,24 +14,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
$("submitButton").click();
event.preventDefault();
},
"Escape": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("submitButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
$("folderName").focus();
const path = new URI().getData("path");
$("submitButton").addEventListener("click", (e) => {

View File

@ -13,24 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
$("submitButton").click();
event.preventDefault();
},
"Escape": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("submitButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
$("name").focus();
$("submitButton").addEventListener("click", (e) => {
e.preventDefault();

View File

@ -13,25 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("tagNameButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("tagNameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const uriAction = window.qBittorrent.Misc.safeTrim(new URI().getData("action"));
const uriHashes = window.qBittorrent.Misc.safeTrim(new URI().getData("hashes"));

View File

@ -13,25 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("renameButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("renameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const name = new URI().getData("name");
// set text field to current value
if (name)

View File

@ -13,24 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
$("renameButton").click();
event.preventDefault();
},
"Escape": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("renameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const oldPath = new URI().getData("oldPath");
$("rename").value = oldPath;

View File

@ -14,25 +14,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("renameButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("renameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const hash = new URI().getData("hash");
const oldPath = new URI().getData("path");
const isFolder = ((new URI().getData("isFolder")) === "true");

View File

@ -61,7 +61,7 @@
// Register keyboard events to modal window
// https://github.com/qbittorrent/qBittorrent/pull/18687#discussion_r1135045726
const keyboard = new Keyboard({
new Keyboard({
defaultEventType: "keydown",
events: {
"Escape": function(event) {
@ -73,8 +73,7 @@
event.preventDefault();
}
}
});
keyboard.activate();
}).activate();
const fileRenamer = new window.qBittorrent.MultiRename.RenameFiles();
fileRenamer.hash = data.hash;

View File

@ -13,24 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": (event) => {
$("renameButton").click();
event.preventDefault();
},
"Escape": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": (event) => {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("renameButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const oldName = new URI().getData("rule");
$("rename").value = oldName;

View File

@ -1728,35 +1728,30 @@ window.addEventListener("DOMContentLoaded", () => {
};
registerDragAndDrop();
new Keyboard({
defaultEventType: "keydown",
events: {
"ctrl+a": function(event) {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "a":
case "A":
if (event.ctrlKey) {
if ((event.target.nodeName === "INPUT") || (event.target.nodeName === "TEXTAREA"))
return;
if (event.target.isContentEditable)
return;
event.preventDefault();
torrentsTable.selectAll();
}
break;
case "Delete":
if ((event.target.nodeName === "INPUT") || (event.target.nodeName === "TEXTAREA"))
return;
if (event.target.isContentEditable)
return;
torrentsTable.selectAll();
event.preventDefault();
},
"delete": function(event) {
if ((event.target.nodeName === "INPUT") || (event.target.nodeName === "TEXTAREA"))
return;
if (event.target.isContentEditable)
return;
deleteSelectedTorrentsFN();
event.preventDefault();
},
"shift+delete": (event) => {
if ((event.target.nodeName === "INPUT") || (event.target.nodeName === "TEXTAREA"))
return;
if (event.target.isContentEditable)
return;
deleteSelectedTorrentsFN(true);
event.preventDefault();
}
deleteSelectedTorrentsFN(event.shiftKey);
break;
}
}).activate();
});
new ClipboardJS(".copyToClipboard", {
text: (trigger) => {

View File

@ -139,28 +139,25 @@ window.qBittorrent.Search ??= (() => {
}, window.qBittorrent.Misc.FILTER_INPUT_DELAY);
});
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(e) {
// accept enter key as a click
e.preventDefault();
e.stopPropagation();
document.getElementById("SearchPanel").addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter": {
event.preventDefault();
event.stopPropagation();
const elem = e.event.srcElement;
if (elem.className.contains("searchInputField")) {
document.getElementById("startSearchButton").click();
return;
}
switch (elem.id) {
switch (event.target.id) {
case "manageSearchPlugins":
manageSearchPlugins();
break;
case "searchPattern":
document.getElementById("startSearchButton").click();
break;
}
break;
}
}
}).activate();
});
// restore search tabs
const searchJobs = JSON.parse(LocalPreferences.get("search_jobs", "[]"));

View File

@ -13,25 +13,20 @@
<script>
"use strict";
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("setLocationButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("setLocationButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const path = new URI().getData("path");
// set text field to current value

View File

@ -16,25 +16,20 @@
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.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
window.addEventListener("DOMContentLoaded", () => {
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("save").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const hashesList = new URI().getData("hashes").split("|");
const origValues = new URI().getData("orig").split("|");

View File

@ -33,6 +33,19 @@
<script>
"use strict";
window.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
event.preventDefault();
$("applyButton").click();
break;
case "Escape":
event.preventDefault();
window.parent.qBittorrent.Client.closeFrameWindow(window);
break;
}
});
const hashes = new URI().getData("hashes").split("|");
const setUpLimit = () => {
const limit = Number($("uplimitUpdatevalue").value) * 1024;
@ -64,24 +77,6 @@
}
};
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(event) {
$("applyButton").click();
event.preventDefault();
},
"Escape": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
},
"Esc": function(event) {
window.parent.qBittorrent.Client.closeFrameWindow(window);
event.preventDefault();
}
}
}).activate();
$("uplimitUpdatevalue").focus();
MochaUI.addUpLimitSlider(hashes);

View File

@ -38,22 +38,28 @@
};
const setup = () => {
new Keyboard({
defaultEventType: "keydown",
events: {
"Enter": function(e) {
// accept enter key as a click
e.preventDefault();
e.stopPropagation();
const windowEl = document.getElementById("installSearchPlugin");
const elem = e.event.srcElement;
if ((elem.id === "newPluginPath") || (elem.id === "newPluginOk"))
newPluginOk();
else if (elem.id === "newPluginCancel")
window.qBittorrent.Client.closeWindow(document.getElementById("installSearchPlugin"));
}
windowEl.addEventListener("keydown", (event) => {
switch (event.key) {
case "Enter":
// accept enter key as a click
event.preventDefault();
event.stopPropagation();
switch (event.target.id) {
case "newPluginCancel":
window.qBittorrent.Client.closeWindow(windowEl);
break;
case "newPluginOk":
case "newPluginPath":
newPluginOk();
break;
}
break;
}
}).activate();
});
$("newPluginPath").select();
};