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/src/scripts/controllers/main.js

255 lines
9.1 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-06-26 16:52:06 +02:00
angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$location', '$document', '$interval', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgNotificationService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $location, $document, $interval, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgMonitorService, ariaNgNotificationService, aria2TaskService, aria2SettingService) {
var pageTitleRefreshPromise = null;
2016-05-17 16:15:28 +02:00
var globalStatRefreshPromise = null;
var refreshPageTitle = function () {
var context = (function (globalStat) {
if (!globalStat) {
return null;
}
return {
downloadingCount: globalStat.numActive,
waitingCount: globalStat.numWaiting,
stoppedCount: globalStat.numStopped,
downloadSpeed: globalStat.downloadSpeed,
uploadSpeed: globalStat.uploadSpeed
2016-12-10 18:50:25 +01:00
};
})($scope.globalStat);
$document[0].title = ariaNgSettingService.getFinalTitle(context);
};
var refreshGlobalStat = function (silent, callback) {
2016-06-10 13:23:16 +02:00
return aria2SettingService.getGlobalStat(function (response) {
2016-08-01 16:49:16 +02:00
if (!response.success && response.data.message === aria2RpcErrors.Unauthorized.message) {
2016-06-10 13:23:16 +02:00
$interval.cancel(globalStatRefreshPromise);
return;
}
2016-06-10 13:23:16 +02:00
if (response.success) {
$scope.globalStat = response.data;
ariaNgMonitorService.recordGlobalStat(response.data);
2016-06-10 13:23:16 +02:00
}
if (callback) {
callback(response);
}
2016-06-04 09:33:40 +02:00
}, silent);
2016-05-17 16:15:28 +02:00
};
2016-06-26 16:52:06 +02:00
if (ariaNgSettingService.getBrowserNotification()) {
ariaNgNotificationService.requestBrowserPermission();
}
2016-05-17 16:15:28 +02:00
$scope.globalStatusContext = {
isEnabled: ariaNgSettingService.getGlobalStatRefreshInterval() > 0,
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
$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++) {
for (var j = 0; j < arguments.length; j++) {
2016-08-01 16:49:16 +02:00
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++) {
2016-08-01 16:49:16 +02:00
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;
2016-08-01 16:49:16 +02:00
if (state === 'start') {
2016-06-01 16:05:36 +02:00
invoke = aria2TaskService.startTasks;
2016-08-01 16:49:16 +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
2016-08-01 16:49:16 +02:00
if (!response.hasError && state === 'start') {
2016-12-10 18:10:56 +01:00
if ($location.path() === '/waiting') {
2016-06-10 13:23:16 +02:00
$location.path('/downloading');
} else {
$route.reload();
}
2016-08-01 16:49:16 +02:00
} else if (!response.hasError && state === 'pause') {
2016-12-10 18:10:56 +01:00
if ($location.path() === '/downloading') {
2016-06-10 13:23:16 +02:00
$location.path('/waiting');
} else {
$route.reload();
}
}
}, (gids.length > 1));
2016-05-30 19:26:41 +02:00
};
2016-12-10 17:25:19 +01:00
$scope.restart = function (task) {
ariaNgCommonService.confirm('Confirm Restart', 'Are you sure you want to restart this task? AriaNg will create a same task after clicking OK.', 'info', function () {
$rootScope.loadPromise = aria2TaskService.restartTask(task.gid, function (response) {
if (!response.success) {
ariaNgCommonService.showError('Failed to restart this task.');
return;
}
refreshGlobalStat(true);
if (response.success) {
if ($location.path() !== '/downloading') {
$location.path('/downloading');
} else {
$route.reload();
}
}
}, false);
});
};
2016-05-31 16:51:12 +02:00
$scope.removeTasks = function () {
var tasks = $rootScope.taskContext.getSelectedTasks();
2016-05-30 19:26:41 +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) {
2016-12-10 17:25:19 +01:00
if ($location.path() !== '/stopped') {
2016-06-10 13:23:16 +02:00
$location.path('/stopped');
2016-12-10 17:25:19 +01:00
} else {
$route.reload();
2016-06-10 13:23:16 +02:00
}
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
});
};
$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-12-10 17:25:19 +01:00
if ($location.path() !== '/stopped') {
2016-06-04 11:10:54 +02:00
$location.path('/stopped');
2016-12-10 17:25:19 +01:00
} else {
$route.reload();
2016-06-04 11:10:54 +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
};
$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-08-01 16:49:16 +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-25 18:09:00 +02:00
return orderType.equals(targetType);
2016-05-17 16:15:28 +02:00
};
if (ariaNgSettingService.getTitleRefreshInterval() > 0) {
pageTitleRefreshPromise = $interval(function () {
refreshPageTitle();
}, ariaNgSettingService.getTitleRefreshInterval());
}
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 (pageTitleRefreshPromise) {
$interval.cancel(pageTitleRefreshPromise);
}
2016-05-17 16:15:28 +02:00
if (globalStatRefreshPromise) {
$interval.cancel(globalStatRefreshPromise);
}
});
2016-05-31 16:51:12 +02:00
refreshGlobalStat(true, function () {
refreshPageTitle();
});
2016-05-13 18:09:12 +02:00
}]);
2016-08-01 16:49:16 +02:00
}());