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 150d192 commit 12d4149
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 26 deletions.
11 changes: 6 additions & 5 deletions src/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,16 @@
<script src="scripts/filters/taskOrderBy.js"></script>
<script src="scripts/filters/taskStatus.js"></script>
<script src="scripts/filters/volume.js"></script>
<script src="scripts/services/ariaNgCommonService.js"></script>
<script src="scripts/services/ariaNgFileService.js"></script>
<script src="scripts/services/ariaNgLanguageLoader.js"></script>
<script src="scripts/services/ariaNgMonitorService.js"></script>
<script src="scripts/services/ariaNgCommonService.js"></script>
<script src="scripts/services/ariaNgNotificationService.js"></script>
<script src="scripts/services/ariaNgLocalizationService.js"></script>
<script src="scripts/services/ariaNgLogService.js"></script>
<script src="scripts/services/ariaNgStorageService.js"></script>
<script src="scripts/services/ariaNgFileService.js"></script>
<script src="scripts/services/ariaNgSettingService.js"></script>
<script src="scripts/services/ariaNgMonitorService.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
18 changes: 6 additions & 12 deletions src/scripts/services/aria2SettingService.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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];
Expand Down Expand Up @@ -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++) {
Expand All @@ -206,7 +202,7 @@
}

var storageKey = getSettingHistoryKey(key);
var history = localStorageService.get(storageKey) || [];
var history = ariaNgStorageService.get(storageKey) || [];
var newHistory = [];
newHistory.push(value);

Expand All @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions src/scripts/services/ariaNgLanguageLoader.js
Original file line number Diff line number Diff line change
@@ -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) === '=') {
Expand Down Expand Up @@ -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);
Expand All @@ -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');
Expand Down
12 changes: 6 additions & 6 deletions src/scripts/services/ariaNgSettingService.js
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -146,7 +146,7 @@
};

var clearAll = function () {
return localStorageService.clearAll();
return ariaNgStorageService.clearAll();
};

var getOption = function (key) {
Expand Down
43 changes: 43 additions & 0 deletions src/scripts/services/ariaNgStorageService.js
Original file line number Diff line number Diff line change
@@ -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;
}
};
}]);
}());

0 comments on commit 12d4149

Please sign in to comment.