Skip to content

Commit

Permalink
feat: add Event DRAWSTOP (improve ux)
Browse files Browse the repository at this point in the history
Avant à l'ajout d'une géométrie si on annulait ("cancel") l'ajout de la
géométrie alors on revenait sur une map sans aucune layer (la layer
d'origine n'était plus présente)
L'ajout de l'event DRAWSTOP permet de gérer ce cas de figure et
améliorer l'UX

Reviewed-by: andriacap
  • Loading branch information
andriacap committed Nov 3, 2023
1 parent 6872f9a commit c614d51
Showing 1 changed file with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ L.Icon.Default.mergeOptions(CustomIcon);
export class LeafletDrawComponent implements OnInit, OnChanges {
public map: Map;
private _currentDraw: any;
private _currentGeojson: any;
private _Le: any;
public drawnItems: any;
// save the current layer type because the edite event do not send it...
Expand Down Expand Up @@ -80,6 +81,8 @@ export class LeafletDrawComponent implements OnInit, OnChanges {
}

this.map.on(this._Le.Draw.Event.DRAWSTART, (e) => {
this._currentGeojson =
this._currentGeojson == null ? { geometry: this.geojson } : this._currentGeojson;
this.mapservice.removeAllLayers(this.map, this.mapservice.fileLayerFeatureGroup);
if (this.map.getZoom() < this.zoomLevel) {
this._commonService.translateToaster('warning', 'Map.ZoomWarning');
Expand Down Expand Up @@ -110,6 +113,7 @@ export class LeafletDrawComponent implements OnInit, OnChanges {

// on draw layer created
this.map.on(this._Le.Draw.Event.CREATED, (e) => {
this.mapservice.removeAllLayers(this.map, this.mapservice.leafletDrawFeatureGroup);
if (this.map.getZoom() < this.zoomLevel) {
this._commonService.translateToaster('warning', 'Map.ZoomWarning');
this.layerDrawed.emit({ geojson: null });
Expand All @@ -120,6 +124,7 @@ export class LeafletDrawComponent implements OnInit, OnChanges {
this.mapservice.leafletDrawFeatureGroup.addLayer(this._currentDraw);
const geojson = this.getGeojsonFromFeatureGroup(this.currentLayerType);
this.mapservice.setGeojsonCoord(geojson);
this._currentGeojson = geojson;
this.layerDrawed.emit(geojson);
}
});
Expand All @@ -128,6 +133,7 @@ export class LeafletDrawComponent implements OnInit, OnChanges {
this.mapservice.map.on(this._Le.Draw.Event.EDITED, (e) => {
const geojson = this.getGeojsonFromFeatureGroup(this.currentLayerType);
this.mapservice.setGeojsonCoord(geojson);
this._currentGeojson = geojson;
this.layerDrawed.emit(geojson);
});

Expand All @@ -143,6 +149,30 @@ export class LeafletDrawComponent implements OnInit, OnChanges {
this.mapservice.setGeojsonCoord(geojson);
}
});

this.map.on(this._Le.Draw.Event.DRAWSTOP, (e) => {
if (this._currentDraw) {
if (
!this.mapservice.leafletDrawFeatureGroup.hasLayer(this._currentDraw) &&
this._currentGeojson
) {
this.loadDrawfromGeoJson(this._currentGeojson.geometry);
}
return;
} else if (this._currentGeojson) {
this.mapservice.removeAllLayers(this.map, this.mapservice.fileLayerFeatureGroup);
const layer: L.Layer = this.mapservice.createGeojson(this._currentGeojson.geometry, false);
if (!this.mapservice.leafletDrawFeatureGroup.hasLayer(layer)) {
this.loadDrawfromGeoJson(this._currentGeojson.geometry);
}
} else {
this.mapservice.removeAllLayers(this.map, this.mapservice.leafletDrawFeatureGroup);
const layer: L.Layer = this.mapservice.createGeojson(this.geojson, false);
if (!this.mapservice.leafletDrawFeatureGroup.hasLayer(layer)) {
this.loadDrawfromGeoJson(this.geojson);
}
}
});
}

getGeojsonFromFeatureGroup(layerType) {
Expand Down Expand Up @@ -208,6 +238,7 @@ export class LeafletDrawComponent implements OnInit, OnChanges {
return;
}
this.mapservice.leafletDrawFeatureGroup.clearLayers();
this._currentGeojson = null;
this.drawControl.remove();
}

Expand Down

0 comments on commit c614d51

Please sign in to comment.