diff --git a/cypress/integration/toolbar.spec.js b/cypress/integration/toolbar.spec.js index eb7bf228..047da511 100644 --- a/cypress/integration/toolbar.spec.js +++ b/cypress/integration/toolbar.spec.js @@ -218,6 +218,11 @@ describe('Testing the Toolbar', () => { expect(testresult).to.equal('clickButton clicked'); cy.get(container).should('not.have.class', 'active'); }); + expect(map.pm.Toolbar.getButton('clickButton')).to.not.equal(undefined); + expect(map.pm.Toolbar.controlExists('clickButton')).to.equal(true); + expect( + 'clickButton' in map.pm.Toolbar.getButtonsInBlock('custom') + ).to.equal(true); }); }); diff --git a/leaflet-geoman.d.ts b/leaflet-geoman.d.ts index ffe82c08..23452077 100644 --- a/leaflet-geoman.d.ts +++ b/leaflet-geoman.d.ts @@ -666,6 +666,18 @@ declare module 'leaflet' { position: L.ControlPosition ): void; + /** Returns all of the active buttons */ + getButtons(): Record + + /** Returns the full button object or undefined if the name does not exist */ + getButton(name: string): L.Control | undefined; + + /** Checks whether a button has been mounted */ + controlExists(name: string): boolean; + + /** Returns all of the custom, active buttons */ + getButtonsInBlock(name: string): Record + /** Returns a Object with the positions for all blocks */ getBlockPositions(): BlockPositions; diff --git a/src/js/Toolbar/L.PM.Toolbar.js b/src/js/Toolbar/L.PM.Toolbar.js index 965cbb99..7cce4ac4 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -563,7 +563,28 @@ const Toolbar = L.Class.extend({ this.changeControlOrder(); return control; }, - + controlExists(name) { + return Boolean(this.getButton(name)); + }, + getButton(name) { + return this.getButtons()[name]; + }, + getButtonsInBlock(name) { + const buttonsInBlock = {}; + if (name) { + for (const buttonName in this.getButtons()) { + const button = this.getButtons()[buttonName]; + // draw controls doesn't have a block + if ( + button._button.tool === name || + (name === 'draw' && !button._button.tool) + ) { + buttonsInBlock[buttonName] = button; + } + } + } + return buttonsInBlock; + }, changeControlOrder(order = []) { const shapeMapping = this._shapeMapping();