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/app/scripts/services/ariaNgSettingService.js

147 lines
4.8 KiB
JavaScript
Raw Normal View History

2016-05-13 18:09:12 +02:00
(function () {
'use strict';
2016-06-04 09:33:40 +02:00
angular.module('ariaNg').factory('ariaNgSettingService', ['$location', '$base64', '$translate', 'amMoment', 'localStorageService', 'ariaNgConstants', 'ariaNgDefaultOptions', 'ariaNgLanguages', function ($location, $base64, $translate, amMoment, localStorageService, ariaNgConstants, ariaNgDefaultOptions, ariaNgLanguages) {
2016-05-16 18:59:27 +02:00
var getDefaultRpcHost = function () {
return $location.$$host;
};
2016-05-13 18:09:12 +02:00
var setOptions = function (options) {
2016-05-16 18:59:27 +02:00
return localStorageService.set(ariaNgConstants.optionStorageKey, options);
2016-05-13 18:09:12 +02:00
};
var getOptions = function () {
2016-05-16 18:59:27 +02:00
var options = localStorageService.get(ariaNgConstants.optionStorageKey);
2016-05-13 18:09:12 +02:00
if (!options) {
options = angular.extend({}, ariaNgDefaultOptions);
setOptions(options);
}
return options;
};
var getOption = function (key) {
var options = getOptions();
if (angular.isUndefined(options[key])) {
options[key] = ariaNgDefaultOptions[key];
setOptions(options);
}
return options[key];
2016-05-13 18:09:12 +02:00
};
var setOption = function (key, value) {
var options = getOptions();
options[key] = value;
setOptions(options);
};
return {
getAllOptions: function () {
var options = angular.extend({}, ariaNgDefaultOptions, getOptions());
2016-05-16 18:59:27 +02:00
if (!options.rpcHost) {
options.rpcHost = getDefaultRpcHost();
}
2016-06-04 09:33:40 +02:00
if (options.secret) {
options.secret = $base64.decode(options.secret);
}
return options;
2016-05-13 18:09:12 +02:00
},
2016-05-26 17:52:34 +02:00
applyLanguage: function (lang) {
if (!ariaNgLanguages[lang]) {
return false;
}
$translate.use(lang);
amMoment.changeLocale(lang);
2016-05-31 16:51:12 +02:00
2016-05-26 17:52:34 +02:00
return true;
},
getLanguage: function () {
return getOption('language');
},
setLanguage: function (value) {
2016-05-26 17:52:34 +02:00
if (this.applyLanguage(value)) {
setOption('language', value);
}
2016-05-13 18:09:12 +02:00
},
2016-05-16 18:59:27 +02:00
getJsonRpcUrl: function () {
var protocol = getOption('protocol');
var rpcHost = getOption('rpcHost');
var rpcPort = getOption('rpcPort');
2016-05-13 18:09:12 +02:00
if (!rpcHost) {
2016-05-16 18:59:27 +02:00
rpcHost = getDefaultRpcHost();
2016-05-13 18:09:12 +02:00
}
2016-05-16 18:59:27 +02:00
return protocol + '://' + rpcHost + ':' + rpcPort + '/jsonrpc';
},
2016-05-16 18:59:27 +02:00
setRpcHost: function (value) {
setOption('rpcHost', value);
},
setRpcPort: function (value) {
2016-05-22 05:23:08 +02:00
setOption('rpcPort', Math.max(parseInt(value), 0));
},
getProtocol: function () {
return getOption('protocol');
},
setProtocol: function (value) {
2016-05-22 05:23:08 +02:00
setOption('protocol', value);
2016-05-13 18:09:12 +02:00
},
2016-06-04 09:33:40 +02:00
getSecret: function () {
var value = getOption('secret');
return (value ? $base64.decode(value) : value);
},
setSecret: function (value) {
if (value) {
value = $base64.encode(value);
}
setOption('secret', value);
},
2016-05-13 18:09:12 +02:00
getGlobalStatRefreshInterval: function () {
return getOption('globalStatRefreshInterval');
},
2016-05-17 16:15:28 +02:00
setGlobalStatRefreshInterval: function (value) {
setOption('globalStatRefreshInterval', Math.max(parseInt(value), 0));
},
2016-05-13 18:09:12 +02:00
getDownloadTaskRefreshInterval: function () {
return getOption('downloadTaskRefreshInterval');
},
2016-05-17 16:15:28 +02:00
setDownloadTaskRefreshInterval: function (value) {
setOption('downloadTaskRefreshInterval', Math.max(parseInt(value), 0));
},
2016-05-13 18:09:12 +02:00
getDisplayOrder: function () {
var value = getOption('displayOrder');
2016-05-13 18:09:12 +02:00
if (!value) {
2016-05-25 18:09:00 +02:00
value = 'default:asc';
2016-05-13 18:09:12 +02:00
}
2016-05-13 18:09:12 +02:00
return value;
},
setDisplayOrder: function (value) {
setOption('displayOrder', value);
},
getFileListDisplayOrder: function () {
var value = getOption('fileListDisplayOrder');
if (!value) {
2016-05-25 18:09:00 +02:00
value = 'default:asc';
}
return value;
},
setFileListDisplayOrder: function (value) {
setOption('fileListDisplayOrder', value);
2016-05-13 18:09:12 +02:00
}
};
}]);
})();