This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/controllers/new.js

216 lines
7.6 KiB
JavaScript
Raw Normal View History

2016-06-18 18:11:19 +02:00
(function () {
'use strict';
2018-08-12 14:26:26 +02:00
angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
2016-06-22 17:12:53 +02:00
var tabOrders = ['links', 'options'];
2017-10-14 13:57:58 +02:00
var parameters = $location.search();
2016-06-18 18:11:19 +02:00
var saveDownloadPath = function (options) {
if (!options || !options.dir) {
return;
}
aria2SettingService.addSettingHistory('dir', options.dir);
};
var downloadByLinks = function (pauseOnAdded, responseCallback) {
var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
var options = angular.copy($scope.context.options);
var tasks = [];
for (var i = 0; i < urls.length; i++) {
if (urls[i] === '' || urls[i].trim() === '') {
continue;
}
tasks.push({
urls: [urls[i].trim()],
options: options
});
}
saveDownloadPath(options);
return aria2TaskService.newUriTasks(tasks, pauseOnAdded, responseCallback);
};
var downloadByTorrent = function (pauseOnAdded, responseCallback) {
var task = {
content: $scope.context.uploadFile.base64Content,
options: angular.copy($scope.context.options)
};
2018-07-11 14:23:15 +02:00
saveDownloadPath(task.options);
return aria2TaskService.newTorrentTask(task, pauseOnAdded, responseCallback);
};
var downloadByMetalink = function (pauseOnAdded, responseCallback) {
var task = {
content: $scope.context.uploadFile.base64Content,
options: angular.copy($scope.context.options)
};
2018-07-11 14:23:15 +02:00
saveDownloadPath(task.options);
return aria2TaskService.newMetalinkTask(task, pauseOnAdded, responseCallback);
};
$scope.context = {
2016-06-22 17:12:53 +02:00
currentTab: 'links',
taskType: 'urls',
urls: '',
uploadFile: null,
2016-06-20 18:20:30 +02:00
availableOptions: (function () {
var keys = aria2SettingService.getNewTaskOptionKeys();
return aria2SettingService.getSpecifiedOptions(keys, {
disableRequired: true
});
2016-06-20 18:20:30 +02:00
})(),
globalOptions: null,
options: {},
optionFilter: {
global: true,
2017-03-19 08:48:38 +01:00
http: false,
bittorrent: false
}
};
2017-10-14 13:57:58 +02:00
if (parameters.url) {
try {
2018-08-12 10:59:21 +02:00
$scope.context.urls = ariaNgCommonService.base64UrlDecode(parameters.url);
2017-10-14 13:57:58 +02:00
} catch (ex) {
ariaNgLogService.error('[NewTaskController] base64 decode error, url=' + parameters.url, ex);
}
}
2016-06-20 17:45:33 +02:00
$scope.changeTab = function (tabName) {
2016-08-01 16:49:16 +02:00
if (tabName === 'options') {
2016-06-20 17:45:33 +02:00
$scope.loadDefaultOption();
}
$scope.context.currentTab = tabName;
};
2021-02-14 05:21:52 +01:00
$rootScope.swipeActions.extendLeftSwipe = function () {
var tabIndex = tabOrders.indexOf($scope.context.currentTab);
if (tabIndex < tabOrders.length - 1) {
2016-06-20 17:45:33 +02:00
$scope.changeTab(tabOrders[tabIndex + 1]);
return true;
} else {
return false;
}
};
2021-02-14 05:21:52 +01:00
$rootScope.swipeActions.extendRightSwipe = function () {
var tabIndex = tabOrders.indexOf($scope.context.currentTab);
if (tabIndex > 0) {
2016-06-20 17:45:33 +02:00
$scope.changeTab(tabOrders[tabIndex - 1]);
return true;
} else {
return false;
}
};
$scope.loadDefaultOption = function () {
if ($scope.context.globalOptions) {
return;
}
$rootScope.loadPromise = aria2SettingService.getGlobalOption(function (response) {
if (response.success) {
$scope.context.globalOptions = response.data;
}
});
};
2016-06-23 18:39:18 +02:00
$scope.openTorrent = function () {
ariaNgFileService.openFileContent({
2019-03-27 15:44:40 +01:00
scope: $scope,
fileFilter: '.torrent',
fileType: 'binary'
}, function (result) {
$scope.context.uploadFile = result;
$scope.context.taskType = 'torrent';
$scope.changeTab('options');
}, function (error) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.showError(error);
2018-05-09 18:42:09 +02:00
}, angular.element('#file-holder'));
2016-06-23 18:39:18 +02:00
};
$scope.openMetalink = function () {
ariaNgFileService.openFileContent({
2019-03-27 15:44:40 +01:00
scope: $scope,
fileFilter: '.meta4,.metalink',
fileType: 'binary'
}, function (result) {
$scope.context.uploadFile = result;
$scope.context.taskType = 'metalink';
$scope.changeTab('options');
}, function (error) {
2018-08-12 14:26:26 +02:00
ariaNgLocalizationService.showError(error);
2018-05-09 18:42:09 +02:00
}, angular.element('#file-holder'));
2016-06-23 18:39:18 +02:00
};
$scope.startDownload = function (pauseOnAdded) {
var responseCallback = function (response) {
if (!response.hasSuccess && !response.success) {
2016-06-18 18:11:19 +02:00
return;
}
var firstTask = null;
if (response.results && response.results.length > 0) {
firstTask = response.results[0];
} else if (response) {
firstTask = response;
}
if (ariaNgSettingService.getAfterCreatingNewTask() === 'task-detail' && firstTask && firstTask.data) {
$location.path('/task/detail/' + firstTask.data);
} else {
if (pauseOnAdded) {
$location.path('/waiting');
} else {
$location.path('/downloading');
}
}
};
if ($scope.context.taskType === 'urls') {
$rootScope.loadPromise = downloadByLinks(pauseOnAdded, responseCallback);
} else if ($scope.context.taskType === 'torrent') {
$rootScope.loadPromise = downloadByTorrent(pauseOnAdded, responseCallback);
} else if ($scope.context.taskType === 'metalink') {
$rootScope.loadPromise = downloadByMetalink(pauseOnAdded, responseCallback);
}
2016-06-18 18:11:19 +02:00
};
$scope.setOption = function (key, value, optionStatus) {
2016-08-01 16:49:16 +02:00
if (value !== '') {
$scope.context.options[key] = value;
} else {
delete $scope.context.options[key];
}
optionStatus.setReady();
};
2016-06-23 16:40:53 +02:00
$scope.urlTextboxKeyDown = function (event) {
2016-08-01 16:49:16 +02:00
if (event.keyCode === 13 && event.ctrlKey && $scope.newTaskForm.$valid) {
$scope.startDownload();
}
};
$scope.getValidUrlsCount = function () {
var urls = ariaNgCommonService.parseUrlsFromOriginInput($scope.context.urls);
return urls ? urls.length : 0;
};
2016-08-01 16:49:16 +02:00
$rootScope.loadPromise = $timeout(function () {}, 100);
2016-06-18 18:11:19 +02:00
}]);
2016-08-01 16:49:16 +02:00
}());