import Base64 from '../base64'; import { getCurrentLinkableLocation, log } from '../utils/helpers'; import { Project } from './project'; export class StorageController { appValues: any = {}; globals: any = {}; currentProject: Project; getProjectName(projectName = null) { let result = this.currentProject !== null && this.currentProject !== undefined && projectName !== false ? this.currentProject.name : projectName; if (result === false) return null; return result; } set(key, value, projectName = null) { if (key.toLowerCase() === 'globals') throw new Error('cannot set globals'); projectName = this.getProjectName(projectName); if (projectName !== null) { if (this.appValues[projectName] === undefined) this.appValues[projectName] = {}; this.appValues[projectName][key] = value; return value; } this.appValues[key] = value; return value; } wipeProjectValues(storageKey, projectName = null, deleteIt = true) { projectName = this.getProjectName(projectName); if (projectName !== null) { if ( this.appValues[projectName] !== undefined && this.appValues[projectName][storageKey] !== undefined ) deleteIt ? delete this.appValues[projectName][storageKey] : (this.appValues[projectName][storageKey] = {}); } if (this.appValues[storageKey] !== undefined) deleteIt ? delete this.appValues[storageKey] : (this.appValues[storageKey] = {}); } wipe() { this.appValues = {}; this.globals = {}; localStorage.clear(); } hasProjectValue(storageKey, key, projectName = null) { projectName = this.getProjectName(projectName); if (projectName !== null) return ( this.appValues[projectName] !== undefined && this.appValues[projectName][storageKey] !== undefined && this.appValues[projectName][storageKey][key] !== undefined ); return ( this.appValues[storageKey] !== undefined && this.appValues[storageKey][key] !== undefined ); } setProjectValues(storageKey, key, value, projectName = null) { key = key.toString(); if (key.toLowerCase() === 'globals') throw new Error('cannot set globals'); projectName = this.getProjectName(projectName); if (projectName !== null) { if (this.appValues[projectName] === undefined) this.appValues[projectName] = {}; if (this.appValues[projectName][storageKey] === undefined) this.appValues[projectName][storageKey] = {}; this.appValues[projectName][storageKey][key] = value; return value; } if (this.appValues[storageKey] === undefined) this.appValues[storageKey] = {}; this.appValues[storageKey][key] = value; return value; } /** * @param {Project} project */ setCurrentProject(project: Project) { this.currentProject = project; } /** * * @param {*} key * @param {*} projectName * @returns */ exists(key: any, projectName: any = null) { projectName = this.getProjectName(projectName); if (projectName !== null) return ( this.appValues[projectName] !== undefined && this.appValues[projectName][key] !== undefined ); return this.appValues[key] !== undefined; } /** * * @param {string} key * @param {string} projectName * @returns */ get(key: string, projectName: string = null) { projectName = this.getProjectName(projectName); if (projectName !== null) return this.appValues[projectName][key]; return this.appValues[key]; } load() { log('loading storage'); let keys = localStorage.getItem('_keys'); if (keys === undefined || keys === null) return; Object.values(JSON.parse(keys)).forEach((key: any) => { let item = localStorage.getItem(key); key = Base64.decode(key); if (item !== undefined && item !== null) this.appValues[key] = item; if ( typeof this.appValues[key] === 'string' && isNaN(this.appValues[key]) ) { let trimmed = this.appValues[key].trim(); try { this.appValues[key] = JSON.parse(trimmed); } catch (error) { log( 'could not parse what ever was in storage item slot ' + key ); this.appValues[key] = trimmed; } } else if ( typeof this.appValues[key] === 'number' || isNaN(this.appValues[key]) === false ) { this.appValues[key] = parseInt(this.appValues[key]); } }); } getPagePreference(key, pageId = null) { pageId = pageId || getCurrentLinkableLocation(); return this.appValues[pageId] === undefined ? undefined : this.appValues[pageId][key]; } setPagePerference(key, value, pageId) { pageId = pageId || getCurrentLinkableLocation(); if (this.appValues[pageId] === undefined) this.appValues[pageId] = {}; this.appValues[pageId][key] = value; } getGemPeference(key, modName) { if (typeof modName !== 'string') throw new Error('mod name must be just the name and a string'); modName = modName.toString(); if (this.appValues['gems'] === undefined) return undefined; return this.appValues['gems'][modName] === undefined ? undefined : this.appValues['gems'][modName][key]; } setGemPreference(key, value, modName) { modName = modName.toLowerCase(); if (this.appValues['gems'] === undefined) { this.appValues['gems'] = { [modName]: { [key]: value, }, }; return this.appValues['gems'][modName][key]; } if (this.appValues['gems'][modName] === undefined) this.appValues['gems'][modName] = {}; this.appValues['gems'][modName][key] = value; return value; } setGlobalPreference(key, value) { if (this.appValues['globals'] === undefined) this.appValues['globals'] = {}; this.appValues['globals'][key] = value; } getGlobalPreference(key) { return this.appValues['globals'] === undefined ? undefined : this.appValues['globals'][key]; } /** * Warning: Packs floating point numbers */ save() { let keys = []; for (let [index, value] of Object.entries(this.appValues)) { if (typeof value === 'object') value = JSON.stringify(value); if (isNaN(value as any) === false) value = parseInt(value as any); index = Base64.encode(index); localStorage.setItem(index, value as any); keys.push(index); } localStorage.setItem('_keys', JSON.stringify(keys)); } } const storageController = new StorageController(); export default storageController;