frequi_origin/src/shared/storage.ts

32 lines
1.1 KiB
TypeScript
Raw Normal View History

2020-08-24 18:05:07 +00:00
import { PlotConfig, EMPTY_PLOTCONFIG, PlotConfigStorage } from '@/types';
2020-06-30 19:00:29 +00:00
const PLOT_CONFIG = 'ft_custom_plot_config';
2020-08-24 18:05:07 +00:00
const PLOT_CONFIG_NAME = 'ft_selected_plot_config';
2020-06-30 19:00:29 +00:00
2020-08-24 18:05:07 +00:00
export function loadPlotConfigName(): string {
2020-08-08 13:25:58 +00:00
return localStorage.getItem(PLOT_CONFIG_NAME) || 'default';
2020-06-30 19:00:29 +00:00
}
2020-08-24 18:05:07 +00:00
export function storePlotConfigName(plotConfigName: string): void {
localStorage.setItem(PLOT_CONFIG_NAME, plotConfigName);
}
export function loadAllCustomPlotConfig(): PlotConfig {
2020-08-08 13:22:52 +00:00
return JSON.parse(localStorage.getItem(PLOT_CONFIG) || '{}');
2020-06-30 19:00:29 +00:00
}
2020-08-24 18:05:07 +00:00
export function loadCustomPlotConfig(configName: string): PlotConfig {
const configs = loadAllCustomPlotConfig();
return configName in configs ? configs[configName] : { ...EMPTY_PLOTCONFIG };
}
export function storeCustomPlotConfig(plotConfig: PlotConfigStorage) {
const existingConfig = loadAllCustomPlotConfig();
// Merge existing with new config
const finalPlotConfig = { ...existingConfig, ...plotConfig };
localStorage.setItem(PLOT_CONFIG, JSON.stringify(finalPlotConfig));
// Store new config name as default
storePlotConfigName(Object.keys(plotConfig)[0]);
}