1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-16 17:50:01 +00:00

768 lines
30 KiB
JavaScript
Raw Normal View History

2008-07-03 14:59:31 +00:00
/*
* MIT License
* Copyright (c) 2008 Ishan Arora <ishan@qbittorrent.org>,
* Christophe Dumez <chris@qbittorrent.org>
2008-07-03 14:59:31 +00:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
2008-07-03 14:59:31 +00:00
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
2008-07-03 14:59:31 +00:00
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
2008-07-04 07:48:15 +00:00
*/
2008-07-03 14:59:31 +00:00
torrentsTable = new TorrentsTable();
2015-11-13 15:02:38 +03:00
torrentPeersTable = new TorrentPeersTable();
2008-12-24 11:28:02 +00:00
2018-04-05 11:59:31 +08:00
var updatePropertiesPanel = function() {};
2018-04-05 11:59:31 +08:00
var updateTorrentData = function() {};
var updateTrackersData = function() {};
var updateTorrentPeersData = function() {};
var updateWebSeedsData = function() {};
var updateTorrentFilesData = function() {};
2018-04-05 11:59:31 +08:00
var updateMainData = function() {};
var alternativeSpeedLimits = false;
var queueing_enabled = true;
var syncMainDataTimerPeriod = 1500;
var clipboardEvent;
var CATEGORIES_ALL = 1;
var CATEGORIES_UNCATEGORIZED = 2;
var category_list = {};
var selected_category = CATEGORIES_ALL;
2018-04-05 11:59:31 +08:00
var setCategoryFilter = function() {};
var selected_filter = getLocalStorageItem('selected_filter', 'all');
2018-04-05 11:59:31 +08:00
var setFilter = function() {};
2018-04-05 11:59:31 +08:00
var loadSelectedCategory = function() {
selected_category = getLocalStorageItem('selected_category', CATEGORIES_ALL);
};
loadSelectedCategory();
function genHash(string) {
var hash = 0;
for (var i = 0; i < string.length; ++i) {
var c = string.charCodeAt(i);
hash = (c + hash * 31) | 0;
}
return hash;
}
2018-04-05 11:59:31 +08:00
window.addEvent('load', function() {
2018-04-05 11:59:31 +08:00
var saveColumnSizes = function() {
var filters_width = $('Filters').getSize().x;
var properties_height_rel = $('propertiesPanel').getSize().y / Window.getSize().y;
localStorage.setItem('filters_width', filters_width);
localStorage.setItem('properties_height_rel', properties_height_rel);
};
window.addEvent('resize', function() {
// Resizing might takes some time.
saveColumnSizes.delay(200);
});
/*MochaUI.Desktop = new MochaUI.Desktop();
MochaUI.Desktop.desktop.setStyles({
'background': '#fff',
'visibility': 'visible'
});*/
MochaUI.Desktop.initialize();
var filt_w = localStorage.getItem('filters_width');
if ($defined(filt_w))
filt_w = filt_w.toInt();
else
filt_w = 120;
new MochaUI.Column({
2018-04-05 11:59:31 +08:00
id: 'filtersColumn',
placement: 'left',
onResize: saveColumnSizes,
width: filt_w,
resizeLimit: [100, 300]
});
new MochaUI.Column({
2018-04-05 11:59:31 +08:00
id: 'mainColumn',
placement: 'main',
width: null,
resizeLimit: [100, 300]
});
2014-12-08 04:31:04 +03:00
setCategoryFilter = function(hash) {
selected_category = hash;
localStorage.setItem('selected_category', selected_category);
highlightSelectedCategory();
if (typeof torrentsTable.tableBody != 'undefined')
updateMainData();
};
2018-04-05 11:59:31 +08:00
setFilter = function(f) {
2014-12-08 04:31:04 +03:00
// Visually Select the right filter
$("all_filter").removeClass("selectedFilter");
$("downloading_filter").removeClass("selectedFilter");
2015-03-29 11:57:36 +01:00
$("seeding_filter").removeClass("selectedFilter");
2014-12-08 04:31:04 +03:00
$("completed_filter").removeClass("selectedFilter");
$("paused_filter").removeClass("selectedFilter");
2015-01-29 20:30:54 +03:00
$("resumed_filter").removeClass("selectedFilter");
2014-12-08 04:31:04 +03:00
$("active_filter").removeClass("selectedFilter");
$("inactive_filter").removeClass("selectedFilter");
2015-12-07 04:05:52 +03:00
$("errored_filter").removeClass("selectedFilter");
2014-12-08 04:31:04 +03:00
$(f + "_filter").addClass("selectedFilter");
selected_filter = f;
2014-12-08 04:31:04 +03:00
localStorage.setItem('selected_filter', f);
// Reload torrents
if (typeof torrentsTable.tableBody != 'undefined')
2015-01-06 23:24:54 +03:00
updateMainData();
};
2014-12-08 04:31:04 +03:00
new MochaUI.Panel({
2018-04-05 11:59:31 +08:00
id: 'Filters',
title: 'Panel',
header: false,
padding: {
top: 0,
right: 0,
bottom: 0,
left: 0
},
2018-04-05 11:59:31 +08:00
loadMethod: 'xhr',
contentURL: 'filters.html',
onContentLoaded: function() {
setFilter(selected_filter);
2014-12-08 04:31:04 +03:00
},
2018-04-05 11:59:31 +08:00
column: 'filtersColumn',
height: 300
});
initializeWindows();
2015-06-17 01:01:57 +02:00
// Show Top Toolbar is enabled by default
var showTopToolbar = true;
if (localStorage.getItem('show_top_toolbar') !== null)
showTopToolbar = localStorage.getItem('show_top_toolbar') == "true";
2015-06-17 01:01:57 +02:00
if (!showTopToolbar) {
$('showTopToolbarLink').firstChild.style.opacity = '0';
$('mochaToolbar').addClass('invisible');
}
// Show Status Bar is enabled by default
var showStatusBar = true;
if (localStorage.getItem('show_status_bar') !== null)
showStatusBar = localStorage.getItem('show_status_bar') === "true";
if (!showStatusBar) {
$('showStatusBarLink').firstChild.style.opacity = '0';
$('desktopFooterWrapper').addClass('invisible');
}
var speedInTitle = localStorage.getItem('speed_in_browser_title_bar') == "true";
if (!speedInTitle)
$('speedInBrowserTitleBarLink').firstChild.style.opacity = '0';
// After showing/hiding the toolbar + status bar
2015-06-17 01:01:57 +02:00
MochaUI.Desktop.setDesktopSize();
2015-01-06 23:24:54 +03:00
var syncMainDataLastResponseId = 0;
var serverState = {};
var removeTorrentFromCategoryList = function(hash) {
if (hash === null || hash === "")
return false;
var removed = false;
Object.each(category_list, function(category) {
if (Object.contains(category.torrents, hash)) {
removed = true;
category.torrents.splice(category.torrents.indexOf(hash), 1);
}
});
return removed;
};
var addTorrentToCategoryList = function(torrent) {
var category = torrent['category'];
if (typeof category === 'undefined')
return false;
if (category.length === 0) { // Empty category
removeTorrentFromCategoryList(torrent['hash']);
return true;
}
var categoryHash = genHash(category);
if (category_list[categoryHash] === null) // This should not happen
2018-04-05 11:59:31 +08:00
category_list[categoryHash] = {
name: category,
torrents: []
};
if (!Object.contains(category_list[categoryHash].torrents, torrent['hash'])) {
removeTorrentFromCategoryList(torrent['hash']);
category_list[categoryHash].torrents = category_list[categoryHash].torrents.combine([torrent['hash']]);
return true;
}
return false;
};
2015-12-07 04:05:52 +03:00
var updateFilter = function(filter, filterTitle) {
$(filter + '_filter').firstChild.childNodes[1].nodeValue = filterTitle.replace('%1', torrentsTable.getFilteredTorrentsNumber(filter, CATEGORIES_ALL));
2015-12-07 04:05:52 +03:00
};
var updateFiltersList = function() {
updateFilter('all', 'QBT_TR(All (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('downloading', 'QBT_TR(Downloading (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('seeding', 'QBT_TR(Seeding (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('completed', 'QBT_TR(Completed (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('resumed', 'QBT_TR(Resumed (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('paused', 'QBT_TR(Paused (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('active', 'QBT_TR(Active (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('inactive', 'QBT_TR(Inactive (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
updateFilter('errored', 'QBT_TR(Errored (%1))QBT_TR[CONTEXT=StatusFilterWidget]');
2015-12-07 04:05:52 +03:00
};
var updateCategoryList = function() {
var categoryList = $('filterCategoryList');
if (!categoryList)
return;
categoryList.empty();
var create_link = function(hash, text, count) {
2018-04-05 11:59:31 +08:00
var html = '<a href="#" onclick="setCategoryFilter(' + hash + ');return false;">'
+ '<img src="theme/inode-directory"/>'
+ escapeHtml(text) + ' (' + count + ')' + '</a>';
var el = new Element('li', {
id: hash,
html: html
});
categoriesFilterContextMenu.addTarget(el);
return el;
};
var all = torrentsTable.getRowIds().length;
var uncategorized = 0;
Object.each(torrentsTable.rows, function(row) {
if (row['full_data'].category.length === 0)
uncategorized += 1;
});
categoryList.appendChild(create_link(CATEGORIES_ALL, 'QBT_TR(All)QBT_TR[CONTEXT=CategoryFilterModel]', all));
categoryList.appendChild(create_link(CATEGORIES_UNCATEGORIZED, 'QBT_TR(Uncategorized)QBT_TR[CONTEXT=CategoryFilterModel]', uncategorized));
var sortedCategories = [];
Object.each(category_list, function(category) {
sortedCategories.push(category.name);
});
sortedCategories.sort();
Object.each(sortedCategories, function(categoryName) {
var categoryHash = genHash(categoryName);
var categoryCount = category_list[categoryHash].torrents.length;
categoryList.appendChild(create_link(categoryHash, categoryName, categoryCount));
});
highlightSelectedCategory();
};
var highlightSelectedCategory = function() {
var categoryList = $('filterCategoryList');
if (!categoryList)
return;
var childrens = categoryList.childNodes;
for (var i in childrens) {
if (childrens[i].id == selected_category)
childrens[i].className = "selectedFilter";
else
childrens[i].className = "";
}
};
2015-01-06 23:24:54 +03:00
var syncMainDataTimer;
2018-04-05 11:59:31 +08:00
var syncMainData = function() {
var url = new URI('api/v2/sync/maindata');
2015-01-06 23:24:54 +03:00
url.setData('rid', syncMainDataLastResponseId);
var request = new Request.JSON({
2018-04-05 11:59:31 +08:00
url: url,
noCache: true,
method: 'get',
onFailure: function() {
var errorDiv = $('error_div');
if (errorDiv)
errorDiv.set('html', 'QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]');
2015-01-06 23:24:54 +03:00
clearTimeout(syncMainDataTimer);
syncMainDataTimer = syncMainData.delay(2000);
},
2018-04-05 11:59:31 +08:00
onSuccess: function(response) {
$('error_div').set('html', '');
if (response) {
var torrentsTableSelectedRows;
var update_categories = false;
var full_update = (response['full_update'] === true);
if (full_update) {
torrentsTableSelectedRows = torrentsTable.selectedRowsIds();
2015-11-13 15:02:38 +03:00
torrentsTable.clear();
category_list = {};
}
if (response['rid']) {
2015-01-06 23:24:54 +03:00
syncMainDataLastResponseId = response['rid'];
}
if (response['categories']) {
response['categories'].each(function(category) {
var categoryHash = genHash(category);
2018-04-05 11:59:31 +08:00
category_list[categoryHash] = {
name: category,
torrents: []
};
});
update_categories = true;
}
if (response['categories_removed']) {
response['categories_removed'].each(function(category) {
var categoryHash = genHash(category);
delete category_list[categoryHash];
});
update_categories = true;
}
if (response['torrents']) {
var updateTorrentList = false;
2015-01-06 23:24:54 +03:00
for (var key in response['torrents']) {
response['torrents'][key]['hash'] = key;
response['torrents'][key]['rowId'] = key;
if (response['torrents'][key]['state'])
response['torrents'][key]['status'] = response['torrents'][key]['state'];
torrentsTable.updateRowData(response['torrents'][key]);
if (addTorrentToCategoryList(response['torrents'][key]))
update_categories = true;
if (response['torrents'][key]['name'])
updateTorrentList = true;
2015-01-06 23:24:54 +03:00
}
if (updateTorrentList)
setupCopyEventHandler();
}
2015-01-06 23:24:54 +03:00
if (response['torrents_removed'])
2018-04-05 11:59:31 +08:00
response['torrents_removed'].each(function(hash) {
torrentsTable.removeRow(hash);
removeTorrentFromCategoryList(hash);
2018-03-14 23:15:51 +08:00
update_categories = true; // Always to update All category
2015-01-06 23:24:54 +03:00
});
torrentsTable.updateTable(full_update);
torrentsTable.altRow();
2015-01-06 23:24:54 +03:00
if (response['server_state']) {
var tmp = response['server_state'];
2018-04-05 11:59:31 +08:00
for (var k in tmp)
serverState[k] = tmp[k];
2015-01-06 23:24:54 +03:00
processServerState();
}
2015-12-07 04:05:52 +03:00
updateFiltersList();
if (update_categories) {
updateCategoryList();
2016-01-20 16:13:54 +03:00
torrentsTableContextMenu.updateCategoriesSubMenu(category_list);
}
if (full_update)
// re-select previously selected rows
torrentsTable.reselectRows(torrentsTableSelectedRows);
}
2015-01-06 23:24:54 +03:00
clearTimeout(syncMainDataTimer);
syncMainDataTimer = syncMainData.delay(syncMainDataTimerPeriod);
}
}).send();
};
2015-01-06 23:24:54 +03:00
updateMainData = function() {
torrentsTable.updateTable();
2015-01-06 23:24:54 +03:00
clearTimeout(syncMainDataTimer);
syncMainDataTimer = syncMainData.delay(100);
};
2018-04-05 11:59:31 +08:00
var processServerState = function() {
var transfer_info = friendlyUnit(serverState.dl_info_speed, true);
2015-01-06 23:24:54 +03:00
if (serverState.dl_rate_limit > 0)
transfer_info += " [" + friendlyUnit(serverState.dl_rate_limit, true) + "]";
transfer_info += " (" + friendlyUnit(serverState.dl_info_data, false) + ")";
2015-01-06 23:24:54 +03:00
$("DlInfos").set('html', transfer_info);
transfer_info = friendlyUnit(serverState.up_info_speed, true);
2015-01-06 23:24:54 +03:00
if (serverState.up_rate_limit > 0)
transfer_info += " [" + friendlyUnit(serverState.up_rate_limit, true) + "]";
transfer_info += " (" + friendlyUnit(serverState.up_info_data, false) + ")";
2015-01-06 23:24:54 +03:00
$("UpInfos").set('html', transfer_info);
2015-05-23 19:20:04 +02:00
if (speedInTitle) {
document.title = "QBT_TR([D: %1, U: %2] qBittorrent %3)QBT_TR[CONTEXT=MainWindow]".replace("%1", friendlyUnit(serverState.dl_info_speed, true)).replace("%2", friendlyUnit(serverState.up_info_speed, true)).replace("%3", "${VERSION}");
document.title += " QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]";
2018-04-05 11:59:31 +08:00
}
else
document.title = "qBittorrent ${VERSION} QBT_TR(Web UI)QBT_TR[CONTEXT=OptionsDialog]";
$('DHTNodes').set('html', 'QBT_TR(DHT: %1 nodes)QBT_TR[CONTEXT=StatusBar]'.replace("%1", serverState.dht_nodes));
2017-03-06 03:05:18 +00:00
// Statistics dialog
2017-03-06 03:05:18 +00:00
if (document.getElementById("statisticspage")) {
2017-12-26 18:55:13 -05:00
$('AlltimeDL').set('html', friendlyUnit(serverState.alltime_dl, false));
$('AlltimeUL').set('html', friendlyUnit(serverState.alltime_ul, false));
$('TotalWastedSession').set('html', friendlyUnit(serverState.total_wasted_session, false));
$('GlobalRatio').set('html', serverState.global_ratio);
$('TotalPeerConnections').set('html', serverState.total_peer_connections);
2018-02-23 14:46:47 -05:00
$('ReadCacheHits').set('html', serverState.read_cache_hits + "%");
2017-12-26 18:55:13 -05:00
$('TotalBuffersSize').set('html', friendlyUnit(serverState.total_buffers_size, false));
2017-12-26 19:05:29 -05:00
$('WriteCacheOverload').set('html', serverState.write_cache_overload + "%");
$('ReadCacheOverload').set('html', serverState.read_cache_overload + "%");
2017-12-26 18:55:13 -05:00
$('QueuedIOJobs').set('html', serverState.queued_io_jobs);
2017-12-26 19:05:29 -05:00
$('AverageTimeInQueue').set('html', serverState.average_time_queue + " ms");
2017-12-26 18:55:13 -05:00
$('TotalQueuedSize').set('html', friendlyUnit(serverState.total_queued_size, false));
2017-03-06 03:05:18 +00:00
}
2015-01-06 23:24:54 +03:00
if (serverState.connection_status == "connected")
$('connectionStatus').src = 'images/skin/connected.png';
else if (serverState.connection_status == "firewalled")
$('connectionStatus').src = 'images/skin/firewalled.png';
else
$('connectionStatus').src = 'images/skin/disconnected.png';
if (queueing_enabled != serverState.queueing) {
queueing_enabled = serverState.queueing;
torrentsTable.columns['priority'].force_hide = !queueing_enabled;
torrentsTable.updateColumn('priority');
if (queueing_enabled) {
2017-11-02 12:14:27 +03:00
$('topPrioItem').removeClass('invisible');
$('increasePrioItem').removeClass('invisible');
$('decreasePrioItem').removeClass('invisible');
$('bottomPrioItem').removeClass('invisible');
$('queueingButtons').removeClass('invisible');
$('queueingMenuItems').removeClass('invisible');
}
else {
2017-11-02 12:14:27 +03:00
$('topPrioItem').addClass('invisible');
$('increasePrioItem').addClass('invisible');
$('decreasePrioItem').addClass('invisible');
$('bottomPrioItem').addClass('invisible');
$('queueingButtons').addClass('invisible');
$('queueingMenuItems').addClass('invisible');
}
}
if (alternativeSpeedLimits != serverState.use_alt_speed_limits) {
alternativeSpeedLimits = serverState.use_alt_speed_limits;
updateAltSpeedIcon(alternativeSpeedLimits);
}
syncMainDataTimerPeriod = serverState.refresh_interval;
if (syncMainDataTimerPeriod < 500)
syncMainDataTimerPeriod = 500;
};
var updateAltSpeedIcon = function(enabled) {
if (enabled)
$('alternativeSpeedLimits').src = "images/slow.png";
else
$('alternativeSpeedLimits').src = "images/slow_off.png";
};
$('alternativeSpeedLimits').addEvent('click', function() {
// Change icon immediately to give some feedback
updateAltSpeedIcon(!alternativeSpeedLimits);
2018-04-05 11:59:31 +08:00
new Request({
url: 'api/v2/transfer/toggleSpeedLimitsMode',
method: 'post',
onComplete: function() {
alternativeSpeedLimits = !alternativeSpeedLimits;
updateMainData();
},
onFailure: function() {
// Restore icon in case of failure
updateAltSpeedIcon(alternativeSpeedLimits);
}
}).send();
});
$('DlInfos').addEvent('click', globalDownloadLimitFN);
$('UpInfos').addEvent('click', globalUploadLimitFN);
2015-06-17 01:01:57 +02:00
$('showTopToolbarLink').addEvent('click', function(e) {
showTopToolbar = !showTopToolbar;
localStorage.setItem('show_top_toolbar', showTopToolbar.toString());
if (showTopToolbar) {
$('showTopToolbarLink').firstChild.style.opacity = '1';
$('mochaToolbar').removeClass('invisible');
}
else {
$('showTopToolbarLink').firstChild.style.opacity = '0';
$('mochaToolbar').addClass('invisible');
}
MochaUI.Desktop.setDesktopSize();
});
$('showStatusBarLink').addEvent('click', function(e) {
showStatusBar = !showStatusBar;
localStorage.setItem('show_status_bar', showStatusBar.toString());
if (showStatusBar) {
$('showStatusBarLink').firstChild.style.opacity = '1';
$('desktopFooterWrapper').removeClass('invisible');
}
else {
$('showStatusBarLink').firstChild.style.opacity = '0';
$('desktopFooterWrapper').addClass('invisible');
}
MochaUI.Desktop.setDesktopSize();
});
$('speedInBrowserTitleBarLink').addEvent('click', function(e) {
speedInTitle = !speedInTitle;
localStorage.setItem('speed_in_browser_title_bar', speedInTitle.toString());
if (speedInTitle)
$('speedInBrowserTitleBarLink').firstChild.style.opacity = '1';
else
$('speedInBrowserTitleBarLink').firstChild.style.opacity = '0';
processServerState();
});
$('StatisticsLink').addEvent('click', StatisticsLinkFN);
2017-03-06 03:05:18 +00:00
new MochaUI.Panel({
2018-04-05 11:59:31 +08:00
id: 'transferList',
title: 'Panel',
header: false,
padding: {
top: 0,
right: 0,
bottom: 0,
left: 0
},
2018-04-05 11:59:31 +08:00
loadMethod: 'xhr',
contentURL: 'transferlist.html',
onContentLoaded: function() {
2015-01-06 23:24:54 +03:00
updateMainData();
},
2018-04-05 11:59:31 +08:00
column: 'mainColumn',
onResize: saveColumnSizes,
height: null
});
var prop_h = localStorage.getItem('properties_height_rel');
if ($defined(prop_h))
prop_h = prop_h.toFloat() * Window.getSize().y;
2010-04-23 16:48:17 +00:00
else
prop_h = Window.getSize().y / 2.0;
2010-04-23 16:48:17 +00:00
new MochaUI.Panel({
2018-04-05 11:59:31 +08:00
id: 'propertiesPanel',
title: 'Panel',
header: true,
padding: {
top: 0,
right: 0,
bottom: 0,
left: 0
},
2018-04-05 11:59:31 +08:00
contentURL: 'properties_content.html',
require: {
css: ['css/Tabs.css', 'css/dynamicTable.css'],
js: ['scripts/prop-general.js', 'scripts/prop-trackers.js', 'scripts/prop-webseeds.js', 'scripts/prop-files.js'],
},
2018-04-05 11:59:31 +08:00
tabsURL: 'properties.html',
tabsOnload: function() {
MochaUI.initializeTabs('propertiesTabs');
updatePropertiesPanel = function() {
if (!$('prop_general').hasClass('invisible'))
updateTorrentData();
else if (!$('prop_trackers').hasClass('invisible'))
updateTrackersData();
2015-11-13 15:02:38 +03:00
else if (!$('prop_peers').hasClass('invisible'))
updateTorrentPeersData();
else if (!$('prop_webseeds').hasClass('invisible'))
updateWebSeedsData();
else if (!$('prop_files').hasClass('invisible'))
updateTorrentFilesData();
};
2018-04-05 11:59:31 +08:00
$('PropGeneralLink').addEvent('click', function(e) {
$('prop_general').removeClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_webseeds').addClass("invisible");
$('prop_files').addClass("invisible");
2015-11-04 23:03:27 +03:00
$('prop_peers').addClass("invisible");
updatePropertiesPanel();
2016-01-15 16:36:33 +03:00
localStorage.setItem('selected_tab', this.id);
});
2018-04-05 11:59:31 +08:00
$('PropTrackersLink').addEvent('click', function(e) {
$('prop_trackers').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_webseeds').addClass("invisible");
$('prop_files').addClass("invisible");
2015-11-04 23:03:27 +03:00
$('prop_peers').addClass("invisible");
updatePropertiesPanel();
2016-01-15 16:36:33 +03:00
localStorage.setItem('selected_tab', this.id);
2015-11-04 23:03:27 +03:00
});
2018-04-05 11:59:31 +08:00
$('PropPeersLink').addEvent('click', function(e) {
2015-11-04 23:03:27 +03:00
$('prop_peers').removeClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_general').addClass("invisible");
$('prop_webseeds').addClass("invisible");
$('prop_files').addClass("invisible");
updatePropertiesPanel();
2016-01-15 16:36:33 +03:00
localStorage.setItem('selected_tab', this.id);
});
2018-04-05 11:59:31 +08:00
$('PropWebSeedsLink').addEvent('click', function(e) {
$('prop_webseeds').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_files').addClass("invisible");
2015-11-04 23:03:27 +03:00
$('prop_peers').addClass("invisible");
updatePropertiesPanel();
2016-01-15 16:36:33 +03:00
localStorage.setItem('selected_tab', this.id);
});
2018-04-05 11:59:31 +08:00
$('PropFilesLink').addEvent('click', function(e) {
$('prop_files').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_webseeds').addClass("invisible");
2015-11-04 23:03:27 +03:00
$('prop_peers').addClass("invisible");
updatePropertiesPanel();
2016-01-15 16:36:33 +03:00
localStorage.setItem('selected_tab', this.id);
});
2018-04-05 11:59:31 +08:00
$('propertiesPanel_collapseToggle').addEvent('click', function(e) {
updatePropertiesPanel();
});
},
2018-04-05 11:59:31 +08:00
column: 'mainColumn',
height: prop_h
});
});
function closeWindows() {
MochaUI.closeAll();
}
function setupCopyEventHandler() {
if (clipboardEvent)
clipboardEvent.destroy();
2018-04-02 01:15:13 +08:00
clipboardEvent = new ClipboardJS('.copyToClipboard', {
text: function(trigger) {
var textToCopy;
switch (trigger.id) {
case "CopyName":
textToCopy = copyNameFN();
break;
case "CopyMagnetLink":
textToCopy = copyMagnetLinkFN();
break;
case "CopyHash":
textToCopy = copyHashFN();
break;
}
return textToCopy;
}
});
}
var keyboardEvents = new Keyboard({
defaultEventType: 'keydown',
events: {
'ctrl+a': function(event) {
torrentsTable.selectAll();
event.preventDefault();
},
'delete': function(event) {
deleteFN();
event.preventDefault();
}
}
});
keyboardEvents.activate();
2015-11-13 15:02:38 +03:00
var loadTorrentPeersTimer;
var syncTorrentPeersLastResponseId = 0;
var show_flags = true;
2018-04-05 11:59:31 +08:00
var loadTorrentPeersData = function() {
if ($('prop_peers').hasClass('invisible')
|| $('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
2015-11-13 15:02:38 +03:00
syncTorrentPeersLastResponseId = 0;
torrentPeersTable.clear();
return;
}
var current_hash = torrentsTable.getCurrentTorrentHash();
if (current_hash === "") {
2015-11-13 15:02:38 +03:00
syncTorrentPeersLastResponseId = 0;
torrentPeersTable.clear();
clearTimeout(loadTorrentPeersTimer);
loadTorrentPeersTimer = loadTorrentPeersData.delay(syncMainDataTimerPeriod);
return;
}
var url = new URI('api/v2/sync/torrentPeers');
2015-11-13 15:02:38 +03:00
url.setData('rid', syncTorrentPeersLastResponseId);
url.setData('hash', current_hash);
var request = new Request.JSON({
url: url,
noCache: true,
method: 'get',
onFailure: function() {
$('error_div').set('html', 'QBT_TR(qBittorrent client is not reachable)QBT_TR[CONTEXT=HttpServer]');
2015-11-13 15:02:38 +03:00
clearTimeout(loadTorrentPeersTimer);
loadTorrentPeersTimer = loadTorrentPeersData.delay(5000);
},
onSuccess: function(response) {
$('error_div').set('html', '');
if (response) {
var full_update = (response['full_update'] === true);
2015-11-13 15:02:38 +03:00
if (full_update) {
torrentPeersTable.clear();
}
if (response['rid']) {
syncTorrentPeersLastResponseId = response['rid'];
}
if (response['peers']) {
for (var key in response['peers']) {
response['peers'][key]['rowId'] = key;
2017-02-11 15:06:15 +08:00
if (response['peers'][key]['client'])
response['peers'][key]['client'] = escapeHtml(response['peers'][key]['client']);
2015-11-13 15:02:38 +03:00
torrentPeersTable.updateRowData(response['peers'][key]);
}
}
if (response['peers_removed'])
2018-04-05 11:59:31 +08:00
response['peers_removed'].each(function(hash) {
2015-11-13 15:02:38 +03:00
torrentPeersTable.removeRow(hash);
});
torrentPeersTable.updateTable(full_update);
torrentPeersTable.altRow();
if (response['show_flags']) {
if (show_flags != response['show_flags']) {
show_flags = response['show_flags'];
torrentPeersTable.columns['country'].force_hide = !show_flags;
torrentPeersTable.updateColumn('country');
}
}
}
else {
torrentPeersTable.clear();
}
clearTimeout(loadTorrentPeersTimer);
loadTorrentPeersTimer = loadTorrentPeersData.delay(syncMainDataTimerPeriod);
}
}).send();
};
2018-04-05 11:59:31 +08:00
updateTorrentPeersData = function() {
2015-11-13 15:02:38 +03:00
clearTimeout(loadTorrentPeersTimer);
loadTorrentPeersData();
};