Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Toolbar block position #626

Merged
merged 4 commits into from
Jul 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,14 +657,38 @@ map.pm.addControls({

Reorder the buttons with
```js
map.pm.Toolbar.changeControlOrder(['drawCircle', 'drawRectangle', 'removalMode', 'editMode'])
map.pm.Toolbar.changeControlOrder(['drawCircle', 'drawRectangle', 'removalMode', 'editMode']);
```

Receive the current order with
```js
map.pm.Toolbar.getControlOrder()
map.pm.Toolbar.getControlOrder();
```

**Position**

You can set different positions per block `draw`, `edit`, `options`⭐, `custom`:

Possible values are `'topleft'`, `'topright'`, `'bottomleft'`, `'bottomright'`

```javascript
map.pm.addControls({
positions: {
draw: 'topright',
edit: 'topleft',
}
});
```

```javascript
map.pm.Toolbar.setBlockPosition('draw','topright');
```

```javascript
map.pm.Toolbar.getBlockPositions();
```


**Adding New/Custom Controls**

```js
Expand Down
48 changes: 45 additions & 3 deletions cypress/integration/toolbar.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ describe('Testing the Toolbar', () => {

cy.get(actions[0]).click();
cy.get(container).should('not.have.class', 'active');
cy.window().then(({map, L}) => {
cy.window().then(() => {
map.pm.enableDraw('PolygonCopy');
map.on('pm:create', (e) => {
e.layer.on('click', (l) => testlayer = l.target)
Expand All @@ -277,7 +277,6 @@ describe('Testing the Toolbar', () => {


cy.get(mapSelector).click(390, 140).then(() => {
console.log(testlayer);
expect(testlayer.options.color).to.equal("red");
})
})
Expand All @@ -303,9 +302,52 @@ describe('Testing the Toolbar', () => {
map.pm.addControls({
oneBlock: true
});
cy.get('.leaflet-pm-toolbar.leaflet-pm-draw').then((container) => {
cy.get('.leaflet-pm-toolbar.leaflet-pm-topleft').then((container) => {
expect(container[0].children.length).to.equal(10);
})
});
});

it('Different block positions', () => {
cy.window().then(({map}) => {
map.pm.addControls({
positions: {
draw: 'topright',
edit: 'topleft'
}
});
cy.get('.leaflet-pm-toolbar.leaflet-pm-edit')
.parent('.leaflet-top.leaflet-left')
.should('exist');
cy.get('.leaflet-pm-toolbar.leaflet-pm-draw')
.parent('.leaflet-top.leaflet-right')
.should('exist');
});
});

it('Different block positions - One Block', () => {
cy.window().then(({map}) => {
map.pm.addControls({
oneBlock: true,
positions: {
draw: 'topright',
edit: 'topleft',
custom: 'topleft',
}
});

map.pm.Toolbar.copyDrawControl("Polygon", {
name: "PolygonCopy",
block: "custom",
className: "leaflet-pm-icon-polygon",
title: "Display text on hover button",
});
cy.get('.leaflet-pm-toolbar.leaflet-pm-topright').then((container) => {
expect(container[0].children.length).to.equal(6);
});
cy.get('.leaflet-pm-toolbar.leaflet-pm-topleft').then((container) => {
expect(container[0].children.length).to.equal(5);
});
});
});
});
2 changes: 1 addition & 1 deletion src/js/Toolbar/L.Controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const PMButton = L.Control.extend({
this._container = this._map.pm.Toolbar.drawContainer;
}
} else {
this._container = this._map.pm.Toolbar.drawContainer;
this._container = this._map.pm.Toolbar._createContainer(this.options.position);
}
this.buttonsDomNode = this._makeButton(this._button);
this._container.appendChild(this.buttonsDomNode);
Expand Down
35 changes: 34 additions & 1 deletion src/js/Toolbar/L.PM.Toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ const Toolbar = L.Class.extend({
customControls: true,
oneBlock: false,
position: 'topleft',
positions: {
draw: '',
edit: '',
options: '',
custom: '',
}
},
customButtons: [],
initialize(map) {
Expand Down Expand Up @@ -62,6 +68,16 @@ const Toolbar = L.Class.extend({

this._defineButtons();
},
_createContainer(name){
const container = `${name}Container`;
if(!this[container]) {
this[container] = L.DomUtil.create(
'div',
`leaflet-pm-toolbar leaflet-pm-${name} leaflet-bar leaflet-control`
);
}
return this[container];
},
getButtons() {
return this.buttons;
},
Expand Down Expand Up @@ -378,14 +394,31 @@ const Toolbar = L.Class.extend({
if (this.options.customControls === false) {
ignoreBtns = ignoreBtns.concat(Object.keys(buttons).filter(btn => buttons[btn]._button.tool === 'custom'));
}

for (const btn in buttons) {
if (this.options[btn] && ignoreBtns.indexOf(btn) === -1) {
// if options say the button should be visible, add it to the map
buttons[btn].setPosition(this.options.position);
let block = buttons[btn]._button.tool;
if(!block) {
// undefined is the draw block
block = 'draw';
}
buttons[btn].setPosition(this._getBtnPosition(block));
buttons[btn].addTo(this.map);
}
}
},
_getBtnPosition(block){
return this.options.positions && this.options.positions[block] ? this.options.positions[block] : this.options.position;
},
setBlockPosition(block,position){
this.options.positions[block] = position;
this._showHideButtons();
this.changeControlOrder();
},
getBlockPositions(){
return this.options.positions;
},
copyDrawControl(copyInstance, options) {

if (!options) {
Expand Down