Skip to content

Commit

Permalink
Additional Control methods (#1295) (minor)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
TurtIeSocks and Falke-Design authored Feb 27, 2024
1 parent b566d7f commit 6e47f00
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
5 changes: 5 additions & 0 deletions cypress/integration/toolbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
12 changes: 12 additions & 0 deletions leaflet-geoman.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,18 @@ declare module 'leaflet' {
position: L.ControlPosition
): void;

/** Returns all of the active buttons */
getButtons(): Record<string, L.Control>

/** 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<string, L.Control>

/** Returns a Object with the positions for all blocks */
getBlockPositions(): BlockPositions;

Expand Down
23 changes: 22 additions & 1 deletion src/js/Toolbar/L.PM.Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 6e47f00

Please sign in to comment.