Skip to content

Commit

Permalink
refactor(store): Refactor north arrow to use the store and remove api…
Browse files Browse the repository at this point in the history
… from footer element

Closes #1329
  • Loading branch information
Johann Levesque committed Oct 6, 2023
1 parent 3434747 commit fe12955
Show file tree
Hide file tree
Showing 33 changed files with 657 additions and 709 deletions.
4 changes: 2 additions & 2 deletions packages/geoview-core/public/templates/default-config.html
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ <h4 id="HLCONF4">4. Load layers with bad config values</h4>
}
]
},
'components': ['overview-map', 'nav-bar'],
'components': ['overview-map'],
'corePackages': [],
'theme': 'dark',
'suportedLanguages': ['en']
Expand Down Expand Up @@ -268,7 +268,7 @@ <h4 id="HLCONF7">7. Load config from function call</h4>
]
}]
},
'components': ['app-bar', 'overview-map', 'nav-bar'],
'components': ['overview-map', 'nav-bar'],
'corePackages': [],
'theme': 'dark',
'suportedLanguages': ['en']
Expand Down
4 changes: 2 additions & 2 deletions packages/geoview-core/public/templates/projections.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h4 id="HPRJ1">1. Web Mercator Projection</h4>
'labeled': true
}
},
'components': ['overview-map'],
'components': ['overview-map', 'north-arrow'],
'corePackages': [],
'theme': 'dark',
'suportedLanguages': ['en']
Expand Down Expand Up @@ -103,7 +103,7 @@ <h4 id="HPRJ2">2. LCC Projection</h4>
'labeled': true
}
},
'components': ['overview-map'],
'components': ['overview-map', 'north-arrow'],
'corePackages': [],
'theme': 'dark',
'suportedLanguages': ['en']
Expand Down
11 changes: 8 additions & 3 deletions packages/geoview-core/src/api/eventProcessors/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import { GeoViewStoreType } from '@/core/stores/geoview-store';
import { AppBarEventProcessor } from './appBar-event-process';
import { MapEventProcessor } from './map-event-process';
import { AppBarEventProcessor } from '@/api/eventProcessors/appBar-event-process';
import { MapEventProcessor } from '@/api/eventProcessors/map-event-process';
import { NotificationEventProcessor } from '@/api/eventProcessors/notification-event-process';

const mapEventProcessor = new MapEventProcessor();
const appBarEventProcessor = new AppBarEventProcessor();
const mapEventProcessor = new MapEventProcessor();
const notificationEventProcessor = new NotificationEventProcessor();

export function initializeEventProcessors(store: GeoViewStoreType) {
mapEventProcessor.onInitialize(store);
appBarEventProcessor.onInitialize(store);
notificationEventProcessor.onInitialize(store);
}

export function destroyEventProcessors() {
mapEventProcessor.onDestroy();
appBarEventProcessor.onDestroy();
notificationEventProcessor.onDestroy();
}
45 changes: 43 additions & 2 deletions packages/geoview-core/src/api/eventProcessors/map-event-process.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { GeoViewStoreType } from '@/core/stores/geoview-store';
import { AbstractEventProcessor } from './abstract-event-processor';
import { api } from '@/app';
import { mapPayload, lngLatPayload, mapMouseEventPayload, numberPayload } from '@/api/events/payloads';
import {
mapPayload,
lngLatPayload,
mapMouseEventPayload,
numberPayload,
payloadIsAMapViewProjection,
PayloadBaseClass,
mapViewProjectionPayload,
} from '@/api/events/payloads';
import { EVENT_NAMES } from '@/api/events/event-types';

export class MapEventProcessor extends AbstractEventProcessor {
Expand Down Expand Up @@ -35,6 +43,17 @@ export class MapEventProcessor extends AbstractEventProcessor {
}
);

const unsubMapProjection = store.subscribe(
(state) => state.mapState.currentProjection,
(cur, prev) => {
// because emit and on from api events can be trigger in loop, compare also the api value
if (cur !== prev && api.maps[mapId].currentProjection !== cur!) {
api.maps[mapId].currentProjection = cur!;
api.event.emit(mapViewProjectionPayload(EVENT_NAMES.MAP.EVENT_MAP_VIEW_PROJECTION_CHANGE, mapId, cur!));
}
}
);

