import ngModule from '../module'; 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'; } } ngModule.service('sessionStorage', SessionStorage); ngModule.service('localStorage', LocalStorage);