diff --git a/packages/chaire-lib-frontend/src/services/map/MapLayerManager.ts b/packages/chaire-lib-frontend/src/services/map/MapLayerManager.ts index 12978ed7..9bb986a6 100644 --- a/packages/chaire-lib-frontend/src/services/map/MapLayerManager.ts +++ b/packages/chaire-lib-frontend/src/services/map/MapLayerManager.ts @@ -205,7 +205,10 @@ class MapboxLayerManager { this._map?.addLayer(this._layersByName[layerName].layer, this.getNextLayerName(layerName)); } - updateLayer(layerName, geojson) { + updateLayer( + layerName: string, + geojson: GeoJSON.FeatureCollection | ((original: GeoJSON.FeatureCollection) => GeoJSON.FeatureCollection) + ) { const newGeojson = typeof geojson === 'function' ? geojson(this._layersByName[layerName].source.data) diff --git a/packages/chaire-lib-frontend/src/services/map/events/GlobalMapEvents.ts b/packages/chaire-lib-frontend/src/services/map/events/GlobalMapEvents.ts index 9309581f..b5c14b69 100644 --- a/packages/chaire-lib-frontend/src/services/map/events/GlobalMapEvents.ts +++ b/packages/chaire-lib-frontend/src/services/map/events/GlobalMapEvents.ts @@ -13,6 +13,8 @@ import Preferences from 'chaire-lib-common/lib/config/Preferences'; import { MapEventHandlerDescription } from '../IMapEventHandler'; import { getLinesInView, offsetOverlappingLines } from 'chaire-lib-common/lib/services/geodata/ManageOverlappingLines'; import { getNodesInView, manageRelocatingNodes } from 'chaire-lib-common/lib/services/geodata/RelocateNodes'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from './MapEventsCallbacks'; // TODO: Make zoomLimit modifiable by user const zoomLimit = 14; //Zoom levels smaller than this will not apply line separation @@ -118,7 +120,10 @@ const applyAestheticChanges = async (boundsGL: MapboxGL.LngLatBounds, zoom: numb return; } - serviceLocator.eventManager.emit('map.updateLayer', 'transitPaths', layer); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: layer + }); serviceLocator.eventManager.emit('map.updateLayers', { transitNodes: transitNodes diff --git a/packages/chaire-lib-frontend/src/services/map/events/MapEventsCallbacks.ts b/packages/chaire-lib-frontend/src/services/map/events/MapEventsCallbacks.ts new file mode 100644 index 00000000..982fe319 --- /dev/null +++ b/packages/chaire-lib-frontend/src/services/map/events/MapEventsCallbacks.ts @@ -0,0 +1,14 @@ +/* + * Copyright 2023, Polytechnique Montreal and contributors + * + * This file is licensed under the MIT License. + * License text available at https://opensource.org/licenses/MIT + */ + +export type MapUpdateLayerEventType = { + name: 'map.updateLayer'; + arguments: { + layerName: string; + data: GeoJSON.FeatureCollection | ((original: GeoJSON.FeatureCollection) => GeoJSON.FeatureCollection); + }; +}; diff --git a/packages/transition-frontend/src/components/forms/accessibilityMap/AccessibilityMapForm.tsx b/packages/transition-frontend/src/components/forms/accessibilityMap/AccessibilityMapForm.tsx index b73d727a..bbb08e83 100644 --- a/packages/transition-frontend/src/components/forms/accessibilityMap/AccessibilityMapForm.tsx +++ b/packages/transition-frontend/src/components/forms/accessibilityMap/AccessibilityMapForm.tsx @@ -44,6 +44,8 @@ import AccessibilityMapStatsComponent from './AccessibilityMapStatsComponent'; import TimeOfTripComponent from '../transitRouting/widgets/TimeOfTripComponent'; import TransitRoutingBaseComponent from '../transitRouting/widgets/TransitRoutingBaseComponent'; import AccessibilityMapBatchForm from './AccessibilityMapBatchForm'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; export interface AccessibilityMapFormProps extends WithTranslation { addEventListeners?: () => void; @@ -89,11 +91,10 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPoints', + data: routingEngine.locationToGeojson() + }); } } @@ -108,7 +109,10 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPoints', + data: routing.locationToGeojson() + }); this.removePolygons(); } @@ -140,12 +144,14 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPolygons', + data: turfFeatureCollection([]) + }); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'accessibilityMapPolygonStrokes', + data: turfFeatureCollection([]) + }); this.setState({ currentResult: undefined, loading: false @@ -157,7 +163,10 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPoints', + data: routing.locationToGeojson() + }); this.removePolygons(); //this.calculateRouting(); } @@ -167,7 +176,10 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPoints', + data: routing.locationToGeojson() + }); this.removePolygons(); //this.calculateRouting(); } @@ -177,8 +189,14 @@ class AccessibilityMapForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'accessibilityMapPolygons', + data: polygons + }); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'accessibilityMapPolygonStrokes', + data: strokes + }); this.setState({ geojsonDownloadUrl: DownloadsUtils.generateJsonDownloadUrl(polygons), diff --git a/packages/transition-frontend/src/components/forms/agency/TransitAgencyButton.tsx b/packages/transition-frontend/src/components/forms/agency/TransitAgencyButton.tsx index 2b138c09..813c9f16 100644 --- a/packages/transition-frontend/src/components/forms/agency/TransitAgencyButton.tsx +++ b/packages/transition-frontend/src/components/forms/agency/TransitAgencyButton.tsx @@ -17,6 +17,8 @@ import Button from '../../parts/Button'; import ButtonCell from '../../parts/ButtonCell'; import ButtonList from '../../parts/ButtonList'; import TransitLineButton from '../line/TransitLineButton'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface AgencyButtonProps extends WithTranslation { agency: Agency; @@ -62,11 +64,10 @@ const TransitAgencyButton: React.FunctionComponent = (props: // reload paths await serviceLocator.collectionManager.get('paths').loadFromServer(serviceLocator.socketEventManager); serviceLocator.collectionManager.refresh('paths'); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); await serviceLocator.collectionManager.get('lines').loadFromServer(serviceLocator.socketEventManager); serviceLocator.collectionManager.refresh('lines'); } @@ -95,11 +96,10 @@ const TransitAgencyButton: React.FunctionComponent = (props: serviceLocator.collectionManager.refresh('paths'); serviceLocator.collectionManager.refresh('lines'); serviceLocator.collectionManager.refresh('agencies'); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.eventManager.emit('progress', { name: 'SavingAgency', progress: 1.0 }); }; diff --git a/packages/transition-frontend/src/components/forms/agency/TransitAgencyEdit.tsx b/packages/transition-frontend/src/components/forms/agency/TransitAgencyEdit.tsx index a6f8529c..c29681f3 100644 --- a/packages/transition-frontend/src/components/forms/agency/TransitAgencyEdit.tsx +++ b/packages/transition-frontend/src/components/forms/agency/TransitAgencyEdit.tsx @@ -23,6 +23,8 @@ import { SaveableObjectForm, SaveableObjectState } from 'chaire-lib-frontend/lib import SelectedObjectButtons from 'chaire-lib-frontend/lib/components/pageParts/SelectedObjectButtons'; import CollectionDownloadButtons from 'chaire-lib-frontend/lib/components/pageParts/CollectionDownloadButtons'; import Agency from 'transition-common/lib/services/agency/Agency'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; const timezoneZoneChoices: { label: string; value: string }[] = []; for (let i = 0, countI = timezones.length; i < countI; i++) { @@ -77,11 +79,10 @@ class TransitAgencyEdit extends SaveableObjectForm('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.collectionManager .get('lines') .loadFromServer(serviceLocator.socketEventManager, serviceLocator.collectionManager); diff --git a/packages/transition-frontend/src/components/forms/line/TransitLineButton.tsx b/packages/transition-frontend/src/components/forms/line/TransitLineButton.tsx index 27a175bf..8058c114 100644 --- a/packages/transition-frontend/src/components/forms/line/TransitLineButton.tsx +++ b/packages/transition-frontend/src/components/forms/line/TransitLineButton.tsx @@ -12,6 +12,8 @@ import Line from 'transition-common/lib/services/line/Line'; import Button from '../../parts/Button'; import ButtonCell from '../../parts/ButtonCell'; import { duplicateLine } from 'transition-common/lib/services/line/LineDuplicator'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface LineButtonProps extends WithTranslation { line: Line; @@ -51,11 +53,10 @@ const TransitLineButton: React.FunctionComponent = (props: Line // reload paths await serviceLocator.collectionManager.get('paths').loadFromServer(serviceLocator.socketEventManager); serviceLocator.collectionManager.refresh('paths'); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); } } serviceLocator.eventManager.emit('progress', { name: 'DeletingLine', progress: 1.0 }); @@ -79,11 +80,10 @@ const TransitLineButton: React.FunctionComponent = (props: Line serviceLocator.collectionManager.refresh('paths'); serviceLocator.collectionManager.refresh('lines'); serviceLocator.collectionManager.refresh('services'); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.eventManager.emit('progress', { name: 'SavingLine', progress: 1.0 }); }; diff --git a/packages/transition-frontend/src/components/forms/line/TransitLineEdit.tsx b/packages/transition-frontend/src/components/forms/line/TransitLineEdit.tsx index f9be2f0d..1f56b1c5 100644 --- a/packages/transition-frontend/src/components/forms/line/TransitLineEdit.tsx +++ b/packages/transition-frontend/src/components/forms/line/TransitLineEdit.tsx @@ -32,6 +32,8 @@ import SelectedObjectButtons from 'chaire-lib-frontend/lib/components/pageParts/ import { SaveableObjectForm, SaveableObjectState } from 'chaire-lib-frontend/lib/components/forms/SaveableObjectForm'; import AgencyCollection from 'transition-common/lib/services/agency/AgencyCollection'; import Preferences from 'chaire-lib-common/lib/config/Preferences'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface LineFormProps extends WithTranslation { line: Line; @@ -87,11 +89,10 @@ class TransitLineEdit extends SaveableObjectForm('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.collectionManager.refresh('lines'); serviceLocator.eventManager.emit('progress', { name: 'DeletingLine', progress: 1.0 }); } else { @@ -140,11 +141,10 @@ class TransitLineEdit extends SaveableObjectForm('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.eventManager.emit('progress', { name: 'SavingLine', progress: 1.0 }); serviceLocator.eventManager.emit('fullSizePanel.hide'); } catch (error) { diff --git a/packages/transition-frontend/src/components/forms/node/TransitNodeEdit.tsx b/packages/transition-frontend/src/components/forms/node/TransitNodeEdit.tsx index 4f8718a7..f07bce72 100644 --- a/packages/transition-frontend/src/components/forms/node/TransitNodeEdit.tsx +++ b/packages/transition-frontend/src/components/forms/node/TransitNodeEdit.tsx @@ -32,6 +32,8 @@ import InputStringFormatted from 'chaire-lib-frontend/lib/components/input/Input import SelectedObjectButtons from 'chaire-lib-frontend/lib/components/pageParts/SelectedObjectButtons'; import NodeStatistics from './TransitNodeStatistics'; import * as Status from 'chaire-lib-common/lib/utils/Status'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface NodeFormProps extends WithTranslation { node: Node; @@ -174,9 +176,12 @@ class TransitNodeEdit extends SaveableObjectForm { - oldGeojson.features[0].geometry.coordinates = coordinates; - return oldGeojson; + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitNodesSelected', + data: (oldGeojson: GeoJSON.FeatureCollection) => { + (oldGeojson.features[0].geometry as GeoJSON.Point).coordinates = coordinates; + return oldGeojson; + } }); } diff --git a/packages/transition-frontend/src/components/forms/node/TransitNodeImportForm.tsx b/packages/transition-frontend/src/components/forms/node/TransitNodeImportForm.tsx index 7e6d9b7a..63e8f1c3 100644 --- a/packages/transition-frontend/src/components/forms/node/TransitNodeImportForm.tsx +++ b/packages/transition-frontend/src/components/forms/node/TransitNodeImportForm.tsx @@ -24,11 +24,10 @@ const NodesImportForm: React.FunctionComponent = (props: Path if (!props.path.isNew()) { serviceLocator.eventManager.emit('progress', { name: 'DeletingPath', progress: 0.0 }); await props.path.delete(serviceLocator.socketEventManager); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.eventManager.emit('progress', { name: 'DeletingPath', progress: 1.0 }); } @@ -133,10 +134,12 @@ const TransitPathButton: React.FunctionComponent = (props: Path await reversedPath.save(serviceLocator.socketEventManager); serviceLocator.collectionManager.refresh('paths'); props.line.refreshPaths(); - serviceLocator.eventManager.emit( + (serviceLocator.eventManager as EventManager).emitEvent( 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() + { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + } ); serviceLocator.selectedObjectsManager.update('line', props.line); serviceLocator.collectionManager.refresh('lines'); @@ -161,11 +164,10 @@ const TransitPathButton: React.FunctionComponent = (props: Path await duplicatePath.save(serviceLocator.socketEventManager); serviceLocator.collectionManager.refresh('paths'); props.line.refreshPaths(); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPaths', - serviceLocator.collectionManager.get('paths').toGeojson() - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.selectedObjectsManager.update('line', props.line); serviceLocator.collectionManager.refresh('lines'); } catch (error) { diff --git a/packages/transition-frontend/src/components/forms/path/TransitPathEdit.tsx b/packages/transition-frontend/src/components/forms/path/TransitPathEdit.tsx index 2f7b4bc7..7f25ec3c 100644 --- a/packages/transition-frontend/src/components/forms/path/TransitPathEdit.tsx +++ b/packages/transition-frontend/src/components/forms/path/TransitPathEdit.tsx @@ -38,6 +38,8 @@ import { parseIntOrNull, parseFloatOrNull } from 'chaire-lib-common/lib/utils/Ma import Path, { pathDirectionArray } from 'transition-common/lib/services/path/Path'; import Line from 'transition-common/lib/services/line/Line'; import { NodeAttributes } from 'transition-common/lib/services/nodes/Node'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; const lineModesConfigByMode = {}; for (let i = 0, countI = lineModesConfig.length; i < countI; i++) { @@ -223,18 +225,16 @@ class TransitPathEdit extends SaveableObjectForm) => { if (this.props.path.isFrozen()) { - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPathWaypointsSelected', - turfFeatureCollection([]) - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([]) + }); return true; } - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPathWaypointsSelected', - turfFeatureCollection([waypointGeojson]) - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([waypointGeojson]) + }); this.setState((oldState) => { return { waypointDraggingAfterNodeIndex: waypointGeojson.properties.afterNodeIndex, @@ -245,28 +245,25 @@ class TransitPathEdit extends SaveableObjectForm { if (this.props.path.isFrozen()) { - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPathWaypointsSelected', - turfFeatureCollection([]) - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([]) + }); return true; } const waypointGeojson = turfPoint(coordinates); - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPathWaypointsSelected', - turfFeatureCollection([waypointGeojson]) - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([waypointGeojson]) + }); }; onReplaceWaypointByNodeId = async (nodeId: string, waypointType = 'engine') => { if (this.props.path.isFrozen()) { - serviceLocator.eventManager.emit( - 'map.updateLayer', - 'transitPathWaypointsSelected', - turfFeatureCollection([]) - ); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([]) + }); return true; } await this.props.path.replaceWaypointByNodeId( @@ -277,12 +274,18 @@ class TransitPathEdit extends SaveableObjectForm('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([]) + }); serviceLocator.eventManager.emit('selected.updateLayers.path'); }; onUpdateWaypoint = (coordinates: [number, number], waypointType = null) => { - serviceLocator.eventManager.emit('map.updateLayer', 'transitPathWaypointsSelected', turfFeatureCollection([])); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPathWaypointsSelected', + data: turfFeatureCollection([]) + }); if (!this.props.path.isFrozen()) { this.props.path .updateWaypoint( diff --git a/packages/transition-frontend/src/components/forms/path/TransitPathImportForm.tsx b/packages/transition-frontend/src/components/forms/path/TransitPathImportForm.tsx index 6dd45131..3054d846 100644 --- a/packages/transition-frontend/src/components/forms/path/TransitPathImportForm.tsx +++ b/packages/transition-frontend/src/components/forms/path/TransitPathImportForm.tsx @@ -9,6 +9,8 @@ import { withTranslation, WithTranslation } from 'react-i18next'; import serviceLocator from 'chaire-lib-common/lib/utils/ServiceLocator'; import FileImportForm from '../../parts/FileImportForm'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface PathImportFormProps { setImporterSelected: (importerSelected: boolean) => void; @@ -32,11 +34,10 @@ const PathsImportForm: React.FunctionComponent('map.updateLayer', { + layerName: 'transitPaths', + data: serviceLocator.collectionManager.get('paths').toGeojson() + }); serviceLocator.eventManager.emit('progress', { name: 'Importing', progress: 1.0 }); closeImporter(); }; diff --git a/packages/transition-frontend/src/components/forms/scenario/TransitScenarioEdit.tsx b/packages/transition-frontend/src/components/forms/scenario/TransitScenarioEdit.tsx index 5a2e59d9..c06b5d19 100644 --- a/packages/transition-frontend/src/components/forms/scenario/TransitScenarioEdit.tsx +++ b/packages/transition-frontend/src/components/forms/scenario/TransitScenarioEdit.tsx @@ -26,6 +26,8 @@ import TransitServiceFilterableList from '../service/TransitServiceFilterableLis import * as Status from 'chaire-lib-common/lib/utils/Status'; import ScenarioLinesDetail from './TransitScenarioLinesDetail'; import { PathAttributes } from 'transition-common/lib/services/path/Path'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; interface ScenarioFormProps extends WithTranslation { scenario: Scenario; @@ -62,13 +64,21 @@ class TransitScenarioEdit extends SaveableObjectForm( + 'map.updateLayer', + { + layerName: 'transitPathsForServices', + data: paths + } + ); this.setState({ paths }); } catch (_error) { - serviceLocator.eventManager.emit( + (serviceLocator.eventManager as EventManager).emitEvent( 'map.updateLayer', - 'transitPathsForServices', - turfFeatureCollection([]) + { + layerName: 'transitPathsForServices', + data: turfFeatureCollection([]) + } ); this.setState({ paths: turfFeatureCollection([]) }); } @@ -81,7 +91,10 @@ class TransitScenarioEdit extends SaveableObjectForm('map.updateLayer', { + layerName: 'transitPathsForServices', + data: turfFeatureCollection([]) + }); } protected onValueChange(path: string, newValue: { value: any; valid?: boolean } = { value: null, valid: true }) { diff --git a/packages/transition-frontend/src/components/forms/transitRouting/RoutingResultComponent.tsx b/packages/transition-frontend/src/components/forms/transitRouting/RoutingResultComponent.tsx index 6a817fb2..88d46417 100644 --- a/packages/transition-frontend/src/components/forms/transitRouting/RoutingResultComponent.tsx +++ b/packages/transition-frontend/src/components/forms/transitRouting/RoutingResultComponent.tsx @@ -20,6 +20,8 @@ import serviceLocator from 'chaire-lib-common/lib/utils/ServiceLocator'; import { TransitRoutingResult } from 'transition-common/lib/services/transitRouting/TransitRoutingResult'; import { default as FormErrors } from 'chaire-lib-frontend/lib/components/pageParts/FormErrors'; import { TransitRoutingAttributes } from 'transition-common/lib/services/transitRouting/TransitRouting'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; export interface RoutingResultStatus { routingResult: UnimodalRouteCalculationResult | TransitRoutingResult; alternativeIndex: number; @@ -33,7 +35,10 @@ export interface TransitRoutingResultsProps extends WithTranslation { const showCurrentAlternative = async (result, alternativeIndex) => { const pathGeojson = await result.getPathGeojson(alternativeIndex, {}); - serviceLocator.eventManager.emit('map.updateLayer', 'routingPoints', result.originDestinationToGeojson()); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'routingPoints', + data: result.originDestinationToGeojson() + }); serviceLocator.eventManager.emit('map.updateLayers', { routingPaths: pathGeojson, routingPathsStrokes: pathGeojson diff --git a/packages/transition-frontend/src/components/forms/transitRouting/TransitRoutingForm.tsx b/packages/transition-frontend/src/components/forms/transitRouting/TransitRoutingForm.tsx index 34b09ce3..73d39264 100644 --- a/packages/transition-frontend/src/components/forms/transitRouting/TransitRoutingForm.tsx +++ b/packages/transition-frontend/src/components/forms/transitRouting/TransitRoutingForm.tsx @@ -38,6 +38,8 @@ import TransitRoutingBaseComponent from './widgets/TransitRoutingBaseComponent'; import ODCoordinatesComponent from './widgets/ODCoordinatesComponent'; import TimeOfTripComponent from './widgets/TimeOfTripComponent'; import { RoutingOrTransitMode } from 'chaire-lib-common/lib/config/routingModes'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; export interface TransitRoutingFormProps extends WithTranslation { // TODO tahini batch routing @@ -90,11 +92,10 @@ class TransitRoutingForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'routingPoints', + data: this.state.object.originDestinationToGeojson() + }); } } @@ -174,7 +175,10 @@ class TransitRoutingForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'routingPoints', + data: routing.originDestinationToGeojson() + }); this.setState({ currentResult: results, @@ -202,7 +206,10 @@ class TransitRoutingForm extends ChangeEventsForm('map.updateLayer', { + layerName: 'routingPoints', + data: routing.originDestinationToGeojson() + }); if (routing.hasOrigin() && routing.hasDestination() && shouldCalculate) { this.calculateRouting(true); } diff --git a/packages/transition-frontend/src/components/forms/transitRouting/widgets/ODCoordinatesComponent.tsx b/packages/transition-frontend/src/components/forms/transitRouting/widgets/ODCoordinatesComponent.tsx index 86f88d51..3743dd56 100644 --- a/packages/transition-frontend/src/components/forms/transitRouting/widgets/ODCoordinatesComponent.tsx +++ b/packages/transition-frontend/src/components/forms/transitRouting/widgets/ODCoordinatesComponent.tsx @@ -13,6 +13,8 @@ import serviceLocator from 'chaire-lib-common/lib/utils/ServiceLocator'; import InputWrapper from 'chaire-lib-frontend/lib/components/input/InputWrapper'; import InputStringFormatted from 'chaire-lib-frontend/lib/components/input/InputStringFormatted'; import Button from 'chaire-lib-frontend/lib/components/input/Button'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; export interface ODCoordinatesComponentProps extends WithTranslation { originGeojson?: GeoJSON.Feature; @@ -160,7 +162,10 @@ class ODCoordinatesComponent extends React.Component('map.updateLayer', { + layerName: 'routingPoints', + data: this.originDestinationToGeojson() + }); } else { this.setState({ destinationLat: coordinates[1], @@ -168,7 +173,10 @@ class ODCoordinatesComponent extends React.Component('map.updateLayer', { + layerName: 'routingPoints', + data: this.originDestinationToGeojson() + }); } }; diff --git a/packages/transition-frontend/src/components/map/TransitionMainMap.tsx b/packages/transition-frontend/src/components/map/TransitionMainMap.tsx index 136d0170..a31a8e10 100644 --- a/packages/transition-frontend/src/components/map/TransitionMainMap.tsx +++ b/packages/transition-frontend/src/components/map/TransitionMainMap.tsx @@ -29,6 +29,8 @@ import { featureCollection as turfFeatureCollection } from '@turf/turf'; import { LayoutSectionProps } from 'chaire-lib-frontend/lib/services/dashboard/DashboardContribution'; import { MapEventHandlerDescription } from 'chaire-lib-frontend/lib/services/map/IMapEventHandler'; import { deleteUnusedNodes } from '../../services/transitNodes/transitNodesUtils'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; MapboxGL.accessToken = process.env.MAPBOX_ACCESS_TOKEN || ''; @@ -231,7 +233,10 @@ class MainMap extends React.Component { mapCustomEvents.addEvents(serviceLocator.eventManager); elementResizedEvent(this.mapContainer, this.onResizeContainer); serviceLocator.eventManager.on('map.updateEnabledLayers', this.updateEnabledLayers); - serviceLocator.eventManager.on('map.updateLayer', this.updateLayer); + (serviceLocator.eventManager as EventManager).onEvent( + 'map.updateLayer', + this.updateLayer + ); serviceLocator.eventManager.on('map.updateLayers', this.updateLayers); serviceLocator.eventManager.on('map.addPopup', this.addPopup); serviceLocator.eventManager.on('map.removePopup', this.removePopup); @@ -481,8 +486,11 @@ class MainMap extends React.Component { this.popupManager.removeAllPopups(); }; - updateLayer = (layerName: string, geojson: GeoJSON.FeatureCollection) => { - this.layerManager.updateLayer(layerName, geojson); + updateLayer = (args: { + layerName: string; + data: GeoJSON.FeatureCollection | ((original: GeoJSON.FeatureCollection) => GeoJSON.FeatureCollection); + }) => { + this.layerManager.updateLayer(args.layerName, args.data); }; updateLayers = (geojsonByLayerName) => { diff --git a/packages/transition-frontend/src/services/dashboard/LayersAndCollectionsService.ts b/packages/transition-frontend/src/services/dashboard/LayersAndCollectionsService.ts index c43a3888..fc1e04c9 100644 --- a/packages/transition-frontend/src/services/dashboard/LayersAndCollectionsService.ts +++ b/packages/transition-frontend/src/services/dashboard/LayersAndCollectionsService.ts @@ -13,6 +13,8 @@ import PlaceCollection from 'transition-common/lib/services/places/PlaceCollecti import ScenarioCollection from 'transition-common/lib/services/scenario/ScenarioCollection'; import ServiceCollection from 'transition-common/lib/services/service/ServiceCollection'; import SimulationCollection from 'transition-common/lib/services/simulation/SimulationCollection'; +import { EventManager } from 'chaire-lib-common/lib/services/events/EventManager'; +import { MapUpdateLayerEventType } from 'chaire-lib-frontend/lib/services/map/events/MapEventsCallbacks'; // TODO: Bring back the missing data types type LoadLayersOptions = { @@ -56,11 +58,17 @@ export const loadLayersAndCollections = async ({ serviceLocator.collectionManager.add('simulations', simulationCollection); await nodeCollection.loadFromServer(serviceLocator.socketEventManager); - serviceLocator.eventManager.emit('map.updateLayer', 'transitNodes', nodeCollection.toGeojson()); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitNodes', + data: nodeCollection.toGeojson() + }); serviceLocator.collectionManager.add('nodes', nodeCollection); await pathCollection.loadFromServer(serviceLocator.socketEventManager); - serviceLocator.eventManager.emit('map.updateLayer', 'transitPaths', pathCollection.toGeojson()); + (serviceLocator.eventManager as EventManager).emitEvent('map.updateLayer', { + layerName: 'transitPaths', + data: pathCollection.toGeojson() + }); serviceLocator.collectionManager.add('paths', pathCollection); await lineCollection.loadFromServer(serviceLocator.socketEventManager, serviceLocator.collectionManager);