WebUI: Use Map instead of Mootools Hash in Torrents table
[qBittorrent.git] / src / webui / www / private / scripts / prop-peers.js
blob71426b9128282a17a8e27ec195a9cc2cac4e5ffd
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2018  Thomas Piccirello <thomas.piccirello@gmail.com>
4  *
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.
9  *
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.
14  *
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.
18  *
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.
27  */
29 "use strict";
31 window.qBittorrent ??= {};
32 window.qBittorrent.PropPeers ??= (() => {
33     const exports = () => {
34         return {
35             updateData: updateData
36         };
37     };
39     const torrentPeersTable = new window.qBittorrent.DynamicTable.TorrentPeersTable();
40     let loadTorrentPeersTimer = -1;
41     let syncTorrentPeersLastResponseId = 0;
42     let show_flags = true;
44     const loadTorrentPeersData = function() {
45         if ($("propPeers").hasClass("invisible")
46             || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
47             syncTorrentPeersLastResponseId = 0;
48             torrentPeersTable.clear();
49             return;
50         }
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;
58         }
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());
69             },
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]);
85                         }
86                     }
87                     if (response["peers_removed"]) {
88                         response["peers_removed"].each((hash) => {
89                             torrentPeersTable.removeRow(hash);
90                         });
91                     }
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");
99                         }
100                     }
101                 }
102                 else {
103                     torrentPeersTable.clear();
104                 }
105             }
106         }).send();
107     };
109     const updateData = function() {
110         clearTimeout(loadTorrentPeersTimer);
111         loadTorrentPeersTimer = -1;
112         loadTorrentPeersData();
113     };
115     const torrentPeersContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
116         targets: "#torrentPeersTableDiv",
117         menu: "torrentPeersMenu",
118         actions: {
119             addPeer: function(element, ref) {
120                 const hash = torrentsTable.getCurrentTorrentID();
121                 if (!hash)
122                     return;
124                 new MochaUI.Window({
125                     id: "addPeersPage",
126                     icon: "images/qbittorrent-tray.svg",
127                     title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
128                     loadMethod: "iframe",
129                     contentURL: "addpeers.html?hash=" + hash,
130                     scrollbars: false,
131                     resizable: false,
132                     maximizable: false,
133                     paddingVertical: 0,
134                     paddingHorizontal: 0,
135                     width: 350,
136                     height: 240
137                 });
138             },
139             banPeer: function(element, ref) {
140                 const selectedPeers = torrentPeersTable.selectedRowsIds();
141                 if (selectedPeers.length === 0)
142                     return;
144                 if (confirm("QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]")) {
145                     new Request({
146                         url: "api/v2/transfer/banPeers",
147                         method: "post",
148                         data: {
149                             hash: torrentsTable.getCurrentTorrentID(),
150                             peers: selectedPeers.join("|")
151                         }
152                     }).send();
153                 }
154             }
155         },
156         offsets: {
157             x: -15,
158             y: 2
159         },
160         onShow: function() {
161             const selectedPeers = torrentPeersTable.selectedRowsIds();
163             if (selectedPeers.length >= 1) {
164                 this.showItem("copyPeer");
165                 this.showItem("banPeer");
166             }
167             else {
168                 this.hideItem("copyPeer");
169                 this.hideItem("banPeer");
170             }
171         }
172     });
174     new ClipboardJS("#CopyPeerInfo", {
175         text: function(trigger) {
176             return torrentPeersTable.selectedRowsIds().join("\n");
177         }
178     });
180     torrentPeersTable.setup("torrentPeersTableDiv", "torrentPeersTableFixedHeaderDiv", torrentPeersContextMenu);
182     return exports();
183 })();
184 Object.freeze(window.qBittorrent.PropPeers);