-
Notifications
You must be signed in to change notification settings - Fork 25
/
Configuration.ts
148 lines (131 loc) · 5.5 KB
/
Configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { readable } from 'svelte/store';
import type { ProviderFactory } from './providers/Provider';
import type { GeocoderFactory } from './geocoders/Geocoder';
import { Location } from './providers/Location';
import { NominatimGeocoder } from './geocoders/NominatimGeocoder';
import { OpenMeteoProvider } from './providers/OpenMeteoProvider';
import { ProviderFactories } from './providers';
export enum StorageMode {
QueryString,
LocalStorage,
}
export enum Units {
Imperial,
Metric,
}
export enum AutoExpand {
Today,
All,
None,
}
export interface Configuration {
providerFactory: ProviderFactory;
providerParams: { [key: string]: string };
geocoderFactory: GeocoderFactory;
location: Location | undefined;
units: Units;
autoexpand: AutoExpand;
title: string;
refreshInterval: number;
showHourlyPrecipitation: boolean;
showHourlyWind: boolean;
}
const DEFAULT_CONFIGURATION: Configuration = {
providerFactory: OpenMeteoProvider,
providerParams: {},
geocoderFactory: NominatimGeocoder,
location: undefined,
units: new Intl.Locale(window.navigator.language).region === 'US' ? Units.Imperial : Units.Metric,
autoexpand: AutoExpand.Today,
title: '',
refreshInterval: 2 * 3600,
showHourlyPrecipitation: true,
showHourlyWind: true,
};
function decodeConfiguration(params: { [key: string]: string }): Configuration {
const providerFactory = ProviderFactories.find((e) => e.id === params['provider']) || DEFAULT_CONFIGURATION.providerFactory;
const providerParams = Object.fromEntries(providerFactory.fields.map((f: { name: string }) => [f.name, params[f.name]]));
const geocoderFactory = DEFAULT_CONFIGURATION.geocoderFactory;
const location = Location.fromString(params['location']) || DEFAULT_CONFIGURATION.location;
const title = params['title'] || DEFAULT_CONFIGURATION.title;
const units = params['units'] === 'metric' ? Units.Metric : params['units'] === 'imperial' ? Units.Imperial : DEFAULT_CONFIGURATION.units;
const autoexpand =
params['autoexpand'] === 'today'
? AutoExpand.Today
: params['autoexpand'] === 'all'
? AutoExpand.All
: params['autoexpand'] === 'none'
? AutoExpand.None
: DEFAULT_CONFIGURATION.autoexpand;
const refreshInterval = parseInt(params['refresh_interval']) || DEFAULT_CONFIGURATION.refreshInterval;
const showHourlyPrecipitation =
params['hourly_precipitation'] === undefined ? DEFAULT_CONFIGURATION.showHourlyPrecipitation : params['hourly_precipitation'] === 'true' ? true : false;
const showHourlyWind = params['hourly_wind'] === undefined ? DEFAULT_CONFIGURATION.showHourlyWind : params['hourly_wind'] === 'true' ? true : false;
return {
providerFactory,
providerParams,
geocoderFactory,
location,
title,
units,
autoexpand,
refreshInterval,
showHourlyPrecipitation,
showHourlyWind,
};
}
function encodeConfiguration(configuration: Configuration): object {
const params: { [key: string]: string } = {};
params['provider'] = configuration.providerFactory.id;
for (const field of configuration.providerFactory.fields) {
if (configuration.providerParams[field.name] !== undefined) {
params[field.name] = configuration.providerParams[field.name];
}
}
if (configuration.location !== undefined && configuration.location !== DEFAULT_CONFIGURATION.location) {
params['location'] = configuration.location.toString();
}
if (configuration.units !== DEFAULT_CONFIGURATION.units) {
params['units'] = configuration.units === Units.Metric ? 'metric' : 'imperial';
}
if (configuration.autoexpand !== DEFAULT_CONFIGURATION.autoexpand) {
params['autoexpand'] = configuration.autoexpand === AutoExpand.Today ? 'today' : configuration.autoexpand === AutoExpand.All ? 'all' : 'none';
}
if (configuration.title !== DEFAULT_CONFIGURATION.title) {
params['title'] = configuration.title;
}
if (configuration.refreshInterval !== DEFAULT_CONFIGURATION.refreshInterval) {
params['refresh_interval'] = configuration.refreshInterval.toString();
}
if (configuration.showHourlyPrecipitation !== DEFAULT_CONFIGURATION.showHourlyPrecipitation) {
params['hourly_precipitation'] = configuration.showHourlyPrecipitation.toString();
}
if (configuration.showHourlyWind !== DEFAULT_CONFIGURATION.showHourlyWind) {
params['hourly_wind'] = configuration.showHourlyWind.toString();
}
return params;
}
export function getStorageMode(): StorageMode {
return new URLSearchParams(window.location.search).get('storage') === 'local' ? StorageMode.LocalStorage : StorageMode.QueryString;
}
export function loadConfiguration(): Configuration {
const storageMode = getStorageMode();
if (storageMode === StorageMode.QueryString) {
return decodeConfiguration(Object.fromEntries(new URLSearchParams(window.location.search).entries()));
} else {
return decodeConfiguration(JSON.parse(window.localStorage.getItem('configuration') || '{}'));
}
}
export function storeConfiguration(configuration: Configuration): void {
const storageMode = getStorageMode();
if (storageMode === StorageMode.QueryString) {
window.location.search = new URLSearchParams(encodeConfiguration(configuration) as Record<string, string>).toString();
} else {
window.localStorage.setItem('configuration', JSON.stringify(encodeConfiguration(configuration)));
window.location.search = new URLSearchParams({ storage: 'local' }).toString();
}
}
export const configuration = readable(DEFAULT_CONFIGURATION, function start(set) {
set(loadConfiguration());
return () => {};
});