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.8 KiB
JavaScript

(function () {
'use strict';
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) {
var location = $location.path().substring(1);
var downloadTaskRefreshPromise = null;
var pauseDownloadTaskRefresh = false;
var needRequestWholeInfo = true;
var refreshDownloadTask = function (silent) {
if (pauseDownloadTaskRefresh) {
return;
}
return aria2TaskService.getTaskList(location, needRequestWholeInfo, function (response) {
if (pauseDownloadTaskRefresh) {
return;
}
if (!response.success) {
if (response.data.message == aria2RpcErrors.Unauthorized.message) {
$interval.cancel(downloadTaskRefreshPromise);
}
return;
}
var isRequestWholeInfo = response.context.requestWholeInfo;
var taskList = response.data;
if (isRequestWholeInfo) {
$rootScope.taskContext.list = taskList;
needRequestWholeInfo = false;
} else {
if (ariaNgCommonService.extendArray(taskList, $rootScope.taskContext.list, 'gid')) {
needRequestWholeInfo = false;
} else {
needRequestWholeInfo = true;
}
}
if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
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;
}
}
}
$rootScope.taskContext.enableSelectAll = $rootScope.taskContext.list && $rootScope.taskContext.list.length > 0;
}, silent);
};
$scope.filterByTaskName = function (task) {
if (!task || !angular.isString(task.taskName)) {
return false;
}
if (!$rootScope.searchContext || !$rootScope.searchContext.text) {
return true;
}
return (task.taskName.toLowerCase().indexOf($rootScope.searchContext.text.toLowerCase()) >= 0);
};
$scope.getOrderType = function () {
return ariaNgSettingService.getDisplayOrder();
};
$scope.isSupportDragTask = function () {
var displayOrder = ariaNgCommonService.parseOrderType(ariaNgSettingService.getDisplayOrder());
return location == 'waiting' && displayOrder.type == 'default';
};
if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {
downloadTaskRefreshPromise = $interval(function () {
refreshDownloadTask(true);
}, ariaNgSettingService.getDownloadTaskRefreshInterval());
}
dragulaService.options($scope, 'task-list', {
revertOnSpill: true,
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;
aria2TaskService.changeTaskPosition(gid, index, function () {
pauseDownloadTaskRefresh = false;
}, true);
});
$scope.$on('$destroy', function () {
pauseDownloadTaskRefresh = true;
if (downloadTaskRefreshPromise) {
$interval.cancel(downloadTaskRefreshPromise);
}
});
$rootScope.loadPromise = refreshDownloadTask(false);
}]);
})();