Skip to content

Commit

Permalink
code refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mayswind committed Aug 12, 2018
1 parent 0c678a1 commit 0a5b049
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 108 deletions.
1 change: 1 addition & 0 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@
<script src="scripts/services/ariaNgSettingService.js"></script>
<script src="scripts/services/ariaNgTitleService.js"></script>
<script src="scripts/services/ariaNgLogService.js"></script>
<script src="scripts/services/ariaNgLocalizationService.js"></script>
<script src="scripts/services/aria2HttpRpcService.js"></script>
<script src="scripts/services/aria2WebSocketRpcService.js"></script>
<script src="scripts/services/aria2RpcService.js"></script>
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/config/initiator.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
(function () {
'use strict';

angular.module('ariaNg').run(['moment', 'ariaNgSettingService', function (moment, ariaNgSettingService) {
angular.module('ariaNg').run(['moment', 'ariaNgLocalizationService', 'ariaNgSettingService', function (moment, ariaNgLocalizationService, ariaNgSettingService) {
var language = ariaNgSettingService.getLanguage();

moment.updateLocale('zh-cn', {
week: null
});

ariaNgSettingService.applyLanguage(language);
ariaNgLocalizationService.applyLanguage(language);
}]);
}());
12 changes: 6 additions & 6 deletions src/scripts/controllers/command.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
(function () {
'use strict';

angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2SettingService', 'aria2TaskService', 'ariaNgLogService', function ($rootScope, $window, $location, $routeParams, ariaNgDefaultOptions, ariaNgCommonService, ariaNgSettingService, aria2SettingService, aria2TaskService, ariaNgLogService) {
angular.module('ariaNg').controller('CommandController', ['$rootScope', '$window', '$location', '$routeParams', 'ariaNgDefaultOptions', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $window, $location, $routeParams, ariaNgDefaultOptions, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
var path = $location.path();

var doNewTaskCommand = function (url, params) {
try {
url = ariaNgCommonService.base64UrlDecode(url);
} catch (ex) {
ariaNgCommonService.showError('URL is not base64 encoded!');
ariaNgLocalizationService.showError('URL is not base64 encoded!');
return false;
}

Expand Down Expand Up @@ -59,20 +59,20 @@
ariaNgLogService.info('[CommandController] set rpc: ' + rpcProtocol + '://' + rpcHost + ':' + rpcPort + '/' + rpcInterface + ', secret: ' + secret);

if (!rpcProtocol || (rpcProtocol !== 'http' && rpcProtocol !== 'https' && rpcProtocol !== 'ws' && rpcProtocol !== 'wss')) {
ariaNgCommonService.showError('Protocol is invalid!');
ariaNgLocalizationService.showError('Protocol is invalid!');
return false;
}

if (!rpcHost) {
ariaNgCommonService.showError('RPC host cannot be empty!');
ariaNgLocalizationService.showError('RPC host cannot be empty!');
return false;
}

if (secret) {
try {
secret = ariaNgCommonService.base64UrlDecode(secret);
} catch (ex) {
ariaNgCommonService.showError('RPC secret is not base64 encoded!');
ariaNgLocalizationService.showError('RPC secret is not base64 encoded!');
return false;
}
}
Expand Down Expand Up @@ -108,7 +108,7 @@
} else if (path.indexOf('/settings/rpc/set') === 0) {
return doSetRpcCommand(params.protocol, params.host, params.port, params.interface, params.secret);
} else {
ariaNgCommonService.showError('Parameter is invalid!');
ariaNgLocalizationService.showError('Parameter is invalid!');
return false;
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/scripts/controllers/debug.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';

angular.module('ariaNg').controller('AriaNgDebugController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgLogService', function ($rootScope, $scope, $location, $timeout, ariaNgConstants, ariaNgCommonService, ariaNgSettingService, ariaNgLogService) {
angular.module('ariaNg').controller('AriaNgDebugController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgConstants', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', function ($rootScope, $scope, $location, $timeout, ariaNgConstants, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService) {
$scope.logMaxCount = ariaNgConstants.cachedDebugLogsLimit;
$scope.currentLog = null;

Expand All @@ -24,7 +24,7 @@

$rootScope.loadPromise = $timeout(function () {
if (!ariaNgSettingService.isEnableDebugMode()) {
ariaNgCommonService.showError('Access Denied!', function () {
ariaNgLocalizationService.showError('Access Denied!', function () {
if (!ariaNgSettingService.isEnableDebugMode()) {
$location.path('/settings/ariang');
}
Expand Down
14 changes: 7 additions & 7 deletions src/scripts/controllers/main.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';

angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$window', '$location', '$document', '$interval', 'clipboard', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgTitleService', 'ariaNgMonitorService', 'ariaNgNotificationService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $window, $location, $document, $interval, clipboard, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgTitleService, ariaNgMonitorService, ariaNgNotificationService, aria2TaskService, aria2SettingService) {
angular.module('ariaNg').controller('MainController', ['$rootScope', '$scope', '$route', '$window', '$location', '$document', '$interval', 'clipboard', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $route, $window, $location, $document, $interval, clipboard, aria2RpcErrors, ariaNgCommonService, ariaNgNotificationService, ariaNgLocalizationService, ariaNgSettingService, ariaNgMonitorService, ariaNgTitleService, aria2TaskService, aria2SettingService) {
var pageTitleRefreshPromise = null;
var globalStatRefreshPromise = null;

Expand Down Expand Up @@ -113,7 +113,7 @@

$rootScope.loadPromise = invoke(gids, function (response) {
if (response.hasError && gids.length > 1) {
ariaNgCommonService.showError('Failed to change some tasks state.');
ariaNgLocalizationService.showError('Failed to change some tasks state.');
}

if (!response.hasSuccess) {
Expand All @@ -139,10 +139,10 @@
};

$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 () {
ariaNgLocalizationService.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.');
ariaNgLocalizationService.showError('Failed to restart this task.');
return;
}

Expand All @@ -166,10 +166,10 @@
return;
}

ariaNgCommonService.confirm('Confirm Remove', 'Are you sure you want to remove the selected task?', 'warning', function () {
ariaNgLocalizationService.confirm('Confirm Remove', 'Are you sure you want to remove the selected task?', 'warning', function () {
$rootScope.loadPromise = aria2TaskService.removeTasks(tasks, function (response) {
if (response.hasError && tasks.length > 1) {
ariaNgCommonService.showError('Failed to remove some task(s).');
ariaNgLocalizationService.showError('Failed to remove some task(s).');
}

if (!response.hasSuccess) {
Expand All @@ -190,7 +190,7 @@
};

$scope.clearStoppedTasks = function () {
ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear stopped tasks?', 'warning', function () {
ariaNgLocalizationService.confirm('Confirm Clear', 'Are you sure you want to clear stopped tasks?', 'warning', function () {
$rootScope.loadPromise = aria2TaskService.clearStoppedTasks(function (response) {
if (!response.success) {
return;
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/controllers/new.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';

angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgLogService', 'ariaNgSettingService', 'ariaNgFileService', 'aria2SettingService', 'aria2TaskService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgLogService, ariaNgSettingService, ariaNgFileService, aria2SettingService, aria2TaskService) {
angular.module('ariaNg').controller('NewTaskController', ['$rootScope', '$scope', '$location', '$timeout', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgFileService', 'ariaNgSettingService', 'aria2TaskService', 'aria2SettingService', function ($rootScope, $scope, $location, $timeout, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgFileService, ariaNgSettingService, aria2TaskService, aria2SettingService) {
var tabOrders = ['links', 'options'];
var parameters = $location.search();

Expand Down Expand Up @@ -133,7 +133,7 @@
$scope.context.taskType = 'torrent';
$scope.changeTab('options');
}, function (error) {
ariaNgCommonService.showError(error);
ariaNgLocalizationService.showError(error);
}, angular.element('#file-holder'));
};

Expand All @@ -143,7 +143,7 @@
$scope.context.taskType = 'metalink';
$scope.changeTab('options');
}, function (error) {
ariaNgCommonService.showError(error);
ariaNgLocalizationService.showError(error);
}, angular.element('#file-holder'));
};

Expand Down
4 changes: 2 additions & 2 deletions src/scripts/controllers/settings-aria2.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
(function () {
'use strict';

angular.module('ariaNg').controller('Aria2SettingsController', ['$rootScope', '$scope', '$location', 'ariaNgConstants', 'ariaNgCommonService', 'aria2SettingService', function ($rootScope, $scope, $location, ariaNgConstants, ariaNgCommonService, aria2SettingService) {
angular.module('ariaNg').controller('Aria2SettingsController', ['$rootScope', '$scope', '$location', 'ariaNgConstants', 'ariaNgLocalizationService', 'aria2SettingService', function ($rootScope, $scope, $location, ariaNgConstants, ariaNgLocalizationService, aria2SettingService) {
var location = $location.path().substring($location.path().lastIndexOf('/') + 1);

$scope.context = {
availableOptions: (function (type) {
var keys = aria2SettingService.getAvailableGlobalOptionsKeys(type);

if (!keys) {
ariaNgCommonService.showError('Type is illegal!');
ariaNgLocalizationService.showError('Type is illegal!');
return;
}

Expand Down
17 changes: 10 additions & 7 deletions src/scripts/controllers/settings-ariang.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';

angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', 'ariaNgLanguages', 'ariaNgCommonService', 'aria2SettingService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgNotificationService', 'ariaNgTitleService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, ariaNgLanguages, ariaNgCommonService, aria2SettingService, ariaNgSettingService, ariaNgMonitorService, ariaNgNotificationService, ariaNgTitleService) {
angular.module('ariaNg').controller('AriaNgSettingsController', ['$rootScope', '$scope', '$routeParams', '$window', '$interval', '$timeout', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgLocalizationService', 'ariaNgSettingService', 'ariaNgMonitorService', 'ariaNgTitleService', 'aria2SettingService', function ($rootScope, $scope, $routeParams, $window, $interval, $timeout, ariaNgLanguages, ariaNgCommonService, ariaNgNotificationService, ariaNgLocalizationService, ariaNgSettingService, ariaNgMonitorService, ariaNgTitleService, aria2SettingService) {
var extendType = $routeParams.extendType;
var lastRefreshPageNotification = null;

Expand All @@ -20,7 +20,7 @@
$window.location.reload();
};

lastRefreshPageNotification = ariaNgNotificationService.notifyInPage('', 'Configuration has been modified, please reload the page for the changes to take effect.', {
lastRefreshPageNotification = ariaNgLocalizationService.notifyInPage('', 'Configuration has been modified, please reload the page for the changes to take effect.', {
delay: false,
type: 'info',
templateUrl: 'setting-changed-notification.html',
Expand Down Expand Up @@ -100,7 +100,10 @@
};

$scope.setLanguage = function (value) {
ariaNgSettingService.setLanguage(value);
if (ariaNgSettingService.setLanguage(value)) {
ariaNgLocalizationService.applyLanguage(value);
}

$scope.updateTitlePreview();
};

Expand Down Expand Up @@ -137,7 +140,7 @@
ariaNgNotificationService.requestBrowserPermission(function (permission) {
if (!ariaNgNotificationService.isPermissionGranted(permission)) {
$scope.context.settings.browserNotification = false;
ariaNgCommonService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.');
ariaNgLocalizationService.showError('You have disabled notification in your browser. You should change your browser\'s settings before you enable this function.');
}
});
}
Expand Down Expand Up @@ -170,7 +173,7 @@
$scope.removeRpcSetting = function (setting) {
var rpcName = (setting.rpcAlias ? setting.rpcAlias : setting.rpcHost + ':' + setting.rpcPort);

ariaNgCommonService.confirm('Confirm Remove', 'Are you sure you want to remove rpc setting "{{rpcName}}"?', 'warning', function () {
ariaNgLocalizationService.confirm('Confirm Remove', 'Are you sure you want to remove rpc setting "{{rpcName}}"?', 'warning', function () {
setNeedRefreshPage();

var index = $scope.context.rpcSettings.indexOf(setting);
Expand All @@ -197,14 +200,14 @@
};

$scope.resetSettings = function () {
ariaNgCommonService.confirm('Confirm Reset', 'Are you sure you want to reset all settings?', 'warning', function () {
ariaNgLocalizationService.confirm('Confirm Reset', 'Are you sure you want to reset all settings?', 'warning', function () {
ariaNgSettingService.resetSettings();
$window.location.reload();
});
};

$scope.clearHistory = function () {
ariaNgCommonService.confirm('Confirm Clear', 'Are you sure you want to clear all settings history?', 'warning', function () {
ariaNgLocalizationService.confirm('Confirm Clear', 'Are you sure you want to clear all settings history?', 'warning', function () {
aria2SettingService.clearSettingsHistorys();
$window.location.reload();
});
Expand Down
8 changes: 4 additions & 4 deletions src/scripts/controllers/status.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(function () {
'use strict';

angular.module('ariaNg').controller('Aria2StatusController', ['$rootScope', '$scope', 'ariaNgCommonService', 'ariaNgSettingService', 'aria2SettingService', function ($rootScope, $scope, ariaNgCommonService, ariaNgSettingService, aria2SettingService) {
angular.module('ariaNg').controller('Aria2StatusController', ['$rootScope', '$scope', 'ariaNgLocalizationService', 'ariaNgSettingService', 'aria2SettingService', function ($rootScope, $scope, ariaNgLocalizationService, ariaNgSettingService, aria2SettingService) {
$scope.context = {
host: ariaNgSettingService.getCurrentRpcUrl(),
status: 'Connecting',
Expand All @@ -11,16 +11,16 @@
$scope.saveSession = function () {
return aria2SettingService.saveSession(function (response) {
if (response.success && response.data === 'OK') {
ariaNgCommonService.showOperationSucceeded('Session has been saved successfully.');
ariaNgLocalizationService.showOperationSucceeded('Session has been saved successfully.');
}
});
};

$scope.shutdown = function () {
ariaNgCommonService.confirm('Confirm Shutdown', 'Are you sure you want to shutdown aria2?', 'warning', function (status) {
ariaNgLocalizationService.confirm('Confirm Shutdown', 'Are you sure you want to shutdown aria2?', 'warning', function (status) {
return aria2SettingService.shutdown(function (response) {
if (response.success && response.data === 'OK') {
ariaNgCommonService.showOperationSucceeded('Aria2 has been shutdown successfully.');
ariaNgLocalizationService.showOperationSucceeded('Aria2 has been shutdown successfully.');
}
});
}, true);
Expand Down
Loading

0 comments on commit 0a5b049

Please sign in to comment.