Skip to content

Commit

Permalink
order the source by name
Browse files Browse the repository at this point in the history
  • Loading branch information
brichet committed Jul 19, 2024
1 parent 9f03229 commit 79e20cd
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 26 deletions.
53 changes: 29 additions & 24 deletions packages/base/src/panelview/components/sources.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
IJGISSources,
IJupyterGISClientState,
IJupyterGISModel,
ISelection,
Expand Down Expand Up @@ -48,7 +47,7 @@ export class SourcesPanel extends Panel {
constructor(options: SourcesPanel.IOptions) {
super();
this._model = options.model;
this.id = 'jupytergis::sourcePanel';
this.id = 'jupytergis::sourcesPanel';
this.addClass(SOURCES_PANEL_CLASS);

this.addWidget(
Expand Down Expand Up @@ -147,8 +146,8 @@ function SourcesBodyComponent(props: IBodyProps): JSX.Element {
const [model, setModel] = useState<IJupyterGISModel | undefined>(
props.model?.jGISModel
);
const [sources, setSources] = useState<IJGISSources>(
model?.getSources() || {}
const [sourceIds, setSourceIds] = useState<string[]>(
Private.sortedSourceIds(model)
);

/**
Expand All @@ -168,11 +167,12 @@ function SourcesBodyComponent(props: IBodyProps): JSX.Element {
*/
useEffect(() => {
const updateSources = () => {
setSources(model?.getSources() || {});
setSourceIds(Private.sortedSourceIds(model));
};
model?.sharedModel.sourcesChanged.connect(updateSources);
model?.clientStateChanged.connect(updateSources);

updateSources();
return () => {
model?.sharedModel.sourcesChanged.disconnect(updateSources);
model?.clientStateChanged.disconnect(updateSources);
Expand All @@ -184,18 +184,20 @@ function SourcesBodyComponent(props: IBodyProps): JSX.Element {
*/
props.model?.documentChanged.connect((_, widget) => {
setModel(widget?.context.model);
setSources(widget?.context.model?.getSources() || {});
});


return (
<div id="jp-gis-sources">
{Object.keys(sources).map(sourceId => (
<SourceComponent
gisModel={model}
sourceId={sourceId}
onClick={onItemClick}
/>
))}
{sourceIds.map(sourceId => {
return (
<SourceComponent
gisModel={model}
sourceId={sourceId}
onClick={onItemClick}
/>
);
})}
</div>
);
}
Expand Down Expand Up @@ -292,17 +294,20 @@ function SourceComponent(props: ISourceProps): JSX.Element {
{name}
</span>
</div>
{/* <Button
title={source.visible ? 'Hide layer' : 'Show layer'}
onClick={toggleVisibility}
minimal
>
<LabIcon.resolveReact
icon={source.visible ? visibilityIcon : nonVisibilityIcon}
className={SOURCE_ICON_CLASS}
tag="span"
/>
</Button> */}
</div>
);
}

namespace Private {
export function sortedSourceIds(model: IJupyterGISModel | undefined): string[] {
const sources = model?.getSources();
if (sources === undefined) {
return [];
}
return Object.keys(sources).sort((id1: string, id2: string): number => {
const name1 = sources[id1].name.toLowerCase();
const name2 = sources[id2].name.toLowerCase();
return name1 < name2 ? -1 : name1 > name2 ? 1 : 0;
});
}
}
6 changes: 4 additions & 2 deletions packages/base/src/panelview/leftpanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ export class LeftPanelWidget extends SidePanel {
const header = new ControlPanelHeader();
this.header.addWidget(header);

const datasources = new SourcesPanel({ model: this._model });
this.addWidget(datasources);
const sourcesPanel = new SourcesPanel({ model: this._model });
sourcesPanel.title.caption = 'Sources';
sourcesPanel.title.label = 'Sources';
this.addWidget(sourcesPanel);

const layerTree = new LayersPanel({ model: this._model });
layerTree.title.caption = 'Layer tree';
Expand Down

0 comments on commit 79e20cd

Please sign in to comment.