2016-05-13 18:09:12 +02:00
|
|
|
(function () {
|
|
|
|
'use strict';
|
|
|
|
|
2016-06-10 19:25:09 +02:00
|
|
|
angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$location', '$interval', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgMonitorService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $location, $interval, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgMonitorService, aria2TaskService, aria2SettingService) {
|
2016-05-17 16:15:28 +02:00
|
|
|
var globalStatRefreshPromise = null;
|
|
|
|
|
2016-06-04 09:33:40 +02:00
|
|
|
var refreshGlobalStat = function (silent) {
|
2016-06-10 13:23:16 +02:00
|
|
|
return aria2SettingService.getGlobalStat(function (response) {
|
|
|
|
if (!response.success && response.data.message == aria2RpcErrors.Unauthorized.message) {
|
|
|
|
$interval.cancel(globalStatRefreshPromise);
|
|
|
|
return;
|
|
|
|
}
|
2016-06-10 19:25:09 +02:00
|
|
|
|
2016-06-10 13:23:16 +02:00
|
|
|
if (response.success) {
|
|
|
|
$scope.globalStat = response.data;
|
2016-06-10 19:25:09 +02:00
|
|
|
ariaNgMonitorService.recordGlobalStat(response.data);
|
2016-06-10 13:23:16 +02:00
|
|
|
}
|
2016-06-04 09:33:40 +02:00
|
|
|
}, silent);
|
2016-05-17 16:15:28 +02:00
|
|
|
};
|
|
|
|
|
2016-06-10 19:25:09 +02:00
|
|
|
$scope.globalStatusContext = {
|
|
|
|
data: ariaNgMonitorService.getGlobalStatsData()
|
|
|
|
};
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
$scope.isTaskSelected = function () {
|
|
|
|
return $rootScope.taskContext.getSelectedTaskIds().length > 0;
|
|
|
|
};
|
2016-05-17 16:15:28 +02:00
|
|
|
|
2016-06-04 10:52:24 +02:00
|
|
|
$scope.isSpecifiedTaskSelected = function () {
|
2016-05-31 16:51:12 +02:00
|
|
|
var selectedTasks = $rootScope.taskContext.getSelectedTasks();
|
2016-05-30 19:26:41 +02:00
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
if (selectedTasks.length < 1) {
|
|
|
|
return false;
|
2016-05-30 19:26:41 +02:00
|
|
|
}
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
for (var i = 0; i < selectedTasks.length; i++) {
|
2016-06-04 10:52:24 +02:00
|
|
|
for (var j = 0; j < arguments.length; j++) {
|
|
|
|
if (selectedTasks[i].status == arguments[j]) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.isSpecifiedTaskShowing = function () {
|
|
|
|
var tasks = $rootScope.taskContext.list;
|
|
|
|
|
|
|
|
if (tasks.length < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (var i = 0; i < tasks.length; i++) {
|
|
|
|
for (var j = 0; j < arguments.length; j++) {
|
|
|
|
if (tasks[i].status == arguments[j]) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-05-30 19:26:41 +02:00
|
|
|
}
|
2016-05-31 16:51:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2016-05-30 19:26:41 +02:00
|
|
|
};
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
$scope.changeTasksState = function (state) {
|
2016-05-30 19:26:41 +02:00
|
|
|
var gids = $rootScope.taskContext.getSelectedTaskIds();
|
|
|
|
|
|
|
|
if (!gids || gids.length < 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
var invoke = null;
|
|
|
|
|
|
|
|
if (state == 'start') {
|
2016-06-01 16:05:36 +02:00
|
|
|
invoke = aria2TaskService.startTasks;
|
2016-05-31 16:51:12 +02:00
|
|
|
} else if (state == 'pause') {
|
2016-06-01 16:05:36 +02:00
|
|
|
invoke = aria2TaskService.pauseTasks;
|
2016-05-31 16:51:12 +02:00
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-10 13:23:16 +02:00
|
|
|
$rootScope.loadPromise = invoke(gids, function (response) {
|
|
|
|
if (response.hasError && gids.length > 1) {
|
|
|
|
ariaNgCommonService.showError('Failed to change some tasks state.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.hasSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:56:33 +02:00
|
|
|
refreshGlobalStat(true);
|
2016-06-10 13:23:16 +02:00
|
|
|
|
|
|
|
if (!response.hasError && state == 'start') {
|
|
|
|
if ($location.path() == '/waiting') {
|
|
|
|
$location.path('/downloading');
|
|
|
|
} else {
|
|
|
|
$route.reload();
|
|
|
|
}
|
|
|
|
} else if (!response.hasError && state == 'pause') {
|
|
|
|
if ($location.path() == '/downloading') {
|
|
|
|
$location.path('/waiting');
|
|
|
|
} else {
|
|
|
|
$route.reload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, (gids.length > 1));
|
2016-05-30 19:26:41 +02:00
|
|
|
};
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
$scope.removeTasks = function () {
|
2016-06-04 10:52:24 +02:00
|
|
|
var tasks = $rootScope.taskContext.getSelectedTasks();
|
2016-05-30 19:26:41 +02:00
|
|
|
|
2016-06-04 10:52:24 +02:00
|
|
|
if (!tasks || tasks.length < 1) {
|
2016-05-30 19:26:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-31 16:51:12 +02:00
|
|
|
ariaNgCommonService.confirm('Confirm Remove', 'Are you sure you want to remove the selected task?', 'warning', function () {
|
2016-06-10 13:23:16 +02:00
|
|
|
$rootScope.loadPromise = aria2TaskService.removeTasks(tasks, function (response) {
|
|
|
|
if (response.hasError && tasks.length > 1) {
|
|
|
|
ariaNgCommonService.showError('Failed to remove some task(s).');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!response.hasSuccess) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:56:33 +02:00
|
|
|
refreshGlobalStat(true);
|
2016-06-10 13:23:16 +02:00
|
|
|
|
|
|
|
if (!response.hasError) {
|
|
|
|
if ($location.path() == '/stopped') {
|
|
|
|
$route.reload();
|
|
|
|
} else {
|
|
|
|
$location.path('/stopped');
|
|
|
|
}
|
2016-06-04 11:10:54 +02:00
|
|
|
}
|
2016-06-10 13:23:16 +02:00
|
|
|
}, (tasks.length > 1));
|
2016-05-30 19:26:41 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-06-04 10:52:24 +02:00
|
|
|
$scope.clearStoppedTasks = function () {
|
|
|
|
ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear stopped tasks?', 'warning', function () {
|
2016-06-10 13:23:16 +02:00
|
|
|
$rootScope.loadPromise = aria2TaskService.clearStoppedTasks(function (response) {
|
|
|
|
if (!response.success) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-06-06 16:56:33 +02:00
|
|
|
refreshGlobalStat(true);
|
2016-06-10 13:23:16 +02:00
|
|
|
|
2016-06-04 11:10:54 +02:00
|
|
|
if ($location.path() == '/stopped') {
|
|
|
|
$route.reload();
|
|
|
|
} else {
|
|
|
|
$location.path('/stopped');
|
|
|
|
}
|
2016-06-04 10:52:24 +02:00
|
|
|
});
|
2016-05-30 19:26:41 +02:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-05-29 17:27:47 +02:00
|
|
|
$scope.selectAllTasks = function () {
|
2016-05-30 16:34:15 +02:00
|
|
|
$rootScope.taskContext.selectAll();
|
2016-05-29 17:27:47 +02:00
|
|
|
};
|
|
|
|
|
2016-05-22 05:13:05 +02:00
|
|
|
$scope.changeDisplayOrder = function (type, autoSetReverse) {
|
2016-05-31 16:51:12 +02:00
|
|
|
var oldType = ariaNgCommonService.parseOrderType(ariaNgSettingService.getDisplayOrder());
|
|
|
|
var newType = ariaNgCommonService.parseOrderType(type);
|
2016-05-22 05:13:05 +02:00
|
|
|
|
|
|
|
if (autoSetReverse && newType.type == oldType.type) {
|
|
|
|
newType.reverse = !oldType.reverse;
|
|
|
|
}
|
|
|
|
|
|
|
|
ariaNgSettingService.setDisplayOrder(newType.getValue());
|
2016-05-17 16:15:28 +02:00
|
|
|
};
|
|
|
|
|
2016-05-25 18:09:00 +02:00
|
|
|
$scope.isSetDisplayOrder = function (type) {
|
2016-05-31 16:51:12 +02:00
|
|
|
var orderType = ariaNgCommonService.parseOrderType(ariaNgSettingService.getDisplayOrder());
|
|
|
|
var targetType = ariaNgCommonService.parseOrderType(type);
|
2016-05-22 05:13:05 +02:00
|
|
|
|
2016-05-25 18:09:00 +02:00
|
|
|
return orderType.equals(targetType);
|
2016-05-17 16:15:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (ariaNgSettingService.getGlobalStatRefreshInterval() > 0) {
|
|
|
|
globalStatRefreshPromise = $interval(function () {
|
2016-06-04 09:33:40 +02:00
|
|
|
refreshGlobalStat(true);
|
2016-05-17 16:15:28 +02:00
|
|
|
}, ariaNgSettingService.getGlobalStatRefreshInterval());
|
|
|
|
}
|
|
|
|
|
|
|
|
$scope.$on('$destroy', function () {
|
|
|
|
if (globalStatRefreshPromise) {
|
|
|
|
$interval.cancel(globalStatRefreshPromise);
|
|
|
|
}
|
|
|
|
});
|
2016-05-31 16:51:12 +02:00
|
|
|
|
2016-06-06 16:56:33 +02:00
|
|
|
refreshGlobalStat(true);
|
2016-05-13 18:09:12 +02:00
|
|
|
}]);
|
|
|
|
})();
|