-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathSettingsModal.svelte
167 lines (143 loc) · 5.63 KB
/
SettingsModal.svelte
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<script lang="ts">
import type { ProviderFactory } from './providers/Provider';
import type { Configuration } from './Configuration';
import { ProviderFactories } from './providers';
import { Location } from './providers/Location';
import { Units, decodeConfiguration, encodeConfiguration } from './Configuration';
import { Modal, Label, Select, Input, Hr, Button, ButtonGroup, Spinner, Radio } from 'flowbite-svelte';
import Icon from '@iconify/svelte';
enum LocationMode {
Geolocation,
Coordinates,
}
/* State */
let modalOpen = false;
let currentConfiguration: Configuration;
/* Settings State */
let providerFactory: ProviderFactory | undefined;
let providerParams: object = {};
let locationMode: LocationMode;
let location: Location;
let units: Units;
let title: string;
let refreshInterval: number;
let valid: boolean;
/* Loading State for location */
let locationLoading: boolean = false;
function updateProviderParams() {
providerParams =
providerFactory === currentConfiguration.providerFactory
? Object.fromEntries(providerFactory.fields.map((f) => [f.name, currentConfiguration.providerParams[f.name]]))
: Object.fromEntries(providerFactory.fields.map((f) => [f.name, '']));
}
export function open() {
const urlParams = Object.fromEntries(new URLSearchParams(window.location.search).entries());
currentConfiguration = decodeConfiguration(urlParams);
providerFactory = ProviderFactories.includes(currentConfiguration.providerFactory) ? currentConfiguration.providerFactory : undefined;
locationMode = currentConfiguration.location ? LocationMode.Coordinates : LocationMode.Geolocation;
location = currentConfiguration.location || new Location('', '');
units = currentConfiguration.units;
title = currentConfiguration.title;
refreshInterval = currentConfiguration.refreshInterval;
updateProviderParams();
modalOpen = true;
}
async function handleLocate() {
locationLoading = true;
location = (await Location.fromGeolocation()) || location;
locationLoading = false;
}
function handleClose() {
modalOpen = false;
}
function handleSave() {
let configuration: Configuration = {
providerFactory,
providerParams,
location: (providerFactory.requiresLocation && locationMode === LocationMode.Coordinates && location.valid() && location) || undefined,
units,
title,
refreshInterval,
};
window.location.search = new URLSearchParams(encodeConfiguration(configuration) as Record<string, string>).toString();
}
$: {
valid =
providerFactory &&
Object.values(providerParams).every((e) => e !== '') &&
(!providerFactory.requiresLocation || locationMode === LocationMode.Geolocation || location.valid());
}
</script>
<Modal bind:open={modalOpen} title="Settings" class="w-full sm:w-2/3">
<div class="space-y-4">
<div>
<Label for="select-provider" class="mb-2">Weather Provider</Label>
<Select
id="select-provider"
items={ProviderFactories.map((provider) => ({ name: provider.description, value: provider }))}
bind:value={providerFactory}
on:change={updateProviderParams}
placeholder="Select Provider"
/>
</div>
{#if providerFactory && providerFactory.fields.length > 0}
{#each providerFactory.fields as field}
<div>
<Label for="input-{field.name}" class="mb-2">{field.description}</Label>
<Input id="input-{field.name}" bind:value={providerParams[field.name]} required />
</div>
{/each}
{/if}
<Hr class="mt-2" height="h-px" />
{#if providerFactory.requiresLocation}
<div>
<Label for="radio-location-mode" class="mb-2">Location</Label>
<div class="flex gap-4 ml-2 my-5">
<Radio id="radio-location-mode" bind:group={locationMode} value={LocationMode.Geolocation}>Geolocation</Radio>
<Radio bind:group={locationMode} value={LocationMode.Coordinates}>Coordinates</Radio>
</div>
</div>
<div>
<Label for="group-location" class="mb-2">Coordinates</Label>
<ButtonGroup id="group-location" class="w-full">
<Input id="input-latitude" bind:value={location.latitude} disabled={locationMode === LocationMode.Geolocation} placeholder="Latitude (decimal)" />
<Input id="input-longitude" bind:value={location.longitude} disabled={locationMode === LocationMode.Geolocation} placeholder="Longitude (decimal)" />
<Button
id="btn-locate"
on:click={handleLocate}
disabled={locationMode === LocationMode.Geolocation || locationLoading}
size="sm"
outline
class="!p-3"
color="light"
>
{#if locationLoading}
<Spinner size="5" color="gray" />
{:else}
<Icon icon="radix-icons:crosshair-2" class="text-lg" />
{/if}
</Button>
</ButtonGroup>
</div>
{/if}
<div>
<Label for="select-units" class="mb-2">Units</Label>
<Select
id="select-units"
items={[
{ name: 'Imperial', value: Units.Imperial },
{ name: 'Metric', value: Units.Metric },
]}
bind:value={units}
/>
</div>
<div>
<Label for="input-title" class="mb-2">Title (optional)</Label>
<Input id="input-title" bind:value={title} required />
</div>
</div>
<svelte:fragment slot="footer">
<Button on:click={handleClose} color="alternative" class="ml-auto">Cancel</Button>
<Button on:click={handleSave} disabled={!valid}>Save</Button>
</svelte:fragment>
</Modal>