Skip to content

Commit

Permalink
Merge pull request Canadian-Geospatial-Platform#1712 from Alex-NRCan/…
Browse files Browse the repository at this point in the history
…feat-corepackages

[FIX] plugins simplification and usage of app-bar for plugins (Canadian-Geospatial-Platform#1712)
  • Loading branch information
jolevesq authored Jan 23, 2024
2 parents 91fc6c7 + 72b0ea2 commit c32e3a9
Show file tree
Hide file tree
Showing 26 changed files with 259 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
},
"theme": "dark",
"components": ["north-arrow", "overview-map"],
"corePackages": ["basemap-panel"],
"corePackages": [],
"appBarTabs": {
"tabs": {
"core": ["basemap-panel"]
}
},
"suportedLanguages": ["en"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
"labeled": true
}
},
"corePackages": ["basemap-panel"],
"corePackages": [],
"appBarTabs": {
"tabs": {
"core": ["basemap-panel"]
}
},
"components": ["overview-map", "north-arrow"],
"theme": "dark",
"suportedLanguages": ["en", "fr"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@
"labeled": true
}
},
"corePackages": ["basemap-panel"],
"corePackages": [],
"appBarTabs": {
"tabs": {
"core": ["basemap-panel"]
}
},
"components": ["north-arrow", "overview-map"],
"theme": "dark",
"suportedLanguages": ["en", "fr"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@
]
}
},
"corePackages": [
"geochart"
],
"corePackages": [],
"suportedLanguages": [
"en",
"fr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,7 @@
"core": ["legend", "layers", "details", "geochart"]
}
},
"corePackages": [
"geochart"
],
"corePackages": [],
"suportedLanguages": [
"en",
"fr"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
]
},
"theme": "dark",
"corePackages": ["geochart"],
"corePackages": [],
"footerTabs": {
"tabs": {
"core": ["geochart"]
}
},
"suportedLanguages": ["en", "fr"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@
]
},
"theme": "light",
"corePackages": ["geochart"],
"corePackages": [],
"footerTabs": {
"tabs": {
"core": ["geochart"]
}
},
"suportedLanguages": ["en", "fr"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
},
"theme": "light",
"components": ["north-arrow", "overview-map"],
"appBarTabs": {
"tabs": {
"core": ["basemap-panel"]
}
},
"corePackages": ["swiper"],
"externalPackages": [],
"suportedLanguages": ["en"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"core": ["legend", "layers", "details", "data-table", "time-slider"]
}
},
"corePackages": ["time-slider"],
"corePackages": [],
"externalPackages": [],
"suportedLanguages": ["en"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
"core": ["legend", "layers", "details", "time-slider"]
}
},
"corePackages": ["time-slider"],
"corePackages": [],
"externalPackages": [],
"suportedLanguages": ["en"]
}
1 change: 0 additions & 1 deletion packages/geoview-core/public/configs/raw-data-table.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@
"core": ["legend", "layers", "details", "data-table"]
}
},
"corePackages": [],
"externalPackages": [],
"suportedLanguages": [
"fr",
Expand Down
7 changes: 6 additions & 1 deletion packages/geoview-core/public/configs/sample-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,12 @@
},
"theme": "dark",
"components": ["north-arrow", "overview-map"],
"corePackages": ["basemap-panel"],
"corePackages": [],
"appBarTabs": {
"tabs": {
"core": ["basemap-panel"]
}
},
"externalPackages": [],
"serviceUrls": {
"keys": "https://geocore.api.geo.ca"
Expand Down
60 changes: 60 additions & 0 deletions packages/geoview-core/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -1576,6 +1576,63 @@
"tabs"
]
},
"TypeAppBarTabsProps": {
"type": "object",
"additionalProperties": false,
"properties": {
"tabs": {
"type": "object",
"description": "Available tabs",
"properties": {
"core": {
"type": "array",
"items": {
"type": "string",
"enum": [
"basemap-panel",
"layers",
"geochart"
]
},
"minItems": 1,
"default": [],
"uniqueItems": true,
"description": "Default core tabs of app-bar to use"
},
"custom": {
"type": "array",
"items": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"defaultTabs": {
"type": "string"
},
"contentHTML": {
"type": "string"
}
}
},
"minItems": 0,
"default": [],
"uniqueItems": true,
"description": "Custom tabs of app-bar to use"
}
},
"additionalProperties": false
},
"collapsed": {
"type": "boolean",
"default": false,
"description": "State of app-bar when map is loaded (expanded or collapsed)"
}
},
"required": [
"tabs"
]
},
"TypeOverviewMapProps": {
"type": "object",
"additionalProperties": false,
Expand Down Expand Up @@ -1696,6 +1753,9 @@
"footerTabs": {
"$ref": "#/definitions/TypeFooterTabsProps"
},
"appBarTabs": {
"$ref": "#/definitions/TypeAppBarTabsProps"
},
"overviewMap": {
"$ref": "#/definitions/TypeOverviewMapProps"
},
Expand Down
6 changes: 4 additions & 2 deletions packages/geoview-core/src/api/event-processors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function initializeEventProcessors(store: GeoviewStoreType) {
mapEventProcessor.onInitialize(store);

// package stores, only create if needed
if (store.getState().mapConfig!.corePackages?.includes('time-slider')) timeSliderEventProcessor.onInitialize(store);
// TODO: Change this check for something more generic that checks in appBarTabs too
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('time-slider')) timeSliderEventProcessor.onInitialize(store);
}

