Skip to content

Commit baa29a2

Browse files
authored
Add event support for nested LayerGroups (#695). Fixes #549 (minor)
* WIP * Layergroup refactoring -> events now called directly instead over a listener on the child layer * Fix double import
1 parent ed4a832 commit baa29a2

27 files changed

+406
-162
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"type":"FeatureCollection",
3+
"features":[
4+
{
5+
"type":"Feature",
6+
"geometry":{
7+
"type":"MultiPoint",
8+
"coordinates":[
9+
[
10+
32.83604819,
11+
40.0169319
12+
]
13+
]
14+
}
15+
},
16+
{
17+
"type":"Feature",
18+
"geometry":{
19+
"type":"MultiPoint",
20+
"coordinates":[
21+
[
22+
32.8298979,
23+
40.02567418
24+
]
25+
]
26+
}
27+
}
28+
]
29+
}

cypress/integration/layergroup.spec.js

Lines changed: 134 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
describe('Edit LayerGroup', () => {
2-
const mapSelector = '#map';
2+
const mapSelector = '#map';
33

44
it('correctly enables geojson featureCollection', () => {
55
cy.drawShape('FeatureCollectionWithCircles');
@@ -43,8 +43,7 @@ describe('Edit LayerGroup', () => {
4343
});
4444

4545
it('supports clearLayers', () => {
46-
47-
cy.window().then(({ L, map }) => {
46+
cy.window().then(({L, map}) => {
4847
const featureGroup = new L.FeatureGroup();
4948
featureGroup.addTo(map)
5049
featureGroup.addLayer(new L.Marker([19.04469, 72.9258]));
@@ -60,10 +59,10 @@ describe('Edit LayerGroup', () => {
6059
let fg2;
6160

6261
// Add layer to group
63-
cy.window().then(({ L, map }) => {
62+
cy.window().then(({L, map}) => {
6463
fg = L.featureGroup().addTo(map);
6564
fg2 = L.featureGroup().addTo(map);
66-
map.on('click',(e)=>console.log(map.latLngToContainerPoint(e.latlng)));
65+
map.on('click', (e) => console.log(map.latLngToContainerPoint(e.latlng)));
6766

6867
map.pm.setGlobalOptions({layerGroup: fg});
6968

@@ -80,15 +79,15 @@ describe('Edit LayerGroup', () => {
8079
});
8180

8281
// check if layer is on group and will be removed from map, when group is removed
83-
cy.window().then(({ map }) => {
82+
cy.window().then(({map}) => {
8483
fg.removeFrom(map);
8584
cy.hasLayers(3);
8685

8786
const count = fg.getLayers().length;
8887
expect(count).to.equal(1);
8988
});
9089
// delete layer from group and map
91-
cy.window().then(({ map }) => {
90+
cy.window().then(({map}) => {
9291
fg.addTo(map);
9392

9493
cy.toolbarButton('delete')
@@ -111,7 +110,7 @@ describe('Edit LayerGroup', () => {
111110
.click(200, 200)
112111
.click(400, 350);
113112
});
114-
cy.window().then(({ map }) => {
113+
cy.window().then(({map}) => {
115114
map.pm.setGlobalOptions({layerGroup: fg2});
116115
cy.hasLayers(5);
117116

@@ -149,4 +148,131 @@ describe('Edit LayerGroup', () => {
149148
cy.hasLayers(4);
150149
});
151150
})
151+
152+
153+
it('pass the fired event of the layer to group', () => {
154+
let fg;
155+
let firedEvent = "";
156+
157+
cy.window().then(({map, L}) => {
158+
fg = L.featureGroup();
159+
fg.on("pm:cut", (e) => {
160+
firedEvent = e.type;
161+
});
162+
163+
map.on('pm:create', (e) => {
164+
e.layer.addTo(fg);
165+
})
166+
});
167+
// activate polygon drawing
168+
cy.toolbarButton('polygon')
169+
.click()
170+
.closest('.button-container')
171+
.should('have.class', 'active');
172+
173+
// draw a polygon
174+
cy.get(mapSelector)
175+
.click(90, 250)
176+
.click(150, 50)
177+
.click(500, 50)
178+
.click(500, 300)
179+
.click(300, 350)
180+
.click(90, 250);
181+
182+
// activate cutting drawing
183+
cy.toolbarButton('cut')
184+
.click()
185+
.closest('.button-container')
186+
.should('have.class', 'active');
187+
188+
// draw a polygon to cut
189+
cy.get(mapSelector)
190+
.click(450, 100)
191+
.click(450, 150)
192+
.click(400, 150)
193+
.click(390, 140)
194+
.click(390, 100)
195+
.click(450, 100);
196+
197+
cy.window().then(() => {
198+
expect(firedEvent).to.equal('pm:cut');
199+
});
200+
});
201+
202+
it('event is fired only once if group has multiple sub-groups with the same layer', () => {
203+
204+
let firedEventCount = 0;
205+
cy.window().then(({map, L}) => {
206+
const group = L.featureGroup().addTo(map);
207+
const layers = L.featureGroup().addTo(group);
208+
const markers = L.featureGroup().addTo(group);
209+
const markersChild = L.featureGroup().addTo(markers);
210+
L.marker(map.getCenter()).addTo(layers).addTo(markers).addTo(markersChild);
211+
212+
group.on('pm:enable', () => {
213+
firedEventCount += 1
214+
});
215+
});
216+
217+
cy.wait(100);
218+
219+
cy.toolbarButton('edit').click();
220+
221+
cy.window().then(() => {
222+
expect(firedEventCount).to.equal(1);
223+
});
224+
});
225+
226+
it('event is fired on every parent group of a layer (once)', () => {
227+
228+
let firedEventCount = 0;
229+
cy.window().then(({map, L}) => {
230+
const group = L.featureGroup().addTo(map);
231+
const layers = L.featureGroup().addTo(group);
232+
const markers = L.featureGroup().addTo(group);
233+
const markersChild = L.featureGroup().addTo(markers);
234+
L.marker(map.getCenter()).addTo(layers).addTo(markers).addTo(markersChild);
235+
236+
group.on('pm:enable', () => {
237+
firedEventCount += 1
238+
});
239+
layers.on('pm:enable', () => {
240+
firedEventCount += 1
241+
});
242+
markers.on('pm:enable', () => {
243+
firedEventCount += 1
244+
});
245+
markersChild.on('pm:enable', () => {
246+
firedEventCount += 1
247+
});
248+
});
249+
250+
cy.wait(100);
251+
252+
cy.toolbarButton('edit').click();
253+
254+
cy.window().then(() => {
255+
expect(firedEventCount).to.equal(4);
256+
});
257+
});
258+
259+
it('event is fired on L.geoJson when it has FeatureCollections', () => {
260+
cy.drawShape('FeatureCollectionEventFire');
261+
262+
let firedEventCount = 0;
263+
cy.get('@feature').then(feature => {
264+
feature.on('pm:enable', () => {
265+
firedEventCount += 1
266+
});
267+
});
268+
269+
cy.wait(100);
270+
271+
cy.toolbarButton('edit').click();
272+
273+
cy.window().then(() => {
274+
expect(firedEventCount).to.equal(2);
275+
});
276+
});
277+
152278
});

cypress/support/commands.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ Cypress.Commands.add('drawShape', (shape, ignore) => {
173173
});
174174
}
175175

176+
if (shape === 'FeatureCollectionEventFire') {
177+
cy.fixture(shape)
178+
.then(json => {
179+
//
180+
const layer = L.geoJSON(json).addTo(map);
181+
const bounds = layer.getBounds();
182+
map.fitBounds(bounds);
183+
184+
return layer;
185+
})
186+
.as('feature');
187+
}
188+
176189
if (shape === 'FeatureCollectionWithCircles') {
177190
cy.fixture(shape, ignore)
178191
.then(json => {

src/js/Draw/L.PM.Draw.Circle.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Draw from './L.PM.Draw';
22

33
import {destinationOnLine, getTranslation} from '../helpers';
4+
import Utils from "../L.PM.Utils";
45

56
Draw.Circle = Draw.extend({
67
initialize(map) {
@@ -83,7 +84,7 @@ Draw.Circle = Draw.extend({
8384
this._otherSnapLayers = [];
8485

8586
// fire drawstart event
86-
this._map.fire('pm:drawstart', {
87+
Utils._fireEvent(this._map,'pm:drawstart', {
8788
shape: this._shape,
8889
workingLayer: this._layer,
8990
});
@@ -120,7 +121,7 @@ Draw.Circle = Draw.extend({
120121
}
121122

122123
// fire drawend event
123-
this._map.fire('pm:drawend', { shape: this._shape });
124+
Utils._fireEvent(this._map,'pm:drawend', { shape: this._shape });
124125
this._setGlobalDrawMode();
125126
},
126127
enabled() {
@@ -205,7 +206,7 @@ Draw.Circle = Draw.extend({
205206
getTranslation('tooltips.finishCircle')
206207
);
207208

208-
this._layer.fire('pm:centerplaced', {
209+
Utils._fireEvent(this._layer,'pm:centerplaced', {
209210
workingLayer: this._layer,
210211
latlng,
211212
shape: this._shape
@@ -249,7 +250,7 @@ Draw.Circle = Draw.extend({
249250
}
250251

251252
// fire the pm:create event and pass shape and layer
252-
this._map.fire('pm:create', {
253+
Utils._fireEvent(this._map,'pm:create', {
253254
shape: this._shape,
254255
layer: circleLayer,
255256
});

src/js/Draw/L.PM.Draw.CircleMarker.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import Draw from './L.PM.Draw';
22

3-
import {destinationOnLine, getTranslation} from '../helpers';
3+
import {destinationOnLine, getTranslation } from '../helpers';
4+
import Utils from "../L.PM.Utils";
45

56
Draw.CircleMarker = Draw.Marker.extend({
67
initialize(map) {
@@ -116,7 +117,7 @@ Draw.CircleMarker = Draw.Marker.extend({
116117
this._layer.bringToBack();
117118

118119
// fire drawstart event
119-
this._map.fire('pm:drawstart', {
120+
Utils._fireEvent(this._map,'pm:drawstart', {
120121
shape: this._shape,
121122
workingLayer: this._layer,
122123
});
@@ -168,7 +169,7 @@ Draw.CircleMarker = Draw.Marker.extend({
168169
}
169170

170171
// fire drawend event
171-
this._map.fire('pm:drawend', { shape: this._shape });
172+
Utils._fireEvent(this._map,'pm:drawend', { shape: this._shape });
172173
this._setGlobalDrawMode();
173174
},
174175
_placeCenterMarker(e) {
@@ -202,7 +203,7 @@ Draw.CircleMarker = Draw.Marker.extend({
202203
getTranslation('tooltips.finishCircle')
203204
);
204205

205-
this._layer.fire('pm:centerplaced', {
206+
Utils._fireEvent(this._layer,'pm:centerplaced', {
206207
shape: this._shape,
207208
workingLayer: this._layer,
208209
latlng,
@@ -274,7 +275,7 @@ Draw.CircleMarker = Draw.Marker.extend({
274275
}
275276

276277
// fire the pm:create event and pass shape and marker
277-
this._map.fire('pm:create', {
278+
Utils._fireEvent(this._map,'pm:create', {
278279
shape: this._shape,
279280
marker, // DEPRECATED
280281
layer: marker,
@@ -318,7 +319,7 @@ Draw.CircleMarker = Draw.Marker.extend({
318319
}
319320

320321
// fire the pm:create event and pass shape and layer
321-
this._map.fire('pm:create', {
322+
Utils._fireEvent(this._map,'pm:create', {
322323
shape: this._shape,
323324
layer: circleLayer,
324325
});

src/js/Draw/L.PM.Draw.Cut.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,21 +39,21 @@ Draw.Cut = Draw.Polygon.extend({
3939

4040
this._editedLayers.forEach(({layer, originalLayer}) =>{
4141
// fire pm:cut on the cutted layer
42-
originalLayer.fire('pm:cut', {
42+
Utils._fireEvent(originalLayer,'pm:cut', {
4343
shape: this._shape,
4444
layer,
4545
originalLayer,
4646
});
4747

4848
// fire pm:cut on the map
49-
this._map.fire('pm:cut', {
49+
Utils._fireEvent(this._map,'pm:cut', {
5050
shape: this._shape,
5151
layer,
5252
originalLayer,
5353
});
5454

5555
// fire edit event after cut
56-
originalLayer.fire('pm:edit', { layer: originalLayer, shape: originalLayer.pm.getShape()});
56+
Utils._fireEvent(originalLayer,'pm:edit', { layer: originalLayer, shape: originalLayer.pm.getShape()});
5757
});
5858
this._editedLayers = [];
5959

0 commit comments

Comments
 (0)