From 0a5b049bfd812ea2b4667aca9f3678100d9a2b99 Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 12 Aug 2018 20:26:26 +0800 Subject: [PATCH] code refactor --- src/index.html | 1 + src/scripts/config/initiator.js | 4 +- src/scripts/controllers/command.js | 12 +-- src/scripts/controllers/debug.js | 4 +- src/scripts/controllers/main.js | 14 +-- src/scripts/controllers/new.js | 6 +- src/scripts/controllers/settings-aria2.js | 4 +- src/scripts/controllers/settings-ariang.js | 17 ++-- src/scripts/controllers/status.js | 8 +- src/scripts/core/root.js | 14 +-- src/scripts/directives/setting.js | 4 +- src/scripts/directives/settingDialog.js | 4 +- src/scripts/filters/longDate.js | 6 +- src/scripts/services/aria2RpcService.js | 6 +- src/scripts/services/aria2TaskService.js | 4 +- src/scripts/services/ariaNgCommonService.js | 27 +++--- .../services/ariaNgLocalizationService.js | 94 +++++++++++++++++++ src/scripts/services/ariaNgMonitorService.js | 4 +- .../services/ariaNgNotificationService.js | 23 +---- src/scripts/services/ariaNgSettingService.js | 19 ++-- src/scripts/services/ariaNgTitleService.js | 12 +-- 21 files changed, 179 insertions(+), 108 deletions(-) create mode 100644 src/scripts/services/ariaNgLocalizationService.js diff --git a/src/index.html b/src/index.html index 289180b..6b9bd43 100644 --- a/src/index.html +++ b/src/index.html @@ -394,6 +394,7 @@ + diff --git a/src/scripts/config/initiator.js b/src/scripts/config/initiator.js index 2bba935..93d5923 100644 --- a/src/scripts/config/initiator.js +++ b/src/scripts/config/initiator.js @@ -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); }]); }()); diff --git a/src/scripts/controllers/command.js b/src/scripts/controllers/command.js index 05e4a5e..a3e5aca 100644 --- a/src/scripts/controllers/command.js +++ b/src/scripts/controllers/command.js @@ -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; } @@ -59,12 +59,12 @@ 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; } @@ -72,7 +72,7 @@ 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; } } @@ -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; } }; diff --git a/src/scripts/controllers/debug.js b/src/scripts/controllers/debug.js index 9b6c46d..8bb3e16 100644 --- a/src/scripts/controllers/debug.js +++ b/src/scripts/controllers/debug.js @@ -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; @@ -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'); } diff --git a/src/scripts/controllers/main.js b/src/scripts/controllers/main.js index 7f51230..4689f67 100644 --- a/src/scripts/controllers/main.js +++ b/src/scripts/controllers/main.js @@ -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; @@ -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) { @@ -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; } @@ -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) { @@ -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; diff --git a/src/scripts/controllers/new.js b/src/scripts/controllers/new.js index 6c61063..e27d77d 100644 --- a/src/scripts/controllers/new.js +++ b/src/scripts/controllers/new.js @@ -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(); @@ -133,7 +133,7 @@ $scope.context.taskType = 'torrent'; $scope.changeTab('options'); }, function (error) { - ariaNgCommonService.showError(error); + ariaNgLocalizationService.showError(error); }, angular.element('#file-holder')); }; @@ -143,7 +143,7 @@ $scope.context.taskType = 'metalink'; $scope.changeTab('options'); }, function (error) { - ariaNgCommonService.showError(error); + ariaNgLocalizationService.showError(error); }, angular.element('#file-holder')); }; diff --git a/src/scripts/controllers/settings-aria2.js b/src/scripts/controllers/settings-aria2.js index 35647d7..0777900 100644 --- a/src/scripts/controllers/settings-aria2.js +++ b/src/scripts/controllers/settings-aria2.js @@ -1,7 +1,7 @@ (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 = { @@ -9,7 +9,7 @@ var keys = aria2SettingService.getAvailableGlobalOptionsKeys(type); if (!keys) { - ariaNgCommonService.showError('Type is illegal!'); + ariaNgLocalizationService.showError('Type is illegal!'); return; } diff --git a/src/scripts/controllers/settings-ariang.js b/src/scripts/controllers/settings-ariang.js index b8bdec6..5a0edcf 100644 --- a/src/scripts/controllers/settings-ariang.js +++ b/src/scripts/controllers/settings-ariang.js @@ -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; @@ -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', @@ -100,7 +100,10 @@ }; $scope.setLanguage = function (value) { - ariaNgSettingService.setLanguage(value); + if (ariaNgSettingService.setLanguage(value)) { + ariaNgLocalizationService.applyLanguage(value); + } + $scope.updateTitlePreview(); }; @@ -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.'); } }); } @@ -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); @@ -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(); }); diff --git a/src/scripts/controllers/status.js b/src/scripts/controllers/status.js index 020a6d1..ab1e24b 100644 --- a/src/scripts/controllers/status.js +++ b/src/scripts/controllers/status.js @@ -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', @@ -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); diff --git a/src/scripts/core/root.js b/src/scripts/core/root.js index 23c7e0b..3d24552 100644 --- a/src/scripts/core/root.js +++ b/src/scripts/core/root.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').run(['$rootScope', '$location', '$document', 'ariaNgCommonService', 'ariaNgNotificationService', 'ariaNgSettingService', 'ariaNgLogService', 'aria2TaskService', function ($rootScope, $location, $document, ariaNgCommonService, ariaNgNotificationService, ariaNgSettingService, ariaNgLogService, aria2TaskService) { + angular.module('ariaNg').run(['$rootScope', '$location', '$document', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2TaskService', function ($rootScope, $location, $document, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, aria2TaskService) { var isUrlMatchUrl2 = function (url, url2) { if (url === url2) { return true; @@ -38,7 +38,7 @@ angular.element('.main-sidebar').addClass('blur'); angular.element('.navbar').addClass('blur'); angular.element('.content-body').addClass('blur'); - ariaNgNotificationService.notifyInPage('', 'You cannot use AriaNg because this browser does not support data storage.', { + ariaNgLocalizationService.notifyInPage('', 'You cannot use AriaNg because this browser does not support data storage.', { type: 'error', delay: false }); @@ -181,7 +181,7 @@ }; ariaNgSettingService.onFirstAccess(function () { - ariaNgNotificationService.notifyInPage('', 'Tap to configure and get started with AriaNg.', { + ariaNgLocalizationService.notifyInPage('', 'Tap to configure and get started with AriaNg.', { delay: false, onClose: function () { $location.path('/settings/ariang'); @@ -190,7 +190,7 @@ }); aria2TaskService.onFirstSuccess(function (event) { - ariaNgNotificationService.notifyInPage('', '{{name}} is connected', { + ariaNgLocalizationService.notifyInPage('', '{{name}} is connected', { type: 'success', contentParams: { name: event.rpcName @@ -207,15 +207,15 @@ }); aria2TaskService.onTaskCompleted(function (event) { - ariaNgNotificationService.notifyTaskComplete(event.task); + ariaNgLocalizationService.notifyTaskComplete(event.task); }); aria2TaskService.onBtTaskCompleted(function (event) { - ariaNgNotificationService.notifyBtTaskComplete(event.task); + ariaNgLocalizationService.notifyBtTaskComplete(event.task); }); aria2TaskService.onTaskErrorOccur(function (event) { - ariaNgNotificationService.notifyTaskError(event.task); + ariaNgLocalizationService.notifyTaskError(event.task); }); $rootScope.$on('$locationChangeStart', function (event) { diff --git a/src/scripts/directives/setting.js b/src/scripts/directives/setting.js index 69d3060..23a0200 100644 --- a/src/scripts/directives/setting.js +++ b/src/scripts/directives/setting.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').directive('ngSetting', ['$timeout', '$q', '$translate', 'ariaNgConstants', 'aria2SettingService', function ($timeout, $q, $translate, ariaNgConstants, aria2SettingService) { + angular.module('ariaNg').directive('ngSetting', ['$timeout', '$q', 'ariaNgConstants', 'ariaNgLocalizationService', 'aria2SettingService', function ($timeout, $q, ariaNgConstants, ariaNgLocalizationService, aria2SettingService) { return { restrict: 'E', templateUrl: 'views/setting.html', @@ -47,7 +47,7 @@ } angular.element(element).tooltip({ - title: $translate.instant(cause, causeParams), + title: ariaNgLocalizationService.getLocalizedText(cause, causeParams), trigger: 'focus', placement: 'auto top', container: element, diff --git a/src/scripts/directives/settingDialog.js b/src/scripts/directives/settingDialog.js index 5d72a3d..9a4b453 100644 --- a/src/scripts/directives/settingDialog.js +++ b/src/scripts/directives/settingDialog.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').directive('ngSettingDialog', ['ariaNgCommonService', 'aria2SettingService', function (ariaNgCommonService, aria2SettingService) { + angular.module('ariaNg').directive('ngSettingDialog', ['ariaNgLocalizationService', 'aria2SettingService', function (ariaNgLocalizationService, aria2SettingService) { return { restrict: 'E', templateUrl: 'views/setting-dialog.html', @@ -30,7 +30,7 @@ var keys = aria2SettingService.getAria2QuickSettingsAvailableOptions(type); if (!keys) { - ariaNgCommonService.showError('Type is illegal!'); + ariaNgLocalizationService.showError('Type is illegal!'); return; } diff --git a/src/scripts/filters/longDate.js b/src/scripts/filters/longDate.js index 7d29d47..322f107 100644 --- a/src/scripts/filters/longDate.js +++ b/src/scripts/filters/longDate.js @@ -1,10 +1,10 @@ (function () { 'use strict'; - angular.module('ariaNg').filter('longDate', ['$translate', 'moment', function ($translate, moment) { + angular.module('ariaNg').filter('longDate', ['ariaNgCommonService', 'ariaNgLocalizationService', function (ariaNgCommonService, ariaNgLocalizationService) { return function (time) { - var format = $translate.instant('format.longdate'); - return moment(time).format(format); + var format = ariaNgLocalizationService.getLongDateFormat(); + return ariaNgCommonService.formatDateTime(time, format); }; }]); }()); diff --git a/src/scripts/services/aria2RpcService.js b/src/scripts/services/aria2RpcService.js index 9a0d9de..b45ba81 100644 --- a/src/scripts/services/aria2RpcService.js +++ b/src/scripts/services/aria2RpcService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgLogService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, aria2RpcErrors, ariaNgCommonService, ariaNgSettingService, ariaNgLogService, aria2HttpRpcService, aria2WebSocketRpcService) { + angular.module('ariaNg').factory('aria2RpcService', ['$q', 'aria2RpcConstants', 'aria2RpcErrors', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', 'aria2HttpRpcService', 'aria2WebSocketRpcService', function ($q, aria2RpcConstants, aria2RpcErrors, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService, aria2HttpRpcService, aria2WebSocketRpcService) { var rpcImplementService = ariaNgSettingService.isCurrentRpcUseWebSocket() ? aria2WebSocketRpcService : aria2HttpRpcService; var isConnected = false; var secret = ariaNgSettingService.getCurrentRpcSecret(); @@ -115,10 +115,10 @@ ariaNgLogService.error('[aria2RpcService.processError] ' + error.message, error); if (aria2RpcErrors[error.message] && aria2RpcErrors[error.message].tipTextKey) { - ariaNgCommonService.showError(aria2RpcErrors[error.message].tipTextKey); + ariaNgLocalizationService.showError(aria2RpcErrors[error.message].tipTextKey); return true; } else { - ariaNgCommonService.showError(error.message); + ariaNgLocalizationService.showError(error.message); return true; } }; diff --git a/src/scripts/services/aria2TaskService.js b/src/scripts/services/aria2TaskService.js index 3f06e21..500491c 100644 --- a/src/scripts/services/aria2TaskService.js +++ b/src/scripts/services/aria2TaskService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('aria2TaskService', ['$q', '$translate', 'bittorrentPeeridService', 'aria2Errors', 'aria2RpcService', 'ariaNgCommonService', 'ariaNgSettingService', 'ariaNgLogService', function ($q, $translate, bittorrentPeeridService, aria2Errors, aria2RpcService, ariaNgCommonService, ariaNgSettingService, ariaNgLogService) { + angular.module('ariaNg').factory('aria2TaskService', ['$q', 'bittorrentPeeridService', 'aria2Errors', 'aria2RpcService', 'ariaNgCommonService', 'ariaNgLocalizationService', 'ariaNgLogService', 'ariaNgSettingService', function ($q, bittorrentPeeridService, aria2Errors, aria2RpcService, ariaNgCommonService, ariaNgLocalizationService, ariaNgLogService, ariaNgSettingService) { var getFileName = function (file) { if (!file) { ariaNgLogService.warn('[aria2TaskService.getFileName] file is null'); @@ -44,7 +44,7 @@ } if (!taskName) { - taskName = $translate.instant('Unknown'); + taskName = ariaNgLocalizationService.getLocalizedText('Unknown'); success = false; } diff --git a/src/scripts/services/ariaNgCommonService.js b/src/scripts/services/ariaNgCommonService.js index 28ac381..1422649 100644 --- a/src/scripts/services/ariaNgCommonService.js +++ b/src/scripts/services/ariaNgCommonService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgCommonService', ['$location', '$timeout', 'base64', 'moment', 'SweetAlert', '$translate', 'ariaNgConstants', function ($location, $timeout, base64, moment, SweetAlert, $translate, ariaNgConstants) { + angular.module('ariaNg').factory('ariaNgCommonService', ['$location', '$timeout', 'base64', 'moment', 'SweetAlert', 'ariaNgConstants', function ($location, $timeout, base64, moment, SweetAlert, ariaNgConstants) { return { base64Encode: function (value) { return base64.encode(value); @@ -18,13 +18,13 @@ return hashedId; }, - showDialog: function (title, text, type, callback) { + showDialog: function (title, text, type, callback, options) { $timeout(function () { SweetAlert.swal({ - title: $translate.instant(title), - text: $translate.instant(text), + title: title, + text: text, type: type, - confirmButtonText: $translate.instant('OK') + confirmButtonText: options && options.confirmButtonText || null }, function () { if (callback) { callback(); @@ -32,22 +32,16 @@ }); }, 100); }, - showError: function (text, callback) { - this.showDialog('Error', text, 'error', callback); - }, - showOperationSucceeded: function (text, callback) { - this.showDialog('Operation Succeeded', text, 'success', callback); - }, confirm: function (title, text, type, callback, notClose, extendSettings) { var options = { - title: $translate.instant(title), - text: $translate.instant(text, (angular.isObject(extendSettings) ? extendSettings.textParams : null)), + title: title, + text: text, type: type, showCancelButton: true, showLoaderOnConfirm: !!notClose, closeOnConfirm: !notClose, - confirmButtonText: $translate.instant('OK'), - cancelButtonText: $translate.instant('Cancel') + confirmButtonText: extendSettings && extendSettings.confirmButtonText || null, + cancelButtonText: extendSettings && extendSettings.cancelButtonText || null }; if (type === 'warning') { @@ -230,6 +224,9 @@ getLongTimeFromUnixTime: function (unixTime) { return moment(unixTime, 'X').format('HH:mm:ss'); }, + formatDateTime: function (datetime, format) { + return moment(datetime).format(format); + }, getTimeOptions: function (timeList, withDisabled) { var options = []; diff --git a/src/scripts/services/ariaNgLocalizationService.js b/src/scripts/services/ariaNgLocalizationService.js new file mode 100644 index 0000000..01c6e20 --- /dev/null +++ b/src/scripts/services/ariaNgLocalizationService.js @@ -0,0 +1,94 @@ +(function () { + 'use strict'; + + angular.module('ariaNg').factory('ariaNgLocalizationService', ['$translate', 'amMoment', 'ariaNgCommonService', 'ariaNgNotificationService', function ($translate, amMoment, ariaNgCommonService, ariaNgNotificationService) { + return { + applyLanguage: function (lang) { + $translate.use(lang); + amMoment.changeLocale(lang); + + return true; + }, + getLocalizedText: function (text, params) { + return $translate.instant(text, params); + }, + getLongDateFormat: function () { + return this.getLocalizedText('format.longdate'); + }, + showDialog: function (title, text, type, callback) { + if (title) { + title = this.getLocalizedText(title); + } + + if (text) { + text = this.getLocalizedText(text); + } + + var options = { + confirmButtonText: this.getLocalizedText('OK') + }; + + ariaNgCommonService.showDialog(title, text, type, callback, options); + }, + showError: function (text, callback) { + this.showDialog('Error', text, 'error', callback); + }, + showOperationSucceeded: function (text, callback) { + this.showDialog('Operation Succeeded', text, 'success', callback); + }, + confirm: function (title, text, type, callback, notClose, extendSettings) { + if (!extendSettings) { + extendSettings = {}; + } + + if (title) { + title = this.getLocalizedText(title); + } + + if (text) { + text = this.getLocalizedText(text, extendSettings.textParams); + } + + extendSettings.confirmButtonText = this.getLocalizedText('OK'); + extendSettings.cancelButtonText = this.getLocalizedText('Cancel'); + + ariaNgCommonService.confirm(title, text, type, callback, notClose, extendSettings); + }, + notifyViaBrowser: function (title, content) { + if (title) { + title = this.getLocalizedText(title); + } + + if (content) { + content = this.getLocalizedText(content); + } + + return ariaNgNotificationService.notifyViaBrowser(title, content); + }, + notifyInPage: function (title, content, options) { + if (!options) { + options = {}; + } + + if (title) { + title = this.getLocalizedText(title, options.titleParams); + } + + if (content) { + content = this.getLocalizedText(content, options.contentParams); + } + + return ariaNgNotificationService.notifyInPage(title, content, options); + }, + notifyTaskComplete: function (task) { + this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : '')); + }, + notifyBtTaskComplete: function (task) { + this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : '')); + }, + notifyTaskError: function (task) { + this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : '')); + } + }; + }]); +}()); diff --git a/src/scripts/services/ariaNgMonitorService.js b/src/scripts/services/ariaNgMonitorService.js index 1dc322d..1dae952 100644 --- a/src/scripts/services/ariaNgMonitorService.js +++ b/src/scripts/services/ariaNgMonitorService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgMonitorService', ['$filter', '$translate', 'ariaNgConstants', 'ariaNgCommonService', function ($filter, $translate, ariaNgConstants, ariaNgCommonService) { + angular.module('ariaNg').factory('ariaNgMonitorService', ['$filter', 'ariaNgConstants', 'ariaNgCommonService', 'ariaNgLocalizationService', function ($filter, ariaNgConstants, ariaNgCommonService, ariaNgLocalizationService) { var currentGlobalStat = {}; var storagesInMemory = {}; var globalStorageKey = 'global'; @@ -29,7 +29,7 @@ show: true, formatter: function (params) { if (params[0].name === '') { - return '
' + $translate.instant('No Data') + '
'; + return '
' + ariaNgLocalizationService.getLocalizedText('No Data') + '
'; } var time = ariaNgCommonService.getLongTimeFromUnixTime(params[0].name); diff --git a/src/scripts/services/ariaNgNotificationService.js b/src/scripts/services/ariaNgNotificationService.js index e8d4b29..487a0f4 100644 --- a/src/scripts/services/ariaNgNotificationService.js +++ b/src/scripts/services/ariaNgNotificationService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', '$translate', 'Notification', 'ariaNgSettingService', function ($notification, $translate, Notification, ariaNgSettingService) { + angular.module('ariaNg').factory('ariaNgNotificationService', ['$notification', 'Notification', 'ariaNgSettingService', function ($notification, Notification, ariaNgSettingService) { var isSupportBrowserNotification = $notification.isSupported; var isPermissionGranted = function (permission) { @@ -39,8 +39,8 @@ }, notifyViaBrowser: function (title, content) { if (isSupportBrowserNotification && ariaNgSettingService.getBrowserNotification()) { - $notification($translate.instant(title), { - body: $translate.instant(content) + $notification(title, { + body: content }); } }, @@ -49,14 +49,6 @@ options = {}; } - if (title) { - title = $translate.instant(title, options.titleParams); - } - - if (content) { - content = $translate.instant(content, options.contentParams); - } - if (!content) { options.message = title; } else { @@ -70,15 +62,6 @@ return Notification[options.type](options); }, - notifyTaskComplete: function (task) { - this.notifyViaBrowser('Download Completed', (task && task.taskName ? task.taskName : '')); - }, - notifyBtTaskComplete: function (task) { - this.notifyViaBrowser('BT Download Completed', (task && task.taskName ? task.taskName : '')); - }, - notifyTaskError: function (task) { - this.notifyViaBrowser('Download Error', (task && task.taskName ? task.taskName : '')); - }, clearNotificationInPage: function () { Notification.clearAll(); } diff --git a/src/scripts/services/ariaNgSettingService.js b/src/scripts/services/ariaNgSettingService.js index a6a9642..c971037 100644 --- a/src/scripts/services/ariaNgSettingService.js +++ b/src/scripts/services/ariaNgSettingService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgSettingService', ['$window', '$location', '$filter', '$translate', 'amMoment', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgLogService', function ($window, $location, $filter, $translate, amMoment, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages, ariaNgCommonService, ariaNgLogService) { + angular.module('ariaNg').factory('ariaNgSettingService', ['$window', '$location', '$filter', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgLogService', function ($window, $location, $filter, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages, ariaNgCommonService, ariaNgLogService) { var browserFeatures = (function () { var supportLocalStroage = localStorageService.isSupported; var supportCookies = $window.navigator.cookieEnabled; @@ -247,23 +247,16 @@ isInsecureProtocolDisabled: function () { return isInsecureProtocolDisabled(); }, - applyLanguage: function (lang) { - if (!ariaNgLanguages[lang]) { - return false; - } - - $translate.use(lang); - amMoment.changeLocale(lang); - - return true; - }, getLanguage: function () { return getOption('language'); }, setLanguage: function (value) { - if (this.applyLanguage(value)) { - setOption('language', value); + if (!ariaNgLanguages[value]) { + return false; } + + setOption('language', value); + return true; }, isEnableDebugMode: function () { return sessionSettings.debugMode; diff --git a/src/scripts/services/ariaNgTitleService.js b/src/scripts/services/ariaNgTitleService.js index ced7fb3..f9456b9 100644 --- a/src/scripts/services/ariaNgTitleService.js +++ b/src/scripts/services/ariaNgTitleService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgTitleService', ['$filter', '$translate', 'ariaNgConstants', 'ariaNgSettingService', function ($filter, $translate, ariaNgConstants, ariaNgSettingService) { + angular.module('ariaNg').factory('ariaNgTitleService', ['$filter', 'ariaNgConstants', 'ariaNgLocalizationService', 'ariaNgSettingService', function ($filter, ariaNgConstants, ariaNgLocalizationService, ariaNgSettingService) { var parseSettings = function (placeholder) { if (!placeholder) { return {}; @@ -63,28 +63,28 @@ var replaceDownloadingCount = function (title, value) { return replacePlaceholders(title, 'downloading', { - prefix: $translate.instant('Downloading') + ': ', + prefix: ariaNgLocalizationService.getLocalizedText('Downloading') + ': ', value: value }); }; var replaceWaitingCount = function (title, value) { return replacePlaceholders(title, 'waiting', { - prefix: $translate.instant('Waiting') + ': ', + prefix: ariaNgLocalizationService.getLocalizedText('Waiting') + ': ', value: value }); }; var replaceStoppedCount = function (title, value) { return replacePlaceholders(title, 'stopped', { - prefix: $translate.instant('Finished / Stopped') + ': ', + prefix: ariaNgLocalizationService.getLocalizedText('Finished / Stopped') + ': ', value: value }); }; var replaceDownloadSpeed = function (title, value) { return replacePlaceholders(title, 'downspeed', { - prefix: $translate.instant('Download') + ': ', + prefix: ariaNgLocalizationService.getLocalizedText('Download') + ': ', value: value, type: 'volume', suffix: '/s' @@ -93,7 +93,7 @@ var replaceUploadSpeed = function (title, value) { return replacePlaceholders(title, 'upspeed', { - prefix: $translate.instant('Upload') + ': ', + prefix: ariaNgLocalizationService.getLocalizedText('Upload') + ': ', value: value, type: 'volume', suffix: '/s'