Skip to content

Commit

Permalink
Fix filter text highlighting in popup
Browse files Browse the repository at this point in the history
  • Loading branch information
dabreegster committed Dec 31, 2023
1 parent 1ca8b9e commit 739baa8
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 22 deletions.
14 changes: 6 additions & 8 deletions src/lib/browse/Filters.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import { onMount } from "svelte";
import type { FeatureUnion, SchemeCollection, SchemeData } from "types";
import InterventionColorSelector from "./InterventionColorSelector.svelte";
import { filterText } from "./stores";
export let schemesGj: SchemeCollection;
// These are immutable; re-create this component if they change
Expand All @@ -18,9 +19,6 @@
// by scheme_reference
export let schemesToBeShown: Set<string> = new Set();
// Read-only, so callers can highlight search terms
export let filterText = "";
export let show = true;
// Dropdown filters
Expand Down Expand Up @@ -51,11 +49,11 @@
// When any filters change, update schemesToBeShown
function filtersUpdated(
filterText: string,
filterTextCopy: string,
filterAuthority: string,
filterFundingProgramme: string
) {
let filterNormalized = filterText.toLowerCase();
let filterNormalized = filterTextCopy.toLowerCase();
let filterFeatures = (feature: FeatureUnion) => {
// Only the name and description fields have anything worth filtering
if (
Expand Down Expand Up @@ -119,7 +117,7 @@
schemesGj = schemesGj;
counts = counts;
}
$: filtersUpdated(filterText, filterAuthority, filterFundingProgramme);
$: filtersUpdated($filterText, filterAuthority, filterFundingProgramme);
</script>

<CollapsibleCard label="Filters">
Expand All @@ -142,9 +140,9 @@
type="text"
class="govuk-input govuk-input--width-10"
id="filterText"
bind:value={filterText}
bind:value={$filterText}
/>
<SecondaryButton on:click={() => (filterText = "")}>Clear</SecondaryButton>
<SecondaryButton on:click={() => ($filterText = "")}>Clear</SecondaryButton>
</FormElement>
<InterventionColorSelector />
</CollapsibleCard>
Expand Down
7 changes: 3 additions & 4 deletions src/lib/browse/InterventionLayer.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
export let schemesGj: SchemeCollection;
export let showSchemes: boolean;
export let filterText: string;
let colorInterventions = colorInterventionsBySchema("v1");
Expand Down Expand Up @@ -57,7 +56,7 @@
}}
>
<Popup let:props>
<InterventionPopup {props} {filterText} />
<InterventionPopup {props} />
</Popup>
</CircleLayer>

Expand All @@ -76,7 +75,7 @@
}}
>
<Popup let:props>
<InterventionPopup {props} {filterText} />
<InterventionPopup {props} />
</Popup>
</LineLayer>
<CircleLayer
Expand Down Expand Up @@ -107,7 +106,7 @@
}}
>
<Popup let:props>
<InterventionPopup {props} {filterText} />
<InterventionPopup {props} />
</Popup>
</FillLayer>
<LineLayer
Expand Down
6 changes: 3 additions & 3 deletions src/lib/browse/InterventionPopup.svelte
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<script lang="ts">
import { prettyPrintMeters } from "lib/maplibre";
import { filterText } from "./stores";
export let props: { [name: string]: any };
export let filterText: string;
// When the user is filtering name/description by freeform text, highlight the matching pieces.
function highlightFilter(input: string): string {
if (!filterText) {
if (!$filterText) {
return input;
}
return input.replace(
new RegExp(filterText, "gi"),
new RegExp($filterText, "gi"),
(match) => `<mark>${match}</mark>`
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib/browse/LayerControls.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
StreetViewTool,
} from "lib/common";
import { CheckboxGroup } from "lib/govuk";
import { interactiveMapLayersEnabled } from "stores";
import CensusOutputAreaLayerControl from "./layers/areas/CensusOutputAreas.svelte";
import CombinedAuthoritiesLayerControl from "./layers/areas/CombinedAuthorities.svelte";
import ImdLayerControl from "./layers/areas/IMD.svelte";
Expand All @@ -30,6 +29,7 @@
import SchoolsLayerControl from "./layers/points/Schools.svelte";
import SportsSpacesLayerControl from "./layers/points/SportsSpaces.svelte";
import VehicleCountsLayerControl from "./layers/points/VehicleCounts.svelte";
import { interactiveMapLayersEnabled } from "./stores";
// Workaround for https://github.com/sveltejs/svelte/issues/7630
$: streetviewEnabled = !$interactiveMapLayersEnabled;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/browse/stores.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { writable, type Writable } from "svelte/store";

export const interactiveMapLayersEnabled: Writable<boolean> = writable(true);
export const filterText: Writable<string> = writable("");
4 changes: 1 addition & 3 deletions src/pages/BrowseSchemes.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
};
let schemes: Map<string, SchemeData> = new Map();
let schemesToBeShown: Set<string> = new Set();
let filterText = "";
let showSchemes = true;
function loadFile(text: string) {
Expand Down Expand Up @@ -72,7 +71,6 @@
bind:schemesGj
{schemes}
bind:schemesToBeShown
bind:filterText
bind:show={showSchemes}
/>
{/if}
Expand All @@ -88,7 +86,7 @@
<div slot="main">
<MapLibreMap style={$mapStyle} startBounds={[-5.96, 49.89, 2.31, 55.94]}>
<Geocoder />
<InterventionLayer {schemesGj} {filterText} {showSchemes} />
<InterventionLayer {schemesGj} {showSchemes} />
<div class="top-right">
<LayerControls />
</div>
Expand Down
3 changes: 0 additions & 3 deletions src/stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ userSettings.subscribe((value) =>

export const mode: Writable<Mode> = writable({ mode: "list" });

// For browse page
export const interactiveMapLayersEnabled: Writable<boolean> = writable(true);

// All feature IDs must:
//
// - be unique
Expand Down

0 comments on commit 739baa8

Please sign in to comment.