geoman-io / leaflet-geoman

🍂🗺️ The most powerful leaflet plugin for drawing and editing geometry layers
https://geoman.io
MIT License
2.21k stars 433 forks source link

How to dinamically disable a button on the Toolbar #642

Closed fpuga closed 4 years ago

fpuga commented 4 years ago

Is there any recommend way to dinamically disable a button (or a mode) in the Toolbar.

I have an entity that can have or not a polygon geometry. This geometry will never be a multipolygon. If it has a geometry it is the only editable layer in the map.

I want all the available buttons visible in the map

map.pm.addControls({
    position: "topleft",
    drawMarker: false,
    drawCircleMarker: false,
    drawPolyline: false,
    drawRectangle: true,
    drawPolygon: true,
    drawCircle: true,
    editMode: true,
    dragMode: true,
    cutPolygon: false,
    removalMode: true,
});

But enable/disable while still visible, after user actions.

AFAIK, i can modify the css getting the buttons with

map.pm.Toolbar.getButtons().drawPolygon._button
// or 
map.pm.Toolbar.getButtons().drawPolygon.buttonsDomNode
// or
document.getElementsByClassName('leaflet-pm-draw')

but i'm not sure about the correct approach to handle the "click" action. Maybe it could be a new feature directly supported by Geoman.

Currently what i'm doing is something like


// disable buttons
document.getElementsByClassName('leaflet-pm-draw')[0].style.pointerEvents = 'none';
document.querySelectorAll('.leaflet-pm-draw a').forEach(a => a.classList.add('leaflet-disabled'))

//enable buttons
document.getElementsByClassName('leaflet-pm-draw')[0].style.pointerEvents = 'auto';
document.querySelectorAll('.leaflet-pm-draw a').forEach(a => a.classList.remove('leaflet-disabled'))
Falke-Design commented 4 years ago

@fpuga Thanks for reporting. This is already on my TODO list also I will close this Issue and add it to the feature requests where users can upvote this request.

Falke-Design commented 3 years ago

This is released with the Version 2.8.0. Call map.pm.Toolbar.setButtonDisabled(name, Boolean)