PageSourceSearch

https://rusaggression.gov.ua//zerum/leaflet/leaflet.draw/edit/handler/Edit.Poly.js

js rusaggression.gov.ua collected 2026-10-03 00:47:01 UTC 13,008 bytes, 520 lines download raw bytes

vendor: 13,008 bytes, lines 1-520
1L.Edit = L.Edit || {};
2
3/**
4 * @class L.Edit.Polyline
5 * @aka L.Edit.Poly
6 * @aka Edit.Poly
7 */
8L.Edit.Poly = L.Handler.extend({
9	// @method initialize(): void
10	initialize: function (poly) {
11
12		this.latlngs = [poly._latlngs];
13		if (poly._holes) {
14			this.latlngs = this.latlngs.concat(poly._holes);
15		}
16
17		this._poly = poly;
18
19		this._poly.on('revert-edited', this._updateLatLngs, this);
20	},
21
22	// Compatibility method to normalize Poly* objects
23	// between 0.7.x and 1.0+
24	_defaultShape: function () {
25		if (!L.Polyline._flat) {
26			return this._poly._latlngs;
27		}
28		return L.Polyline._flat(this._poly._latlngs) ? this._poly._latlngs : this._poly._latlngs[0];
29	},
30
31	_eachVertexHandler: function (callback) {
32		for (var i = 0; i < this._verticesHandlers.length; i++) {
33			callback(this._verticesHandlers[i]);
34		}
35	},
36
37	// @method addHooks(): void
38	// Add listener hooks to this handler
39	addHooks: function () {
40		this._initHandlers();
41		this._eachVertexHandler(function (handler) {
42			handler.addHooks();
43		});
44	},
45
46	// @method removeHooks(): void
47	// Remove listener hooks from this handler
48	removeHooks: function () {
49		this._eachVertexHandler(function (handler) {
50			handler.removeHooks();
51		});
52	},
53
54	// @method updateMarkers(): void
55	// Fire an update for each vertex handler
56	updateMarkers: function () {
57		this._eachVertexHandler(function (handler) {
58			handler.updateMarkers();
59		});
60	},
61
62	_initHandlers: function () {
63		this._verticesHandlers = [];
64		for (var i = 0; i < this.latlngs.length; i++) {
65			this._verticesHandlers.push(new L.Edit.PolyVerticesEdit(this._poly, this.latlngs[i], this._poly.options.poly));
66		}
67	},
68
69	_updateLatLngs: function (e) {
70		this.latlngs = [e.layer._latlngs];
71		if (e.layer._holes) {
72			this.latlngs = this.latlngs.concat(e.layer._holes);
73		}
74	}
75
76});
77
78/**
79 * @class L.Edit.PolyVerticesEdit
80 * @aka Edit.PolyVerticesEdit
81 */
82L.Edit.PolyVerticesEdit = L.Handler.extend({
83	options: {
84		icon: new L.DivIcon({
85			iconSize: new L.Point(8, 8),
86			className: 'leaflet-div-icon leaflet-editing-icon'
87		}),
88		touchIcon: new L.DivIcon({
89			iconSize: new L.Point(20, 20),
90			className: 'leaflet-div-icon leaflet-editing-icon leaflet-touch-icon'
91		}),
92		drawError: {
93			color: '#b00b00',
94			timeout: 1000
95		}
96
97
98	},
99
100	// @method intialize(): void
101	initialize: function (poly, latlngs, options) {
102		// if touch, switch to touch icon
103		if (L.Browser.touch) {
104			this.options.icon = this.options.touchIcon;
105		}
106		this._poly = poly;
107
108		if (options && options.drawError) {
109			options.drawError = L.Util.extend({}, this.options.drawError, options.drawError);
110		}
111
112		this._latlngs = latlngs;
113
114		L.setOptions(this, options);
115	},
116
117	// Compatibility method to normalize Poly* objects
118	// between 0.7.x and 1.0+
119	_defaultShape: function () {
120		if (!L.Polyline._flat) {
121			return this._latlngs;
122		}
123		return L.Polyline._flat(this._latlngs) ? this._latlngs : this._latlngs[0];
124	},
125
126	// @method addHooks(): void
127	// Add listener hooks to this handler.
128	addHooks: function () {
129		var poly = this._poly;
130		var path = poly._path;
131
132		if (!(poly instanceof L.Polygon)) {
133			poly.options.fill = false;
134			if (poly.options.editing) {
135				poly.options.editing.fill = false;
136			}
137		}
138
139		if (path) {
140			if (poly.options.editing && poly.options.editing.className) {
141				if (poly.options.original.className) {
142					poly.options.original.className.split(' ').forEach(function (className) {
143						L.DomUtil.removeClass(path, className);
144					});
145				}
146				poly.options.editing.className.split(' ').forEach(function (className) {
147					L.DomUtil.addClass(path, className);
148				});
149			}
150		}
151
152		poly.setStyle(poly.options.editing);
153
154		if (this._poly._map) {
155
156			this._map = this._poly._map; // Set map
157
158			if (!this._markerGroup) {
159				this._initMarkers();
160			}
161			this._poly._map.addLayer(this._markerGroup);
162		}
163	},
164
165	// @method removeHooks(): void
166	// Remove listener hooks from this handler.
167	removeHooks: function () {
168		var poly = this._poly;
169		var path = poly._path;
170
171		if (path) {
172			if (poly.options.editing && poly.options.editing.className) {
173				poly.options.editing.className.split(' ').forEach(function (className) {
174					L.DomUtil.removeClass(path, className);
175				});
176				if (poly.options.original.className) {
177					poly.options.original.className.split(' ').forEach(function (className) {
178						L.DomUtil.addClass(path, className);
179					});
180				}
181			}
182		}
183
184		poly.setStyle(poly.options.original);
185
186		if (poly._map) {
187			poly._map.removeLayer(this._markerGroup);
188			delete this._markerGroup;
189			delete this._markers;
190		}
191	},
192
193	// @method updateMarkers(): void
194	// Clear markers and update their location
195	updateMarkers: function () {
196		this._markerGroup.clearLayers();
197		this._initMarkers();
198	},
199
200	_initMarkers: function () {
201		if (!this._markerGroup) {
202			this._markerGroup = new L.LayerGroup();
203		}
204		this._markers = [];
205
206		var latlngs = this._defaultShape(),
207			i, j, len, marker;
208
209		for (i = 0, len = latlngs.length; i < len; i++) {
210
211			marker = this._createMarker(latlngs[i], i);
212			marker.on('click', this._onMarkerClick, this);
213			marker.on('contextmenu', this._onContextMenu, this);
214			this._markers.push(marker);
215		}
216
217		var markerLeft, markerRight;
218
219		for (i = 0, j = len - 1; i < len; j = i++) {
220			if (i === 0 && !(L.Polygon && (this._poly instanceof L.Polygon))) {
221				continue;
222			}
223
224			markerLeft = this._markers[j];
225			markerRight = this._markers[i];
226
227			this._createMiddleMarker(markerLeft, markerRight);
228			this._updatePrevNext(markerLeft, markerRight);
229		}
230	},
231
232	_createMarker: function (latlng, index) {
233		// Extending L.Marker in TouchEvents.js to include touch.
234		var marker = new L.Marker.Touch(latlng, {
235			draggable: true,
236			icon: this.options.icon,
237		});
238
239		marker._origLatLng = latlng;
240		marker._index = index;
241
242		marker
243			.on('dragstart', this._onMarkerDragStart, this)
244			.on('drag', this._onMarkerDrag, this)
245			.on('dragend', this._fireEdit, this)
246			.on('touchmove', this._onTouchMove, this)
247			.on('touchend', this._fireEdit, this)
248			.on('MSPointerMove', this._onTouchMove, this)
249			.on('MSPointerUp', this._fireEdit, this);
250
251		this._markerGroup.addLayer(marker);
252
253		return marker;
254	},
255
256	_onMarkerDragStart: function () {
257		this._poly.fire('editstart');
258	},
259
260	_spliceLatLngs: function () {
261		var latlngs = this._defaultShape();
262		var removed = [].splice.apply(latlngs, arguments);
263		this._poly._convertLatLngs(latlngs, true);
264		this._poly.redraw();
265		return removed;
266	},
267
268	_removeMarker: function (marker) {
269		var i = marker._index;
270
271		this._markerGroup.removeLayer(marker);
272		this._markers.splice(i, 1);
273		this._spliceLatLngs(i, 1);
274		this._updateIndexes(i, -1);
275
276		marker
277			.off('dragstart', this._onMarkerDragStart, this)
278			.off('drag', this._onMarkerDrag, this)
279			.off('dragend', this._fireEdit, this)
280			.off('touchmove', this._onMarkerDrag, this)
281			.off('touchend', this._fireEdit, this)
282			.off('click', this._onMarkerClick, this)
283			.off('MSPointerMove', this._onTouchMove, this)
284			.off('MSPointerUp', this._fireEdit, this);
285	},
286
287	_fireEdit: function () {
288		this._poly.edited = true;
289		this._poly.fire('edit');
290		this._poly._map.fire(L.Draw.Event.EDITVERTEX, {layers: this._markerGroup, poly: this._poly});
291	},
292
293	_onMarkerDrag: function (e) {
294		var marker = e.target;
295		var poly = this._poly;
296
297		var oldOrigLatLng = L.LatLngUtil.cloneLatLng(marker._origLatLng);
298		L.extend(marker._origLatLng, marker._latlng);
299		if (poly.options.poly) {
300			var tooltip = poly._map._editTooltip; // Access the tooltip
301
302			// If we don't allow intersections and the polygon intersects
303			if (!poly.options.poly.allowIntersection && poly.intersects()) {
304				L.extend(marker._origLatLng, oldOrigLatLng);
305				marker.setLatLng(oldOrigLatLng);
306				var originalColor = poly.options.color;
307				poly.setStyle({color: this.options.drawError.color});
308				if (tooltip) {
309					tooltip.updateContent({
310						text: L.drawLocal.draw.handlers.polyline.error
311					});
312				}
313
314				// Reset everything back to normal after a second
315				setTimeout(function () {
316					poly.setStyle({color: originalColor});
317					if (tooltip) {
318						tooltip.updateContent({
319							text: L.drawLocal.edit.handlers.edit.tooltip.text,
320							subtext: L.drawLocal.edit.handlers.edit.tooltip.subtext
321						});
322					}
323				}, 1000);
324			}
325		}
326
327		if (marker._middleLeft) {
328			marker._middleLeft.setLatLng(this._getMiddleLatLng(marker._prev, marker));
329		}
330		if (marker._middleRight) {
331			marker._middleRight.setLatLng(this._getMiddleLatLng(marker, marker._next));
332		}
333
334		//refresh the bounds when draging
335		this._poly._bounds._southWest = L.latLng(Infinity, Infinity);
336		this._poly._bounds._northEast = L.latLng(-Infinity, -Infinity);
337		var latlngs = this._poly.getLatLngs();
338		this._poly._convertLatLngs(latlngs, true);
339		this._poly.redraw();
340		this._poly.fire('editdrag');
341	},
342
343	_onMarkerClick: function (e) {
344
345		var minPoints = L.Polygon && (this._poly instanceof L.Polygon) ? 4 : 3,
346			marker = e.target;
347
348		// If removing this point would create an invalid polyline/polygon don't remove
349		if (this._defaultShape().length < minPoints) {
350			return;
351		}
352
353		// remove the marker
354		this._removeMarker(marker);
355
356		// update prev/next links of adjacent markers
357		this._updatePrevNext(marker._prev, marker._next);
358
359		// remove ghost markers near the removed marker
360		if (marker._middleLeft) {
361			this._markerGroup.removeLayer(marker._middleLeft);
362		}
363		if (marker._middleRight) {
364			this._markerGroup.removeLayer(marker._middleRight);
365		}
366
367		// create a ghost marker in place of the removed one
368		if (marker._prev && marker._next) {
369			this._createMiddleMarker(marker._prev, marker._next);
370
371		} else if (!marker._prev) {
372			marker._next._middleLeft = null;
373
374		} else if (!marker._next) {
375			marker._prev._middleRight = null;
376		}
377
378		this._fireEdit();
379	},
380
381	_onContextMenu: function (e) {
382		var marker = e.target;
383		var poly = this._poly;
384		this._poly._map.fire(L.Draw.Event.MARKERCONTEXT, {marker: marker, layers: this._markerGroup, poly: this._poly});
385		L.DomEvent.stopPropagation;
386	},
387
388	_onTouchMove: function (e) {
389
390		var layerPoint = this._map.mouseEventToLayerPoint(e.originalEvent.touches[0]),
391			latlng = this._map.layerPointToLatLng(layerPoint),
392			marker = e.target;
393
394		L.extend(marker._origLatLng, latlng);
395
396		if (marker._middleLeft) {
397			marker._middleLeft.setLatLng(this._getMiddleLatLng(marker._prev, marker));
398		}
399		if (marker._middleRight) {
400			marker._middleRight.setLatLng(this._getMiddleLatLng(marker, marker._next));
401		}
402
403		this._poly.redraw();
404		this.updateMarkers();
405	},
406
407	_updateIndexes: function (index, delta) {
408		this._markerGroup.eachLayer(function (marker) {
409			if (marker._index > index) {
410				marker._index += delta;
411			}
412		});
413	},
414
415	_createMiddleMarker: function (marker1, marker2) {
416		var latlng = this._getMiddleLatLng(marker1, marker2),
417			marker = this._createMarker(latlng),
418			onClick,
419			onDragStart,
420			onDragEnd;
421
422		marker.setOpacity(0.6);
423
424		marker1._middleRight = marker2._middleLeft = marker;
425
426		onDragStart = function () {
427			marker.off('touchmove', onDragStart, this);
428			var i = marker2._index;
429
430			marker._index = i;
431
432			marker
433				.off('click', onClick, this)
434				.on('click', this._onMarkerClick, this);
435
436			latlng.lat = marker.getLatLng().lat;
437			latlng.lng = marker.getLatLng().lng;
438			this._spliceLatLngs(i, 0, latlng);
439			this._markers.splice(i, 0, marker);
440
441			marker.setOpacity(1);
442
443			this._updateIndexes(i, 1);
444			marker2._index++;
445			this._updatePrevNext(marker1, marker);
446			this._updatePrevNext(marker, marker2);
447
448			this._poly.fire('editstart');
449		};
450
451		onDragEnd = function () {
452			marker.off('dragstart', onDragStart, this);
453			marker.off('dragend', onDragEnd, this);
454			marker.off('touchmove', onDragStart, this);
455
456			this._createMiddleMarker(marker1, marker);
457			this._createMiddleMarker(marker, marker2);
458		};
459
460		onClick = function () {
461			onDragStart.call(this);
462			onDragEnd.call(this);
463			this._fireEdit();
464		};
465
466		marker
467			.on('click', onClick, this)
468			.on('dragstart', onDragStart, this)
469			.on('dragend', onDragEnd, this)
470			.on('touchmove', onDragStart, this);
471
472		this._markerGroup.addLayer(marker);
473	},
474
475	_updatePrevNext: function (marker1, marker2) {
476		if (marker1) {
477			marker1._next = marker2;
478		}
479		if (marker2) {
480			marker2._prev = marker1;
481		}
482	},
483
484	_getMiddleLatLng: function (marker1, marker2) {
485		var map = this._poly._map,
486			p1 = map.project(marker1.getLatLng()),
487			p2 = map.project(marker2.getLatLng());
488
489		return map.unproject(p1._add(p2)._divideBy(2));
490	}
491});
492
493L.Polyline.addInitHook(function () {
494
495	// Check to see if handler has already been initialized. This is to support versions of Leaflet that still have L.Handler.PolyEdit
496	if (this.editing) {
497		return;
498	}
499
500	if (L.Edit.Poly) {
501
502		this.editing = new L.Edit.Poly(this);
503
504		if (this.options.editable) {
505			this.editing.enable();
506		}
507	}
508
509	this.on('add', function () {
510		if (this.editing && this.editing.enabled()) {
511			this.editing.addHooks();
512		}
513	});
514
515	this.on('remove', function () {
516		if (this.editing && this.editing.enabled()) {
517			this.editing.removeHooks();
518		}
519	});
520});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.