UNPKG

systelab-preferences

Version:
313 lines (303 loc) 11.5 kB
import * as i0 from '@angular/core'; import { Injectable, NgModule } from '@angular/core'; class StorageService { constructor(storage) { this.storage = storage; this.prefix = 'Systelab'; } usePrefix(prefix) { this.prefix = prefix; } getPrefix() { return this.prefix; } clear() { this.storage.clear(); } put(key, value) { this.storage.setItem(this.prefix + '.' + key, JSON.stringify(value)); } get(key, defaultValue) { const value = this.parse(this.storage.getItem(this.prefix + '.' + key) || 'null') || null; return value ? value : defaultValue; } remove(key) { this.storage.removeItem(this.prefix + '.' + key); } removeStartsWith(startWith) { for (let i = this.storage.length - 1; i >= 0; i--) { if (this.storage.key(i).startsWith(this.prefix + '.' + startWith)) { this.storage.removeItem(this.storage.key(i)); } } } removeEndsWith(endsWith) { for (let i = this.storage.length - 1; i >= 0; i--) { if (this.storage.key(i).endsWith(endsWith)) { this.storage.removeItem(this.storage.key(i)); } } } parse(text) { try { return JSON.parse(text) || null; } catch (e) { return text; } } } class LocalStorageService extends StorageService { constructor() { super(localStorage); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: LocalStorageService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: LocalStorageService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: LocalStorageService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [] }); const compressor = pako; class MemoryStorageService { constructor() { this.preferences = new Map(); } clear() { this.preferences.clear(); } put(key, value) { this.preferences.set(key, value); } get(key, defaultValue) { const value = this.preferences.get(key); if (value === undefined || value === null) { if (defaultValue === undefined || defaultValue === null) { return value; } else { return defaultValue; } } else { return value; } } remove(key) { this.preferences.delete(key); } removeStartsWith(startWith) { this.preferences.forEach((value, key) => { if (key.startsWith(startWith)) { this.remove(key); } }); } removeEndsWith(endsWith) { this.preferences.forEach((value, key) => { if (key.endsWith(endsWith)) { this.remove(key); } }); } getInCompressFormat() { const compressed = compressor.deflate(this.getInStringFormat()); // Uint8Array const binaryString = String.fromCharCode(...compressed); return btoa(binaryString); } getInStringFormat() { const passToServer = {}; this.preferences.forEach((value, key) => passToServer[key] = value); return JSON.stringify(passToServer); } putFromCompressFormat(compressed) { this.clear(); // Do your best try { const binaryString = atob(compressed); const byteArray = Uint8Array.from(binaryString, c => c.charCodeAt(0)); const result = compressor.inflate(byteArray, { to: 'string' }); const parsed = JSON.parse(result); Object.keys(parsed).forEach(key => this.preferences.set(key, parsed[key])); } catch (ex) { } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: MemoryStorageService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: MemoryStorageService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: MemoryStorageService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }] }); class SessionStorageService extends StorageService { constructor() { super(sessionStorage); } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SessionStorageService, deps: [], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SessionStorageService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SessionStorageService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [] }); var StorageType; (function (StorageType) { StorageType[StorageType["IN_MEMORY_STORAGE"] = 1] = "IN_MEMORY_STORAGE"; StorageType[StorageType["LOCAL_STORAGE"] = 2] = "LOCAL_STORAGE"; StorageType[StorageType["SESSION_STORAGE"] = 3] = "SESSION_STORAGE"; })(StorageType || (StorageType = {})); class PreferencesService { constructor(memoryService, localService, sessionService) { this.memoryService = memoryService; this.localService = localService; this.sessionService = sessionService; this.storage = StorageType.IN_MEMORY_STORAGE; } setStorage(storage) { this.storage = storage; } getStorage() { return this.storage; } usePrefix(prefix) { if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.usePrefix(prefix); } else if (this.storage === StorageType.SESSION_STORAGE) { this.sessionService.usePrefix(prefix); } else { console.error('You can not set a prefix for In-memory preferences'); } } getPrefix() { if (this.storage === StorageType.LOCAL_STORAGE) { return this.localService.getPrefix(); } else if (this.storage === StorageType.SESSION_STORAGE) { return this.sessionService.getPrefix(); } else { return undefined; } } clear() { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.clear(); } else if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.clear(); } else { this.sessionService.clear(); } } put(key, value) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.put(key, value); } else if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.put(key, value); } else { this.sessionService.put(key, value); } } get(key, defaultValue) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { return this.memoryService.get(key, defaultValue); } else if (this.storage === StorageType.LOCAL_STORAGE) { return this.localService.get(key, defaultValue); } else { return this.sessionService.get(key, defaultValue); } } remove(key) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.remove(key); } else if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.remove(key); } else { this.sessionService.remove(key); } } removeStartsWith(startWith) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.removeStartsWith(startWith); } else if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.removeStartsWith(startWith); } else { this.sessionService.removeStartsWith(startWith); } } removeEndsWith(endsWith) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.removeEndsWith(endsWith); } else if (this.storage === StorageType.LOCAL_STORAGE) { this.localService.removeEndsWith(endsWith); } else { this.sessionService.removeEndsWith(endsWith); } } getInStringFormat() { if (this.storage === StorageType.IN_MEMORY_STORAGE) { return this.memoryService.getInStringFormat(); } else { return ''; } } getInCompressFormat() { if (this.storage === StorageType.IN_MEMORY_STORAGE) { return this.memoryService.getInCompressFormat(); } else { return ''; } } putFromCompressFormat(compressed) { if (this.storage === StorageType.IN_MEMORY_STORAGE) { this.memoryService.putFromCompressFormat(compressed); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: PreferencesService, deps: [{ token: MemoryStorageService }, { token: LocalStorageService }, { token: SessionStorageService }], target: i0.ɵɵFactoryTarget.Injectable }); } static { this.ɵprov = i0.ɵɵngDeclareInjectable({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: PreferencesService, providedIn: 'root' }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: PreferencesService, decorators: [{ type: Injectable, args: [{ providedIn: 'root' }] }], ctorParameters: () => [{ type: MemoryStorageService }, { type: LocalStorageService }, { type: SessionStorageService }] }); class SystelabPreferencesModule { static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SystelabPreferencesModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule }); } static { this.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "14.0.0", version: "20.1.1", ngImport: i0, type: SystelabPreferencesModule }); } static { this.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SystelabPreferencesModule }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.1", ngImport: i0, type: SystelabPreferencesModule, decorators: [{ type: NgModule, args: [{}] }] }); /* * Public API Surface of systelab-preferences */ /** * Generated bundle index. Do not edit. */ export { LocalStorageService, MemoryStorageService, PreferencesService, SessionStorageService, StorageType, SystelabPreferencesModule }; //# sourceMappingURL=systelab-preferences.mjs.map