Skip to content

Commit a6c0029

Browse files
committed
[CHORE] Reuse shared text and unsaved-change helpers
1 parent 8e0b9db commit a6c0029

2 files changed

Lines changed: 9 additions & 26 deletions

File tree

src/lib/components/explorer/advanced/AdvancedFilteringPage.svelte

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
<script lang="ts">
2-
import { resolve } from '$app/paths';
3-
import { goto, beforeNavigate } from '$app/navigation';
42
import { onMount, onDestroy } from 'svelte';
53
import { config } from '$lib/configuration.svelte';
64
import Content from '$lib/components/Content.svelte';
75
import Modal from '$lib/components/Modal.svelte';
86
import AdvancedFiltering from '$lib/components/explorer/advanced/AdvancedFiltering.svelte';
97
import { panelOpen } from '$lib/stores/SidePanel';
8+
import { createUnsavedGuard } from '$lib/utilities/UnsavedGuard.svelte';
109
1110
interface Props {
1211
backUrl: string;
@@ -16,8 +15,7 @@
1615
const { backUrl, backTitle }: Props = $props();
1716
1817
let advancedFilteringRef: ReturnType<typeof AdvancedFiltering>;
19-
let showUnsavedModal = $state(false);
20-
let bypassGuard = false;
18+
const guard = createUnsavedGuard(() => advancedFilteringRef?.hasUnsavedChanges() ?? false);
2119
2220
onMount(() => {
2321
$panelOpen = false;
@@ -27,46 +25,30 @@
2725
$panelOpen = true;
2826
});
2927
30-
beforeNavigate(({ cancel }) => {
31-
if (!bypassGuard && advancedFilteringRef?.hasUnsavedChanges()) {
32-
cancel();
33-
showUnsavedModal = true;
34-
}
35-
});
36-
3728
function applyChanges() {
3829
advancedFilteringRef?.applyChanges();
3930
}
4031
4132
function handleCancel() {
42-
showUnsavedModal = false;
33+
guard.take();
4334
}
4435
4536
function handleDiscard() {
46-
showUnsavedModal = false;
47-
bypassGuard = true;
48-
goto(resolve(backUrl as '/'));
37+
guard.take();
38+
guard.navigate(backUrl);
4939
}
5040
5141
function handleModalApply() {
52-
showUnsavedModal = false;
42+
guard.take();
5343
advancedFilteringRef?.applyChanges();
5444
}
55-
56-
function handleBeforeUnload(event: BeforeUnloadEvent) {
57-
if (advancedFilteringRef?.hasUnsavedChanges()) {
58-
event.preventDefault();
59-
}
60-
}
6145
</script>
6246

63-
<svelte:window onbeforeunload={handleBeforeUnload} />
64-
6547
<svelte:head>
6648
<title>{config.branding.applicationName} | Advanced Query Builder</title>
6749
</svelte:head>
6850

69-
<Modal bind:open={showUnsavedModal} title="Unsaved Changes" closeable={true} onclose={handleCancel}>
51+
<Modal bind:open={guard.open} title="Unsaved Changes" closeable={true} onclose={handleCancel}>
7052
<p class="mb-6">You have unsaved changes to your filters. What would you like to do?</p>
7153
<footer class="flex justify-end gap-2">
7254
<button class="btn border preset-tonal-error" onclick={handleDiscard}>Discard Changes</button>

src/lib/utilities/Plotly.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PlotlyHTMLElement, Root, Data, Config, Layout } from 'plotly.js-basic-dist-min';
22
import { config } from '$lib/configuration.svelte';
3+
import { truncate } from '$lib/utilities/Strings';
34

45
const MAX_TITLE_LENGTH = 60;
56

@@ -84,7 +85,7 @@ function getSubTitle(inData: CategoricalPlotData | ContinuousPlotData) {
8485
}
8586

8687
function shortenTitle(title: string) {
87-
return title.length > MAX_TITLE_LENGTH ? title.substring(0, MAX_TITLE_LENGTH - 3) + '...' : title;
88+
return truncate(title, MAX_TITLE_LENGTH);
8889
}
8990

9091
export function normalizeCount(value: CountValue): ObfuscatedCount {

0 commit comments

Comments
 (0)