2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2009 Christophe Dumez <chris@qbittorrent.org>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * In addition, as a special exception, the copyright holders give permission to
20 * link this program with the OpenSSL project's "OpenSSL" library (or with
21 * modified versions of it that use the same license as the "OpenSSL" library),
22 * and distribute the linked executables. You must obey the GNU General Public
23 * License in all respects for all of the code used other than "OpenSSL". If you
24 * modify file(s), you may extend this exception to your version of the file(s),
25 * but you are not obligated to do so. If you do not wish to do so, delete this
26 * exception statement from your version.
31 window.qBittorrent ??= {};
32 window.qBittorrent.ContextMenu ??= (() => {
33 const exports = () => {
35 ContextMenu: ContextMenu,
36 TorrentsTableContextMenu: TorrentsTableContextMenu,
37 StatusesFilterContextMenu: StatusesFilterContextMenu,
38 CategoriesFilterContextMenu: CategoriesFilterContextMenu,
39 TagsFilterContextMenu: TagsFilterContextMenu,
40 TrackersFilterContextMenu: TrackersFilterContextMenu,
41 SearchPluginsTableContextMenu: SearchPluginsTableContextMenu,
42 RssFeedContextMenu: RssFeedContextMenu,
43 RssArticleContextMenu: RssArticleContextMenu,
44 RssDownloaderRuleContextMenu: RssDownloaderRuleContextMenu
48 let lastShownContextMenu = null;
50 constructor(options) {
69 this.menu = $(this.options.menu);
70 this.targets = $$(this.options.targets);
73 this.fx = new Fx.Tween(this.menu, {
75 duration: this.options.fadeSpeed,
77 this.menu.style.visibility = (getComputedStyle(this.menu).opacity > 0) ? "visible" : "hidden";
81 // hide and begin the listener
82 this.hide().startListener();
85 this.menu.style.position = "absolute";
86 this.menu.style.top = "-900000px";
87 this.menu.style.display = "block";
90 adjustMenuPosition(e) {
91 this.updateMenuItems();
93 const scrollableMenuMaxHeight = document.documentElement.clientHeight * 0.75;
95 if (this.menu.hasClass("scrollableMenu"))
96 this.menu.style.maxHeight = `${scrollableMenuMaxHeight}px`;
98 // draw the menu off-screen to know the menu dimensions
99 this.menu.style.left = "-999em";
100 this.menu.style.top = "-999em";
103 let xPosMenu = e.pageX + this.options.offsets.x;
104 let yPosMenu = e.pageY + this.options.offsets.y;
105 if ((xPosMenu + this.menu.offsetWidth) > document.documentElement.clientWidth)
106 xPosMenu -= this.menu.offsetWidth;
107 if ((yPosMenu + this.menu.offsetHeight) > document.documentElement.clientHeight)
108 yPosMenu = document.documentElement.clientHeight - this.menu.offsetHeight;
113 this.menu.style.left = `${xPosMenu}px`;
114 this.menu.style.top = `${yPosMenu}px`;
115 this.menu.style.position = "absolute";
116 this.menu.style.zIndex = "2000";
118 // position the sub-menu
119 const uls = this.menu.getElementsByTagName("ul");
120 for (let i = 0; i < uls.length; ++i) {
122 if (ul.hasClass("scrollableMenu"))
123 ul.style.maxHeight = `${scrollableMenuMaxHeight}px`;
124 const rectParent = ul.parentNode.getBoundingClientRect();
125 const xPosOrigin = rectParent.left;
126 const yPosOrigin = rectParent.bottom;
127 let xPos = xPosOrigin + rectParent.width - 1;
128 let yPos = yPosOrigin - rectParent.height - 1;
129 if ((xPos + ul.offsetWidth) > document.documentElement.clientWidth)
130 xPos -= (ul.offsetWidth + rectParent.width - 2);
131 if ((yPos + ul.offsetHeight) > document.documentElement.clientHeight)
132 yPos = document.documentElement.clientHeight - ul.offsetHeight;
137 ul.style.marginLeft = `${xPos - xPosOrigin}px`;
138 ul.style.marginTop = `${yPos - yPosOrigin}px`;
142 setupEventListeners(elem) {
143 elem.addEventListener("contextmenu", (e) => {
144 this.triggerMenu(e, elem);
146 elem.addEventListener("click", (e) => {
150 elem.addEventListener("touchstart", (e) => {
152 this.touchStartAt = performance.now();
153 this.touchStartEvent = e;
154 }, { passive: true });
155 elem.addEventListener("touchend", (e) => {
156 const now = performance.now();
157 const touchStartAt = this.touchStartAt;
158 const touchStartEvent = this.touchStartEvent;
160 this.touchStartAt = null;
161 this.touchStartEvent = null;
163 const isTargetUnchanged = (Math.abs(e.event.pageX - touchStartEvent.event.pageX) <= 10) && (Math.abs(e.event.pageY - touchStartEvent.event.pageY) <= 10);
164 if (((now - touchStartAt) >= this.options.touchTimer) && isTargetUnchanged)
165 this.triggerMenu(touchStartEvent, elem);
166 }, { passive: true });
170 // prevent long press from selecting this text
171 t.style.userSelect = "none";
173 this.targets[this.targets.length] = t;
174 this.setupEventListeners(t);
177 searchAndAddTargets() {
178 document.querySelectorAll(this.options.targets).forEach((target) => { this.addTarget(target); });
182 if (this.options.disabled)
185 // prevent default, if told to
186 if (this.options.stopEvent) {
190 // record this as the trigger
191 this.options.element = $(el);
192 this.adjustMenuPosition(e);
197 // get things started
200 this.targets.each((el) => {
201 this.setupEventListeners(el);
205 this.menu.addEventListener("click", (e) => {
206 const menuItem = e.target.closest("li");
211 if (!menuItem.classList.contains("disabled")) {
212 const anchor = menuItem.firstElementChild;
213 this.execute(anchor.href.split("#")[1], this.options.element);
214 this.options.onClick.call(this, anchor, e);
221 // hide on body click
222 $(document.body).addEventListener("click", () => {
231 if (lastShownContextMenu && (lastShownContextMenu !== this))
232 lastShownContextMenu.hide();
234 this.options.onShow.call(this);
235 lastShownContextMenu = this;
241 if (lastShownContextMenu && (lastShownContextMenu.menu.style.visibility !== "hidden")) {
243 this.options.onHide.call(this);
248 setItemChecked(item, checked) {
249 this.menu.getElement("a[href$=" + item + "]").firstChild.style.opacity =
254 getItemChecked(item) {
255 return this.menu.getElement("a[href$=" + item + "]").firstChild.style.opacity !== "0";
260 this.menu.getElement("a[href$=" + item + "]").parentNode.addClass("invisible");
266 this.menu.getElement("a[href$=" + item + "]").parentNode.removeClass("invisible");
270 // enable/disable an item
271 setEnabled(item, enabled) {
272 this.menu.querySelector(`:scope a[href$="${item}"]`).parentElement.classList.toggle("disabled", !enabled);
276 // disable the entire menu
278 this.options.disabled = true;
282 // enable the entire menu
284 this.options.disabled = false;
289 execute(action, element) {
290 if (this.options.actions[action])
291 this.options.actions[action](element, this, action);
296 class FilterListContextMenu extends ContextMenu {
297 constructor(options) {
299 this.torrentObserver = new MutationObserver((records, observer) => {
300 this.updateTorrentActions();
304 startTorrentObserver() {
305 this.torrentObserver.observe(torrentsTable.tableBody, { childList: true });
308 stopTorrentObserver() {
309 this.torrentObserver.disconnect();
312 updateTorrentActions() {
313 const torrentsVisible = torrentsTable.tableBody.children.length > 0;
314 this.setEnabled("startTorrents", torrentsVisible)
315 .setEnabled("stopTorrents", torrentsVisible)
316 .setEnabled("deleteTorrents", torrentsVisible);
320 class TorrentsTableContextMenu extends ContextMenu {
322 let all_are_seq_dl = true;
323 let there_are_seq_dl = false;
324 let all_are_f_l_piece_prio = true;
325 let there_are_f_l_piece_prio = false;
326 let all_are_downloaded = true;
327 let all_are_stopped = true;
328 let there_are_stopped = false;
329 let all_are_force_start = true;
330 let there_are_force_start = false;
331 let all_are_super_seeding = true;
332 let all_are_auto_tmm = true;
333 let there_are_auto_tmm = false;
334 let thereAreV1Hashes = false;
335 let thereAreV2Hashes = false;
336 const tagCount = new Map();
337 const categoryCount = new Map();
339 const selectedRows = torrentsTable.selectedRowsIds();
340 selectedRows.forEach((item, index) => {
341 const data = torrentsTable.getRow(item).full_data;
343 if (data["seq_dl"] !== true)
344 all_are_seq_dl = false;
346 there_are_seq_dl = true;
348 if (data["f_l_piece_prio"] !== true)
349 all_are_f_l_piece_prio = false;
351 there_are_f_l_piece_prio = true;
353 if (data["progress"] !== 1.0) // not downloaded
354 all_are_downloaded = false;
355 else if (data["super_seeding"] !== true)
356 all_are_super_seeding = false;
358 if ((data["state"] !== "stoppedUP") && (data["state"] !== "stoppedDL"))
359 all_are_stopped = false;
361 there_are_stopped = true;
363 if (data["force_start"] !== true)
364 all_are_force_start = false;
366 there_are_force_start = true;
368 if (data["auto_tmm"] === true)
369 there_are_auto_tmm = true;
371 all_are_auto_tmm = false;
373 if (data["infohash_v1"] !== "")
374 thereAreV1Hashes = true;
376 if (data["infohash_v2"] !== "")
377 thereAreV2Hashes = true;
379 const torrentTags = data["tags"].split(", ");
380 for (const tag of torrentTags) {
381 const count = tagCount.get(tag);
382 tagCount.set(tag, ((count !== undefined) ? (count + 1) : 1));
385 const torrentCategory = data["category"];
386 const count = categoryCount.get(torrentCategory);
387 categoryCount.set(torrentCategory, ((count !== undefined) ? (count + 1) : 1));
390 // hide renameFiles when more than 1 torrent is selected
391 if (selectedRows.length === 1) {
392 const data = torrentsTable.getRow(selectedRows[0]).full_data;
393 const metadata_downloaded = !((data["state"] === "metaDL") || (data["state"] === "forcedMetaDL") || (data["total_size"] === -1));
395 // hide renameFiles when metadata hasn't been downloaded yet
397 ? this.showItem("renameFiles")
398 : this.hideItem("renameFiles");
401 this.hideItem("renameFiles");
404 if (all_are_downloaded) {
405 this.hideItem("downloadLimit");
406 this.menu.getElement("a[href$=uploadLimit]").parentNode.addClass("separator");
407 this.hideItem("sequentialDownload");
408 this.hideItem("firstLastPiecePrio");
409 this.showItem("superSeeding");
410 this.setItemChecked("superSeeding", all_are_super_seeding);
413 const show_seq_dl = (all_are_seq_dl || !there_are_seq_dl);
414 const show_f_l_piece_prio = (all_are_f_l_piece_prio || !there_are_f_l_piece_prio);
416 if (!show_seq_dl && show_f_l_piece_prio)
417 this.menu.getElement("a[href$=firstLastPiecePrio]").parentNode.addClass("separator");
419 this.menu.getElement("a[href$=firstLastPiecePrio]").parentNode.removeClass("separator");
422 this.showItem("sequentialDownload");
424 this.hideItem("sequentialDownload");
426 if (show_f_l_piece_prio)
427 this.showItem("firstLastPiecePrio");
429 this.hideItem("firstLastPiecePrio");
431 this.setItemChecked("sequentialDownload", all_are_seq_dl);
432 this.setItemChecked("firstLastPiecePrio", all_are_f_l_piece_prio);
434 this.showItem("downloadLimit");
435 this.menu.getElement("a[href$=uploadLimit]").parentNode.removeClass("separator");
436 this.hideItem("superSeeding");
439 this.showItem("start");
440 this.showItem("stop");
441 this.showItem("forceStart");
443 this.hideItem("stop");
444 else if (all_are_force_start)
445 this.hideItem("forceStart");
446 else if (!there_are_stopped && !there_are_force_start)
447 this.hideItem("start");
449 if (!all_are_auto_tmm && there_are_auto_tmm) {
450 this.hideItem("autoTorrentManagement");
453 this.showItem("autoTorrentManagement");
454 this.setItemChecked("autoTorrentManagement", all_are_auto_tmm);
457 this.setEnabled("copyInfohash1", thereAreV1Hashes);
458 this.setEnabled("copyInfohash2", thereAreV2Hashes);
460 const contextTagList = $("contextTagList");
461 tagList.forEach((tag, tagHash) => {
462 const checkbox = contextTagList.getElement(`a[href="#Tag/${tag.name}"] input[type="checkbox"]`);
463 const count = tagCount.get(tag.name);
464 const hasCount = (count !== undefined);
465 const isLesser = (count < selectedRows.length);
466 checkbox.indeterminate = (hasCount ? isLesser : false);
467 checkbox.checked = (hasCount ? !isLesser : false);
470 const contextCategoryList = document.getElementById("contextCategoryList");
471 category_list.forEach((category, categoryHash) => {
472 const categoryIcon = contextCategoryList.querySelector(`a[href$="#Category/${category.name}"] img`);
473 const count = categoryCount.get(category.name);
474 const isEqual = ((count !== undefined) && (count === selectedRows.length));
475 categoryIcon.classList.toggle("highlightedCategoryIcon", isEqual);
479 updateCategoriesSubMenu(categoryList) {
480 const contextCategoryList = $("contextCategoryList");
481 contextCategoryList.getChildren().each(c => c.destroy());
483 const createMenuItem = (text, imgURL, clickFn) => {
484 const anchor = document.createElement("a");
485 anchor.textContent = text;
486 anchor.addEventListener("click", () => { clickFn(); });
488 const img = document.createElement("img");
493 const item = document.createElement("li");
494 item.appendChild(anchor);
498 contextCategoryList.appendChild(createMenuItem("QBT_TR(New...)QBT_TR[CONTEXT=TransferListWidget]", "images/list-add.svg", torrentNewCategoryFN));
499 contextCategoryList.appendChild(createMenuItem("QBT_TR(Reset)QBT_TR[CONTEXT=TransferListWidget]", "images/edit-clear.svg", () => { torrentSetCategoryFN(0); }));
501 const sortedCategories = [];
502 categoryList.forEach((category, hash) => sortedCategories.push({
503 categoryName: category.name,
506 sortedCategories.sort((left, right) => window.qBittorrent.Misc.naturalSortCollator.compare(
507 left.categoryName, right.categoryName));
510 for (const { categoryName, categoryHash } of sortedCategories) {
511 const anchor = document.createElement("a");
512 anchor.href = `#Category/${categoryName}`;
513 anchor.textContent = categoryName;
514 anchor.addEventListener("click", (event) => {
515 event.preventDefault();
516 torrentSetCategoryFN(categoryHash);
519 const img = document.createElement("img");
520 img.src = "images/view-categories.svg";
523 const setCategoryItem = document.createElement("li");
524 setCategoryItem.appendChild(anchor);
526 setCategoryItem.addClass("separator");
530 contextCategoryList.appendChild(setCategoryItem);
534 updateTagsSubMenu(tagList) {
535 const contextTagList = $("contextTagList");
536 while (contextTagList.firstChild !== null)
537 contextTagList.removeChild(contextTagList.firstChild);
539 const createMenuItem = (text, imgURL, clickFn) => {
540 const anchor = document.createElement("a");
541 anchor.textContent = text;
542 anchor.addEventListener("click", () => { clickFn(); });
544 const img = document.createElement("img");
549 const item = document.createElement("li");
550 item.appendChild(anchor);
554 contextTagList.appendChild(createMenuItem("QBT_TR(Add...)QBT_TR[CONTEXT=TransferListWidget]", "images/list-add.svg", torrentAddTagsFN));
555 contextTagList.appendChild(createMenuItem("QBT_TR(Remove All)QBT_TR[CONTEXT=TransferListWidget]", "images/edit-clear.svg", torrentRemoveAllTagsFN));
557 const sortedTags = [];
558 tagList.forEach((tag, hash) => sortedTags.push({
562 sortedTags.sort((left, right) => window.qBittorrent.Misc.naturalSortCollator.compare(left.tagName, right.tagName));
564 for (let i = 0; i < sortedTags.length; ++i) {
565 const { tagName, tagHash } = sortedTags[i];
567 const input = document.createElement("input");
568 input.type = "checkbox";
569 input.addEventListener("click", (event) => {
570 input.checked = !input.checked;
573 const anchor = document.createElement("a");
574 anchor.href = `#Tag/${tagName}`;
575 anchor.textContent = tagName;
576 anchor.addEventListener("click", (event) => {
577 event.preventDefault();
578 torrentSetTagsFN(tagHash, !input.checked);
580 anchor.prepend(input);
582 const setTagItem = document.createElement("li");
583 setTagItem.appendChild(anchor);
585 setTagItem.addClass("separator");
587 contextTagList.appendChild(setTagItem);
592 class StatusesFilterContextMenu extends FilterListContextMenu {
594 this.updateTorrentActions();
598 class CategoriesFilterContextMenu extends FilterListContextMenu {
600 const id = Number(this.options.element.id);
601 if ((id !== CATEGORIES_ALL) && (id !== CATEGORIES_UNCATEGORIZED)) {
602 this.showItem("editCategory");
603 this.showItem("deleteCategory");
604 if (useSubcategories)
605 this.showItem("createSubcategory");
607 this.hideItem("createSubcategory");
610 this.hideItem("editCategory");
611 this.hideItem("deleteCategory");
612 this.hideItem("createSubcategory");
615 this.updateTorrentActions();
619 class TagsFilterContextMenu extends FilterListContextMenu {
621 const id = Number(this.options.element.id);
622 if ((id !== TAGS_ALL) && (id !== TAGS_UNTAGGED))
623 this.showItem("deleteTag");
625 this.hideItem("deleteTag");
627 this.updateTorrentActions();
631 class TrackersFilterContextMenu extends FilterListContextMenu {
633 const id = Number(this.options.element.id);
634 if ((id !== TRACKERS_ALL) && (id !== TRACKERS_TRACKERLESS))
635 this.showItem("deleteTracker");
637 this.hideItem("deleteTracker");
639 this.updateTorrentActions();
643 class SearchPluginsTableContextMenu extends ContextMenu {
645 const enabledColumnIndex = (text) => {
646 const columns = $("searchPluginsTableFixedHeaderRow").getChildren("th");
647 for (let i = 0; i < columns.length; ++i) {
648 if (columns[i].textContent === "Enabled")
653 this.showItem("Enabled");
654 this.setItemChecked("Enabled", (this.options.element.getChildren("td")[enabledColumnIndex()].textContent === "Yes"));
656 this.showItem("Uninstall");
660 class RssFeedContextMenu extends ContextMenu {
662 const selectedRows = window.qBittorrent.Rss.rssFeedTable.selectedRowsIds();
663 this.menu.getElement("a[href$=newSubscription]").parentNode.addClass("separator");
664 switch (selectedRows.length) {
666 // remove separator on top of newSubscription entry to avoid double line
667 this.menu.getElement("a[href$=newSubscription]").parentNode.removeClass("separator");
668 // menu when nothing selected
669 this.hideItem("update");
670 this.hideItem("markRead");
671 this.hideItem("rename");
672 this.hideItem("edit");
673 this.hideItem("delete");
674 this.showItem("newSubscription");
675 this.showItem("newFolder");
676 this.showItem("updateAll");
677 this.hideItem("copyFeedURL");
680 if (selectedRows[0] === "0") {
681 // menu when "unread" feed selected
682 this.showItem("update");
683 this.showItem("markRead");
684 this.hideItem("rename");
685 this.hideItem("edit");
686 this.hideItem("delete");
687 this.showItem("newSubscription");
688 this.hideItem("newFolder");
689 this.hideItem("updateAll");
690 this.hideItem("copyFeedURL");
692 else if (window.qBittorrent.Rss.rssFeedTable.getRow(selectedRows[0]).full_data.dataUid === "") {
693 // menu when single folder selected
694 this.showItem("update");
695 this.showItem("markRead");
696 this.showItem("rename");
697 this.hideItem("edit");
698 this.showItem("delete");
699 this.showItem("newSubscription");
700 this.showItem("newFolder");
701 this.hideItem("updateAll");
702 this.hideItem("copyFeedURL");
705 // menu when single feed selected
706 this.showItem("update");
707 this.showItem("markRead");
708 this.showItem("rename");
709 this.showItem("edit");
710 this.showItem("delete");
711 this.showItem("newSubscription");
712 this.hideItem("newFolder");
713 this.hideItem("updateAll");
714 this.showItem("copyFeedURL");
718 // menu when multiple items selected
719 this.showItem("update");
720 this.showItem("markRead");
721 this.hideItem("rename");
722 this.hideItem("edit");
723 this.showItem("delete");
724 this.hideItem("newSubscription");
725 this.hideItem("newFolder");
726 this.hideItem("updateAll");
727 this.showItem("copyFeedURL");
733 class RssArticleContextMenu extends ContextMenu {};
735 class RssDownloaderRuleContextMenu extends ContextMenu {
736 adjustMenuPosition(e) {
737 this.updateMenuItems();
739 // draw the menu off-screen to know the menu dimensions
740 this.menu.style.left = "-999em";
741 this.menu.style.top = "-999em";
743 let xPosMenu = e.pageX + this.options.offsets.x - $("rssdownloaderpage").offsetLeft;
744 let yPosMenu = e.pageY + this.options.offsets.y - $("rssdownloaderpage").offsetTop;
745 if ((xPosMenu + this.menu.offsetWidth) > document.documentElement.clientWidth)
746 xPosMenu -= this.menu.offsetWidth;
747 if ((yPosMenu + this.menu.offsetHeight) > document.documentElement.clientHeight)
748 yPosMenu = document.documentElement.clientHeight - this.menu.offsetHeight;
749 xPosMenu = Math.max(xPosMenu, 0);
750 yPosMenu = Math.max(yPosMenu, 0);
752 this.menu.style.left = `${xPosMenu}px`;
753 this.menu.style.top = `${yPosMenu}px`;
754 this.menu.style.position = "absolute";
755 this.menu.style.zIndex = "2000";
758 const selectedRows = window.qBittorrent.RssDownloader.rssDownloaderRulesTable.selectedRowsIds();
759 this.showItem("addRule");
760 switch (selectedRows.length) {
762 // menu when nothing selected
763 this.hideItem("deleteRule");
764 this.hideItem("renameRule");
765 this.hideItem("clearDownloadedEpisodes");
768 // menu when single item selected
769 this.showItem("deleteRule");
770 this.showItem("renameRule");
771 this.showItem("clearDownloadedEpisodes");
774 // menu when multiple items selected
775 this.showItem("deleteRule");
776 this.hideItem("renameRule");
777 this.showItem("clearDownloadedEpisodes");
785 Object.freeze(window.qBittorrent.ContextMenu);