Skip to content

Commit

Permalink
refactor(layer-manager): Move remove added layers to the dialog, use …
Browse files Browse the repository at this point in the history
…eventBus for interaction with draw
  • Loading branch information
FilipLeitner authored and jmacura committed Jun 20, 2024
1 parent eebfddf commit a9740af
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import {HsDialogComponent} from 'hslayers-ng/common/dialogs';
import {HsDialogContainerService} from 'hslayers-ng/common/dialogs';
import {HsEventBusService} from 'hslayers-ng/shared/event-bus';
import {HsLayerManagerService} from 'hslayers-ng/shared/layer-manager';
import {HsMapService} from 'hslayers-ng/shared/map';
import {Layer} from 'ol/layer';
import {Source} from 'ol/source';
import {
getBase,
getRemovable,
getShowInLayerManager,
} from 'hslayers-ng/common/extensions';

@Component({
selector: 'hs-layermanager-remove-all-dialog',
templateUrl: './remove-all-dialog.component.html',
Expand All @@ -19,10 +28,36 @@ export class HsLayerManagerRemoveAllDialogComponent
public HsDialogContainerService: HsDialogContainerService,
public HsEventBusService: HsEventBusService,
public hsCompositionsParserService: HsCompositionsParserService,
private hsMapService: HsMapService,
) {}

/**
* Remove all non-base layers that were added to the map by user.
* Doesn't remove layers added through app config (In case we want it to be 'removable', it can be set to true in the config.)
*/
removeAllLayers(reloadComposition?: boolean): void {
this.HsLayerManagerService.removeAllLayers();
const to_be_removed = [];
this.hsMapService
.getMap()
.getLayers()
.forEach((lyr: Layer<Source>) => {
if (getRemovable(lyr) == true) {
if (!getBase(lyr)) {
if (
getShowInLayerManager(lyr) == undefined ||
getShowInLayerManager(lyr) == true
) {
to_be_removed.push(lyr);
}
}
}
});
while (to_be_removed.length > 0) {
this.hsMapService.getMap().removeLayer(to_be_removed.shift());
}

this.HsEventBusService.addedLayersRemoved.next();

if (reloadComposition) {
this.HsEventBusService.compositionLoadStarts.next(
this.data.composition_id,
Expand Down
7 changes: 5 additions & 2 deletions projects/hslayers/shared/draw/draw.service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Injectable, NgZone} from '@angular/core';
import {lastValueFrom} from 'rxjs';
import {lastValueFrom, merge} from 'rxjs';

import {Circle} from 'ol/geom';
import {Cluster, Source, Vector as VectorSource} from 'ol/source';
Expand Down Expand Up @@ -126,7 +126,10 @@ export class HsDrawService extends HsDrawServiceParams {
this.pendingLayers = pendingLayers;
});

this.hsEventBusService.mapResets.subscribe(() => {
merge(
this.hsEventBusService.addedLayersRemoved,
this.hsEventBusService.mapResets,
).subscribe(() => {
this.addedLayersRemoved = true;
this.fillDrawableLayers();
});
Expand Down
4 changes: 4 additions & 0 deletions projects/hslayers/shared/event-bus/event-bus.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export class HsEventBusService {
compositionLoads: Subject<any> = new Subject();
compositionEdits: Subject<void> = new Subject();
layerRemovals: Subject<Layer<Source>> = new Subject();
/**
* Fired when non-base layers that were added to the map by user were removed
*/
addedLayersRemoved: Subject<void> = new Subject();
/**
* Fires when new layer is added to the app.
* Suppressed for layers defined in default_layers in HsConfig.
Expand Down
38 changes: 3 additions & 35 deletions projects/hslayers/shared/layer-manager/layer-manager.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import {HsAddDataOwsService} from 'hslayers-ng/shared/add-data';
import {HsBaseLayerDescriptor} from 'hslayers-ng/types';
import {HsConfig} from 'hslayers-ng/config';
import {HsDimensionTimeService} from 'hslayers-ng/shared/get-capabilities';
import {HsDrawService} from 'hslayers-ng/shared/draw';
import {HsEventBusService} from 'hslayers-ng/shared/event-bus';
import {HsLanguageService} from 'hslayers-ng/shared/language';
import {HsLayerDescriptor, HsLayerLoadProgress} from 'hslayers-ng/types';
Expand Down Expand Up @@ -49,7 +48,6 @@ import {
getName,
getPath,
getQueryCapabilities,
getRemovable,
getShowInLayerManager,
getSubLayers,
getThumbnail,
Expand Down Expand Up @@ -126,7 +124,6 @@ export class HsLayerManagerService {
constructor(
public hsConfig: HsConfig,
public hsDimensionTimeService: HsDimensionTimeService,
public hsDrawService: HsDrawService,
public hsEventBusService: HsEventBusService,
public hsLanguageService: HsLanguageService,
public hsLayerEditorVectorLayerService: HsLayerEditorVectorLayerService,
Expand Down Expand Up @@ -234,7 +231,6 @@ export class HsLayerManagerService {
* Layers also get automatic watcher for changing visibility (to synchronize visibility in map and layer manager).
* Position is calculated for each layer and for time layers time properties are created.
* Each layer is also inserted in correct layer list and inserted into folder structure.
* @private
* @param e - Event object emitted by OL add layer event
* @param suspendEvents - If set to true, no new values for layerAdditions, layerManagerUpdates or compositionEdits observables will be emitted.
*/
Expand Down Expand Up @@ -271,7 +267,9 @@ export class HsLayerManagerService {
abstract: getAbstract(layer),
layer,
grayed:
!this.hsLayerManagerVisibilityService.isLayerInResolutionInterval(layer),
!this.hsLayerManagerVisibilityService.isLayerInResolutionInterval(
layer,
),
visible: layer.getVisible(),
showInLayerManager,
uid: this.hsUtilsService.generateUuid(),
Expand Down Expand Up @@ -600,36 +598,6 @@ export class HsLayerManagerService {
}
}

/**
* Remove all non-base layers that were added to the map by user.
* Doesn't remove layers added through app config (In case we want it to be 'removable', it can be set to true in the config.)
* (PRIVATE)
* @private
*/
removeAllLayers(): void {
const to_be_removed = [];
this.hsMapService
.getMap()
.getLayers()
.forEach((lyr: Layer<Source>) => {
if (getRemovable(lyr) == true) {
if (getBase(lyr) == undefined || getBase(lyr) == false) {
if (
getShowInLayerManager(lyr) == undefined ||
getShowInLayerManager(lyr) == true
) {
to_be_removed.push(lyr);
}
}
}
});
while (to_be_removed.length > 0) {
this.hsMapService.getMap().removeLayer(to_be_removed.shift());
}
this.hsDrawService.addedLayersRemoved = true;
this.hsDrawService.fillDrawableLayers();
}

/**
* Create events for checking if layer is being loaded or is loaded for ol.layer.Image or ol.layer.Tile
* @param layer - Layer which is being added
Expand Down

0 comments on commit a9740af

Please sign in to comment.