From 12d41491047c5dd7664c96ed2b2981e0060100db Mon Sep 17 00:00:00 2001 From: MaysWind Date: Sun, 12 Aug 2018 20:47:48 +0800 Subject: [PATCH] code refactor --- src/index.html | 13 +++--- src/scripts/services/aria2SettingService.js | 18 +++----- src/scripts/services/ariaNgLanguageLoader.js | 6 +-- src/scripts/services/ariaNgSettingService.js | 12 +++--- src/scripts/services/ariaNgStorageService.js | 43 ++++++++++++++++++++ 5 files changed, 65 insertions(+), 27 deletions(-) create mode 100644 src/scripts/services/ariaNgStorageService.js diff --git a/src/index.html b/src/index.html index 6b9bd43..8747e02 100644 --- a/src/index.html +++ b/src/index.html @@ -386,15 +386,16 @@ - - - + - - - + + + + + + diff --git a/src/scripts/services/aria2SettingService.js b/src/scripts/services/aria2SettingService.js index 4976246..5f2eecf 100644 --- a/src/scripts/services/aria2SettingService.js +++ b/src/scripts/services/aria2SettingService.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('aria2SettingService', ['localStorageService', 'ariaNgConstants', 'aria2AllOptions', 'aria2GlobalAvailableOptions', 'aria2QuickSettingsAvailableOptions', 'aria2TaskAvailableOptions', 'aria2RpcService', 'ariaNgLogService', function (localStorageService, ariaNgConstants, aria2AllOptions, aria2GlobalAvailableOptions, aria2QuickSettingsAvailableOptions, aria2TaskAvailableOptions, aria2RpcService, ariaNgLogService) { + angular.module('ariaNg').factory('aria2SettingService', ['ariaNgConstants', 'aria2AllOptions', 'aria2GlobalAvailableOptions', 'aria2QuickSettingsAvailableOptions', 'aria2TaskAvailableOptions', 'aria2RpcService', 'ariaNgLogService', 'ariaNgStorageService', function (ariaNgConstants, aria2AllOptions, aria2GlobalAvailableOptions, aria2QuickSettingsAvailableOptions, aria2TaskAvailableOptions, aria2RpcService, ariaNgLogService, ariaNgStorageService) { var processStatResult = function (stat) { if (!stat) { return stat; @@ -20,10 +20,6 @@ return ariaNgConstants.settingHistoryKeyPrefix + '.' + key; }; - var isSettingHistoryKey = function (key) { - return key.indexOf(ariaNgConstants.settingHistoryKeyPrefix + '.') === 0; - }; - return { isOptionKeyValid: function (key) { var option = aria2AllOptions[key]; @@ -191,7 +187,7 @@ } var storageKey = getSettingHistoryKey(key); - var history = localStorageService.get(storageKey) || []; + var history = ariaNgStorageService.get(storageKey) || []; var newHistory = []; for (var i = 0; i < Math.min(history.length, ariaNgConstants.historyMaxStoreCount); i++) { @@ -206,7 +202,7 @@ } var storageKey = getSettingHistoryKey(key); - var history = localStorageService.get(storageKey) || []; + var history = ariaNgStorageService.get(storageKey) || []; var newHistory = []; newHistory.push(value); @@ -216,17 +212,15 @@ } } - localStorageService.set(storageKey, newHistory); + ariaNgStorageService.set(storageKey, newHistory); return newHistory; }, clearSettingsHistorys: function () { - var keys = localStorageService.keys(); + var keys = ariaNgStorageService.keys(ariaNgConstants.settingHistoryKeyPrefix + '.'); for (var i = 0; i < keys.length; i++) { - if (isSettingHistoryKey(keys[i])) { - localStorageService.remove(keys[i]); - } + ariaNgStorageService.remove(keys[i]); } }, getGlobalOption: function (callback, silent) { diff --git a/src/scripts/services/ariaNgLanguageLoader.js b/src/scripts/services/ariaNgLanguageLoader.js index 707e52e..4fe90e3 100644 --- a/src/scripts/services/ariaNgLanguageLoader.js +++ b/src/scripts/services/ariaNgLanguageLoader.js @@ -1,7 +1,7 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'localStorageService', 'ariaNgConstants', 'ariaNgLanguages', 'ariaNgNotificationService', 'ariaNgLogService', function ($http, $q, localStorageService, ariaNgConstants, ariaNgLanguages, ariaNgNotificationService, ariaNgLogService) { + angular.module('ariaNg').factory('ariaNgLanguageLoader', ['$http', '$q', 'ariaNgConstants', 'ariaNgLanguages', 'ariaNgNotificationService', 'ariaNgLogService', 'ariaNgStorageService', function ($http, $q, ariaNgConstants, ariaNgLanguages, ariaNgNotificationService, ariaNgLogService, ariaNgStorageService) { var getKeyValuePair = function (line) { for (var i = 0; i < line.length; i++) { if (i > 0 && line.charAt(i - 1) !== '\\' && line.charAt(i) === '=') { @@ -90,7 +90,7 @@ } var languageKey = ariaNgConstants.languageStorageKeyPrefix + '.' + options.key; - var languageResource = localStorageService.get(languageKey); + var languageResource = ariaNgStorageService.get(languageKey); if (languageResource) { deferred.resolve(languageResource); @@ -103,7 +103,7 @@ method: 'GET' }).then(function onSuccess(response) { var languageObject = getLanguageObject(response.data); - localStorageService.set(languageKey, languageObject); + ariaNgStorageService.set(languageKey, languageObject); return deferred.resolve(languageObject); }).catch(function onError(response) { ariaNgLogService.warn('[ariaNgLanguageLoader] cannot get language resource'); diff --git a/src/scripts/services/ariaNgSettingService.js b/src/scripts/services/ariaNgSettingService.js index c971037..44c77f7 100644 --- a/src/scripts/services/ariaNgSettingService.js +++ b/src/scripts/services/ariaNgSettingService.js @@ -1,10 +1,10 @@ (function () { 'use strict'; - angular.module('ariaNg').factory('ariaNgSettingService', ['$window', '$location', '$filter', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgLogService', function ($window, $location, $filter, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages, ariaNgCommonService, ariaNgLogService) { + angular.module('ariaNg').factory('ariaNgSettingService', ['$window', '$location', '$filter', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', 'ariaNgCommonService', 'ariaNgLogService', 'ariaNgStorageService', function ($window, $location, $filter, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages, ariaNgCommonService, ariaNgLogService, ariaNgStorageService) { var browserFeatures = (function () { - var supportLocalStroage = localStorageService.isSupported; - var supportCookies = $window.navigator.cookieEnabled; + var supportLocalStroage = ariaNgStorageService.isLocalStorageSupported(); + var supportCookies = ariaNgStorageService.isCookiesSupported(); return { localStroage: supportLocalStroage, @@ -110,11 +110,11 @@ }; var setOptions = function (options) { - return localStorageService.set(ariaNgConstants.optionStorageKey, options); + return ariaNgStorageService.set(ariaNgConstants.optionStorageKey, options); }; var getOptions = function () { - var options = localStorageService.get(ariaNgConstants.optionStorageKey); + var options = ariaNgStorageService.get(ariaNgConstants.optionStorageKey); if (options && !ariaNgLanguages[options.language]) { options.language = getLanguageNameFromAliasOrDefaultLanguage(options.language); @@ -146,7 +146,7 @@ }; var clearAll = function () { - return localStorageService.clearAll(); + return ariaNgStorageService.clearAll(); }; var getOption = function (key) { diff --git a/src/scripts/services/ariaNgStorageService.js b/src/scripts/services/ariaNgStorageService.js new file mode 100644 index 0000000..b74a3aa --- /dev/null +++ b/src/scripts/services/ariaNgStorageService.js @@ -0,0 +1,43 @@ +(function () { + 'use strict'; + + angular.module('ariaNg').factory('ariaNgStorageService', ['$window', 'localStorageService', function ($window, localStorageService) { + return { + isLocalStorageSupported: function () { + return localStorageService.isSupported; + }, + isCookiesSupported: function () { + return localStorageService.cookie.isSupported; + }, + get: function (key) { + return localStorageService.get(key); + }, + set: function (key, value) { + return localStorageService.set(key, value); + }, + remove: function (key) { + return localStorageService.remove(key); + }, + clearAll: function () { + return localStorageService.clearAll(); + }, + keys: function (prefix) { + var allKeys = localStorageService.keys(); + + if (!allKeys || !allKeys.length || !prefix) { + return allKeys; + } + + var result = []; + + for (var i = 0; i < allKeys.length; i++) { + if (allKeys[i].indexOf(prefix) >= 0) { + result.push(allKeys[i]); + } + } + + return result; + } + }; + }]); +}());