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/app/scripts/controllers/list.js
2016-05-17 23:32:36 +08:00

108 lines
3.9 KiB
JavaScript

(function () {
'use strict';
angular.module('ariaNg').controller('DownloadListController', ['$scope', '$window', '$location', '$interval', 'translateFilter', 'aria2RpcService', 'ariaNgSettingService', 'utils', function ($scope, $window, $location, $interval, translateFilter, aria2RpcService, ariaNgSettingService, utils) {
var location = $location.path().substring(1);
var downloadTaskRefreshPromise = null;
var getTitleWidth = function () {
var titleColumn = angular.element('.task-table > .row > .col-md-8:first-child');
if (titleColumn.length > 0) {
return titleColumn.width();
} else {
var taskTable = angular.element('.task-table');
if ($window.innerWidth <= 767) {
return taskTable.width();
} else {
return taskTable.width() / 12 * 8;
}
}
};
var calculateDownloadRemainTime = function (remainBytes, downloadSpeed) {
if (downloadSpeed == 0) {
return 0;
}
return remainBytes / downloadSpeed;
};
var processDownloadTask = function (task) {
var totalLength = parseInt(task.totalLength);
var completedLength = parseInt(task.completedLength);
var remainLength = totalLength - completedLength;
if (task.bittorrent && task.bittorrent.info) {
task.taskName = task.bittorrent.info.name;
} else if (task.files && task.files.length >= 1) {
task.taskName = utils.getFileNameFromPath(task.files[0].path);
} else {
task.taskName = translateFilter('Unknown');
}
task.fileSize = totalLength;
task.completePercent = completedLength / totalLength * 100;
task.idle = task.downloadSpeed == 0;
task.remainTime = calculateDownloadRemainTime(remainLength, task.downloadSpeed);
};
var refreshDownloadTask = function () {
var invokeMethod = null;
var params = [];
if (location == 'downloading') {
invokeMethod = aria2RpcService.tellActive;
} else if (location == 'waiting') {
invokeMethod = aria2RpcService.tellWaiting;
params = [0, 1000];
} else if (location == 'stopped') {
invokeMethod = aria2RpcService.tellStopped;
params = [0, 1000];
}
if (invokeMethod) {
return invokeMethod({
params: params,
callback: function (result) {
if (result && result.length > 0) {
for (var i = 0; i < result.length; i++) {
processDownloadTask(result[i]);
}
}
if (!utils.replaceArray(result, $scope.downloadTasks, 'gid')) {
$scope.downloadTasks = result;
}
}
});
}
};
$scope.loadPromise = refreshDownloadTask();
angular.element($window).bind('resize', function () {
$scope.titleWidth = getTitleWidth();
});
$scope.titleWidth = getTitleWidth();
$scope.getOrderType = function () {
return ariaNgSettingService.getDisplayOrder();
};
if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {
downloadTaskRefreshPromise = $interval(function () {
refreshDownloadTask();
}, ariaNgSettingService.getDownloadTaskRefreshInterval());
}
$scope.$on('$destroy', function () {
if (downloadTaskRefreshPromise) {
$interval.cancel(downloadTaskRefreshPromise);
}
});
}]);
})();