Skip to content
Merged
Show file tree
Hide file tree
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
42 changes: 42 additions & 0 deletions cypress/e2e/toolbar.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
3 changes: 3 additions & 0 deletions leaflet-geoman.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1087,6 +1087,9 @@ declare module 'leaflet' {

/** Disable button by control name */
setButtonDisabled(name: TOOLBAR_CONTROL_ORDER, state: boolean): void;

/** Deletes and removes a Control from the Toolbar */
deleteControl(name: string): void;
}

type KEYBOARD_EVENT_TYPE = 'current' | 'keydown' | 'keyup';
Expand Down
7 changes: 7 additions & 0 deletions src/js/Toolbar/L.PM.Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down