From 6e47f005fa52d54d81ba76c9668d9f97035ade24 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:47:37 -0500 Subject: [PATCH] Additional Control methods (#1295) (minor) * extra custom control methods - adds `buttonExists(name: string)` to get the status of a button - adds `getCustomButtons()` that returns an object of all of the custom controls - ts definitions * docs * fix: suggestions * fix: add some very basic tests * Apply suggestions from code review * Update src/js/Toolbar/L.PM.Toolbar.js --------- Co-authored-by: Florian Bischof --- cypress/integration/toolbar.spec.js | 5 +++++ leaflet-geoman.d.ts | 12 ++++++++++++ src/js/Toolbar/L.PM.Toolbar.js | 23 ++++++++++++++++++++++- 3 files changed, 39 insertions(+), 1 deletion(-) 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();