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

127 lines
4.9 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-06-10 13:23:16 +02:00
angular.module('ariaNg').controller('DownloadListController', ['$rootScope', '$scope', '$window', '$location', '$route', '$interval', 'dragulaService', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2TaskService', function ($rootScope, $scope, $window, $location, $route, $interval, dragulaService, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, aria2TaskService) {
2016-05-13 18:09:12 +02:00
var location = $location.path().substring(1);
2016-05-17 16:15:28 +02:00
var downloadTaskRefreshPromise = null;
var pauseDownloadTaskRefresh = false;
2016-05-24 16:22:47 +02:00
var needRequestWholeInfo = true;
2016-05-13 18:09:12 +02:00
2016-06-04 09:33:40 +02:00
var refreshDownloadTask = function (silent) {
if (pauseDownloadTaskRefresh) {
return;
}
2016-06-10 13:23:16 +02:00
return aria2TaskService.getTaskList(location, needRequestWholeInfo, function (response) {
2016-06-22 17:04:11 +02:00
if (pauseDownloadTaskRefresh) {
return;
}
2016-06-10 13:23:16 +02:00
if (!response.success) {
2016-08-01 16:49:16 +02:00
if (response.data.message === aria2RpcErrors.Unauthorized.message) {
2016-06-10 13:23:16 +02:00
$interval.cancel(downloadTaskRefreshPromise);
}
return;
}
var isRequestWholeInfo = response.context.requestWholeInfo;
2016-06-10 13:23:16 +02:00
var taskList = response.data;
if (isRequestWholeInfo) {
$rootScope.taskContext.list = taskList;
needRequestWholeInfo = false;
} else {
if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
for (var i = 0; i < $rootScope.taskContext.list.length; i++) {
var task = $rootScope.taskContext.list[i];
delete task.verifiedLength;
delete task.verifyIntegrityPending;
}
}
if (ariaNgCommonService.extendArray(taskList, $rootScope.taskContext.list, 'gid')) {
2016-05-31 16:51:12 +02:00
needRequestWholeInfo = false;
} else {
needRequestWholeInfo = true;
2016-05-13 18:09:12 +02:00
}
2016-05-31 16:51:12 +02:00
}
if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
2016-06-01 16:05:36 +02:00
aria2TaskService.processDownloadTasks($rootScope.taskContext.list);
if (!isRequestWholeInfo) {
var hasFullStruct = false;
for (var i = 0; i < $rootScope.taskContext.list.length; i++) {
var task = $rootScope.taskContext.list[i];
if (task.hasTaskName || task.files || task.bittorrent) {
hasFullStruct = true;
break;
}
}
if (!hasFullStruct) {
needRequestWholeInfo = true;
$rootScope.taskContext.list.length = 0;
return;
}
}
2016-05-31 16:51:12 +02:00
}
$rootScope.taskContext.enableSelectAll = $rootScope.taskContext.list && $rootScope.taskContext.list.length > 0;
2016-06-04 09:33:40 +02:00
}, silent);
2016-05-17 16:15:28 +02:00
};
$scope.getOrderType = function () {
return ariaNgSettingService.getDisplayOrder();
};
$scope.isSupportDragTask = function () {
if (!ariaNgSettingService.getDragAndDropTasks()) {
return false;
}
var displayOrder = ariaNgCommonService.parseOrderType(ariaNgSettingService.getDisplayOrder());
2016-08-01 16:49:16 +02:00
return location === 'waiting' && displayOrder.type === 'default';
};
2016-05-17 16:15:28 +02:00
if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {
downloadTaskRefreshPromise = $interval(function () {
2016-06-04 09:33:40 +02:00
refreshDownloadTask(true);
2016-05-17 16:15:28 +02:00
}, ariaNgSettingService.getDownloadTaskRefreshInterval());
}
dragulaService.options($scope, 'task-list', {
revertOnSpill: true,
2016-06-20 18:20:30 +02:00
moves: function () {
return $scope.isSupportDragTask();
}
});
$scope.$on('task-list.drop-model', function (el, target, source) {
var element = angular.element(target);
var gid = element.attr('data-gid');
var index = element.index();
pauseDownloadTaskRefresh = true;
2016-06-10 13:23:16 +02:00
aria2TaskService.changeTaskPosition(gid, index, function () {
pauseDownloadTaskRefresh = false;
2016-06-10 13:23:16 +02:00
}, true);
});
$scope.$on('$destroy', function () {
2016-06-22 17:04:11 +02:00
pauseDownloadTaskRefresh = true;
if (downloadTaskRefreshPromise) {
$interval.cancel(downloadTaskRefreshPromise);
}
});
2016-05-31 16:51:12 +02:00
2016-06-04 09:33:40 +02:00
$rootScope.loadPromise = refreshDownloadTask(false);
2016-05-13 18:09:12 +02:00
}]);
2016-08-01 16:49:16 +02:00
}());