export function destroyEventProcessors(store: GeoviewStoreType) {
Expand All @@ -30,5 +31,6 @@ export function destroyEventProcessors(store: GeoviewStoreType) {
mapEventProcessor.onDestroy(store);

// package stores, only destroy if created
if (store.getState().mapConfig!.corePackages?.includes('time-slider')) timeSliderEventProcessor.onDestroy(store);
// TODO: Change this check for something more generic that checks in appBarTabs too
if (store.getState().mapConfig!.footerTabs?.tabs.core.includes('time-slider')) timeSliderEventProcessor.onDestroy(store);
}
75 changes: 4 additions & 71 deletions packages/geoview-core/src/api/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ import { whenThisThen, showError } from '@/core/utils/utilities';
import { api } from '@/app';
import { AbstractPlugin } from './abstract-plugin';
import { TypePluginStructure } from './plugin-types';
import { toJsonObject, TypeJsonObject, TypeJsonValue } from '@/core/types/global-types';
import { UIEventProcessor } from '../event-processors/event-processor-children/ui-event-processor';
import { logger } from '@/core/utils/logger';
import { TypeJsonObject, TypeJsonValue } from '@/core/types/global-types';

/**
* Class to manage plugins
Expand Down Expand Up @@ -208,16 +206,9 @@ export class Plugin {
* @param {string} mapId the map id to remove the plugin from
*/
removePlugin = (pluginId: string, mapId: string): void => {
if (mapId) {
if (!api.maps?.[mapId]?.plugins?.[pluginId]) {
const plugin = api.maps[mapId].plugins[pluginId];

// call the removed function on the plugin
if (typeof plugin.removed === 'function') plugin.removed();
}

delete api.maps[mapId].plugins[pluginId];
}
// Get the plugin and remove it
api.maps[mapId]?.plugins[pluginId]?.removed?.();
delete api.maps[mapId].plugins[pluginId];
};

/**
Expand All @@ -239,62 +230,4 @@ export class Plugin {
}
}
};

/**
* A function that will load each plugin on a map then checks if there are a next plugin to load
*
* @param {string} mapIndex the map index to load the plugin at
* @param {string} pluginIndex the plugin index to load
*/
loadPlugin = (mapIndex: number, pluginIndex: number) => {
const mapId = Object.keys(api.maps)[mapIndex];

// check if the map at this index have core packages and if there is a package at the plugin index
const corePackages = UIEventProcessor.getCorePackageComponents(mapId);
if (corePackages[pluginIndex]) {
const pluginId = corePackages[pluginIndex];

// load the plugin from the script tag or create it
this.loadScript(pluginId).then((constructor) => {
// add the plugin by passing in the loaded constructor from the script tag
this.addPlugin(
pluginId,
mapId,
constructor,
toJsonObject({
mapId,
})
);

// check if there is a next plugin at the current map index
if (corePackages[pluginIndex + 1]) {
// load next plugin at the same map index
this.loadPlugin(mapIndex, pluginIndex + 1);
// if no more plugins at current map index then check if there is another map
} else if (Object.keys(api.maps)[mapIndex + 1]) {
// try to load first plugin at the next map
this.loadPlugin(mapIndex + 1, 0);
}
});
// if previous map did not have any packages then try to load packages from next map
} else if (Object.keys(api.maps)[mapIndex + 1]) {
// load packages at next map if exists
this.loadPlugin(mapIndex + 1, 0);
// if no plugins loaded then call init callback function
}
};

/**
* Load plugins provided by map config
*/
loadPlugins = (): void => {
// Log
logger.logTraceCore('loadPlugins');

// check if a map exists
if (Object.keys(api.maps)[0]) {
// start loading core packages on first map and first package
this.loadPlugin(0, 0);
}
};
}
31 changes: 30 additions & 1 deletion packages/geoview-core/src/core/components/app-bar/app-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { useState, useRef, useEffect, useCallback, Fragment } from 'react';
import { useTheme } from '@mui/material/styles';
import { Box, List, ListItem, Panel, IconButton } from '@/ui';

