WebUI: Use Map instead of Mootools Hash in Torrents table
[qBittorrent.git] / src / webui / www / private / scripts / prop-general.js
blob1c1dde576c2fd96c894edb2af5eea313ca2bfcf4
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2009  Christophe Dumez <chris@qbittorrent.org>
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.PropGeneral ??= (() => {
33     const exports = () => {
34         return {
35             updateData: updateData
36         };
37     };
39     const piecesBar = new window.qBittorrent.PiecesBar.PiecesBar([], {
40         height: 16
41     });
42     $("progress").appendChild(piecesBar);
44     const clearData = function() {
45         $("time_elapsed").textContent = "";
46         $("eta").textContent = "";
47         $("nb_connections").textContent = "";
48         $("total_downloaded").textContent = "";
49         $("total_uploaded").textContent = "";
50         $("dl_speed").textContent = "";
51         $("up_speed").textContent = "";
52         $("dl_limit").textContent = "";
53         $("up_limit").textContent = "";
54         $("total_wasted").textContent = "";
55         $("seeds").textContent = "";
56         $("peers").textContent = "";
57         $("share_ratio").textContent = "";
58         $("popularity").textContent = "";
59         $("reannounce").textContent = "";
60         $("last_seen").textContent = "";
61         $("total_size").textContent = "";
62         $("pieces").textContent = "";
63         $("created_by").textContent = "";
64         $("addition_date").textContent = "";
65         $("completion_date").textContent = "";
66         $("creation_date").textContent = "";
67         $("torrent_hash_v1").textContent = "";
68         $("torrent_hash_v2").textContent = "";
69         $("save_path").textContent = "";
70         $("comment").textContent = "";
71         $("private").textContent = "";
72         piecesBar.clear();
73     };
75     let loadTorrentDataTimer = -1;
76     const loadTorrentData = function() {
77         if ($("propGeneral").hasClass("invisible")
78             || $("propertiesPanel_collapseToggle").hasClass("panel-expand")) {
79             // Tab changed, don't do anything
80             return;
81         }
82         const current_id = torrentsTable.getCurrentTorrentID();
83         if (current_id === "") {
84             clearData();
85             clearTimeout(loadTorrentDataTimer);
86             loadTorrentDataTimer = loadTorrentData.delay(5000);
87             return;
88         }
89         const url = new URI("api/v2/torrents/properties?hash=" + current_id);
90         new Request.JSON({
91             url: url,
92             method: "get",
93             noCache: true,
94             onFailure: function() {
95                 $("error_div").textContent = "QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]";
96                 clearTimeout(loadTorrentDataTimer);
97                 loadTorrentDataTimer = loadTorrentData.delay(10000);
98             },
99             onSuccess: function(data) {
100                 $("error_div").textContent = "";
101                 if (data) {
102                     // Update Torrent data
104                     const timeElapsed = (data.seeding_time > 0)
105                         ? "QBT_TR(%1 (seeded for %2))QBT_TR[CONTEXT=PropertiesWidget]"
106                         .replace("%1", window.qBittorrent.Misc.friendlyDuration(data.time_elapsed))
107                         .replace("%2", window.qBittorrent.Misc.friendlyDuration(data.seeding_time))
108                         : window.qBittorrent.Misc.friendlyDuration(data.time_elapsed);
109                     $("time_elapsed").textContent = timeElapsed;
111                     $("eta").textContent = window.qBittorrent.Misc.friendlyDuration(data.eta, window.qBittorrent.Misc.MAX_ETA);
113                     const nbConnections = "QBT_TR(%1 (%2 max))QBT_TR[CONTEXT=PropertiesWidget]"
114                         .replace("%1", data.nb_connections)
115                         .replace("%2", ((data.nb_connections_limit < 0) ? "∞" : data.nb_connections_limit));
116                     $("nb_connections").textContent = nbConnections;
118                     const totalDownloaded = "QBT_TR(%1 (%2 this session))QBT_TR[CONTEXT=PropertiesWidget]"
119                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.total_downloaded))
120                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.total_downloaded_session));
121                     $("total_downloaded").textContent = totalDownloaded;
123                     const totalUploaded = "QBT_TR(%1 (%2 this session))QBT_TR[CONTEXT=PropertiesWidget]"
124                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.total_uploaded))
125                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.total_uploaded_session));
126                     $("total_uploaded").textContent = totalUploaded;
128                     const dlSpeed = "QBT_TR(%1 (%2 avg.))QBT_TR[CONTEXT=PropertiesWidget]"
129                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.dl_speed, true))
130                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.dl_speed_avg, true));
131                     $("dl_speed").textContent = dlSpeed;
133                     const upSpeed = "QBT_TR(%1 (%2 avg.))QBT_TR[CONTEXT=PropertiesWidget]"
134                         .replace("%1", window.qBittorrent.Misc.friendlyUnit(data.up_speed, true))
135                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.up_speed_avg, true));
136                     $("up_speed").textContent = upSpeed;
138                     const dlLimit = (data.dl_limit === -1)
139                         ? "∞"
140                         : window.qBittorrent.Misc.friendlyUnit(data.dl_limit, true);
141                     $("dl_limit").textContent = dlLimit;
143                     const upLimit = (data.up_limit === -1)
144                         ? "∞"
145                         : window.qBittorrent.Misc.friendlyUnit(data.up_limit, true);
146                     $("up_limit").textContent = upLimit;
148                     $("total_wasted").textContent = window.qBittorrent.Misc.friendlyUnit(data.total_wasted);
150                     const seeds = "QBT_TR(%1 (%2 total))QBT_TR[CONTEXT=PropertiesWidget]"
151                         .replace("%1", data.seeds)
152                         .replace("%2", data.seeds_total);
153                     $("seeds").textContent = seeds;
155                     const peers = "QBT_TR(%1 (%2 total))QBT_TR[CONTEXT=PropertiesWidget]"
156                         .replace("%1", data.peers)
157                         .replace("%2", data.peers_total);
158                     $("peers").textContent = peers;
160                     $("share_ratio").textContent = data.share_ratio.toFixed(2);
162                     $("popularity").textContent = data.popularity.toFixed(2);
164                     $("reannounce").textContent = window.qBittorrent.Misc.friendlyDuration(data.reannounce);
166                     const lastSeen = (data.last_seen >= 0)
167                         ? new Date(data.last_seen * 1000).toLocaleString()
168                         : "QBT_TR(Never)QBT_TR[CONTEXT=PropertiesWidget]";
169                     $("last_seen").textContent = lastSeen;
171                     const totalSize = (data.total_size >= 0) ? window.qBittorrent.Misc.friendlyUnit(data.total_size) : "";
172                     $("total_size").textContent = totalSize;
174                     const pieces = (data.pieces_num >= 0)
175                         ? "QBT_TR(%1 x %2 (have %3))QBT_TR[CONTEXT=PropertiesWidget]"
176                         .replace("%1", data.pieces_num)
177                         .replace("%2", window.qBittorrent.Misc.friendlyUnit(data.piece_size))
178                         .replace("%3", data.pieces_have)
179                         : "";
180                     $("pieces").textContent = pieces;
182                     $("created_by").textContent = data.created_by;
184                     const additionDate = (data.addition_date >= 0)
185                         ? new Date(data.addition_date * 1000).toLocaleString()
186                         : "QBT_TR(Unknown)QBT_TR[CONTEXT=HttpServer]";
187                     $("addition_date").textContent = additionDate;
189                     const completionDate = (data.completion_date >= 0)
190                         ? new Date(data.completion_date * 1000).toLocaleString()
191                         : "";
192                     $("completion_date").textContent = completionDate;
194                     const creationDate = (data.creation_date >= 0)
195                         ? new Date(data.creation_date * 1000).toLocaleString()
196                         : "";
197                     $("creation_date").textContent = creationDate;
199                     const torrentHashV1 = (data.infohash_v1 !== "")
200                         ? data.infohash_v1
201                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]";
202                     $("torrent_hash_v1").textContent = torrentHashV1;
204                     const torrentHashV2 = (data.infohash_v2 !== "")
205                         ? data.infohash_v2
206                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]";
207                     $("torrent_hash_v2").textContent = torrentHashV2;
209                     $("save_path").textContent = data.save_path;
211                     $("comment").innerHTML = window.qBittorrent.Misc.parseHtmlLinks(window.qBittorrent.Misc.escapeHtml(data.comment));
213                     $("private").textContent = (data.has_metadata
214                         ? (data.private
215                             ? "QBT_TR(Yes)QBT_TR[CONTEXT=PropertiesWidget]"
216                             : "QBT_TR(No)QBT_TR[CONTEXT=PropertiesWidget]")
217                         : "QBT_TR(N/A)QBT_TR[CONTEXT=PropertiesWidget]");
218                 }
219                 else {
220                     clearData();
221                 }
222                 clearTimeout(loadTorrentDataTimer);
223                 loadTorrentDataTimer = loadTorrentData.delay(5000);
224             }
225         }).send();
227         const piecesUrl = new URI("api/v2/torrents/pieceStates?hash=" + current_id);
228         new Request.JSON({
229             url: piecesUrl,
230             method: "get",
231             noCache: true,
232             onFailure: function() {
233                 $("error_div").textContent = "QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]";
234                 clearTimeout(loadTorrentDataTimer);
235                 loadTorrentDataTimer = loadTorrentData.delay(10000);
236             },
237             onSuccess: function(data) {
238                 $("error_div").textContent = "";
240                 if (data)
241                     piecesBar.setPieces(data);
242                 else
243                     clearData();
245                 clearTimeout(loadTorrentDataTimer);
246                 loadTorrentDataTimer = loadTorrentData.delay(5000);
247             }
248         }).send();
249     };
251     const updateData = function() {
252         clearTimeout(loadTorrentDataTimer);
253         loadTorrentDataTimer = -1;
254         loadTorrentData();
255     };
257     return exports();
258 })();
259 Object.freeze(window.qBittorrent.PropGeneral);