Rename WebUI content files
[qBittorrent.git] / src / webui / www / private / scripts / prop-peers.js
blob08f10f5f49f1edbaa16c57983320b165af8372d6
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 let loadTorrentPeersTimer;
32 let syncTorrentPeersLastResponseId = 0;
33 let show_flags = true;
34 const loadTorrentPeersData = function() {
35 if ($('prop_peers').hasClass('invisible')
36 || $('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
37 syncTorrentPeersLastResponseId = 0;
38 torrentPeersTable.clear();
39 return;
41 const current_hash = torrentsTable.getCurrentTorrentHash();
42 if (current_hash === "") {
43 syncTorrentPeersLastResponseId = 0;
44 torrentPeersTable.clear();
45 clearTimeout(loadTorrentPeersTimer);
46 loadTorrentPeersTimer = loadTorrentPeersData.delay(getSyncMainDataInterval());
47 return;
49 const url = new URI('api/v2/sync/torrentPeers');
50 url.setData('rid', syncTorrentPeersLastResponseId);
51 url.setData('hash', current_hash);
52 new Request.JSON({
53 url: url,
54 noCache: true,
55 method: 'get',
56 onComplete: function() {
57 clearTimeout(loadTorrentPeersTimer);
58 loadTorrentPeersTimer = loadTorrentPeersData.delay(getSyncMainDataInterval());
60 onSuccess: function(response) {
61 $('error_div').set('html', '');
62 if (response) {
63 const full_update = (response['full_update'] === true);
64 if (full_update)
65 torrentPeersTable.clear();
66 if (response['rid'])
67 syncTorrentPeersLastResponseId = response['rid'];
68 if (response['peers']) {
69 for (const key in response['peers']) {
70 response['peers'][key]['rowId'] = key;
72 if (response['peers'][key]['client'])
73 response['peers'][key]['client'] = escapeHtml(response['peers'][key]['client']);
75 torrentPeersTable.updateRowData(response['peers'][key]);
78 if (response['peers_removed']) {
79 response['peers_removed'].each(function(hash) {
80 torrentPeersTable.removeRow(hash);
81 });
83 torrentPeersTable.updateTable(full_update);
84 torrentPeersTable.altRow();
86 if (response['show_flags']) {
87 if (show_flags != response['show_flags']) {
88 show_flags = response['show_flags'];
89 torrentPeersTable.columns['country'].force_hide = !show_flags;
90 torrentPeersTable.updateColumn('country');
94 else {
95 torrentPeersTable.clear();
98 }).send();
101 updateTorrentPeersData = function() {
102 clearTimeout(loadTorrentPeersTimer);
103 loadTorrentPeersData();
106 const torrentPeersContextMenu = new ContextMenu({
107 targets: '#torrentPeersTableDiv',
108 menu: 'torrentPeersMenu',
109 actions: {
110 addPeer: function(element, ref) {
111 const hash = torrentsTable.getCurrentTorrentHash();
112 if (!hash)
113 return;
115 new MochaUI.Window({
116 id: 'addPeersPage',
117 title: "QBT_TR(Add Peers)QBT_TR[CONTEXT=PeersAdditionDialog]",
118 loadMethod: 'iframe',
119 contentURL: 'addpeers.html?hash=' + hash,
120 scrollbars: false,
121 resizable: false,
122 maximizable: false,
123 paddingVertical: 0,
124 paddingHorizontal: 0,
125 width: 350,
126 height: 240
129 banPeer: function(element, ref) {
130 const selectedPeers = torrentPeersTable.selectedRowsIds();
131 if (selectedPeers.length === 0)
132 return;
134 if (confirm('QBT_TR(Are you sure you want to permanently ban the selected peers?)QBT_TR[CONTEXT=PeerListWidget]')) {
135 new Request({
136 url: 'api/v2/torrents/banPeers',
137 noCache: true,
138 method: 'post',
139 data: {
140 hash: torrentsTable.getCurrentTorrentHash(),
141 peers: selectedPeers.join('|')
143 }).send();
147 offsets: {
148 x: -15,
149 y: 2
151 onShow: function() {
152 const selectedPeers = torrentPeersTable.selectedRowsIds();
154 if (selectedPeers.length >= 1) {
155 this.showItem('copyPeer');
156 this.showItem('banPeer');
158 else {
159 this.hideItem('copyPeer');
160 this.hideItem('banPeer');
165 new ClipboardJS('#CopyPeerInfo', {
166 text: function(trigger) {
167 return torrentPeersTable.selectedRowsIds().join("\n");
171 torrentPeersTable.setup('torrentPeersTableDiv', 'torrentPeersTableFixedHeaderDiv', torrentPeersContextMenu);