WebUI: Add ability to toggle alternating row colors in tables
[qBittorrent.git] / src / webui / www / private / scripts / prop-peers.js
blobee42f8520fce4638fd20681cb18bfeb2809d4b73
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2018 Thomas Piccirello <thomas.piccirello@gmail.com>
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.
29 "use strict";
31 window.qBittorrent ??= {};
32 window.qBittorrent.PropPeers ??= (() => {
33 const exports = () => {
34 return {
35 updateData: updateData
39 const torrentPeersTable = new window.qBittorrent.DynamicTable.TorrentPeersTable();
40 let loadTorrentPeersTimer;
41 let syncTorrentPeersLastResponseId = 0;
42 let show_flags = true;
44 const loadTorrentPeersData = function() {
45 if ($("prop_peers").hasClass("invisible")
46 || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
47 syncTorrentPeersLastResponseId = 0;
48 torrentPeersTable.clear();
49 return;
51 const current_hash = torrentsTable.getCurrentTorrentID();
52 if (current_hash === "") {
53 syncTorrentPeersLastResponseId = 0;
54 torrentPeersTable.clear();
55 clearTimeout(loadTorrentPeersTimer);
56 loadTorrentPeersTimer = loadTorrentPeersData.delay(window.qBittorrent.Client.getSyncMainDataInterval());
57 return;
59 const url = new URI("api/v2/sync/torrentPeers");
60 url.setData("rid", syncTorrentPeersLastResponseId);
61 url.setData("hash", current_hash);
62 new Request.JSON({
63 url: url,
64 method: "get",
65 noCache: true,
66 onComplete: function() {
67 clearTimeout(loadTorrentPeersTimer);
68 loadTorrentPeersTimer = loadTorrentPeersData.delay(window.qBittorrent.Client.getSyncMainDataInterval());
70 onSuccess: function(response) {
71 $("error_div").textContent = "";
72 if (response) {
73 const full_update = (response["full_update"] === true);
74 if (full_update)
75 torrentPeersTable.clear();
76 if (response["rid"])
77 syncTorrentPeersLastResponseId = response["rid"];
78 if (response["peers"]) {
79 for (const key in response["peers"]) {
80 if (!Object.hasOwn(response["peers"], key))
81 continue;
83 response["peers"][key]["rowId"] = key;
84 torrentPeersTable.updateRowData(response["peers"][key]);
87 if (response["peers_removed"]) {
88 response["peers_removed"].each((hash) => {
89 torrentPeersTable.removeRow(hash);
90 });
92 torrentPeersTable.updateTable(full_update);
94 if (response["show_flags"]) {
95 if (show_flags !== response["show_flags"]) {
96 show_flags = response["show_flags"];
97 torrentPeersTable.columns["country"].force_hide = !show_flags;
98 torrentPeersTable.updateColumn("country");
102 else {
103 torrentPeersTable.clear();
106 }).send();
109 const updateData = function() {
110 clearTimeout(loadTorrentPeersTimer);
111 loadTorrentPeersData();
114 const torrentPeersContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
115 targets: "#torrentPeersTableDiv",
116 menu: "torrentPeersMenu",
117 actions: {
118 addPeer: function(element, ref) {
119 const hash = torrentsTable.getCurrentTorrentID();
120 if (!hash)
121 return;
123 new MochaUI.Window({
124 id: "addPeersPage",
125 title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
126 loadMethod: "iframe",
127 contentURL: "addpeers.html?hash=" + hash,
128 scrollbars: false,
129 resizable: false,
130 maximizable: false,
131 paddingVertical: 0,
132 paddingHorizontal: 0,
133 width: 350,
134 height: 240
137 banPeer: function(element, ref) {
138 const selectedPeers = torrentPeersTable.selectedRowsIds();
139 if (selectedPeers.length === 0)
140 return;
142 if (confirm("QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]")) {
143 new Request({
144 url: "api/v2/transfer/banPeers",
145 method: "post",
146 data: {
147 hash: torrentsTable.getCurrentTorrentID(),
148 peers: selectedPeers.join("|")
150 }).send();
154 offsets: {
155 x: -15,
156 y: 2
158 onShow: function() {
159 const selectedPeers = torrentPeersTable.selectedRowsIds();
161 if (selectedPeers.length >= 1) {
162 this.showItem("copyPeer");
163 this.showItem("banPeer");
165 else {
166 this.hideItem("copyPeer");
167 this.hideItem("banPeer");
172 new ClipboardJS("#CopyPeerInfo", {
173 text: function(trigger) {
174 return torrentPeersTable.selectedRowsIds().join("\n");
178 torrentPeersTable.setup("torrentPeersTableDiv", "torrentPeersTableFixedHeaderDiv", torrentPeersContextMenu);
180 return exports();
181 })();
182 Object.freeze(window.qBittorrent.PropPeers);