salix/front/core/services/config.js

80 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2019-10-09 22:47:29 +00:00
import ngModule from '../module';
/**
* Saves and loads the application configuration.
*/
export default class Config {
constructor($http, vnApp, $translate, $window) {
Object.assign(this, {
$http,
vnApp,
$translate,
2019-10-11 15:38:04 +00:00
storage: $window.localStorage,
2019-10-09 22:47:29 +00:00
user: {},
2019-10-11 15:38:04 +00:00
local: {}
2019-10-09 22:47:29 +00:00
});
this.params = [
'warehouseFk',
'companyFk',
'bankFk'
];
}
initialize() {
for (let param of this.params)
this.local[param] = this.getItem(param);
return this.$http.get('UserConfigs/getUserConfig')
2019-10-09 22:47:29 +00:00
.then(res => {
for (let param of this.params)
this.user[param] = res.data[param];
2019-10-11 15:38:04 +00:00
})
.finally(() => this.mergeParams());
2019-10-09 22:47:29 +00:00
}
mergeParams() {
for (let param of this.params) {
let local = this.local[param];
this[param] = local != null && local != ''
? local
: this.user[param];
}
}
setUser(param, value) {
this.user[param] = value;
this.mergeParams();
let params = {[param]: value};
return this.$http.post('UserConfigs/setUserConfig', params)
2019-10-11 15:38:04 +00:00
.then(() => this.showSaved());
2019-10-09 22:47:29 +00:00
}
setLocal(param, value) {
this.setItem(param, value);
this.local[param] = value;
this.mergeParams();
2019-10-11 15:38:04 +00:00
this.showSaved();
}
showSaved() {
this.vnApp.showSuccess(this.$translate.instant('Data saved!'));
2019-10-09 22:47:29 +00:00
}
getItem(key) {
let value = this.storage[key];
return value !== undefined ? JSON.parse(value) : value;
}
setItem(key, value) {
if (value == null)
this.storage.removeItem(key);
else
this.storage[key] = JSON.stringify(value);
}
}
Config.$inject = ['$http', 'vnApp', '$translate', '$window'];
ngModule.service('vnConfig', Config);