Display External IP Address in status bar
[qBittorrent.git] / src / webui / www / private / scripts / prop-webseeds.js
blob7cebc49c83ba278bde1abb7e617fb78c8b287a4a
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2015 Diego de las Heras <ngosang@hotmail.es>
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.PropWebseeds ??= (() => {
33 const exports = () => {
34 return {
35 updateData: updateData,
36 clear: clear
40 const torrentWebseedsTable = new window.qBittorrent.DynamicTable.TorrentWebseedsTable();
42 let current_hash = "";
44 let loadWebSeedsDataTimer = -1;
45 const loadWebSeedsData = () => {
46 if ($("propWebSeeds").hasClass("invisible")
47 || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
48 // Tab changed, don't do anything
49 return;
51 const new_hash = torrentsTable.getCurrentTorrentID();
52 if (new_hash === "") {
53 torrentWebseedsTable.clear();
54 clearTimeout(loadWebSeedsDataTimer);
55 return;
57 if (new_hash !== current_hash) {
58 torrentWebseedsTable.clear();
59 current_hash = new_hash;
61 new Request.JSON({
62 url: new URI("api/v2/torrents/webseeds").setData("hash", current_hash),
63 method: "get",
64 noCache: true,
65 onComplete: () => {
66 clearTimeout(loadWebSeedsDataTimer);
67 loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
69 onSuccess: (webseeds) => {
70 const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
71 torrentWebseedsTable.clear();
73 if (webseeds) {
74 // Update WebSeeds data
75 webseeds.each((webseed) => {
76 torrentWebseedsTable.updateRowData({
77 rowId: webseed.url,
78 url: webseed.url,
79 });
80 });
83 torrentWebseedsTable.updateTable(false);
85 if (selectedWebseeds.length > 0)
86 torrentWebseedsTable.reselectRows(selectedWebseeds);
88 }).send();
91 const updateData = () => {
92 clearTimeout(loadWebSeedsDataTimer);
93 loadWebSeedsDataTimer = -1;
94 loadWebSeedsData();
97 const torrentWebseedsContextMenu = new window.qBittorrent.ContextMenu.ContextMenu({
98 targets: "#torrentWebseedsTableDiv",
99 menu: "torrentWebseedsMenu",
100 actions: {
101 AddWebSeeds: (element, ref) => {
102 addWebseedFN();
104 EditWebSeed: (element, ref) => {
105 // only allow editing of one row
106 element.firstChild.click();
107 editWebSeedFN(element);
109 RemoveWebSeed: (element, ref) => {
110 removeWebSeedFN(element);
113 offsets: {
114 x: 0,
115 y: 2
117 onShow: function() {
118 const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
120 if (selectedWebseeds.length === 0) {
121 this.hideItem("EditWebSeed");
122 this.hideItem("RemoveWebSeed");
123 this.hideItem("CopyWebseedUrl");
125 else {
126 if (selectedWebseeds.length === 1)
127 this.showItem("EditWebSeed");
128 else
129 this.hideItem("EditWebSeed");
131 this.showItem("RemoveWebSeed");
132 this.showItem("CopyWebseedUrl");
137 const addWebseedFN = () => {
138 if (current_hash.length === 0)
139 return;
141 new MochaUI.Window({
142 id: "webseedsPage",
143 title: "QBT_TR(Add web seeds)QBT_TR[CONTEXT=HttpServer]",
144 loadMethod: "iframe",
145 contentURL: "addwebseeds.html?hash=" + current_hash,
146 scrollbars: true,
147 resizable: false,
148 maximizable: false,
149 closable: true,
150 paddingVertical: 0,
151 paddingHorizontal: 0,
152 width: 500,
153 height: 260,
154 onCloseComplete: () => {
155 updateData();
160 const editWebSeedFN = (element) => {
161 if (current_hash.length === 0)
162 return;
164 const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
165 if (selectedWebseeds.length > 1)
166 return;
168 const webseedUrl = selectedWebseeds[0];
170 new MochaUI.Window({
171 id: "webseedsPage",
172 title: "QBT_TR(Web seed editing)QBT_TR[CONTEXT=PropertiesWidget]",
173 loadMethod: "iframe",
174 contentURL: "editwebseed.html?hash=" + current_hash + "&url=" + encodeURIComponent(webseedUrl),
175 scrollbars: true,
176 resizable: false,
177 maximizable: false,
178 closable: true,
179 paddingVertical: 0,
180 paddingHorizontal: 0,
181 width: 500,
182 height: 150,
183 onCloseComplete: () => {
184 updateData();
189 const removeWebSeedFN = (element) => {
190 if (current_hash.length === 0)
191 return;
193 const selectedWebseeds = torrentWebseedsTable.selectedRowsIds();
194 new Request({
195 url: "api/v2/torrents/removeWebSeeds",
196 method: "post",
197 data: {
198 hash: current_hash,
199 urls: selectedWebseeds.map(webseed => encodeURIComponent(webseed)).join("|")
201 onSuccess: () => {
202 updateData();
204 }).send();
207 const clear = () => {
208 torrentWebseedsTable.clear();
211 new ClipboardJS("#CopyWebseedUrl", {
212 text: (trigger) => {
213 return torrentWebseedsTable.selectedRowsIds().join("\n");
217 torrentWebseedsTable.setup("torrentWebseedsTableDiv", "torrentWebseedsTableFixedHeaderDiv", torrentWebseedsContextMenu);
219 return exports();
220 })();
221 Object.freeze(window.qBittorrent.PropWebseeds);