-
Notifications
You must be signed in to change notification settings - Fork 428
/
L.PM.Edit.Line.js
830 lines (697 loc) · 24.2 KB
/
L.PM.Edit.Line.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
import kinks from '@turf/kinks';
import lineIntersect from '@turf/line-intersect';
import get from 'lodash/get';
import Edit from './L.PM.Edit';
import { copyLatLngs, hasValues, removeEmptyCoordRings } from '../helpers';
import MarkerLimits from '../Mixins/MarkerLimits';
// Shit's getting complicated in here with Multipolygon Support. So here's a quick note about it:
// Multipolygons with holes means lots of nested, multidimensional arrays.
// In order to find a value inside such an array you need a path to adress it directly.
// Example: var arr = [[['a', 'b'], ['c']]];
// The indexPath to 'b' is [0, 0, 1]. The indexPath to 'c' is [0, 1, 0].
// So I can get 'b' with: arr[0][0][1].
// Got it? Now you know what is meant when you read "indexPath" around here. Have fun 👍
Edit.Line = Edit.extend({
includes: [MarkerLimits],
_shape: 'Line',
initialize(layer) {
this._layer = layer;
this._enabled = false;
},
enable(options) {
L.Util.setOptions(this, options);
this._map = this._layer._map;
// cancel when map isn't available, this happens when the polygon is removed before this fires
if (!this._map) {
return;
}
// layer is not allowed to edit
if (!this.options.allowEditing) {
this.disable();
return;
}
if (this.enabled()) {
// if it was already enabled, disable first
// we don't block enabling again because new options might be passed
this.disable();
}
// change state
this._enabled = true;
// init markers
this._initMarkers();
this.applyOptions();
// if shape gets removed from map, disable edit mode
this._layer.on('remove', this.disable, this);
if (!this.options.allowSelfIntersection) {
this._layer.on(
'pm:vertexremoved',
this._handleSelfIntersectionOnVertexRemoval,
this
);
}
if (!this.options.allowSelfIntersection) {
if (this._layer.options.color !== '#f00000ff') {
this.cachedColor = this._layer.options.color;
this.isRed = false;
} else {
this.isRed = true;
}
this._handleLayerStyle();
} else {
this.cachedColor = undefined;
}
this._fireEnable();
},
disable() {
// if it's not enabled, it doesn't need to be disabled
if (!this.enabled()) {
return;
}
// prevent disabling if polygon is being dragged
if (this._dragging) {
return;
}
this._enabled = false;
this._markerGroup.clearLayers();
this._markerGroup.removeFrom(this._map);
// remove listener
this._layer.off('remove', this.disable, this);
if (!this.options.allowSelfIntersection) {
this._layer.off(
'pm:vertexremoved',
this._handleSelfIntersectionOnVertexRemoval,
this
);
}
// remove draggable class
const el = this._layer._path
? this._layer._path
: this._layer._renderer._container;
L.DomUtil.removeClass(el, 'leaflet-pm-draggable');
if (this._layerEdited) {
this._fireUpdate();
}
this._layerEdited = false;
this._fireDisable();
},
enabled() {
return this._enabled;
},
toggleEdit(options) {
if (!this.enabled()) {
this.enable(options);
} else {
this.disable();
}
return this.enabled();
},
applyOptions() {
if (this.options.snappable) {
this._initSnappableMarkers();
} else {
this._disableSnapping();
}
},
_initMarkers() {
const map = this._map;
const coords = this._layer.getLatLngs();
// cleanup old ones first
if (this._markerGroup) {
this._markerGroup.clearLayers();
}
// add markerGroup to map, markerGroup includes regular and middle markers
this._markerGroup = new L.FeatureGroup();
this._markerGroup._pmTempLayer = true;
// handle coord-rings (outer, inner, etc)
const handleRing = (coordsArr) => {
// if there is another coords ring, go a level deep and do this again
if (Array.isArray(coordsArr[0])) {
return coordsArr.map(handleRing, this);
}
// the marker array, it includes only the markers of vertexes (no middle markers)
const ringArr = coordsArr.map(this._createMarker, this);
if (this.options.hideMiddleMarkers !== true) {
// create small markers in the middle of the regular markers
coordsArr.map((v, k) => {
// find the next index fist
const nextIndex = this.isPolygon()
? (k + 1) % coordsArr.length
: k + 1;
// create the marker
return this._createMiddleMarker(ringArr[k], ringArr[nextIndex]);
});
}
return ringArr;
};
// create markers
this._markers = handleRing(coords);
// handle possible limitation: maximum number of markers
this.filterMarkerGroup();
// add markerGroup to map
map.addLayer(this._markerGroup);
},
// creates initial markers for coordinates
_createMarker(latlng) {
const marker = new L.Marker(latlng, {
draggable: true,
icon: L.divIcon({ className: 'marker-icon' }),
});
this._setPane(marker, 'vertexPane');
marker._pmTempLayer = true;
if (this.options.rotate) {
marker.on('dragstart', this._onRotateStart, this);
marker.on('drag', this._onRotate, this);
marker.on('dragend', this._onRotateEnd, this);
} else {
marker.on('click', this._onVertexClick, this);
marker.on('dragstart', this._onMarkerDragStart, this);
marker.on('move', this._onMarkerDrag, this);
marker.on('dragend', this._onMarkerDragEnd, this);
if (!this.options.preventMarkerRemoval) {
marker.on(this.options.removeVertexOn, this._removeMarker, this);
}
}
this._markerGroup.addLayer(marker);
return marker;
},
// creates the middle markes between coordinates
_createMiddleMarker(leftM, rightM) {
// cancel if there are no two markers
if (!leftM || !rightM) {
return false;
}
const latlng = L.PM.Utils.calcMiddleLatLng(
this._map,
leftM.getLatLng(),
rightM.getLatLng()
);
const middleMarker = this._createMarker(latlng);
const middleIcon = L.divIcon({
className: 'marker-icon marker-icon-middle',
});
middleMarker.setIcon(middleIcon);
middleMarker.leftM = leftM;
middleMarker.rightM = rightM;
// save reference to this middle markers on the neighboor regular markers
leftM._middleMarkerNext = middleMarker;
rightM._middleMarkerPrev = middleMarker;
middleMarker.on(this.options.addVertexOn, this._onMiddleMarkerClick, this);
middleMarker.on('movestart', this._onMiddleMarkerMoveStart, this);
return middleMarker;
},
_onMiddleMarkerClick(e) {
const middleMarker = e.target;
if (!this._vertexValidation('add', e)) {
return;
}
// TODO: move the next two lines inside _addMarker() as soon as
// https://github.com/Leaflet/Leaflet/issues/4484
// is fixed
const icon = L.divIcon({ className: 'marker-icon' });
middleMarker.setIcon(icon);
this._addMarker(middleMarker, middleMarker.leftM, middleMarker.rightM);
},
_onMiddleMarkerMoveStart(e) {
const middleMarker = e.target;
middleMarker.on('moveend', this._onMiddleMarkerMoveEnd, this);
if (!this._vertexValidation('add', e)) {
middleMarker.on('move', this._onMiddleMarkerMovePrevent, this);
return;
}
middleMarker._dragging = true;
// TODO: This is a workaround. Remove the moveend listener and
// callback as soon as this is fixed:
// https://github.com/Leaflet/Leaflet/issues/4484
this._addMarker(middleMarker, middleMarker.leftM, middleMarker.rightM);
},
_onMiddleMarkerMovePrevent(e) {
const middleMarker = e.target;
this._vertexValidationDrag(middleMarker);
},
_onMiddleMarkerMoveEnd(e) {
const middleMarker = e.target;
middleMarker.off('move', this._onMiddleMarkerMovePrevent, this);
middleMarker.off('moveend', this._onMiddleMarkerMoveEnd, this);
if (!this._vertexValidationDragEnd(middleMarker)) {
return;
}
const icon = L.divIcon({ className: 'marker-icon' });
middleMarker.setIcon(icon);
// timeout is needed else this._onVertexClick fires the event because it is called after deleting the flag
setTimeout(() => {
delete middleMarker._dragging;
}, 100);
},
// adds a new marker from a middlemarker
_addMarker(newM, leftM, rightM) {
// first, make this middlemarker a regular marker
newM.off('movestart', this._onMiddleMarkerMoveStart, this);
newM.off(this.options.addVertexOn, this._onMiddleMarkerClick, this);
// now, create the polygon coordinate point for that marker
// and push into marker array
// and associate polygon coordinate with marker coordinate
const latlng = newM.getLatLng();
const coords = this._layer._latlngs;
// remove linked markers
delete newM.leftM;
delete newM.rightM;
// the index path to the marker inside the multidimensional marker array
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
leftM
);
// define the coordsRing that is edited
const coordsRing = indexPath.length > 1 ? get(coords, parentPath) : coords;
// define the markers array that is edited
const markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;
// add coordinate to coordinate array
coordsRing.splice(index + 1, 0, latlng);
// add marker to marker array
markerArr.splice(index + 1, 0, newM);
// set new latlngs to update polygon
this._layer.setLatLngs(coords);
// create the new middlemarkers
if (this.options.hideMiddleMarkers !== true) {
this._createMiddleMarker(leftM, newM);
this._createMiddleMarker(newM, rightM);
}
// fire edit event
this._fireEdit();
this._layerEdited = true;
this._fireChange(this._layer.getLatLngs(), 'Edit');
this._fireVertexAdded(
newM,
L.PM.Utils.findDeepMarkerIndex(this._markers, newM).indexPath,
latlng
);
if (this.options.snappable) {
this._initSnappableMarkers();
}
},
hasSelfIntersection() {
// check for self intersection of the layer and return true/false
const selfIntersection = kinks(this._layer.toGeoJSON(15));
return selfIntersection.features.length > 0;
},
_handleSelfIntersectionOnVertexRemoval() {
// check for selfintersection again (mainly to reset the style)
const selfIntersection = this._handleLayerStyle(true);
if (selfIntersection) {
// reset coordinates
this._layer.setLatLngs(this._coordsBeforeEdit);
this._coordsBeforeEdit = null;
// re-enable markers for the new coords
this._initMarkers();
}
},
_handleLayerStyle(flash) {
const layer = this._layer;
let selfIntersection;
let intersection;
if (this.options.allowSelfIntersection) {
selfIntersection = false;
} else {
intersection = kinks(this._layer.toGeoJSON(15));
selfIntersection = intersection.features.length > 0;
}
if (selfIntersection) {
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit
) {
this._updateDisabledMarkerStyle(this._markers, true);
}
if (this.isRed) {
return selfIntersection;
}
// if it does self-intersect, mark or flash it red
if (flash) {
this._flashLayer();
} else {
layer.setStyle({ color: '#f00000ff' });
this.isRed = true;
}
// fire intersect event
this._fireIntersect(intersection);
} else {
// if not, reset the style to the default color
layer.setStyle({ color: this.cachedColor });
this.isRed = false;
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit
) {
this._updateDisabledMarkerStyle(this._markers, false);
}
}
return selfIntersection;
},
_flashLayer() {
if (!this.cachedColor) {
this.cachedColor = this._layer.options.color;
}
this._layer.setStyle({ color: '#f00000ff' });
this.isRed = true;
window.setTimeout(() => {
this._layer.setStyle({ color: this.cachedColor });
this.isRed = false;
}, 200);
},
_updateDisabledMarkerStyle(markers, disabled) {
markers.forEach((marker) => {
if (Array.isArray(marker)) {
this._updateDisabledMarkerStyle(marker, disabled);
} else if (marker._icon) {
if (disabled && !this._checkMarkerAllowedToDrag(marker)) {
L.DomUtil.addClass(marker._icon, 'vertexmarker-disabled');
} else {
L.DomUtil.removeClass(marker._icon, 'vertexmarker-disabled');
}
}
});
},
_removeMarker(e) {
// the marker that should be removed
const marker = e.target;
if (!this._vertexValidation('remove', e)) {
return;
}
// if self intersection isn't allowed, save the coords upon dragstart
// in case we need to reset the layer
if (!this.options.allowSelfIntersection) {
this._coordsBeforeEdit = copyLatLngs(
this._layer,
this._layer.getLatLngs()
);
}
// coords of the layer
let coords = this._layer.getLatLngs();
// the index path to the marker inside the multidimensional marker array
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
// only continue if this is NOT a middle marker (those can't be deleted)
if (!indexPath) {
return;
}
// define the coordsRing that is edited
const coordsRing = indexPath.length > 1 ? get(coords, parentPath) : coords;
// define the markers array that is edited
let markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;
// prevent removal of the layer if the vertex count is below minimum
if (!this.options.removeLayerBelowMinVertexCount) {
// if on a line only 2 vertices left or on a polygon 3 vertices left, don't allow to delete
if (
coordsRing.length <= 2 ||
(this.isPolygon() && coordsRing.length <= 3)
) {
this._flashLayer();
return;
}
}
// remove coordinate
coordsRing.splice(index, 1);
// set new latlngs to the polygon
this._layer.setLatLngs(coords);
// if a polygon has less than 3 vertices, remove all of them. We will remove only one here, the if-clause after that will handle the rest
if (this.isPolygon() && coordsRing.length <= 2) {
coordsRing.splice(0, coordsRing.length);
}
let layerRemoved = false;
// if the ring of the line has no coordinates left, remove the last coord too
if (coordsRing.length <= 1) {
coordsRing.splice(0, coordsRing.length);
// Clean up MultiPolygon
if (parentPath.length > 1 && indexPath.length > 1) {
coords = removeEmptyCoordRings(coords);
}
// set new coords
this._layer.setLatLngs(coords);
// re-enable editing so unnecessary markers are removed
this._initMarkers();
layerRemoved = true;
}
// if no coords are left, remove the layer
if (!hasValues(coords)) {
this._layer.remove();
}
// remove all empty coord-rings
coords = removeEmptyCoordRings(coords);
this._layer.setLatLngs(coords);
// remove empty marker arrays
this._markers = removeEmptyCoordRings(this._markers);
// No need to calculate the middle marker when the layer was removed
if (!layerRemoved) {
// get new markerArr because we cleaned up coords and markers array
markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;
// now handle the middle markers
// remove the marker and the middlemarkers next to it from the map
if (marker._middleMarkerPrev) {
this._markerGroup.removeLayer(marker._middleMarkerPrev);
}
if (marker._middleMarkerNext) {
this._markerGroup.removeLayer(marker._middleMarkerNext);
}
// remove the marker from the map
this._markerGroup.removeLayer(marker);
if (markerArr) {
let rightMarkerIndex;
let leftMarkerIndex;
if (this.isPolygon()) {
// find neighbor marker-indexes
rightMarkerIndex = (index + 1) % markerArr.length;
leftMarkerIndex = (index + (markerArr.length - 1)) % markerArr.length;
} else {
// find neighbor marker-indexes
leftMarkerIndex = index - 1 < 0 ? undefined : index - 1;
rightMarkerIndex =
index + 1 >= markerArr.length ? undefined : index + 1;
}
// don't create middlemarkers if there is only one marker left
if (rightMarkerIndex !== leftMarkerIndex) {
const leftM = markerArr[leftMarkerIndex];
const rightM = markerArr[rightMarkerIndex];
if (this.options.hideMiddleMarkers !== true) {
this._createMiddleMarker(leftM, rightM);
}
}
// remove the marker from the markers array
markerArr.splice(index, 1);
}
}
// fire edit event
this._fireEdit();
this._layerEdited = true;
// fire vertex removal event
// TODO: maybe fire latlng as well?
this._fireVertexRemoved(marker, indexPath);
this._fireChange(this._layer.getLatLngs(), 'Edit');
},
updatePolygonCoordsFromMarkerDrag(marker) {
// update polygon coords
const coords = this._layer.getLatLngs();
// get marker latlng
const latlng = marker.getLatLng();
// get indexPath of Marker
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
// update coord
const parent = indexPath.length > 1 ? get(coords, parentPath) : coords;
parent.splice(index, 1, latlng);
// set new coords on layer
this._layer.setLatLngs(coords);
},
_getNeighborMarkers(marker) {
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
// the markers neighbors
const markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;
// find the indizes of next and previous markers
const nextMarkerIndex = (index + 1) % markerArr.length;
const prevMarkerIndex = (index + (markerArr.length - 1)) % markerArr.length;
// get prev and next marker
const prevMarker = markerArr[prevMarkerIndex];
const nextMarker = markerArr[nextMarkerIndex];
return { prevMarker, nextMarker };
},
_checkMarkerAllowedToDrag(marker) {
const { prevMarker, nextMarker } = this._getNeighborMarkers(marker);
const prevLine = L.polyline([prevMarker.getLatLng(), marker.getLatLng()]);
const nextLine = L.polyline([marker.getLatLng(), nextMarker.getLatLng()]);
let prevLineIntersectionLen = lineIntersect(
this._layer.toGeoJSON(15),
prevLine.toGeoJSON(15)
).features.length;
let nextLineIntersectionLen = lineIntersect(
this._layer.toGeoJSON(15),
nextLine.toGeoJSON(15)
).features.length;
// The first and last line has one intersection fewer because they are not connected
if (marker.getLatLng() === this._markers[0][0].getLatLng()) {
nextLineIntersectionLen += 1;
} else if (
marker.getLatLng() ===
this._markers[0][this._markers[0].length - 1].getLatLng()
) {
prevLineIntersectionLen += 1;
}
// <= 2 the start and end point of the line always intersecting because they have the same coords.
if (prevLineIntersectionLen <= 2 && nextLineIntersectionLen <= 2) {
return false;
}
return true;
},
_onMarkerDragStart(e) {
const marker = e.target;
// When intersection is true while calling enable(), the cachedColor is already set
if (!this.cachedColor) {
this.cachedColor = this._layer.options.color;
}
if (!this._vertexValidation('move', e)) {
return;
}
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, marker);
this._fireMarkerDragStart(e, indexPath);
// if self intersection isn't allowed, save the coords upon dragstart
// in case we need to reset the layer
if (!this.options.allowSelfIntersection) {
this._coordsBeforeEdit = copyLatLngs(
this._layer,
this._layer.getLatLngs()
);
}
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit &&
this.hasSelfIntersection()
) {
this._markerAllowedToDrag = this._checkMarkerAllowedToDrag(marker);
} else {
this._markerAllowedToDrag = null;
}
},
_onMarkerDrag(e) {
// dragged marker
const marker = e.target;
if (!this._vertexValidationDrag(marker)) {
return;
}
const { indexPath, index, parentPath } = L.PM.Utils.findDeepMarkerIndex(
this._markers,
marker
);
// only continue if this is NOT a middle marker
if (!indexPath) {
return;
}
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit &&
this.hasSelfIntersection() &&
this._markerAllowedToDrag === false
) {
this._layer.setLatLngs(this._coordsBeforeEdit);
// re-enable markers for the new coords
this._initMarkers();
// check for selfintersection again (mainly to reset the style)
this._handleLayerStyle();
return;
}
this.updatePolygonCoordsFromMarkerDrag(marker);
// the dragged markers neighbors
const markerArr =
indexPath.length > 1 ? get(this._markers, parentPath) : this._markers;
// find the indizes of next and previous markers
const nextMarkerIndex = (index + 1) % markerArr.length;
const prevMarkerIndex = (index + (markerArr.length - 1)) % markerArr.length;
// update middle markers on the left and right
// be aware that "next" and "prev" might be interchanged, depending on the geojson array
const markerLatLng = marker.getLatLng();
// get latlng of prev and next marker
const prevMarkerLatLng = markerArr[prevMarkerIndex].getLatLng();
const nextMarkerLatLng = markerArr[nextMarkerIndex].getLatLng();
if (marker._middleMarkerNext) {
const middleMarkerNextLatLng = L.PM.Utils.calcMiddleLatLng(
this._map,
markerLatLng,
nextMarkerLatLng
);
marker._middleMarkerNext.setLatLng(middleMarkerNextLatLng);
}
if (marker._middleMarkerPrev) {
const middleMarkerPrevLatLng = L.PM.Utils.calcMiddleLatLng(
this._map,
markerLatLng,
prevMarkerLatLng
);
marker._middleMarkerPrev.setLatLng(middleMarkerPrevLatLng);
}
// if self intersection is not allowed, handle it
if (!this.options.allowSelfIntersection) {
this._handleLayerStyle();
}
this._fireMarkerDrag(e, indexPath);
this._fireChange(this._layer.getLatLngs(), 'Edit');
},
_onMarkerDragEnd(e) {
const marker = e.target;
if (!this._vertexValidationDragEnd(marker)) {
return;
}
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, marker);
// if self intersection is not allowed but this edit caused a self intersection,
// reset and cancel; do not fire events
let intersection = this.hasSelfIntersection();
if (
intersection &&
this.options.allowSelfIntersectionEdit &&
this._markerAllowedToDrag
) {
intersection = false;
}
const intersectionReset =
!this.options.allowSelfIntersection && intersection;
this._fireMarkerDragEnd(e, indexPath, intersectionReset);
if (intersectionReset) {
// reset coordinates
this._layer.setLatLngs(this._coordsBeforeEdit);
this._coordsBeforeEdit = null;
// re-enable markers for the new coords
this._initMarkers();
if (this.options.snappable) {
this._initSnappableMarkers();
}
// check for selfintersection again (mainly to reset the style)
this._handleLayerStyle();
this._fireLayerReset(e, indexPath);
return;
}
if (
!this.options.allowSelfIntersection &&
this.options.allowSelfIntersectionEdit
) {
this._handleLayerStyle();
}
// fire edit event
this._fireEdit();
this._layerEdited = true;
this._fireChange(this._layer.getLatLngs(), 'Edit');
},
_onVertexClick(e) {
const vertex = e.target;
if (vertex._dragging) {
return;
}
const { indexPath } = L.PM.Utils.findDeepMarkerIndex(this._markers, vertex);
this._fireVertexClick(e, indexPath);
},
});