import { api, useGeoViewMapId } from '@/app';
import { AbstractPlugin, TypeJsonObject, TypeJsonValue, api, toJsonObject, useGeoViewMapId } from '@/app';
import { EVENT_NAMES } from '@/api/events/event-types';

import { payloadIsAButtonPanel, ButtonPanelPayload, PayloadBaseClass } from '@/api/events/payloads';
Expand All @@ -16,6 +16,7 @@ import Version from './buttons/version';
import { getSxClasses } from './app-bar-style';
import { useUIActiveFocusItem, useUIAppbarComponents } from '@/core/stores/store-interface-and-intial-values/ui-state';
import { useMapInteraction } from '@/core/stores/store-interface-and-intial-values/map-state';
import { useGeoViewConfig } from '@/core/stores/geoview-store';
import { logger } from '@/core/utils/logger';

/**
Expand All @@ -39,6 +40,9 @@ export function Appbar(): JSX.Element {

const appBarPanelCloseListenerFunction = () => setSelectedAppbarButtonId('');

// get store config for app bar tabs to add (similar logic as in footer-tabs)
const appBarTabsConfig = useGeoViewConfig()?.appBarTabs;

// #region REACT HOOKS
const addButtonPanel = useCallback(
(payload: ButtonPanelPayload) => {
Expand Down Expand Up @@ -97,6 +101,31 @@ export function Appbar(): JSX.Element {
}, [addButtonPanel, mapId, removeButtonPanel, selectedAppBarButtonId]);
// #endregion

/**
* Create default tabs from configuration parameters (similar logic as in footer-tabs).
*/
useEffect(() => {
// Log
logger.logTraceUseEffect('app-bar.appBarTabsConfig');

// Packages tab
if (appBarTabsConfig && appBarTabsConfig.tabs.core.includes('basemap-panel')) {
// create a new tab by loading the plugin
api.plugin
.loadScript('basemap-panel')
.then((constructor: AbstractPlugin | ((pluginId: string, props: TypeJsonObject) => TypeJsonValue)) => {
api.plugin.addPlugin(
'basemap-panel',
mapId,
constructor,
toJsonObject({
mapId,
})
);
});
}
}, [appBarTabsConfig, mapId]);

return (
<Box sx={sxClasses.appBar} ref={appBar}>
<Box sx={sxClasses.appBarButtons}>
Expand Down
Loading

0 comments on commit c32e3a9

Please sign in to comment.