list.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. (function () {
  2. 'use strict';
  3. 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) {
  4. var location = $location.path().substring(1);
  5. var downloadTaskRefreshPromise = null;
  6. var pauseDownloadTaskRefresh = false;
  7. var needRequestWholeInfo = true;
  8. var refreshDownloadTask = function (silent) {
  9. if (pauseDownloadTaskRefresh) {
  10. return;
  11. }
  12. return aria2TaskService.getTaskList(location, needRequestWholeInfo, function (response) {
  13. if (pauseDownloadTaskRefresh) {
  14. return;
  15. }
  16. if (!response.success) {
  17. if (response.data.message === aria2RpcErrors.Unauthorized.message) {
  18. $interval.cancel(downloadTaskRefreshPromise);
  19. }
  20. return;
  21. }
  22. var isRequestWholeInfo = response.context.requestWholeInfo;
  23. var taskList = response.data;
  24. if (isRequestWholeInfo) {
  25. $rootScope.taskContext.list = taskList;
  26. needRequestWholeInfo = false;
  27. } else {
  28. if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
  29. for (var i = 0; i < $rootScope.taskContext.list.length; i++) {
  30. var task = $rootScope.taskContext.list[i];
  31. delete task.verifiedLength;
  32. delete task.verifyIntegrityPending;
  33. }
  34. }
  35. if (ariaNgCommonService.extendArray(taskList, $rootScope.taskContext.list, 'gid')) {
  36. needRequestWholeInfo = false;
  37. } else {
  38. needRequestWholeInfo = true;
  39. }
  40. }
  41. if ($rootScope.taskContext.list && $rootScope.taskContext.list.length > 0) {
  42. aria2TaskService.processDownloadTasks($rootScope.taskContext.list);
  43. if (!isRequestWholeInfo) {
  44. var hasFullStruct = false;
  45. for (var i = 0; i < $rootScope.taskContext.list.length; i++) {
  46. var task = $rootScope.taskContext.list[i];
  47. if (task.hasTaskName || task.files || task.bittorrent) {
  48. hasFullStruct = true;
  49. break;
  50. }
  51. }
  52. if (!hasFullStruct) {
  53. needRequestWholeInfo = true;
  54. $rootScope.taskContext.list.length = 0;
  55. return;
  56. }
  57. }
  58. }
  59. $rootScope.taskContext.enableSelectAll = $rootScope.taskContext.list && $rootScope.taskContext.list.length > 0;
  60. }, silent);
  61. };
  62. $scope.getOrderType = function () {
  63. return ariaNgSettingService.getDisplayOrder();
  64. };
  65. $scope.isSupportDragTask = function () {
  66. if (!ariaNgSettingService.getDragAndDropTasks()) {
  67. return false;
  68. }
  69. var displayOrder = ariaNgCommonService.parseOrderType(ariaNgSettingService.getDisplayOrder());
  70. return location === 'waiting' && displayOrder.type === 'default';
  71. };
  72. if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {
  73. downloadTaskRefreshPromise = $interval(function () {
  74. refreshDownloadTask(true);
  75. }, ariaNgSettingService.getDownloadTaskRefreshInterval());
  76. }
  77. dragulaService.options($scope, 'task-list', {
  78. revertOnSpill: true,
  79. moves: function () {
  80. return $scope.isSupportDragTask();
  81. }
  82. });
  83. $scope.$on('task-list.drop-model', function (el, target, source) {
  84. var element = angular.element(target);
  85. var gid = element.attr('data-gid');
  86. var index = element.index();
  87. pauseDownloadTaskRefresh = true;
  88. aria2TaskService.changeTaskPosition(gid, index, function () {
  89. pauseDownloadTaskRefresh = false;
  90. }, true);
  91. });
  92. $scope.$on('$destroy', function () {
  93. pauseDownloadTaskRefresh = true;
  94. if (downloadTaskRefreshPromise) {
  95. $interval.cancel(downloadTaskRefreshPromise);
  96. }
  97. });
  98. $rootScope.keydownActions.selectAll = $scope.selectAllTasks;
  99. $rootScope.keydownActions.delete = $scope.removeTasks;
  100. $rootScope.loadPromise = refreshDownloadTask(false);
  101. }]);
  102. }());