From 8b215b640d40d10396c5e804ce96b5e9f8eb3310 Mon Sep 17 00:00:00 2001 From: timelyportfolio Date: Mon, 31 Jul 2017 13:20:33 -0500 Subject: [PATCH 1/2] proof of concept for https://github.com/bhaskarvk/leaflet.extras/issues/96 --- inst/htmlwidgets/lib/draw/draw-bindings.js | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/inst/htmlwidgets/lib/draw/draw-bindings.js b/inst/htmlwidgets/lib/draw/draw-bindings.js index 0f497cf7..bdd7d48c 100644 --- a/inst/htmlwidgets/lib/draw/draw-bindings.js +++ b/inst/htmlwidgets/lib/draw/draw-bindings.js @@ -89,7 +89,6 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti } var layer = e.layer; - editableFeatureGroup.addLayer(layer); // assign a unique key to the newly created feature var featureId = L.stamp(layer); @@ -106,6 +105,15 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti if(typeof layer.getRadius === 'function') { layer.feature.properties.radius = layer.getRadius(); } +debugger; + // add the newly drawn shape/feature to layerManager + // so that can be used from R side methods + map.layerManager.addLayer( + layer, + null, //category + String(L.stamp(layer)), //layerId use stamp + targetGroup //group which is targetGroup + ); if (!HTMLWidgets.shinyMode) return; @@ -174,6 +182,16 @@ LeafletWidget.methods.addDrawToolbar = function(targetLayerId, targetGroup, opti if(typeof layer.getRadius === 'function') { layer.feature.properties.radius = layer.getRadius(); } + + + // remove the deleted shape/feature from layerManager + // for proper R side methods + // layerManager only supports removal with category and layerId + // as far as I can tell + map.layerManager.removeLayer( + "null", //category + String(L.stamp(layer)) // layerId - we used L.stamp + ); }); if (!HTMLWidgets.shinyMode) return; From 3220e17fdd2d97be205401ebc0d63bcdd935240c Mon Sep 17 00:00:00 2001 From: timelyportfolio Date: Mon, 31 Jul 2017 14:15:52 -0500 Subject: [PATCH 2/2] try to find existing category to remove deleted non-Leaflet.draw features --- inst/htmlwidgets/lib/draw/draw-bindings.js | 23 +++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/inst/htmlwidgets/lib/draw/draw-bindings.js b/inst/htmlwidgets/lib/draw/draw-bindings.js index bdd7d48c..8300ccfc 100644 --- a/inst/htmlwidgets/lib/draw/draw-bindings.js +++ b/inst/htmlwidgets/lib/draw/draw-bindings.js @@ -111,7 +111,7 @@ debugger; map.layerManager.addLayer( layer, null, //category - String(L.stamp(layer)), //layerId use stamp + layer.options.layerId || String(L.stamp(layer)), //layerId or if no layerId use stamp targetGroup //group which is targetGroup ); @@ -188,9 +188,26 @@ debugger; // for proper R side methods // layerManager only supports removal with category and layerId // as far as I can tell + + // if feature not from Leaflet.draw then need to find category + // probably should make this a separate testable function + var category = null; + // layerManager does not expose category getter for us + // so have to rely on internal _byCategory + if(Object.keys(map.layerManager._byCategory)) { + // surely there is a better way + Object.keys(map.layerManager._byCategory).forEach(function(cat) { + if( + Object.keys(map.layerManager._byCategory[cat]).indexOf(String(L.stamp(layer))) > -1 + ) { + category = cat; + } + }); + } + map.layerManager.removeLayer( - "null", //category - String(L.stamp(layer)) // layerId - we used L.stamp + category || "null", //category + layer.options.layerId || String(L.stamp(layer)) // layerId - we used L.stamp ); });