WebUI: Use Map instead of Mootools Hash in Torrents table
[qBittorrent.git] / src / webui / www / private / scripts / speedslider.js
blob8cbc45e90a72d28094fa67b213c1349e84373e00
1 /*
2  * Bittorrent Client using Qt and libtorrent.
3  * Copyright (C) 2019  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 MochaUI.extend({
32     addUpLimitSlider: function(hashes) {
33         if ($("uplimitSliderarea")) {
34             // Get global upload limit
35             let maximum = 500;
36             new Request({
37                 url: "api/v2/transfer/uploadLimit",
38                 method: "get",
39                 data: {},
40                 onSuccess: function(data) {
41                     if (data) {
42                         const tmp = data.toInt();
43                         if (tmp > 0) {
44                             maximum = tmp / 1024.0;
45                         }
46                         else {
47                             if (hashes[0] === "global")
48                                 maximum = 10000;
49                             else
50                                 maximum = 1000;
51                         }
52                     }
53                     // Get torrents upload limit
54                     // And create slider
55                     if (hashes[0] === "global") {
56                         let up_limit = maximum;
57                         if (up_limit < 0)
58                             up_limit = 0;
59                         maximum = 10000;
60                         new Slider($("uplimitSliderarea"), $("uplimitSliderknob"), {
61                             steps: maximum,
62                             offset: 0,
63                             initialStep: up_limit.round(),
64                             onChange: function(pos) {
65                                 if (pos > 0) {
66                                     $("uplimitUpdatevalue").value = pos;
67                                     $("upLimitUnit").style.visibility = "visible";
68                                 }
69                                 else {
70                                     $("uplimitUpdatevalue").value = "∞";
71                                     $("upLimitUnit").style.visibility = "hidden";
72                                 }
73                             }.bind(this)
74                         });
75                         // Set default value
76                         if (up_limit === 0) {
77                             $("uplimitUpdatevalue").value = "∞";
78                             $("upLimitUnit").style.visibility = "hidden";
79                         }
80                         else {
81                             $("uplimitUpdatevalue").value = up_limit.round();
82                             $("upLimitUnit").style.visibility = "visible";
83                         }
84                     }
85                     else {
86                         new Request.JSON({
87                             url: "api/v2/torrents/uploadLimit",
88                             method: "post",
89                             data: {
90                                 hashes: hashes.join("|")
91                             },
92                             onSuccess: function(data) {
93                                 if (data) {
94                                     let up_limit = data[hashes[0]];
95                                     for (const key in data) {
96                                         if (up_limit !== data[key]) {
97                                             up_limit = 0;
98                                             break;
99                                         }
100                                     }
101                                     if (up_limit < 0)
102                                         up_limit = 0;
103                                     new Slider($("uplimitSliderarea"), $("uplimitSliderknob"), {
104                                         steps: maximum,
105                                         offset: 0,
106                                         initialStep: (up_limit / 1024.0).round(),
107                                         onChange: function(pos) {
108                                             if (pos > 0) {
109                                                 $("uplimitUpdatevalue").value = pos;
110                                                 $("upLimitUnit").style.visibility = "visible";
111                                             }
112                                             else {
113                                                 $("uplimitUpdatevalue").value = "∞";
114                                                 $("upLimitUnit").style.visibility = "hidden";
115                                             }
116                                         }.bind(this)
117                                     });
118                                     // Set default value
119                                     if (up_limit === 0) {
120                                         $("uplimitUpdatevalue").value = "∞";
121                                         $("upLimitUnit").style.visibility = "hidden";
122                                     }
123                                     else {
124                                         $("uplimitUpdatevalue").value = (up_limit / 1024.0).round();
125                                         $("upLimitUnit").style.visibility = "visible";
126                                     }
127                                 }
128                             }
129                         }).send();
130                     }
131                 }
132             }).send();
133         }
134     },
136     addDlLimitSlider: function(hashes) {
137         if ($("dllimitSliderarea")) {
138             // Get global upload limit
139             let maximum = 500;
140             new Request({
141                 url: "api/v2/transfer/downloadLimit",
142                 method: "get",
143                 data: {},
144                 onSuccess: function(data) {
145                     if (data) {
146                         const tmp = data.toInt();
147                         if (tmp > 0) {
148                             maximum = tmp / 1024.0;
149                         }
150                         else {
151                             if (hashes[0] === "global")
152                                 maximum = 10000;
153                             else
154                                 maximum = 1000;
155                         }
156                     }
157                     // Get torrents download limit
158                     // And create slider
159                     if (hashes[0] === "global") {
160                         let dl_limit = maximum;
161                         if (dl_limit < 0)
162                             dl_limit = 0;
163                         maximum = 10000;
164                         new Slider($("dllimitSliderarea"), $("dllimitSliderknob"), {
165                             steps: maximum,
166                             offset: 0,
167                             initialStep: dl_limit.round(),
168                             onChange: function(pos) {
169                                 if (pos > 0) {
170                                     $("dllimitUpdatevalue").value = pos;
171                                     $("dlLimitUnit").style.visibility = "visible";
172                                 }
173                                 else {
174                                     $("dllimitUpdatevalue").value = "∞";
175                                     $("dlLimitUnit").style.visibility = "hidden";
176                                 }
177                             }.bind(this)
178                         });
179                         // Set default value
180                         if (dl_limit === 0) {
181                             $("dllimitUpdatevalue").value = "∞";
182                             $("dlLimitUnit").style.visibility = "hidden";
183                         }
184                         else {
185                             $("dllimitUpdatevalue").value = dl_limit.round();
186                             $("dlLimitUnit").style.visibility = "visible";
187                         }
188                     }
189                     else {
190                         new Request.JSON({
191                             url: "api/v2/torrents/downloadLimit",
192                             method: "post",
193                             data: {
194                                 hashes: hashes.join("|")
195                             },
196                             onSuccess: function(data) {
197                                 if (data) {
198                                     let dl_limit = data[hashes[0]];
199                                     for (const key in data) {
200                                         if (dl_limit !== data[key]) {
201                                             dl_limit = 0;
202                                             break;
203                                         }
204                                     }
205                                     if (dl_limit < 0)
206                                         dl_limit = 0;
207                                     new Slider($("dllimitSliderarea"), $("dllimitSliderknob"), {
208                                         steps: maximum,
209                                         offset: 0,
210                                         initialStep: (dl_limit / 1024.0).round(),
211                                         onChange: function(pos) {
212                                             if (pos > 0) {
213                                                 $("dllimitUpdatevalue").value = pos;
214                                                 $("dlLimitUnit").style.visibility = "visible";
215                                             }
216                                             else {
217                                                 $("dllimitUpdatevalue").value = "∞";
218                                                 $("dlLimitUnit").style.visibility = "hidden";
219                                             }
220                                         }.bind(this)
221                                     });
222                                     // Set default value
223                                     if (dl_limit === 0) {
224                                         $("dllimitUpdatevalue").value = "∞";
225                                         $("dlLimitUnit").style.visibility = "hidden";
226                                     }
227                                     else {
228                                         $("dllimitUpdatevalue").value = (dl_limit / 1024.0).round();
229                                         $("dlLimitUnit").style.visibility = "visible";
230                                     }
231                                 }
232                             }
233                         }).send();
234                     }
235                 }
236             }).send();
237         }
238     }