const unsubMapZoom = store.subscribe(
(state) => state.mapState.zoom,
(cur, prev) => {
Expand All @@ -55,7 +74,29 @@ export class MapEventProcessor extends AbstractEventProcessor {
}
);

// TODO: add a destroy events on store/map destroy
api.event.on(
EVENT_NAMES.MAP.EVENT_MAP_VIEW_PROJECTION_CHANGE,
(payload: PayloadBaseClass) => {
// because emit and on from api events can be trigger in loop, compare also the api value
if (payloadIsAMapViewProjection(payload) && api.maps[mapId].currentProjection !== payload.projection!) {
api.maps[mapId].currentProjection = payload.projection!;
store.setState({
mapState: { ...store.getState().mapState, currentProjection: payload.projection! },
});
}
},
mapId
);

// add to arr of subscriptions so it can be destroyed later
this.subscriptionArr.push(unsubMapLoaded, unsubMapCenterCoord, unsubMapPointerPosition, unsubMapZoom, unsubMapSingleClick);
this.subscriptionArr.push(
unsubMapLoaded,
unsubMapCenterCoord,
unsubMapPointerPosition,
unsubMapProjection,
unsubMapZoom,
unsubMapSingleClick
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { GeoViewStoreType } from '@/core/stores/geoview-store';
import { AbstractEventProcessor } from './abstract-event-processor';
import { PayloadBaseClass, payloadIsANotification } from '@/api/events/payloads';
import { EVENT_NAMES } from '@/api/events/event-types';
import { api, generateId } from '@/app';

export class NotificationEventProcessor extends AbstractEventProcessor {
onInitialize(store: GeoViewStoreType) {
const { mapId } = store.getState();

// TODO: add a destroy events on store/map destroy
// when the add notification is triggered, add it to the strore notifications array
api.event.on(
EVENT_NAMES.NOTIFICATIONS.NOTIFICATION_ADD,
(payload: PayloadBaseClass) => {
if (payloadIsANotification(payload)) {
store.setState((state) => ({
notificationState: {
notifications: [
...state.notificationState.notifications,
{ key: generateId(), notificationType: payload.notificationType, message: payload.message },
],
},
}));
}
},
mapId
);

// add to arr of subscriptions so it can be destroyed later
this.subscriptionArr.push();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,12 @@ import { EventStringId } from '../event-types';
*/

/** Valid keys for the NOTIFICATIONS */
export type NotificationsEventKey = 'NOTIFICATION_ADD' | 'NOTIFICATION_REMOVE';
export type NotificationsEventKey = 'NOTIFICATION_ADD';

/** Record that associates NOTIFICATIONS event keys to their event string id */
export const NOTIFICATIONS: Record<NotificationsEventKey, EventStringId> = {
/**
* Event triggered when a new notification has been added
*/
NOTIFICATION_ADD: 'notification/add',

/**
* Event triggered when a notification has been removed
*/
NOTIFICATION_REMOVE: 'notification/remove',
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { PayloadBaseClass } from './payload-base-class';
import { EventStringId, EVENT_NAMES } from '../event-types';

/** Valid events that can create NotificationPayload */
const validEvents: EventStringId[] = [EVENT_NAMES.NOTIFICATIONS.NOTIFICATION_ADD, EVENT_NAMES.NOTIFICATIONS.NOTIFICATION_REMOVE];
const validEvents: EventStringId[] = [EVENT_NAMES.NOTIFICATIONS.NOTIFICATION_ADD];

/**
* type guard function that redefines a PayloadBaseClass as a NotificationPayload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { TypeButtonPanel } from '@/ui/panel/panel-types';

import Export from './buttons/export';
import Geolocator from './buttons/geolocator';
import Notifications from './buttons/notifications';
import Notifications from '@/core/components/notifications/notifications';
import Version from './buttons/version';
import ExportModal from '../export/export-modal';

Expand Down

This file was deleted.

Loading

0 comments on commit fe12955

Please sign in to comment.