Skip to content

Commit 8952807

Browse files
Add deleteControl method (#1575)
* delete custom controll on toolbar * Update leaflet-geoman.d.ts * Add test --------- Co-authored-by: Florian Bischof <[email protected]>
1 parent e2d43de commit 8952807

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

cypress/e2e/toolbar.cy.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,46 @@ describe('Testing the Toolbar', () => {
525525
done();
526526
});
527527
});
528+
529+
it('Deletes custom control and adds a new one with the same name', () => {
530+
const clickSpy = cy.spy();
531+
const clickSpyNew = cy.spy();
532+
533+
cy.window().then(({ map }) => {
534+
map.pm.Toolbar.createCustomControl({
535+
name: 'alertBox',
536+
onClick: clickSpy,
537+
toggle: false,
538+
block: 'custom',
539+
});
540+
541+
cy.toolbarButtonContainer('alertBox', map).then((container) => {
542+
container[0].children[0].click(); // button
543+
});
544+
});
545+
546+
cy.window().then(({ map }) => {
547+
// expect needs to be in the this block, otherwise it will be executed before the click event
548+
expect(clickSpy.callCount).to.be.eq(1);
549+
550+
map.pm.Toolbar.deleteControl('alertBox');
551+
552+
map.pm.Toolbar.createCustomControl({
553+
name: 'alertBox',
554+
onClick: clickSpyNew,
555+
toggle: false,
556+
block: 'custom',
557+
});
558+
559+
cy.toolbarButtonContainer('alertBox', map).then((container) => {
560+
container[0].children[0].click(); // button
561+
});
562+
});
563+
564+
cy.window().then(() => {
565+
// expect needs to be in the this block, otherwise it will be executed before the click event
566+
expect(clickSpy.callCount).to.be.eq(1);
567+
expect(clickSpyNew.callCount).to.be.eq(1);
568+
});
569+
});
528570
});

leaflet-geoman.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,9 @@ declare module 'leaflet' {
10871087

10881088
/** Disable button by control name */
10891089
setButtonDisabled(name: TOOLBAR_CONTROL_ORDER, state: boolean): void;
1090+
1091+
/** Deletes and removes a Control from the Toolbar */
1092+
deleteControl(name: string): void;
10901093
}
10911094

10921095
type KEYBOARD_EVENT_TYPE = 'current' | 'keydown' | 'keyup';

src/js/Toolbar/L.PM.Toolbar.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,13 @@ const Toolbar = L.Class.extend({
151151

152152
this.isVisible = false;
153153
},
154+
deleteControl(name) {
155+
const btnName = this._btnNameMapping(name);
156+
if (this.buttons[btnName]) {
157+
this.buttons[btnName].remove();
158+
delete this.buttons[btnName];
159+
}
160+
},
154161
toggleControls(options = this.options) {
155162
if (this.isVisible) {
156163
this.removeControls();

0 commit comments

Comments
 (0)