Skip to content

Commit

Permalink
Register the icons in a single Map object
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Jul 22, 2024
1 parent 125c3e8 commit 763115c
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 64 deletions.
59 changes: 14 additions & 45 deletions packages/base/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,11 @@ import {
import { JupyterFrontEnd } from '@jupyterlab/application';
import { showErrorMessage, WidgetTracker } from '@jupyterlab/apputils';
import { ITranslator } from '@jupyterlab/translation';
import { redoIcon, undoIcon } from '@jupyterlab/ui-components';

import { CommandIDs, icons } from './constants';
import { LayerBrowserWidget } from './dialogs/layerBrowserDialog';
import { CreationFormDialog } from './dialogs/formdialog';
import { JupyterGISWidget } from './widget';
import { geoJSONIcon } from './icons';

/**
* The command IDs.
*/
export namespace CommandIDs {
export const createNew = 'jupytergis:create-new-jGIS-file';
export const redo = 'jupytergis:redo';
export const undo = 'jupytergis:undo';

// Layers and sources commands
export const openLayerBrowser = 'jupytergis:openLayerBrowser';
export const newGeoJSONLayer = 'jupytergis:newGeoJSONLayer';
export const newVectorTileLayer = 'jupytergis:newVectorTileLayer';

// Sources only commands
export const newGeoJSONSource = 'jupytergis:newGeoJSONSource';
export const removeSource = 'jupytergis:removeSource';
export const renameSource = 'jupytergis:renameSource';

// Layers only commands
export const newVectorLayer = 'jupytergis:newVectorLayer';

export const renameLayer = 'jupytergis:renameLayer';
export const removeLayer = 'jupytergis:removeLayer';
export const renameGroup = 'jupytergis:renameGroup';
export const removeGroup = 'jupytergis:removeGroup';

export const moveLayersToGroup = 'jupytergis:moveLayersToGroup';
export const moveLayerToNewGroup = 'jupytergis:moveLayerToNewGroup';
}

/**
* Add the commands to the application's command registry.
Expand Down Expand Up @@ -73,7 +42,7 @@ export function addCommands(
return current.context.model.sharedModel.redo();
}
},
icon: redoIcon
...icons.get(CommandIDs.redo)?.icon
});

commands.addCommand(CommandIDs.undo, {
Expand All @@ -90,7 +59,7 @@ export function addCommands(
return current.context.model.sharedModel.undo();
}
},
icon: undoIcon
...icons.get(CommandIDs.undo)
});

/**
Expand All @@ -103,23 +72,23 @@ export function addCommands(
? tracker.currentWidget.context.model.sharedModel.editable
: false;
},
iconClass: 'fa fa-book-open',
execute: Private.createLayerBrowser(
tracker,
layerBrowserRegistry,
formSchemaRegistry
)
),
...icons.get(CommandIDs.openLayerBrowser)
});

commands.addCommand(CommandIDs.newGeoJSONLayer, {
label: trans.__('New vector layer'),
label: trans.__('New geoJSON layer'),
isEnabled: () => {
return tracker.currentWidget
? tracker.currentWidget.context.model.sharedModel.editable
: false;
},
icon: geoJSONIcon,
execute: Private.createGeoJSONLayer(tracker, formSchemaRegistry)
execute: Private.createGeoJSONLayer(tracker, formSchemaRegistry),
...icons.get(CommandIDs.newGeoJSONLayer)
});

commands.addCommand(CommandIDs.newVectorTileLayer, {
Expand All @@ -129,8 +98,8 @@ export function addCommands(
? tracker.currentWidget.context.model.sharedModel.editable
: false;
},
iconClass: 'fa fa-vector-square',
execute: Private.createVectorTileLayer(tracker, formSchemaRegistry)
execute: Private.createVectorTileLayer(tracker, formSchemaRegistry),
...icons.get(CommandIDs.newVectorTileLayer)
});

/**
Expand All @@ -146,8 +115,8 @@ export function addCommands(
? tracker.currentWidget.context.model.sharedModel.editable
: false;
},
icon: geoJSONIcon,
execute: Private.createGeoJSONSource(tracker, formSchemaRegistry)
execute: Private.createGeoJSONSource(tracker, formSchemaRegistry),
...icons.get(CommandIDs.newGeoJSONSource)?.icon
});

commands.addCommand(CommandIDs.removeSource, {
Expand Down Expand Up @@ -314,8 +283,8 @@ export function addCommands(
? tracker.currentWidget.context.model.sharedModel.editable
: false;
},
iconClass: 'fa fa-vector-square',
execute: Private.createVectorLayer(tracker, formSchemaRegistry)
execute: Private.createVectorLayer(tracker, formSchemaRegistry),
...icons.get(CommandIDs.newVectorLayer)
});
}

Expand Down
58 changes: 58 additions & 0 deletions packages/base/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { LabIcon, redoIcon, undoIcon } from '@jupyterlab/ui-components';
import { geoJSONIcon, rasterIcon } from './icons';

/**
* The command IDs.
*/
export namespace CommandIDs {
export const createNew = 'jupytergis:create-new-jGIS-file';
export const redo = 'jupytergis:redo';
export const undo = 'jupytergis:undo';

// Layers and sources commands
export const openLayerBrowser = 'jupytergis:openLayerBrowser';
export const newGeoJSONLayer = 'jupytergis:newGeoJSONLayer';
export const newVectorTileLayer = 'jupytergis:newVectorTileLayer';

// Sources only commands
export const newGeoJSONSource = 'jupytergis:newGeoJSONSource';
export const removeSource = 'jupytergis:removeSource';
export const renameSource = 'jupytergis:renameSource';

// Layers only commands
export const newVectorLayer = 'jupytergis:newVectorLayer';

export const renameLayer = 'jupytergis:renameLayer';
export const removeLayer = 'jupytergis:removeLayer';
export const renameGroup = 'jupytergis:renameGroup';
export const removeGroup = 'jupytergis:removeGroup';

export const moveLayersToGroup = 'jupytergis:moveLayersToGroup';
export const moveLayerToNewGroup = 'jupytergis:moveLayerToNewGroup';
}

interface IRegisteredIcon {
icon?: LabIcon;
iconClass?: string;
}

const iconObject = {
RasterSource: { icon: rasterIcon },
GeoJSONSource: { icon: geoJSONIcon },
VectorTileSource: { iconClass: 'fa fa-vector-square' },
RasterLayer: { icon: rasterIcon },
[CommandIDs.redo]: { icon: redoIcon },
[CommandIDs.undo]: { icon: undoIcon },
[CommandIDs.openLayerBrowser]: { iconClass: 'fa fa-book-open' },
[CommandIDs.newGeoJSONLayer]: { icon: geoJSONIcon },
[CommandIDs.newVectorTileLayer]: { iconClass: 'fa fa-vector-square' },
[CommandIDs.newGeoJSONSource]: { icon: geoJSONIcon },
[CommandIDs.newVectorLayer]: { iconClass: 'fa fa-vector-square' }
};

/**
* The registered icons
*/
export const icons = new Map<string, IRegisteredIcon>(
Object.entries(iconObject)
);
1 change: 1 addition & 0 deletions packages/base/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './commands';
export * from './constants';
export * from './dialogs/formdialog';
export * from './mainview';
export * from './tools';
Expand Down
7 changes: 4 additions & 3 deletions packages/base/src/panelview/components/layers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {
} from '@jupyterlab/ui-components';
import { Panel } from '@lumino/widgets';
import React, { MouseEvent, useEffect, useState } from 'react';
import { nonVisibilityIcon, rasterIcon, visibilityIcon } from '../../icons';
import { icons } from '../../constants';
import { nonVisibilityIcon, visibilityIcon } from '../../icons';
import { IControlPanelModel } from '../../types';

const LAYERS_PANEL_CLASS = 'jp-gis-layerPanel';
Expand Down Expand Up @@ -368,9 +369,9 @@ function LayerComponent(props: ILayerProps): JSX.Element {
onClick={setSelection}
onContextMenu={setSelection}
>
{layer.type === 'RasterLayer' && (
{icons.has(layer.type) && (
<LabIcon.resolveReact
icon={rasterIcon}
{...icons.get(layer.type)}
className={LAYER_ICON_CLASS}
/>
)}
Expand Down
18 changes: 3 additions & 15 deletions packages/base/src/panelview/components/sources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { DOMUtils } from '@jupyterlab/apputils';
import { LabIcon, ReactWidget } from '@jupyterlab/ui-components';
import { Panel } from '@lumino/widgets';
import React, { MouseEvent, useEffect, useState } from 'react';
import { geoJSONIcon, rasterIcon } from '../../icons';
import { icons } from '../../constants';
import { IControlPanelModel } from '../../types';

const SOURCES_PANEL_CLASS = 'jp-gis-sourcePanel';
Expand Down Expand Up @@ -294,21 +294,9 @@ function SourceComponent(props: ISourceProps): JSX.Element {
onClick={setSelection}
onContextMenu={setSelection}
>
{source.type === 'RasterSource' && (
{icons.has(source.type) && (
<LabIcon.resolveReact
icon={rasterIcon}
className={SOURCE_ICON_CLASS}
/>
)}
{source.type === 'GeoJSONSource' && (
<LabIcon.resolveReact
icon={geoJSONIcon}
className={SOURCE_ICON_CLASS}
/>
)}
{source.type === 'VectorTileSource' && (
<LabIcon.resolveReact
iconClass={'fa fa-vector-square'}
{...icons.get(source.type)}
className={SOURCE_ICON_CLASS}
/>
)}
Expand Down
2 changes: 1 addition & 1 deletion packages/base/src/toolbar/widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { CommandRegistry } from '@lumino/commands';
import { Widget } from '@lumino/widgets';

import * as React from 'react';
import { CommandIDs } from '../commands';
import { CommandIDs } from '../constants';
import { UsersItem } from './usertoolbaritem';

export const TOOLBAR_SEPARATOR_CLASS = 'jGIS-Toolbar-Separator';
Expand Down

0 comments on commit 763115c

Please sign in to comment.