2016-05-23 19:06:50 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
angular.module('ariaNg').controller('TaskDetailController', ['$rootScope', '$scope', '$routeParams', '$interval', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $routeParams, $interval, ariaNgCommonService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
|
2016-05-24 19:30:02 +02:00
|
|
|
var tabOrders = ['overview', 'blocks', 'filelist', 'btpeers'];
|
2016-05-23 19:06:50 +02:00
|
|
|
var downloadTaskRefreshPromise = null;
|
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
var getAvailableOptions = function (status, isBittorrent) {
|
|
|
|
var keys = aria2SettingService.getAvailableTaskOptionKeys(status, isBittorrent);
|
2016-05-25 17:33:06 +02:00
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
if (!keys) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return aria2SettingService.getSpecifiedOptions(keys);
|
|
|
|
};
|
2016-05-25 17:33:06 +02:00
|
|
|
|
2016-06-04 09:33:40 +02:00
|
|
|
var refreshBtPeers = function (task, silent) {
|
2016-06-01 16:05:36 +02:00
|
|
|
return aria2TaskService.getBtTaskPeers(task.gid, function (result) {
|
2016-05-31 16:51:12 +02:00
|
|
|
if (!ariaNgCommonService.extendArray(result, $scope.peers, 'peerId')) {
|
|
|
|
$scope.peers = result;
|
2016-05-24 17:30:33 +02:00
|
|
|
}
|
2016-05-31 16:51:12 +02:00
|
|
|
|
2016-06-01 16:05:36 +02:00
|
|
|
$scope.healthPercent = aria2TaskService.estimateHealthPercentFromPeers(task, $scope.peers);
|
2016-06-04 09:33:40 +02:00
|
|
|
}, silent);
|
2016-05-24 17:30:33 +02:00
|
|
|
};
|
|
|
|
|
2016-06-04 09:33:40 +02:00
|
|
|
var refreshDownloadTask = function (silent) {
|
2016-06-01 16:05:36 +02:00
|
|
|
return aria2TaskService.getTaskStatus($routeParams.gid, function (result) {
|
2016-05-31 16:51:12 +02:00
|
|
|
if (result.status == 'active' && result.bittorrent) {
|
2016-06-04 09:33:40 +02:00
|
|
|
refreshBtPeers(result, true);
|
2016-05-31 16:51:12 +02:00
|
|
|
} else {
|
|
|
|
if (tabOrders.indexOf('btpeers') >= 0) {
|
|
|
|
tabOrders.splice(tabOrders.indexOf('btpeers'), 1);
|
2016-05-24 17:30:33 +02:00
|
|
|
}
|
2016-05-31 16:51:12 +02:00
|
|
|
}
|
2016-05-24 17:30:33 +02:00
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
if (!$scope.task || $scope.task.status != result.status) {
|
|
|
|
$scope.availableOptions = getAvailableOptions(result.status, !!result.bittorrent);
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
$scope.task = ariaNgCommonService.copyObjectTo(result, $scope.task);
|
2016-05-30 19:26:41 +02:00
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
$rootScope.taskContext.list = [$scope.task];
|
|
|
|
$rootScope.taskContext.selected = {};
|
|
|
|
$rootScope.taskContext.selected[$scope.task.gid] = true;
|
2016-06-04 09:33:40 +02:00
|
|
|
}, silent);
|
2016-05-23 19:06:50 +02:00
|
|
|
};
|
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
$scope.context = {
|
|
|
|
currentTab: 'overview'
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.healthPercent = 0;
|
|
|
|
$scope.availableOptions = [];
|
|
|
|
|
2016-05-24 19:30:02 +02:00
|
|
|
$rootScope.swipeActions.extentLeftSwipe = function () {
|
|
|
|
var tabIndex = tabOrders.indexOf($scope.context.currentTab);
|
|
|
|
|
|
|
|
if (tabIndex < tabOrders.length - 1) {
|
|
|
|
$scope.context.currentTab = tabOrders[tabIndex + 1];
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.swipeActions.extentRightSwipe = function () {
|
|
|
|
var tabIndex = tabOrders.indexOf($scope.context.currentTab);
|
|
|
|
|
|
|
|
if (tabIndex > 0) {
|
|
|
|
$scope.context.currentTab = tabOrders[tabIndex - 1];
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-05-25 17:33:06 +02:00
|
|
|
$scope.changeFileListDisplayOrder = function (type, autoSetReverse) {
|
2016-05-31 16:51:12 +02:00
|
|
|
var oldType = ariaNgCommonService.parseOrderType(ariaNgSettingService.getFileListDisplayOrder());
|
|
|
|
var newType = ariaNgCommonService.parseOrderType(type);
|
2016-05-25 17:33:06 +02:00
|
|
|
|
|
|
|
if (autoSetReverse && newType.type == oldType.type) {
|
|
|
|
newType.reverse = !oldType.reverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
ariaNgSettingService.setFileListDisplayOrder(newType.getValue());
|
|
|
|
};
|
|
|
|
|
2016-05-25 18:09:00 +02:00
|
|
|
$scope.isSetFileListDisplayOrder = function (type) {
|
2016-05-31 16:51:12 +02:00
|
|
|
var orderType = ariaNgCommonService.parseOrderType(ariaNgSettingService.getFileListDisplayOrder());
|
|
|
|
var targetType = ariaNgCommonService.parseOrderType(type);
|
2016-05-25 17:33:06 +02:00
|
|
|
|
2016-05-25 18:09:00 +02:00
|
|
|
return orderType.equals(targetType);
|
2016-05-25 17:33:06 +02:00
|
|
|
};
|
2016-05-25 18:09:00 +02:00
|
|
|
|
2016-05-25 17:33:06 +02:00
|
|
|
$scope.getFileListOrderType = function () {
|
|
|
|
return ariaNgSettingService.getFileListDisplayOrder();
|
|
|
|
};
|
|
|
|
|
2016-06-01 18:02:10 +02:00
|
|
|
$scope.loadTaskOption = function (task) {
|
|
|
|
$rootScope.loadPromise = aria2TaskService.getTaskOptions(task.gid, function (result) {
|
|
|
|
$scope.options = result;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-02 18:16:27 +02:00
|
|
|
$scope.setOption = function (key, value, optionStatus) {
|
2016-06-01 18:02:10 +02:00
|
|
|
return aria2TaskService.setTaskOption($scope.task.gid, key, value, function (result) {
|
2016-06-02 18:16:27 +02:00
|
|
|
if (result == 'OK') {
|
|
|
|
optionStatus.setSuccess();
|
|
|
|
} else {
|
|
|
|
optionStatus.setFailed();
|
|
|
|
}
|
2016-06-01 18:02:10 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-23 19:06:50 +02:00
|
|
|
if (ariaNgSettingService.getDownloadTaskRefreshInterval() > 0) {
|
|
|
|
downloadTaskRefreshPromise = $interval(function () {
|
2016-06-04 09:33:40 +02:00
|
|
|
refreshDownloadTask(true);
|
2016-05-23 19:06:50 +02:00
|
|
|
}, ariaNgSettingService.getDownloadTaskRefreshInterval());
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
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-23 19:06:50 +02:00
|
|
|
}]);
|
|
|
|
})();
|