frequi_origin/src/shared/storage.ts

36 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-08-24 18:05:07 +00:00
import { PlotConfig, EMPTY_PLOTCONFIG, PlotConfigStorage } from '@/types';
// TODO: Outdated, this module can be removed
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-08 13:57:36 +00:00
export function getPlotConfigName(): 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);
}
2020-08-08 13:57:36 +00:00
export function getAllCustomPlotConfig(): 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
2020-08-08 13:57:36 +00:00
export function getAllPlotConfigNames(): Array<string> {
return Object.keys(getAllCustomPlotConfig());
}
export function getCustomPlotConfig(configName: string): PlotConfig {
const configs = getAllCustomPlotConfig();
2020-08-24 18:05:07 +00:00
return configName in configs ? configs[configName] : { ...EMPTY_PLOTCONFIG };
}
export function storeCustomPlotConfig(plotConfig: PlotConfigStorage) {
2020-08-08 13:57:36 +00:00
const existingConfig = getAllCustomPlotConfig();
2020-08-24 18:05:07 +00:00
// 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]);
}