Skip to content

Commit

Permalink
WebUI: Add context menu to search tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
skomerko committed Oct 5, 2024
1 parent e75bcbe commit 2475cfb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
31 changes: 26 additions & 5 deletions src/webui/www/private/scripts/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ window.qBittorrent.Search ??= (() => {
maxUnit: 3
};

const searchResultsTabsContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
targets: ".searchTab",
menu: "searchResultsTabsMenu",
actions: {
closeTab: (tab) => { closeSearchTab(tab); },
closeAllTabs: () => {
for (const tab of document.querySelectorAll("#searchTabs .searchTab"))
closeSearchTab(tab);
}
},
offsets: {
x: -15,
y: -53
},
onShow: function() {
setActiveTab(this.options.element);
}
});

const init = function() {
// load "Search in" preference from local storage
$("searchInTorrentName").value = (LocalPreferences.get("search_in_filter") === "names") ? "names" : "everywhere";
Expand Down Expand Up @@ -168,21 +187,22 @@ window.qBittorrent.Search ??= (() => {
width: "8",
height: "8",
style: "padding-right: 7px; margin-bottom: -1px; margin-left: -5px",
onclick: "qBittorrent.Search.closeSearchTab(event, this);",
onclick: "qBittorrent.Search.closeSearchTab(this);",
});
closeTabElem.inject(tabElem, "top");

tabElem.appendChild(getStatusIconElement("QBT_TR(Searching...)QBT_TR[CONTEXT=SearchJobWidget]", "images/queued.svg"));

const listItem = document.createElement("li");
listItem.id = newTabId;
listItem.classList.add("selected");
listItem.classList.add("selected", "searchTab");
listItem.addEventListener("click", (e) => {
setActiveTab(listItem);
document.getElementById("startSearchButton").lastChild.textContent = "QBT_TR(Search)QBT_TR[CONTEXT=SearchEngineWidget]";
});
listItem.appendChild(tabElem);
$("searchTabs").appendChild(listItem);
searchResultsTabsContextMenu.addTarget(listItem);

// unhide the results elements
if (numSearchTabs() >= 1) {
Expand Down Expand Up @@ -214,10 +234,11 @@ window.qBittorrent.Search ??= (() => {
updateSearchResultsData(searchId);
};

const closeSearchTab = function(e, el) {
e.stopPropagation();
const closeSearchTab = function(el) {
const tab = el.closest("li.searchTab");
if (!tab)
return;

const tab = el.parentElement.parentElement;
const searchId = getSearchIdFromTab(tab);
const isTabSelected = tab.hasClass("selected");
const newTabToSelect = isTabSelected ? (tab.nextSibling || tab.previousSibling) : null;
Expand Down
4 changes: 4 additions & 0 deletions src/webui/www/private/views/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@
</ul>
</li>
</ul>
<ul id="searchResultsTabsMenu" class="contextMenu">
<li><a href="#closeTab">QBT_TR(Close tab)QBT_TR[CONTEXT=SearchJobWidget]</a></li>
<li><a href="#closeAllTabs">QBT_TR(Close all tabs)QBT_TR[CONTEXT=SearchJobWidget]</a></li>
</ul>

0 comments on commit 2475cfb

Please sign in to comment.