-
Notifications
You must be signed in to change notification settings - Fork 32
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
[Datahub] Added dynamic legend generation based on map context #1069
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export * from './lib/components/map-container/map-container.component' | ||
export * from './lib/components/map-container/map-settings.token' | ||
export * from './lib/components/feature-detail/feature-detail.component' | ||
export * from './lib/components/map-legend/map-legend.component' | ||
export * from './lib/map-utils' |
5 changes: 5 additions & 0 deletions
5
libs/ui/map/src/lib/components/map-legend/map-legend.component.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
.geosdk--legend-container { | ||
overflow: auto; | ||
ronitjadhav marked this conversation as resolved.
Show resolved
Hide resolved
|
||
white-space: normal; | ||
word-wrap: break-word; | ||
} |
1 change: 1 addition & 0 deletions
1
libs/ui/map/src/lib/components/map-legend/map-legend.component.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<div *ngIf="legendHTML" [innerHTML]="legendHTML.outerHTML"></div> |
150 changes: 150 additions & 0 deletions
150
libs/ui/map/src/lib/components/map-legend/map-legend.component.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import { ComponentFixture, TestBed } from '@angular/core/testing' | ||
import { MapLegendComponent } from './map-legend.component' | ||
import { MapContext } from '@geospatial-sdk/core' | ||
import { createLegendFromLayer } from '@geospatial-sdk/legend' | ||
|
||
jest.mock('@geospatial-sdk/legend', () => ({ | ||
createLegendFromLayer: jest.fn(), | ||
})) | ||
|
||
describe('MapLegendComponent', () => { | ||
let component: MapLegendComponent | ||
let fixture: ComponentFixture<MapLegendComponent> | ||
|
||
beforeEach(async () => { | ||
await TestBed.configureTestingModule({ | ||
imports: [MapLegendComponent], | ||
}).compileComponents() | ||
}) | ||
|
||
beforeEach(() => { | ||
fixture = TestBed.createComponent(MapLegendComponent) | ||
component = fixture.componentInstance | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('should create', () => { | ||
expect(component).toBeTruthy() | ||
}) | ||
|
||
describe('Change of map-context', () => { | ||
it('should create legend on first change', async () => { | ||
const mockContext: MapContext = { | ||
layers: [ | ||
{ | ||
id: 'test-layer', | ||
}, | ||
], | ||
} as MapContext | ||
|
||
const mockLegendElement = document.createElement('div') | ||
;(createLegendFromLayer as jest.Mock).mockResolvedValue(mockLegendElement) | ||
|
||
const legendStatusChangeSpy = jest.spyOn( | ||
component.legendStatusChange, | ||
'emit' | ||
) | ||
|
||
await component.ngOnChanges({ | ||
context: { | ||
currentValue: mockContext, | ||
previousValue: null, | ||
firstChange: true, | ||
isFirstChange: () => true, | ||
}, | ||
}) | ||
|
||
expect(createLegendFromLayer).toHaveBeenCalledWith(mockContext.layers[0]) | ||
expect(component.legendHTML).toBe(mockLegendElement) | ||
expect(legendStatusChangeSpy).toHaveBeenCalledWith(true) | ||
}) | ||
|
||
it('should create legend and emit status on subsequent context changes', async () => { | ||
const mockContext: MapContext = { | ||
layers: [ | ||
{ | ||
id: 'test-layer', | ||
}, | ||
], | ||
} as MapContext | ||
|
||
const mockLegendElement = document.createElement('div') | ||
;(createLegendFromLayer as jest.Mock).mockResolvedValue(mockLegendElement) | ||
|
||
const legendStatusChangeSpy = jest.spyOn( | ||
component.legendStatusChange, | ||
'emit' | ||
) | ||
|
||
await component.ngOnChanges({ | ||
context: { | ||
currentValue: mockContext, | ||
previousValue: {}, | ||
firstChange: false, | ||
isFirstChange: () => false, | ||
}, | ||
}) | ||
|
||
expect(createLegendFromLayer).toHaveBeenCalledWith(mockContext.layers[0]) | ||
expect(component.legendHTML).toBe(mockLegendElement) | ||
expect(legendStatusChangeSpy).toHaveBeenCalledWith(true) | ||
}) | ||
|
||
it('should emit nothing when no legend is created', async () => { | ||
const mockContext: MapContext = { | ||
layers: [ | ||
{ | ||
id: 'test-layer', | ||
}, | ||
], | ||
} as MapContext | ||
|
||
;(createLegendFromLayer as jest.Mock).mockResolvedValue(false) | ||
|
||
const legendStatusChangeSpy = jest.spyOn( | ||
component.legendStatusChange, | ||
'emit' | ||
) | ||
|
||
await component.ngOnChanges({ | ||
context: { | ||
currentValue: mockContext, | ||
previousValue: {}, | ||
firstChange: false, | ||
isFirstChange: () => false, | ||
}, | ||
}) | ||
|
||
expect(createLegendFromLayer).toHaveBeenCalledWith(mockContext.layers[0]) | ||
expect(component.legendHTML).toBe(false) | ||
expect(legendStatusChangeSpy).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('should handle multiple layers', async () => { | ||
const mockContext: MapContext = { | ||
layers: [{ id: 'layer-1' }, { id: 'layer-2' }], | ||
} as MapContext | ||
|
||
const mockLegendElement = document.createElement('div') | ||
;(createLegendFromLayer as jest.Mock).mockResolvedValue(mockLegendElement) | ||
|
||
const legendStatusChangeSpy = jest.spyOn( | ||
component.legendStatusChange, | ||
'emit' | ||
) | ||
|
||
await component.ngOnChanges({ | ||
context: { | ||
currentValue: mockContext, | ||
previousValue: {}, | ||
firstChange: false, | ||
isFirstChange: () => false, | ||
}, | ||
}) | ||
|
||
expect(createLegendFromLayer).toHaveBeenCalledWith(mockContext.layers[0]) | ||
expect(component.legendHTML).toBe(mockLegendElement) | ||
expect(legendStatusChangeSpy).toHaveBeenCalledWith(true) | ||
}) | ||
}) | ||
}) |
39 changes: 39 additions & 0 deletions
39
libs/ui/map/src/lib/components/map-legend/map-legend.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
Component, | ||
EventEmitter, | ||
Input, | ||
OnChanges, | ||
Output, | ||
SimpleChanges, | ||
ViewEncapsulation, | ||
} from '@angular/core' | ||
import { MapContext } from '@geospatial-sdk/core' | ||
import { createLegendFromLayer } from '@geospatial-sdk/legend' | ||
import { NgIf } from '@angular/common' | ||
|
||
@Component({ | ||
selector: 'gn-ui-map-legend', | ||
templateUrl: './map-legend.component.html', | ||
standalone: true, | ||
styleUrls: ['./map-legend.component.css'], | ||
encapsulation: ViewEncapsulation.None, | ||
imports: [NgIf], | ||
}) | ||
export class MapLegendComponent implements OnChanges { | ||
@Input() context: MapContext | null | ||
@Output() legendStatusChange = new EventEmitter<boolean>() | ||
legendHTML: HTMLElement | false | ||
|
||
async ngOnChanges(changes: SimpleChanges) { | ||
if ('context' in changes) { | ||
const mapContext = changes['context'].currentValue | ||
if (mapContext.layers && mapContext.layers.length > 0) { | ||
const mapContextLayer = mapContext.layers[0] | ||
this.legendHTML = await createLegendFromLayer(mapContextLayer) | ||
if (this.legendHTML) { | ||
this.legendStatusChange.emit(true) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test is failing because this adds another subscription to
mapContext$
, which triggers the extent computation one more time; in the tests we only resolve the extent computation promise once, so the other does not complete and as such one of the subscriptions doesn't receive the full computed context.Long story short, I think a good fix is to add this:
withLatestFrom(this.mdViewFacade.metadata$), map(([context, metadata]) => { if (context.view) return context const extent = this.mapUtils.getRecordExtent(metadata) const view = extent ? { extent } : null return { ...context, view, } }), + shareReplay(1) // line 172 )
Then we won't do unnecessary extent computations :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanksa a lot :)