2018-02-10 15:18:01 +00:00
|
|
|
import ngModule from '../module';
|
2017-11-15 06:12:52 +00:00
|
|
|
|
|
|
|
class VnStorage {
|
|
|
|
constructor() {
|
|
|
|
this._type = '';
|
|
|
|
this.prefix = 'vn';
|
|
|
|
}
|
|
|
|
set type(value) {
|
|
|
|
this._type = value;
|
|
|
|
this.checkSupport();
|
|
|
|
}
|
|
|
|
get type() {
|
|
|
|
return this._type;
|
|
|
|
}
|
|
|
|
get webStorage() {
|
|
|
|
return window[this.type];
|
|
|
|
}
|
|
|
|
checkSupport() {
|
|
|
|
try {
|
|
|
|
let supported = (this.type in window && window[this.type] !== null);
|
|
|
|
if (supported) {
|
|
|
|
let key = '__' + Math.round(Math.random() * 1e7);
|
|
|
|
let webStorage = window[this.type];
|
|
|
|
webStorage.setItem(key, '');
|
|
|
|
webStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.error('VnStorage.notification.error', e.message);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
get(param) {
|
|
|
|
let toRead = this.webStorage.getItem(`${this.prefix}.${param}`);
|
|
|
|
if (toRead && toRead.startsWith('jsonObject:')) {
|
|
|
|
toRead = JSON.parse(toRead.replace('jsonObject:', ''));
|
|
|
|
}
|
|
|
|
return toRead;
|
|
|
|
}
|
|
|
|
set(param, data) {
|
|
|
|
let toStorage = typeof data === 'object' ? `jsonObject:${JSON.stringify(data)}` : data;
|
|
|
|
this.webStorage.setItem(`${this.prefix}.${param}`, toStorage);
|
|
|
|
}
|
|
|
|
remove(param) {
|
|
|
|
this.webStorage.removeItem(`${this.prefix}.${param}`);
|
|
|
|
}
|
|
|
|
clear() {
|
|
|
|
this.webStorage.clear();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class SessionStorage extends VnStorage {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.type = 'sessionStorage';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class LocalStorage extends VnStorage {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.type = 'localStorage';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-10 15:18:01 +00:00
|
|
|
ngModule.service('sessionStorage', SessionStorage);
|
|
|
|
ngModule.service('localStorage', LocalStorage);
|