Leaflet.draw icon indicating copy to clipboard operation
Leaflet.draw copied to clipboard

Stop edit mode

Open paulomann opened this issue 9 years ago • 8 comments

How can I stop the edit mode manually? There is a way to do that? If there is, I did not found it.

Thanks!

paulomann avatar Mar 08 '16 16:03 paulomann

If you don't have the toolbar, you should be able to disable() the polygon.

            var polygonSelection = new L.FeatureGroup();
// Add to leaflet...
           var currentDrawer = new L.Draw.Rectangle(leaflet, shapeParameters);
// Create... edit...
            polygonSelection.clearLayers(); // Removes layers completely
            if (currentDrawer != null) {
               currentDrawer.disable();
                currentDrawer = null;
            }

ddproxy avatar Mar 08 '16 16:03 ddproxy

Sorry, maybe I did not explained well.

There is a situation like this :

  1. User click in the edit icon in the toolbar(i.e., he enters in the edit mode).
  2. My application pops up a modal.
  3. The user close the modal without selecting any operation.

In this situation I want to stop the edit mode when the user closes the modal without selecting any operation. So I have a toolbar and an icon to edit. But I want something like this :

drawControl.edit.stop();

paulomann avatar Mar 08 '16 16:03 paulomann

@Greenbald Would you happen to have a jsfiddle I could take a look at?

ddproxy avatar Mar 08 '16 17:03 ddproxy

Try this;

for (var toolbarId in map.drawControl._toolbars) {
    if (map.drawControl._toolbars.hasOwnProperty(toolbarId) && map.drawControl._toolbars[toolbarId] instanceof L.EditToolbar) {
        map.drawControl._toolbars[toolbarId].disable();
    }
 }

sstenn avatar Aug 08 '18 13:08 sstenn

Hi @sstenn , how do you access drawControl from map, I can't find it in the object.

Here is my config :

"leaflet": "^1.5.1", "leaflet-draw": "^1.0.3",

Here's my code :

var drawControls = new L.Control.Draw( { my draw options } );
map.addControl(drawControls);

I actually can find elements in the drawControls object directly but when I apply your code in mine, I get this error and I suppose it is because i'm not working with the map.

app.js:55974 Uncaught TypeError: Cannot read property 'disable' of undefined
    at NewClass.removeHooks (app.js:55974)
    at NewClass.disable (app.js:8014)
    at NewClass._disableLayerEdit (app.js:55975)
    at NewClass.eachLayer (app.js:9152)
    at NewClass.removeHooks (app.js:55975)
    at NewClass.disable (app.js:8014)
    at NewClass.disable (app.js:55975)
    at NewClass.removeToolbar (app.js:55975)
    at NewClass.removeToolbar (app.js:55975)
    at NewClass.onRemove (app.js:55975)

If I follow your solution, I can't find the drawControl object in my map object.

mho22 avatar May 19 '20 12:05 mho22

Hi @Itemshopp

Try to add the drawControl like this:

map.drawControl = new L.Control.Draw( { my draw options } )
map.addControl(map.drawControls);

sstenn avatar May 19 '20 13:05 sstenn

Thanks for your answer. It worked but the problem still persists.

The problem I get comes like this :

I draw a marker. I click on the edit button, I then click on another button that will call this function :

disableDrawControls(map)
    {
        Object.values(map.drawControls._toolbars.edit._modes).forEach( (mode) =>
        {
            if(mode.handler.enabled())
            {
                mode.handler.revertLayers();
                mode.handler.disable();
            }
        });

        map.removeControl(this.drawControls);
    }

I then get this error :

app.js:55974 Uncaught TypeError: Cannot read property 'disable' of undefined
    at NewClass.removeHooks (app.js:55974)
    at NewClass.disable (app.js:8014)
    at NewClass._disableLayerEdit (app.js:55975)
    at NewClass.eachLayer (app.js:9152)
    at NewClass.removeHooks (app.js:55975)
    at NewClass.disable (app.js:8014)
    at NewClass.disable (app.js:55975)
    at app.js:89531
    at Array.forEach (<anonymous>)
    at DrawManager.disableDrawControls (app.js:89526)

Et when I go deeper in the error, it says the marker.dragging.disable() function can't be called because the dragging option doesn't exist.

Is there any error in my code ?

mho22 avatar May 22 '20 11:05 mho22

It is linked with this PR Remove marker.dragging when not on the map

mho22 avatar May 29 '20 09:05 mho22