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

WIP - handle Leaflet.draw add/delete with R leaflet layerManger #98

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
37 changes: 36 additions & 1 deletion inst/htmlwidgets/lib/draw/draw-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
layer.options.layerId || String(L.stamp(layer)), //layerId or if no layerId use stamp
targetGroup //group which is targetGroup
);

if (!HTMLWidgets.shinyMode) return;

Expand Down Expand Up @@ -174,6 +182,33 @@ 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

// 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(
category || "null", //category
layer.options.layerId || String(L.stamp(layer)) // layerId - we used L.stamp
);
});

if (!HTMLWidgets.shinyMode) return;
Expand Down