Skip to content

Commit

Permalink
Small refactoring based on code review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
rogup committed May 1, 2024
1 parent 9ae02fb commit 9b52fe6
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 20 deletions.
6 changes: 3 additions & 3 deletions src/map-app/app/map-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EventBus } from "../eventbus";
import "./map"; // Seems to be needed to prod the leaflet CSS into loading.
import { SidebarPresenter } from "./presenter/sidebar";
import { PhraseBook } from "../localisations";
import { toString as _toString, getViewportWidth } from '../utils';
import { toString as _toString, canDisplayExpandedSidebar } from '../utils';
import { Action, AppState, PropEquality, StateManager, TextSearch } from "./state-manager";
import { StateChange } from "../undo-stack";
import { Dictionary } from "../common-types";
Expand Down Expand Up @@ -138,7 +138,7 @@ export class MapUI {
this.stateManager.propFilter(new PropEquality(propName, value, isMulti));
}

if (getViewportWidth() > 1080) // on smaller screens, wait until user clicks Apply Filters
if (canDisplayExpandedSidebar()) // on smaller screens, wait until user clicks Apply Filters
EventBus.Sidebar.showInitiativeList.pub();
}

Expand All @@ -161,7 +161,7 @@ export class MapUI {
console.log("Search submitted: [" + text + "]");
this.stateManager.textSearch(new TextSearch(text));

if (getViewportWidth() > 1080) {// on smaller screens, wait until user clicks Apply Filters
if (canDisplayExpandedSidebar()) {// on smaller screens, wait until user clicks Apply Filters
EventBus.Sidebar.showInitiativeList.pub();
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/map-app/app/view/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { EventBus } from '../../eventbus';
import * as d3 from 'd3';
import { SidebarPresenter } from '../presenter/sidebar';
import { BaseView } from './base';
import { getViewportWidth } from '../../utils';
import { canDisplayInitiativePopups } from '../../utils';
import { Initiative } from '../model/initiative';

export class SidebarView extends BaseView {
Expand Down Expand Up @@ -121,8 +121,6 @@ export class SidebarView extends BaseView {
d3.select("#map-app-sidebar i").attr("class", "fa fa-angle-left");

this.presenter.changeSidebar(); // Refresh the content of the sidebar
if (document.getElementById("dir-filter") && getViewportWidth() > 1080)
document.getElementById("dir-filter")?.focus();
}

updateSidebarWidth() {
Expand Down Expand Up @@ -243,7 +241,7 @@ export class SidebarView extends BaseView {
.html(initiativeContent);
initiativeSidebar.classed("sea-initiative-sidebar-open", true);

if (getViewportWidth() <= 800)
if (!canDisplayInitiativePopups())
EventBus.Sidebar.showSidebar.pub();
}
}
26 changes: 13 additions & 13 deletions src/map-app/app/view/sidebar/initiatives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,26 @@ export class InitiativesSidebarView extends BaseSidebarView {
d3.select("#search-box").property("value", txt);
}

submitCallback = (event: Event) => {
event.preventDefault(); // prevent page reloads on submit

const oldSearchText = this.presenter.parent.mapui.getSearchText()
const newSearchText = this.getSearchText();

if (newSearchText !== oldSearchText) {
this.presenter.performSearch(newSearchText);
}
};

createSearchBox(container: d3DivSelection) {
const submitCallback = (event: Event) => {
event.preventDefault(); // prevent page reloads on submit

const oldSearchText = this.presenter.parent.mapui.getSearchText()
const newSearchText = this.getSearchText();

if (newSearchText !== oldSearchText) {
this.presenter.performSearch(newSearchText);
}
};

const form = container
.append("form")
.attr("id", "map-app-search-form")
.attr(
"class",
"w3-card-2 w3-round map-app-search-form"
)
.on("submit", this.submitCallback);
.on("submit", submitCallback);

const selection = form
.append("div")
Expand Down Expand Up @@ -95,7 +95,7 @@ export class InitiativesSidebarView extends BaseSidebarView {
.attr("autocomplete", "off")
// If we don't submit the search on blur, selecting another filter will reset the search text
// https://github.com/digitalcommons/mykomap/issues/197
.on("blur", this.submitCallback);
.on("blur", submitCallback);
}

private createFilterCount(container: d3DivSelection) {
Expand Down
15 changes: 15 additions & 0 deletions src/map-app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,3 +256,18 @@ export function filterSet<T>(set: Set<T>, predicate: Predicate<T>): Set<T> {
*/
export const getViewportWidth = () =>
Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0);

/**
* We can display an expanded sidebar if the viewport is wide enough (over 1080 pixels), otherwise
* we collapse to a single sidebar.
*/
export const canDisplayExpandedSidebar = () =>
getViewportWidth() > 1080;


/**
* We can display initiative popups if the viewport is wide enough (over 800 pixels), otherwise we
* display the info in the sidebar.
*/
export const canDisplayInitiativePopups = () =>
getViewportWidth() > 800;

0 comments on commit 9b52fe6

Please sign in to comment.