WebUI: migrate to fetch API
[qBittorrent.git] / src / webui / www / private / scripts / speedslider.js
blob250b70cb18fd65825a9fa486b6984a858e88ff57
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: (hashes) => {
33         if ($("uplimitSliderarea")) {
34             // Get global upload limit
35             fetch("api/v2/transfer/uploadLimit", {
36                     method: "GET",
37                     cache: "no-store"
38                 })
39                 .then(async (response) => {
40                     if (!response.ok)
41                         return;
43                     const data = await response.text();
45                     let maximum = 500;
46                     const tmp = Number(data);
47                     if (tmp > 0) {
48                         maximum = tmp / 1024.0;
49                     }
50                     else {
51                         if (hashes[0] === "global")
52                             maximum = 10000;
53                         else
54                             maximum = 1000;
55                     }
57                     // Get torrents upload limit
58                     // And create slider
59                     if (hashes[0] === "global") {
60                         let up_limit = maximum;
61                         if (up_limit < 0)
62                             up_limit = 0;
63                         maximum = 10000;
64                         new Slider($("uplimitSliderarea"), $("uplimitSliderknob"), {
65                             steps: maximum,
66                             offset: 0,
67                             initialStep: up_limit.round(),
68                             onChange: (pos) => {
69                                 if (pos > 0) {
70                                     $("uplimitUpdatevalue").value = pos;
71                                     $("upLimitUnit").style.visibility = "visible";
72                                 }
73                                 else {
74                                     $("uplimitUpdatevalue").value = "∞";
75                                     $("upLimitUnit").style.visibility = "hidden";
76                                 }
77                             }
78                         });
79                         // Set default value
80                         if (up_limit === 0) {
81                             $("uplimitUpdatevalue").value = "∞";
82                             $("upLimitUnit").style.visibility = "hidden";
83                         }
84                         else {
85                             $("uplimitUpdatevalue").value = up_limit.round();
86                             $("upLimitUnit").style.visibility = "visible";
87                         }
88                     }
89                     else {
90                         fetch("api/v2/torrents/uploadLimit", {
91                                 method: "POST",
92                                 body: new URLSearchParams({
93                                     "hashes": hashes.join("|")
94                                 })
95                             })
96                             .then(async (response) => {
97                                 if (!response.ok)
98                                     return;
100                                 const data = await response.json();
102                                 let up_limit = data[hashes[0]];
103                                 for (const key in data) {
104                                     if (up_limit !== data[key]) {
105                                         up_limit = 0;
106                                         break;
107                                     }
108                                 }
109                                 if (up_limit < 0)
110                                     up_limit = 0;
111                                 new Slider($("uplimitSliderarea"), $("uplimitSliderknob"), {
112                                     steps: maximum,
113                                     offset: 0,
114                                     initialStep: (up_limit / 1024.0).round(),
115                                     onChange: (pos) => {
116                                         if (pos > 0) {
117                                             $("uplimitUpdatevalue").value = pos;
118                                             $("upLimitUnit").style.visibility = "visible";
119                                         }
120                                         else {
121                                             $("uplimitUpdatevalue").value = "∞";
122                                             $("upLimitUnit").style.visibility = "hidden";
123                                         }
124                                     }
125                                 });
126                                 // Set default value
127                                 if (up_limit === 0) {
128                                     $("uplimitUpdatevalue").value = "∞";
129                                     $("upLimitUnit").style.visibility = "hidden";
130                                 }
131                                 else {
132                                     $("uplimitUpdatevalue").value = (up_limit / 1024.0).round();
133                                     $("upLimitUnit").style.visibility = "visible";
134                                 }
135                             });
136                     }
137                 });
138         }
139     },
141     addDlLimitSlider: (hashes) => {
142         if ($("dllimitSliderarea")) {
143             // Get global upload limit
144             fetch("api/v2/transfer/downloadLimit", {
145                     method: "GET",
146                     cache: "no-store"
147                 })
148                 .then(async (response) => {
149                     if (!response.ok)
150                         return;
152                     const data = await response.text();
154                     let maximum = 500;
155                     const tmp = Number(data);
156                     if (tmp > 0) {
157                         maximum = tmp / 1024.0;
158                     }
159                     else {
160                         if (hashes[0] === "global")
161                             maximum = 10000;
162                         else
163                             maximum = 1000;
164                     }
166                     // Get torrents download limit
167                     // And create slider
168                     if (hashes[0] === "global") {
169                         let dl_limit = maximum;
170                         if (dl_limit < 0)
171                             dl_limit = 0;
172                         maximum = 10000;
173                         new Slider($("dllimitSliderarea"), $("dllimitSliderknob"), {
174                             steps: maximum,
175                             offset: 0,
176                             initialStep: dl_limit.round(),
177                             onChange: (pos) => {
178                                 if (pos > 0) {
179                                     $("dllimitUpdatevalue").value = pos;
180                                     $("dlLimitUnit").style.visibility = "visible";
181                                 }
182                                 else {
183                                     $("dllimitUpdatevalue").value = "∞";
184                                     $("dlLimitUnit").style.visibility = "hidden";
185                                 }
186                             }
187                         });
188                         // Set default value
189                         if (dl_limit === 0) {
190                             $("dllimitUpdatevalue").value = "∞";
191                             $("dlLimitUnit").style.visibility = "hidden";
192                         }
193                         else {
194                             $("dllimitUpdatevalue").value = dl_limit.round();
195                             $("dlLimitUnit").style.visibility = "visible";
196                         }
197                     }
198                     else {
199                         fetch("api/v2/torrents/downloadLimit", {
200                                 method: "POST",
201                                 body: new URLSearchParams({
202                                     "hashes": hashes.join("|")
203                                 })
204                             })
205                             .then(async (response) => {
206                                 if (!response.ok)
207                                     return;
209                                 const data = await response.json();
211                                 let dl_limit = data[hashes[0]];
212                                 for (const key in data) {
213                                     if (dl_limit !== data[key]) {
214                                         dl_limit = 0;
215                                         break;
216                                     }
217                                 }
218                                 if (dl_limit < 0)
219                                     dl_limit = 0;
220                                 new Slider($("dllimitSliderarea"), $("dllimitSliderknob"), {
221                                     steps: maximum,
222                                     offset: 0,
223                                     initialStep: (dl_limit / 1024.0).round(),
224                                     onChange: (pos) => {
225                                         if (pos > 0) {
226                                             $("dllimitUpdatevalue").value = pos;
227                                             $("dlLimitUnit").style.visibility = "visible";
228                                         }
229                                         else {
230                                             $("dllimitUpdatevalue").value = "∞";
231                                             $("dlLimitUnit").style.visibility = "hidden";
232                                         }
233                                     }
234                                 });
235                                 // Set default value
236                                 if (dl_limit === 0) {
237                                     $("dllimitUpdatevalue").value = "∞";
238                                     $("dlLimitUnit").style.visibility = "hidden";
239                                 }
240                                 else {
241                                     $("dllimitUpdatevalue").value = (dl_limit / 1024.0).round();
242                                     $("dlLimitUnit").style.visibility = "visible";
243                                 }
244                             });
245                     }
246                 });
247         }
248     }