Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix basemaps order #7170

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Add support for Cloud Optimised Geotiff (cog) in Cesium mode. Currently supports EPSG 4326 and 3857. There is experimental support for other projections but performance might suffer and there could be other issues.
- Fix `Workbench.collapseAll()` and `Workbench.expandAll()` for References.
- Add to the "doZoomTo" function the case of an imagery layer with imageryProvider.rectangle
- Fix basemaps order
- [The next improvement]

#### 8.7.7 - 2024-10-01
Expand Down
19 changes: 8 additions & 11 deletions lib/Models/BaseMaps/BaseMapsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,17 @@ export class BaseMapsModel extends CreateModel(BaseMapsTraits) {
get baseMapItems(): BaseMapItem[] {
const enabledBaseMaps: BaseMapItem[] = [];

this.items.forEach((baseMapItem) => {
if (
baseMapItem.item &&
!ModelReference.isRemoved(baseMapItem.item) &&
(!this.enabledBaseMaps ||
this.enabledBaseMaps.includes(baseMapItem.item))
) {
const itemModel = this.terria.getModelById(BaseModel, baseMapItem.item);
if (MappableMixin.isMixedInto(itemModel))
this.enabledBaseMaps.forEach((baseMapItem) => {
Copy link
Contributor

@zoran995 zoran995 Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.enabledBaseMaps might be undefined, and when undefined it should show all base maps available.
i.e. simple sorting before return in the old code would do the trick

// Sort based on enabledBaseMaps order
if (this.enabledBaseMaps) {
  enabledBaseMaps.sort((a, b) => {
    const aIndex = this.enabledBaseMaps!.indexOf(a.item.uniqueId!);
    const bIndex = this.enabledBaseMaps!.indexOf(b.item.uniqueId!);
    return aIndex - bIndex;
  });
}

another option that comes to my mind is to go with the utility function and do mapping over proper array

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zoran995 what do you think about

    const baseMapList =
      this.enabledBaseMaps ?? this.items.map((item) => item.item);
    baseMapList.forEach((baseMapItem) => {
      const item = this.items.find((item) => item.item === baseMapItem);
      if (item && !ModelReference.isRemoved(baseMapItem)) {
        const itemModel = this.terria.getModelById(BaseModel, baseMapItem);
        if (MappableMixin.isMixedInto(itemModel)) {
          enabledBaseMaps.push({
            image: item.image,
            contrastColor: item.contrastColor,
            item: itemModel
          });
        }
      }
    });

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on first look, it should work correctly and the rest is a personal preference on the code structure. It would be great to cover sorting with the unit test to avoid regression bugs in future, there are tests for BaseMapsModel

https://github.com/TerriaJS/terriajs/blob/2490260c78e4b7666496bfc6d0ec677eac63a98c/test/Models/BaseMaps/BaseMapsModelSpec.ts

const item = this.items.find((item) => item.item === baseMapItem);
if (item && !ModelReference.isRemoved(baseMapItem)) {
const itemModel = this.terria.getModelById(BaseModel, baseMapItem);
if (MappableMixin.isMixedInto(itemModel)) {
enabledBaseMaps.push({
image: baseMapItem.image,
contrastColor: baseMapItem.contrastColor,
image: item.image,
contrastColor: item.contrastColor,
item: itemModel
});
}
}
});

Expand Down