From 06bb2064f52eee50b6373e2c4a324277f88a8b92 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Wed, 18 Jan 2023 23:10:17 -0500 Subject: [PATCH 1/6] 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 --- leaflet-geoman.d.ts | 9 +++++++++ src/js/Toolbar/L.PM.Toolbar.js | 11 ++++++++++- 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/leaflet-geoman.d.ts b/leaflet-geoman.d.ts index 9adfc90d..95d7f972 100644 --- a/leaflet-geoman.d.ts +++ b/leaflet-geoman.d.ts @@ -646,6 +646,15 @@ declare module 'leaflet' { position: L.ControlPosition ): void; + /** Returns all of the active buttons */ + getButtons(): Record + + /** Checks whether a button has been mounted */ + controlExists(name: string): boolean; + + /** Returns all of the custom, active buttons */ + getCustomControls(): 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..e1b1b5ef 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -563,7 +563,16 @@ const Toolbar = L.Class.extend({ this.changeControlOrder(); return control; }, - + controlExists(name) { + return Boolean(this.getButtons()[name]); + }, + getCustomControls() { + return Object.fromEntries( + Object.entries(this.getButtons()).filter( + ([, v]) => v._button.tool === 'custom' + ) + ); + }, changeControlOrder(order = []) { const shapeMapping = this._shapeMapping(); From 32b3fc06b48cefdcf9255ff5815ee7c59d6aad17 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Wed, 18 Jan 2023 23:45:38 -0500 Subject: [PATCH 2/6] docs --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 797e9751..c7f40153 100644 --- a/README.md +++ b/README.md @@ -1056,6 +1056,9 @@ The following methods are available on `map.pm.Toolbar`: | createCustomControl(`options`) | - | To add a custom Control to the Toolbar. [Details](#adding-newcustom-controls) | | copyDrawControl(`instance`, `options`) | `Object` | Creates a copy of a draw Control. Returns the `drawInstance` and the `control`. | | changeActionsOfControl(`name`, `actions`) | - | Change the actions of an existing button. | +| getButtons() | `Object` | Get an object of the current buttons button. | +| controlExists(`name`) | `Boolean` | Checks whether a button has been mounted button. | +| getCustomControls()) | `Object` | Gets an object of the custom controls that are mounted button. | #### Customize Controls From a308886c0664735d97bb9345aa61dbec36364825 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Sat, 24 Feb 2024 12:31:08 -0500 Subject: [PATCH 3/6] fix: suggestions --- leaflet-geoman.d.ts | 5 ++++- src/js/Toolbar/L.PM.Toolbar.js | 21 +++++++++++++++------ 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/leaflet-geoman.d.ts b/leaflet-geoman.d.ts index 5034a250..23452077 100644 --- a/leaflet-geoman.d.ts +++ b/leaflet-geoman.d.ts @@ -668,12 +668,15 @@ declare module 'leaflet' { /** 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 */ - getCustomControls(): Record + 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 e1b1b5ef..5366b835 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -566,12 +566,21 @@ const Toolbar = L.Class.extend({ controlExists(name) { return Boolean(this.getButtons()[name]); }, - getCustomControls() { - return Object.fromEntries( - Object.entries(this.getButtons()).filter( - ([, v]) => v._button.tool === 'custom' - ) - ); + getButton(name) { + return this.getButtons()[name]; + }, + getButtonsInBlock(name) { + const buttonsInBlock = {}; + if (name) { + for (const buttonName in map.pm.Toolbar.getButtons()) { + const button = map.pm.Toolbar.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(); From 3da257e44530029f52754887146c14b265ebc870 Mon Sep 17 00:00:00 2001 From: Derick M <58572875+TurtIeSocks@users.noreply.github.com> Date: Sat, 24 Feb 2024 13:26:07 -0500 Subject: [PATCH 4/6] fix: add some very basic tests --- cypress/integration/toolbar.spec.js | 5 +++++ src/js/Toolbar/L.PM.Toolbar.js | 9 ++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) 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/src/js/Toolbar/L.PM.Toolbar.js b/src/js/Toolbar/L.PM.Toolbar.js index 5366b835..7895bd8a 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -572,10 +572,13 @@ const Toolbar = L.Class.extend({ getButtonsInBlock(name) { const buttonsInBlock = {}; if (name) { - for (const buttonName in map.pm.Toolbar.getButtons()) { - const button = map.pm.Toolbar.getButtons()[buttonName]; + 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)) { + if ( + button._button.tool === name || + (name === 'draw' && !button._button.tool) + ) { buttonsInBlock[buttonName] = button; } } From 39b168b56618746a7a447186554d7462a72eb627 Mon Sep 17 00:00:00 2001 From: Florian Bischof Date: Tue, 27 Feb 2024 22:39:26 +0100 Subject: [PATCH 5/6] Apply suggestions from code review --- src/js/Toolbar/L.PM.Toolbar.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/js/Toolbar/L.PM.Toolbar.js b/src/js/Toolbar/L.PM.Toolbar.js index 7895bd8a..80752626 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -564,7 +564,7 @@ const Toolbar = L.Class.extend({ return control; }, controlExists(name) { - return Boolean(this.getButtons()[name]); + return Boolean(this.getButton(name)); }, getButton(name) { return this.getButtons()[name]; @@ -573,7 +573,7 @@ const Toolbar = L.Class.extend({ const buttonsInBlock = {}; if (name) { for (const buttonName in this.getButtons()) { - const button = this.getButtons()[buttonName]; + const button = this.getButton(buttonName); // draw controls doesn't have a block if ( button._button.tool === name || From 28c76ecdd5f9cdbe15b659dc915f39e78bd721df Mon Sep 17 00:00:00 2001 From: Florian Bischof Date: Tue, 27 Feb 2024 22:40:14 +0100 Subject: [PATCH 6/6] Update src/js/Toolbar/L.PM.Toolbar.js --- src/js/Toolbar/L.PM.Toolbar.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/Toolbar/L.PM.Toolbar.js b/src/js/Toolbar/L.PM.Toolbar.js index 80752626..7cce4ac4 100644 --- a/src/js/Toolbar/L.PM.Toolbar.js +++ b/src/js/Toolbar/L.PM.Toolbar.js @@ -573,7 +573,7 @@ const Toolbar = L.Class.extend({ const buttonsInBlock = {}; if (name) { for (const buttonName in this.getButtons()) { - const button = this.getButton(buttonName); + const button = this.getButtons()[buttonName]; // draw controls doesn't have a block if ( button._button.tool === name ||