From 2fedd394f5139c845ba7f0e8983a4fe96214a2d0 Mon Sep 17 00:00:00 2001 From: boyan Date: Sat, 26 Apr 2025 05:02:59 +0200 Subject: [PATCH 1/3] delete custom controll on toolbar --- leaflet-geoman.d.ts | 3 +++ src/js/Toolbar/L.PM.Toolbar.js | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/leaflet-geoman.d.ts b/leaflet-geoman.d.ts index 554978ca..6d266ee4 100644 --- a/leaflet-geoman.d.ts +++ b/leaflet-geoman.d.ts @@ -1087,6 +1087,9 @@ declare module 'leaflet' { /** Disable button by control name */ setButtonDisabled(name: TOOLBAR_CONTROL_ORDER, state: boolean): void; + + /** Removes a custom Control from the Toolbar */ + deleteControl(name: string): void; } type KEYBOARD_EVENT_TYPE = 'current' | 'keydown' | 'keyup'; diff --git a/src/js/Toolbar/L.PM.Toolbar.js b/src/js/Toolbar/L.PM.Toolbar.js index 24dfad32..ca45006b 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -151,6 +151,13 @@ const Toolbar = L.Class.extend({ this.isVisible = false; }, + deleteControl(name) { + const btnName = this._btnNameMapping(name); + if (this.buttons[btnName]) { + this.buttons[btnName].remove(); + delete this.buttons[btnName]; + } + }, toggleControls(options = this.options) { if (this.isVisible) { this.removeControls(); From ef814448a90c61b6167c6ee97ddbefccdd1f530d Mon Sep 17 00:00:00 2001 From: Florian Bischof Date: Sun, 27 Apr 2025 08:23:25 +0200 Subject: [PATCH 2/3] Update leaflet-geoman.d.ts --- leaflet-geoman.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/leaflet-geoman.d.ts b/leaflet-geoman.d.ts index 6d266ee4..d43950dc 100644 --- a/leaflet-geoman.d.ts +++ b/leaflet-geoman.d.ts @@ -1088,7 +1088,7 @@ declare module 'leaflet' { /** Disable button by control name */ setButtonDisabled(name: TOOLBAR_CONTROL_ORDER, state: boolean): void; - /** Removes a custom Control from the Toolbar */ + /** Deletes and removes a Control from the Toolbar */ deleteControl(name: string): void; } From 0b1099fa84ab2233f568f65abba97f3bcdf766f3 Mon Sep 17 00:00:00 2001 From: Florian Bischof Date: Sun, 27 Apr 2025 08:44:56 +0200 Subject: [PATCH 3/3] Add test --- cypress/e2e/toolbar.cy.js | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/cypress/e2e/toolbar.cy.js b/cypress/e2e/toolbar.cy.js index 047da511..9a907034 100644 --- a/cypress/e2e/toolbar.cy.js +++ b/cypress/e2e/toolbar.cy.js @@ -525,4 +525,46 @@ describe('Testing the Toolbar', () => { done(); }); }); + + it('Deletes custom control and adds a new one with the same name', () => { + const clickSpy = cy.spy(); + const clickSpyNew = cy.spy(); + + cy.window().then(({ map }) => { + map.pm.Toolbar.createCustomControl({ + name: 'alertBox', + onClick: clickSpy, + toggle: false, + block: 'custom', + }); + + cy.toolbarButtonContainer('alertBox', map).then((container) => { + container[0].children[0].click(); // button + }); + }); + + cy.window().then(({ map }) => { + // expect needs to be in the this block, otherwise it will be executed before the click event + expect(clickSpy.callCount).to.be.eq(1); + + map.pm.Toolbar.deleteControl('alertBox'); + + map.pm.Toolbar.createCustomControl({ + name: 'alertBox', + onClick: clickSpyNew, + toggle: false, + block: 'custom', + }); + + cy.toolbarButtonContainer('alertBox', map).then((container) => { + container[0].children[0].click(); // button + }); + }); + + cy.window().then(() => { + // expect needs to be in the this block, otherwise it will be executed before the click event + expect(clickSpy.callCount).to.be.eq(1); + expect(clickSpyNew.callCount).to.be.eq(1); + }); + }); });