Skip to content

Commit c7d92fc

Browse files
author
Vincent van der Wal
committed
optimize timeslider
1 parent 9b23c67 commit c7d92fc

File tree

4 files changed

+54
-119
lines changed

4 files changed

+54
-119
lines changed

src/lib/components/time-slider.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export type TimeSliderOptions = {
22
container: HTMLElement;
33
initialDate: Date;
44
onChange: (date: Date) => void;
5+
resolution?: number;
56
};
67

78
function pad2(n: number) {
@@ -22,26 +23,30 @@ function formatDateInputValue(date: Date) {
2223
return `${date.getFullYear()}-${pad2(date.getMonth() + 1)}-${pad2(date.getDate())}`;
2324
}
2425

25-
export function createTimeSlider({ container, initialDate, onChange }: TimeSliderOptions) {
26+
export function createTimeSlider({
27+
container,
28+
initialDate,
29+
onChange,
30+
resolution = 1
31+
}: TimeSliderOptions) {
2632
let currentDate = getLocalMidnight(initialDate);
2733
let currentHour = initialDate.getHours();
2834

2935
container.innerHTML = `
30-
<div>
36+
<div style="display:flex; gap: 0.5em; justify-items: center; align-items: center;">
3137
<button id="prev_hour" type="button">&lt;</button>
32-
<span id="slider_time_label">${formatSliderLabel(currentDate, currentHour)}</span>
38+
<span style="white-space:nowrap;" id="slider_time_label">${formatSliderLabel(currentDate, currentHour)}</span>
3339
<button id="next_hour" type="button">&gt;</button>
3440
</div>
3541
<input
3642
type="range"
3743
id="time_slider"
3844
min="0"
3945
max="23"
40-
step="1"
46+
step="${resolution}"
4147
value="${currentHour}"
4248
style="width: 200px;"
4349
/>
44-
<br>
4550
<input
4651
type="date"
4752
id="date_picker"
@@ -73,8 +78,8 @@ export function createTimeSlider({ container, initialDate, onChange }: TimeSlide
7378
});
7479

7580
prevBtn.addEventListener('click', () => {
76-
if (currentHour > 0) {
77-
currentHour--;
81+
if (currentHour > resolution - 1) {
82+
currentHour -= resolution;
7883
} else {
7984
currentHour = 23;
8085
currentDate.setDate(currentDate.getDate() - 1);
@@ -86,8 +91,8 @@ export function createTimeSlider({ container, initialDate, onChange }: TimeSlide
8691
});
8792

8893
nextBtn.addEventListener('click', () => {
89-
if (currentHour < 23) {
90-
currentHour++;
94+
if (currentHour < 23 - resolution) {
95+
currentHour += resolution;
9196
} else {
9297
currentHour = 0;
9398
currentDate.setDate(currentDate.getDate() + 1);

src/lib/utils/domains.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ export const domains: Array<Domain> = [
292292
}
293293
},
294294
time_interval: 1,
295-
model_interval: 3,
295+
model_interval: 6,
296296
variables: [
297297
'cape',
298298
//'cloud_cover', <- only per hPa
@@ -326,7 +326,7 @@ export const domains: Array<Domain> = [
326326
}
327327
},
328328
time_interval: 1,
329-
model_interval: 3,
329+
model_interval: 6,
330330
variables: [
331331
'cloud_cover',
332332
'precipitation',
@@ -366,7 +366,7 @@ export const domains: Array<Domain> = [
366366
}
367367
},
368368
time_interval: 1,
369-
model_interval: 3,
369+
model_interval: 1,
370370
variables: [
371371
'cape',
372372
'cloud_cover',
@@ -408,7 +408,7 @@ export const domains: Array<Domain> = [
408408
}
409409
},
410410
time_interval: 1,
411-
model_interval: 3,
411+
model_interval: 1,
412412
variables: [
413413
'cape',
414414
'cloud_cover',
@@ -443,8 +443,8 @@ export const domains: Array<Domain> = [
443443
return this;
444444
}
445445
},
446-
time_interval: 1,
447-
model_interval: 3,
446+
time_interval: 3,
447+
model_interval: 6,
448448
variables: [
449449
'cape',
450450
'cloud_cover',
@@ -475,8 +475,8 @@ export const domains: Array<Domain> = [
475475
return this;
476476
}
477477
},
478-
time_interval: 1,
479-
model_interval: 3,
478+
time_interval: 6,
479+
model_interval: 6,
480480
variables: [
481481
'cape',
482482
'cloud_cover',

src/routes/+page.svelte

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@
183183
184184
mapBounds = map.getBounds();
185185
186+
timeSliderApi.setDisabled(true);
187+
186188
omUrl = getOMUrl();
187189
if (map.getLayer('omFileLayer')) {
188190
map.removeLayer('omFileLayer');
@@ -217,6 +219,8 @@
217219
checked++;
218220
if (source.loaded() || checked >= 30) {
219221
checked = 0;
222+
timeSliderApi.setDisabled(false);
223+
220224
clearInterval(checkSourceLoadedInterval);
221225
}
222226
}, 100);
@@ -444,14 +448,18 @@
444448
url.searchParams.set('time', newDate.toISOString().replace(/[:Z]/g, '').slice(0, 15));
445449
history.pushState({}, '', url);
446450
changeOMfileURL();
447-
}
451+
},
452+
resolution: domain.time_interval
448453
});
449454
});
450455
});
451456
onDestroy(() => {
452457
if (map) {
453458
map.remove();
454459
}
460+
if (timeSliderContainer) {
461+
timeSliderContainer.innerHTML = ``;
462+
}
455463
});
456464
457465
const getDomainData = async (latest = true) => {
@@ -531,33 +539,26 @@
531539
{/if}
532540
{/if}
533541
<div
534-
class=" bg-background/35 absolute bottom-0 left-8 w-[195px] rounded px-3 py-2 text-xs overflow-ellipsis"
542+
class=" bg-background/35 absolute bottom-0 left-8 w-[165px] rounded px-2 py-2 text-xs overflow-ellipsis"
535543
>
536-
<div>Domain: <span class="ml-auto text-end">{domain.label}</span></div>
537-
<div>
538-
Local time: <span class="ml-auto text-end"
539-
>{timeSelected.getFullYear() +
540-
'-' +
541-
pad(timeSelected.getMonth() + 1) +
542-
'-' +
543-
timeSelected.getDate() +
544-
' ' +
545-
timeSelected.getHours() +
546-
':' +
547-
pad(timeSelected.getMinutes())}</span
548-
>
544+
<div class=" overflow-hidden">
545+
<p class="truncate">
546+
Domain: {domain.label}
547+
</p>
548+
<p class="truncate">
549+
Variable: {variable.label}
550+
</p>
549551
</div>
550-
<div>Variable: <span class="ml-auto text-end">{variable.label}</span></div>
551552
</div>
552553
</div>
553554
<div
554-
class="bg-background/40 absolute bottom-18 left-[50%] mx-auto transform-[translate(-50%)] px-4 py-4 {!showTimeSelector
555+
class="bg-background/40 absolute bottom-14.5 left-[50%] mx-auto transform-[translate(-50%)] rounded-lg px-4 py-4 {!showTimeSelector
555556
? 'pointer-events-none opacity-0'
556557
: 'opacity-100'}"
557558
>
558559
<div
559560
bind:this={timeSliderContainer}
560-
class="time-slider-container flex flex-col items-center"
561+
class="time-slider-container flex flex-col items-center gap-0"
561562
></div>
562563
</div>
563564
<div class="absolute">

src/styles.css

Lines changed: 13 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -169,80 +169,6 @@ body {
169169
min-height: 100%;
170170
}
171171

172-
.nav-logo {
173-
color: white;
174-
display: flex;
175-
align-items: center;
176-
text-decoration: none;
177-
gap: 5px;
178-
padding-top: 25px;
179-
padding-left: 25px;
180-
}
181-
182-
.nav-logo svg {
183-
margin-top: -5px;
184-
}
185-
186-
.nav-logo:hover {
187-
text-decoration: underline;
188-
}
189-
190-
.info-wrapper {
191-
position: absolute;
192-
bottom: 15px;
193-
left: 50%;
194-
transform: translate(-50%);
195-
}
196-
197-
#info_box {
198-
background: rgba(255, 255, 255, 0.8);
199-
border-radius: 15px;
200-
padding: 15px 20px;
201-
color: black;
202-
text-align: center;
203-
}
204-
205-
.maplibregl-popup {
206-
pointer-events: none;
207-
}
208-
209-
#time_slider_container button {
210-
border-width: 1px;
211-
background-color: white;
212-
color: black;
213-
padding: 5px;
214-
border-radius: 5px;
215-
font-size: 15px;
216-
padding: 0.5rem 0.75rem;
217-
font-weight: bold;
218-
}
219-
220-
#slider_time_label {
221-
margin: 0 0.3rem;
222-
}
223-
224-
.date-time-selection {
225-
margin-top: 0.5rem;
226-
border-width: 1px;
227-
}
228-
229-
.dark .domain-selection,
230-
.dark .variable-selection,
231-
.dark .date-time-selection {
232-
background-color: rgba(100, 100, 100, 0.8);
233-
color: white;
234-
}
235-
236-
.dark #time_slider_container button {
237-
background-color: rgba(100, 100, 100, 0.8);
238-
color: white;
239-
}
240-
241-
.dark #darkmode_toggle {
242-
background-color: rgba(100, 100, 100, 0.8);
243-
color: white;
244-
}
245-
246172
.dark .maplibregl-ctrl.maplibregl-ctrl-group {
247173
background-color: rgba(40, 40, 40, 0.95);
248174
}
@@ -279,18 +205,21 @@ body {
279205
font-weight: bold;
280206
}
281207

282-
.time-slider-container button {
208+
.dark .time-slider-container button {
209+
background: rgba(100, 100, 100, 0.8);
210+
color: white;
211+
}
212+
213+
.date-time-selection {
214+
border-radius: 0.25em;
283215
border-width: 1px;
284-
background-color: white;
285-
color: black;
286-
padding: 5px;
287-
border-radius: 5px;
288-
font-size: 15px;
289-
padding: 0.3rem 0.75rem;
290-
font-weight: bold;
291216
}
292217

293-
.dark .time-slider-container button {
294-
background: rgba(100, 100, 100, 0.8);
218+
.dark .date-time-selection {
219+
background-color: rgba(100, 100, 100, 0.8);
295220
color: white;
296221
}
222+
223+
#time_slider {
224+
margin: 0.6em 0;
225+
}

0 commit comments

Comments
 (0)