From d048384f03855e71129ce11f0387af622bb66f04 Mon Sep 17 00:00:00 2001 From: rogup Date: Fri, 10 May 2024 17:42:20 +0100 Subject: [PATCH] Ensure 'clear filters' clears all filters and doesn't just reset them to initial state --- src/map-app/app/map-ui.ts | 6 ++--- .../app/presenter/sidebar/initiatives.ts | 4 --- src/map-app/app/state-manager.ts | 27 +++++++++++++------ src/map-app/app/view/sidebar.ts | 2 +- src/map-app/app/view/sidebar/base.ts | 4 +-- src/map-app/app/view/sidebar/directory.ts | 2 +- src/map-app/eventbus.ts | 2 +- 7 files changed, 27 insertions(+), 20 deletions(-) diff --git a/src/map-app/app/map-ui.ts b/src/map-app/app/map-ui.ts index 898f7a42..d2c79208 100644 --- a/src/map-app/app/map-ui.ts +++ b/src/map-app/app/map-ui.ts @@ -49,7 +49,7 @@ export class MapUI { }; EventBus.Map.initiativeClicked.sub(initiative => this.onInitiativeClicked(initiative)); - EventBus.Map.resetSearch.sub(() => this.resetSearch()); + EventBus.Map.clearFiltersAndSearch.sub(() => this.clearFiltersAndSearch()); } // This inspects the config and constructs an appropriate set of @@ -114,8 +114,8 @@ export class MapUI { this.stateManager.clearPropFilter(filterName); } - resetSearch(): void { - this.stateManager.reset(); + clearFiltersAndSearch(): void { + this.stateManager.clearFiltersAndSearch(); } /// Returns a list of property values matching the given filter diff --git a/src/map-app/app/presenter/sidebar/initiatives.ts b/src/map-app/app/presenter/sidebar/initiatives.ts index 23b58a2c..011899d8 100644 --- a/src/map-app/app/presenter/sidebar/initiatives.ts +++ b/src/map-app/app/presenter/sidebar/initiatives.ts @@ -35,10 +35,6 @@ export class InitiativesSidebarPresenter extends BaseSidebarPresenter { this.parent.mapui.performSearch(text); } - resetSearch() { - this.parent.mapui.resetSearch(); - } - changeSearchText(txt: string) { this.view.changeSearchText(txt); } diff --git a/src/map-app/app/state-manager.ts b/src/map-app/app/state-manager.ts index 62ee9736..f775efb2 100644 --- a/src/map-app/app/state-manager.ts +++ b/src/map-app/app/state-manager.ts @@ -138,10 +138,6 @@ export class AppState { ); return new AppStateChange(textSearch, result); } - - clearTextSearch(): AppStateChange|undefined { - return this.addTextSearch(new TextSearch('')); - } addPropEquality(propEq: PropEquality): AppStateChange|undefined { const oldPropFilter = this.propFilters[propEq.propName]; @@ -200,6 +196,17 @@ export class AppState { ); return new AppStateChange(action, result); } + + removePropEqualitiesAndClearTextSearch(): AppStateChange|undefined { + if (Object.keys(this.propFilters).length === 0 && this.textSearch.searchText === '') + return undefined; // No change + + const result = new AppState( + this.allInitiatives, + this.allInitiatives + ); + return new AppStateChange(undefined, result); + } } @@ -335,10 +342,6 @@ export class StateManager { this.onChange(stateChange); } - clearTextSearch(): void { - return this.textSearch(new TextSearch('')); - } - propFilter(propEq: PropEquality): void { const stateChange = this.stack.current.result.addPropEquality(propEq); if (stateChange === undefined) @@ -363,6 +366,14 @@ export class StateManager { this.onChange(stateChange); } + clearFiltersAndSearch(): void { + const stateChange = this.stack.current.result.removePropEqualitiesAndClearTextSearch(); + if (stateChange === undefined) + return; // No change + this.stack.push(stateChange); + this.onChange(stateChange); + } + altValues(propName: string) { return this.stack.current.result.altValues(propName); } diff --git a/src/map-app/app/view/sidebar.ts b/src/map-app/app/view/sidebar.ts index e83b95ec..30e5c90a 100644 --- a/src/map-app/app/view/sidebar.ts +++ b/src/map-app/app/view/sidebar.ts @@ -45,7 +45,7 @@ export class SidebarView extends BaseView { .attr("class", "w3-button w3-border-0 ml-auto") .attr("title", labels.showDirectory) .on("click", () => { - this.presenter.mapui.resetSearch(); + this.presenter.mapui.clearFiltersAndSearch(); this.hideInitiativeList(); this.presenter.changeSidebar("directory"); diff --git a/src/map-app/app/view/sidebar/base.ts b/src/map-app/app/view/sidebar/base.ts index ecbd3f4d..d9dcbf9f 100644 --- a/src/map-app/app/view/sidebar/base.ts +++ b/src/map-app/app/view/sidebar/base.ts @@ -149,9 +149,9 @@ export abstract class BaseSidebarView extends BaseView { .attr("class", "w3-button w3-border-0 mobile-only") .attr("title", labels.showDirectory) .on("click", function () { + EventBus.Map.clearFiltersAndSearch.pub(); EventBus.Sidebar.hideInitiativeList.pub(); EventBus.Sidebar.showDirectory.pub(); - EventBus.Map.resetSearch.pub(); EventBus.Markers.needToShowLatestSelection.pub([]); }) .append("i") @@ -177,8 +177,8 @@ export abstract class BaseSidebarView extends BaseView { .attr("class", "ml-auto clear-filters-button") .text(labels.clearFilters) .on("click", () => { + EventBus.Map.clearFiltersAndSearch.pub(); EventBus.Sidebar.hideInitiativeList.pub(); - EventBus.Map.resetSearch.pub(); EventBus.Markers.needToShowLatestSelection.pub([]); }) } diff --git a/src/map-app/app/view/sidebar/directory.ts b/src/map-app/app/view/sidebar/directory.ts index 8fc60af1..d17a7d61 100644 --- a/src/map-app/app/view/sidebar/directory.ts +++ b/src/map-app/app/view/sidebar/directory.ts @@ -140,7 +140,7 @@ export class DirectorySidebarView extends BaseSidebarView { .classed(classname, true) .classed("sea-directory-field", true) .on("click", (event: MouseEvent) => { - this.presenter.parent.mapui.resetSearch(); + this.presenter.parent.mapui.clearFiltersAndSearch(); this.presenter.parent.mapui.changeFilters(propName, propValue); EventBus.Sidebar.showInitiativeList.pub(); this.resetFilterSearch(); diff --git a/src/map-app/eventbus.ts b/src/map-app/eventbus.ts index 0b03311e..66f3ba66 100644 --- a/src/map-app/eventbus.ts +++ b/src/map-app/eventbus.ts @@ -75,7 +75,7 @@ export namespace EventBus { export const selectAndZoomOnInitiative = new PostalTopic("Map.selectAndZoomOnInitiative"); export const initiativeClicked = new PostalTopic("Map.initiativeClicked"); export const setActiveArea = new PostalTopic("Map.setActiveArea"); - export const resetSearch = new PostalTopic("Map.resetSearch"); + export const clearFiltersAndSearch = new PostalTopic("Map.clearFiltersAndSearch"); } export namespace Marker { export const selectionToggled = new PostalTopic("Marker.SelectionToggled");