qBittorrent/src/webui/www/private/scripts/mocha-init.js

750 lines
24 KiB
JavaScript
Raw Normal View History

/* -----------------------------------------------------------------
ATTACH MOCHA LINK EVENTS
Notes: Here is where you define your windows and the events that open them.
If you are not using links to run Mocha methods you can remove this function.
If you need to add link events to links within windows you are creating, do
it in the onContentLoaded function of the new window.
----------------------------------------------------------------- */
2014-11-30 01:40:26 +03:00
/* Define localStorage object for older browsers */
if (typeof localStorage == 'undefined') {
window['localStorage'] = {
getItem: function(name) {
return Cookie.read(name);
},
setItem: function(name, value) {
Cookie.write(name, value, {
duration: 365 * 10
});
2014-11-30 01:40:26 +03:00
}
};
2014-11-30 01:40:26 +03:00
}
function getLocalStorageItem(name, defaultVal) {
2018-09-27 06:10:51 +03:00
var val = localStorage.getItem(name);
if (val === null || val === undefined)
val = defaultVal;
return val;
}
var deleteFN = function() {};
var startFN = function() {};
var pauseFN = function() {};
initializeWindows = function() {
2018-01-24 06:15:44 +03:00
saveWindowSize = function(windowId) {
var size = $(windowId).getSize();
localStorage.setItem('window_' + windowId + '_width', size.x);
localStorage.setItem('window_' + windowId + '_height', size.y);
};
loadWindowWidth = function(windowId, defaultValue) {
return getLocalStorageItem('window_' + windowId + '_width', defaultValue);
};
loadWindowHeight = function(windowId, defaultValue) {
return getLocalStorageItem('window_' + windowId + '_height', defaultValue);
};
function addClickEvent(el, fn) {
['Link', 'Button'].each(function(item) {
if ($(el + item)) {
$(el + item).addEvent('click', fn);
}
});
}
addClickEvent('download', function(e) {
new Event(e).stop();
showDownloadPage();
});
showDownloadPage = function(urls) {
2018-01-24 06:15:44 +03:00
var id = 'downloadPage';
var contentUrl = 'download.html';
if (urls && urls.length)
contentUrl += '?urls=' + urls.join("|");
new MochaUI.Window({
2018-01-24 06:15:44 +03:00
id: id,
title: "QBT_TR(Download from URLs)QBT_TR[CONTEXT=downloadFromURL]",
loadMethod: 'iframe',
contentURL: contentUrl,
addClass: 'windowFrame', // fixes iframe scrolling on iOS Safari
scrollbars: true,
maximizable: false,
closable: true,
paddingVertical: 0,
paddingHorizontal: 0,
2018-01-24 06:15:44 +03:00
width: loadWindowWidth(id, 500),
height: loadWindowHeight(id, 420),
onResize: function() {
saveWindowSize(id);
}
});
2015-01-06 23:24:54 +03:00
updateMainData();
};
addClickEvent('preferences', function(e) {
new Event(e).stop();
2018-01-24 06:15:44 +03:00
var id = 'preferencesPage';
new MochaUI.Window({
2018-01-24 06:15:44 +03:00
id: id,
title: "QBT_TR(Options)QBT_TR[CONTEXT=OptionsDialog]",
loadMethod: 'xhr',
toolbar: true,
contentURL: 'preferences_content.html',
require: {
css: ['css/Tabs.css']
},
toolbarURL: 'preferences.html',
maximizable: false,
closable: true,
paddingVertical: 0,
paddingHorizontal: 0,
2018-01-24 06:15:44 +03:00
width: loadWindowWidth(id, 700),
2018-08-22 07:47:00 +03:00
height: loadWindowHeight(id, 500),
2018-01-24 06:15:44 +03:00
onResize: function() {
saveWindowSize(id);
}
});
});
addClickEvent('upload', function(e) {
new Event(e).stop();
2018-01-24 06:15:44 +03:00
var id = 'uploadPage';
new MochaUI.Window({
2018-01-24 06:15:44 +03:00
id: id,
title: "QBT_TR(Upload local torrent)QBT_TR[CONTEXT=HttpServer]",
loadMethod: 'iframe',
contentURL: 'upload.html',
addClass: 'windowFrame', // fixes iframe scrolling on iOS Safari
scrollbars: true,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
2018-01-24 06:15:44 +03:00
width: loadWindowWidth(id, 500),
height: loadWindowHeight(id, 260),
onResize: function() {
saveWindowSize(id);
}
});
2015-01-06 23:24:54 +03:00
updateMainData();
});
globalUploadLimitFN = function() {
new MochaUI.Window({
id: 'uploadLimitPage',
title: "QBT_TR(Global Upload Speed Limit)QBT_TR[CONTEXT=MainWindow]",
loadMethod: 'iframe',
2015-02-01 20:08:45 +03:00
contentURL: 'uploadlimit.html?hashes=global',
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 424,
height: 80
});
};
uploadLimitFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new MochaUI.Window({
id: 'uploadLimitPage',
title: "QBT_TR(Torrent Upload Speed Limiting)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
contentURL: 'uploadlimit.html?hashes=' + hashes.join("|"),
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 424,
height: 80
});
}
};
shareRatioFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
var shareRatio = null;
var torrentsHaveSameShareRatio = true;
// check if all selected torrents have same share ratio
for (var i = 0; i < hashes.length; ++i) {
var hash = hashes[i];
var row = torrentsTable.rows[hash].full_data;
var origValues = row.ratio_limit + "|" + row.seeding_time_limit + "|" + row.max_ratio + "|" + row.max_seeding_time;
// initialize value
if (shareRatio === null)
shareRatio = origValues;
if (origValues !== shareRatio) {
torrentsHaveSameShareRatio = false;
break;
}
}
// if all torrents have same share ratio, display that share ratio. else use the default
var orig = torrentsHaveSameShareRatio ? shareRatio : "";
new MochaUI.Window({
id: 'shareRatioPage',
title: "QBT_TR(Torrent Upload/Download Ratio Limiting)QBT_TR[CONTEXT=UpDownRatioDialog]",
loadMethod: 'iframe',
contentURL: 'shareratio.html?hashes=' + hashes.join("|") + '&orig=' + orig,
scrollbars: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 424,
height: 175
});
}
};
2014-12-09 19:54:35 +03:00
toggleSequentialDownloadFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
2014-12-09 19:54:35 +03:00
new Request({
url: 'api/v2/torrents/toggleSequentialDownload',
2014-12-09 19:54:35 +03:00
method: 'post',
data: {
hashes: hashes.join("|")
2014-12-09 19:54:35 +03:00
}
}).send();
2015-01-06 23:24:54 +03:00
updateMainData();
2014-12-09 19:54:35 +03:00
}
};
toggleFirstLastPiecePrioFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
2014-12-09 19:54:35 +03:00
new Request({
url: 'api/v2/torrents/toggleFirstLastPiecePrio',
2014-12-09 19:54:35 +03:00
method: 'post',
data: {
hashes: hashes.join("|")
2014-12-09 19:54:35 +03:00
}
}).send();
2015-01-06 23:24:54 +03:00
updateMainData();
2014-12-09 19:54:35 +03:00
}
};
setSuperSeedingFN = function(val) {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/setSuperSeeding',
method: 'post',
data: {
value: val,
hashes: hashes.join("|")
}
}).send();
updateMainData();
}
};
setForceStartFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/setForceStart',
method: 'post',
data: {
value: 'true',
hashes: hashes.join("|")
}
}).send();
updateMainData();
}
};
globalDownloadLimitFN = function() {
new MochaUI.Window({
id: 'downloadLimitPage',
title: "QBT_TR(Global Download Speed Limit)QBT_TR[CONTEXT=MainWindow]",
loadMethod: 'iframe',
2015-02-01 20:45:37 +03:00
contentURL: 'downloadlimit.html?hashes=global',
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 424,
height: 80
});
};
2017-03-06 06:05:18 +03:00
StatisticsLinkFN = function() {
2018-03-05 09:40:21 +03:00
var id = 'statisticspage';
2017-03-06 06:05:18 +03:00
new MochaUI.Window({
2018-03-05 09:40:21 +03:00
id: id,
title: 'QBT_TR(Statistics)QBT_TR[CONTEXT=StatsDialog]',
2017-03-06 06:05:18 +03:00
loadMethod: 'xhr',
contentURL: 'statistics.html',
maximizable: false,
padding: 10,
2018-03-05 09:40:21 +03:00
width: loadWindowWidth(id, 275),
height: loadWindowHeight(id, 370),
onResize: function() {
saveWindowSize(id);
}
});
};
2017-03-06 06:05:18 +03:00
downloadLimitFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new MochaUI.Window({
id: 'downloadLimitPage',
title: "QBT_TR(Torrent Download Speed Limiting)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
contentURL: 'downloadlimit.html?hashes=' + hashes.join("|"),
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 424,
height: 80
});
}
};
deleteFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new MochaUI.Window({
id: 'confirmDeletionPage',
title: "QBT_TR(Deletion confirmation)QBT_TR[CONTEXT=confirmDeletionDlg]",
loadMethod: 'iframe',
contentURL: 'confirmdeletion.html?hashes=' + hashes.join("|"),
scrollbars: false,
resizable: false,
maximizable: false,
padding: 10,
width: 424,
height: 140
});
2015-01-06 23:24:54 +03:00
updateMainData();
}
};
addClickEvent('delete', function(e) {
new Event(e).stop();
deleteFN();
});
pauseFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/pause',
method: 'post',
data: {
hashes: hashes.join("|")
}
}).send();
2015-01-06 23:24:54 +03:00
updateMainData();
}
};
startFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/resume',
method: 'post',
data: {
hashes: hashes.join("|")
}
}).send();
2015-01-06 23:24:54 +03:00
updateMainData();
}
};
autoTorrentManagementFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
var enable = false;
hashes.each(function(hash, index) {
var row = torrentsTable.rows[hash];
if (!row.full_data.auto_tmm)
enable = true;
});
new Request({
url: 'api/v2/torrents/setAutoManagement',
method: 'post',
data: {
hashes: hashes.join("|"),
enable: enable
}
}).send();
updateMainData();
}
};
recheckFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
2018-07-23 03:33:39 +03:00
new Request({
url: 'api/v2/torrents/recheck',
method: 'post',
data: {
hashes: hashes.join("|"),
}
}).send();
updateMainData();
2018-07-23 03:33:17 +03:00
}
};
reannounceFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/reannounce',
method: 'post',
data: {
hashes: hashes.join("|"),
}
}).send();
updateMainData();
}
};
setLocationFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
var hash = hashes[0];
var row = torrentsTable.rows[hash];
var path = encodeURIComponent(row.full_data.save_path);
new MochaUI.Window({
id: 'setLocationPage',
title: "QBT_TR(Set location)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
contentURL: 'setlocation.html?hashes=' + hashes.join('|') + '&path=' + path,
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 400,
height: 130
});
}
};
renameFN = function() {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length == 1) {
var hash = hashes[0];
var row = torrentsTable.rows[hash];
if (row) {
new MochaUI.Window({
id: 'renamePage',
title: "QBT_TR(Rename)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
2018-03-14 07:56:57 +03:00
contentURL: 'rename.html?hash=' + hashes[0] + '&name=' + row.full_data.name,
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 250,
height: 100
});
}
}
};
2018-04-05 06:59:31 +03:00
torrentNewCategoryFN = function() {
var action = "set";
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new MochaUI.Window({
id: 'newCategoryPage',
title: "QBT_TR(New Category)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
contentURL: 'newcategory.html?action=' + action + '&hashes=' + hashes.join('|'),
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 250,
height: 150
});
}
};
2018-04-05 06:59:31 +03:00
torrentSetCategoryFN = function(categoryHash) {
var categoryName = '';
if (categoryHash != 0)
categoryName = category_list[categoryHash].name;
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/setCategory',
method: 'post',
data: {
hashes: hashes.join("|"),
category: categoryName
}
}).send();
}
};
2018-04-05 06:59:31 +03:00
createCategoryFN = function() {
var action = "create";
2016-01-21 15:32:13 +03:00
new MochaUI.Window({
id: 'newCategoryPage',
title: "QBT_TR(New Category)QBT_TR[CONTEXT=CategoryFilterWidget]",
2016-01-21 15:32:13 +03:00
loadMethod: 'iframe',
contentURL: 'newcategory.html?action=' + action,
2016-01-21 15:32:13 +03:00
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 250,
height: 150
});
updateMainData();
};
editCategoryFN = function(categoryHash) {
var action = "edit";
var categoryName = category_list[categoryHash].name;
var savePath = category_list[categoryHash].savePath;
new MochaUI.Window({
id: 'editCategoryPage',
title: "QBT_TR(Edit Category)QBT_TR[CONTEXT=TransferListWidget]",
loadMethod: 'iframe',
contentURL: 'newcategory.html?action=' + action + '&categoryName=' + categoryName + '&savePath=' + savePath,
scrollbars: false,
resizable: false,
maximizable: false,
paddingVertical: 0,
paddingHorizontal: 0,
width: 250,
height: 150
2016-01-21 15:32:13 +03:00
});
updateMainData();
};
2018-04-05 06:59:31 +03:00
removeCategoryFN = function(categoryHash) {
2016-01-21 16:42:20 +03:00
var categoryName = category_list[categoryHash].name;
new Request({
url: 'api/v2/torrents/removeCategories',
2016-01-21 16:42:20 +03:00
method: 'post',
data: {
categories: categoryName
}
}).send();
setCategoryFilter(CATEGORIES_ALL);
};
2018-04-05 06:59:31 +03:00
deleteUnusedCategoriesFN = function() {
var categories = [];
for (var hash in category_list) {
if (torrentsTable.getFilteredTorrentsNumber('all', hash) === 0)
categories.push(category_list[hash].name);
}
new Request({
url: 'api/v2/torrents/removeCategories',
method: 'post',
data: {
categories: categories.join('\n')
2016-01-21 16:42:20 +03:00
}
}).send();
setCategoryFilter(CATEGORIES_ALL);
};
2018-04-05 06:59:31 +03:00
startTorrentsByCategoryFN = function(categoryHash) {
var hashes = torrentsTable.getFilteredTorrentsHashes('all', categoryHash);
if (hashes.length) {
new Request({
url: 'api/v2/torrents/resume',
method: 'post',
data: {
hashes: hashes.join("|")
}
}).send();
updateMainData();
}
};
2018-04-05 06:59:31 +03:00
pauseTorrentsByCategoryFN = function(categoryHash) {
var hashes = torrentsTable.getFilteredTorrentsHashes('all', categoryHash);
if (hashes.length) {
new Request({
url: 'api/v2/torrents/pause',
method: 'post',
data: {
hashes: hashes.join("|")
}
}).send();
updateMainData();
}
};
2018-04-05 06:59:31 +03:00
deleteTorrentsByCategoryFN = function(categoryHash) {
var hashes = torrentsTable.getFilteredTorrentsHashes('all', categoryHash);
if (hashes.length) {
new MochaUI.Window({
id: 'confirmDeletionPage',
title: "QBT_TR(Deletion confirmation)QBT_TR[CONTEXT=confirmDeletionDlg]",
loadMethod: 'iframe',
contentURL: 'confirmdeletion.html?hashes=' + hashes.join("|"),
scrollbars: false,
resizable: false,
maximizable: false,
padding: 10,
width: 424,
height: 140
});
updateMainData();
}
};
copyNameFN = function() {
var selectedRows = torrentsTable.selectedRowsIds();
var names = [];
if (selectedRows.length) {
var rows = torrentsTable.getFilteredAndSortedRows();
for (var i = 0; i < selectedRows.length; ++i) {
var hash = selectedRows[i];
names.push(rows[hash].full_data.name);
}
}
return names.join("\n");
};
copyMagnetLinkFN = function() {
var selectedRows = torrentsTable.selectedRowsIds();
var magnets = [];
if (selectedRows.length) {
var rows = torrentsTable.getFilteredAndSortedRows();
for (var i = 0; i < selectedRows.length; ++i) {
var hash = selectedRows[i];
magnets.push(rows[hash].full_data.magnet_uri);
}
}
return magnets.join("\n");
};
copyHashFN = function() {
return torrentsTable.selectedRowsIds().join("\n");
};
['pause', 'resume'].each(function(item) {
addClickEvent(item + 'All', function(e) {
new Event(e).stop();
new Request({
url: 'api/v2/torrents/' + item,
method: 'post',
data: {
hashes: "all"
}
}).send();
updateMainData();
});
});
['pause', 'resume', 'recheck'].each(function(item) {
addClickEvent(item, function(e) {
new Event(e).stop();
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
hashes.each(function(hash, index) {
new Request({
url: 'api/v2/torrents/' + item,
method: 'post',
data: {
hashes: hash
}
}).send();
});
2015-01-06 23:24:54 +03:00
updateMainData();
}
});
});
['decreasePrio', 'increasePrio', 'topPrio', 'bottomPrio'].each(function(item) {
addClickEvent(item, function(e) {
new Event(e).stop();
setPriorityFN(item);
});
});
setPriorityFN = function(cmd) {
var hashes = torrentsTable.selectedRowsIds();
if (hashes.length) {
new Request({
url: 'api/v2/torrents/' + cmd,
method: 'post',
data: {
hashes: hashes.join("|")
}
}).send();
2015-01-06 23:24:54 +03:00
updateMainData();
}
};
addClickEvent('about', function(e) {
new Event(e).stop();
2018-01-24 06:15:44 +03:00
var id = 'aboutpage';
new MochaUI.Window({
2018-01-24 06:15:44 +03:00
id: id,
title: 'QBT_TR(About)QBT_TR[CONTEXT=AboutDlg]',
loadMethod: 'xhr',
contentURL: 'about.html',
2018-01-24 06:15:44 +03:00
padding: 10,
width: loadWindowWidth(id, 550),
height: loadWindowHeight(id, 290),
onResize: function() {
saveWindowSize(id);
}
});
});
addClickEvent('logout', function(e) {
new Event(e).stop();
new Request({
url: 'api/v2/auth/logout',
method: 'post',
onSuccess: function() {
window.location.reload();
}
}).send();
});
addClickEvent('shutdown', function(e) {
new Event(e).stop();
if (confirm('QBT_TR(Are you sure you want to quit qBittorrent?)QBT_TR[CONTEXT=MainWindow]')) {
new Request({
url: 'api/v2/app/shutdown',
onSuccess: function() {
document.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\"><html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title>QBT_TR(qBittorrent has been shutdown.)QBT_TR[CONTEXT=HttpServer]</title><style type=\"text/css\">body { text-align: center; }</style></head><body><h1>QBT_TR(qBittorrent has been shutdown.)QBT_TR[CONTEXT=HttpServer]</h1></body></html>");
stop();
}
}).send();
}
});
2012-08-19 21:25:21 +04:00
// Deactivate menu header links
$$('a.returnFalse').each(function(el) {
el.addEvent('click', function(e) {
new Event(e).stop();
});
});
};