WebUI: Improve accuracy of trackers list
[qBittorrent.git] / src / webui / www / private / scripts / download.js
blobb18ff23a14016ba5794876f6529cf0af54e06234
1 /*
2 * MIT License
3 * Copyright (c) 2008 Ishan Arora <ishan@qbittorrent.org>
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
24 'use strict';
26 if (window.qBittorrent === undefined) {
27 window.qBittorrent = {};
30 window.qBittorrent.Download = (function() {
31 const exports = function() {
32 return {
33 changeCategorySelect: changeCategorySelect,
34 changeTMM: changeTMM
38 let categories = {};
39 let defaultSavePath = "";
41 const getCategories = function() {
42 new Request.JSON({
43 url: 'api/v2/torrents/categories',
44 method: 'get',
45 noCache: true,
46 onSuccess: function(data) {
47 if (data) {
48 categories = data;
49 for (const i in data) {
50 const category = data[i];
51 const option = new Element("option");
52 option.set('value', category.name);
53 option.set('html', category.name);
54 $('categorySelect').appendChild(option);
58 }).send();
61 const getPreferences = function() {
62 const pref = window.parent.qBittorrent.Cache.preferences.get();
64 defaultSavePath = pref.save_path;
65 $('savepath').setProperty('value', defaultSavePath);
66 $('startTorrent').checked = !pref.add_stopped_enabled;
67 $('addToTopOfQueue').checked = pref.add_to_top_of_queue;
69 if (pref.auto_tmm_enabled == 1) {
70 $('autoTMM').selectedIndex = 1;
71 $('savepath').disabled = true;
73 else {
74 $('autoTMM').selectedIndex = 0;
77 if (pref.torrent_stop_condition === "MetadataReceived") {
78 $('stopCondition').selectedIndex = 1;
80 else if (pref.torrent_stop_condition === "FilesChecked") {
81 $('stopCondition').selectedIndex = 2;
83 else {
84 $('stopCondition').selectedIndex = 0;
87 if (pref.torrent_content_layout === "Subfolder") {
88 $('contentLayout').selectedIndex = 1;
90 else if (pref.torrent_content_layout === "NoSubfolder") {
91 $('contentLayout').selectedIndex = 2;
93 else {
94 $('contentLayout').selectedIndex = 0;
98 const changeCategorySelect = function(item) {
99 if (item.value == "\\other") {
100 item.nextElementSibling.hidden = false;
101 item.nextElementSibling.value = "";
102 item.nextElementSibling.select();
104 if ($('autoTMM').selectedIndex == 1)
105 $('savepath').value = defaultSavePath;
107 else {
108 item.nextElementSibling.hidden = true;
109 const text = item.options[item.selectedIndex].textContent;
110 item.nextElementSibling.value = text;
112 if ($('autoTMM').selectedIndex == 1) {
113 const categoryName = item.value;
114 const category = categories[categoryName];
115 let savePath = defaultSavePath;
116 if (category !== undefined)
117 savePath = (category['savePath'] !== "") ? category['savePath'] : `${defaultSavePath}/${categoryName}`;
118 $('savepath').value = savePath;
123 const changeTMM = function(item) {
124 if (item.selectedIndex == 1) {
125 $('savepath').disabled = true;
127 const categorySelect = $('categorySelect');
128 const categoryName = categorySelect.options[categorySelect.selectedIndex].value;
129 const category = categories[categoryName];
130 $('savepath').value = (category === undefined) ? "" : category['savePath'];
132 else {
133 $('savepath').disabled = false;
134 $('savepath').value = defaultSavePath;
138 $(window).addEventListener("load", function() {
139 getPreferences();
140 getCategories();
143 return exports();
144 })();
146 Object.freeze(window.qBittorrent.Download);