src/pyams_gis/resources/js/Draw/edit/handler/Edit.Poly.min.js
changeset 75 a430cc4ae715
equal deleted inserted replaced
74:31687784fa16 75:a430cc4ae715
       
     1 L.Edit=L.Edit||{},L.Edit.Poly=L.Handler.extend({initialize:function(t){this.latlngs=[t._latlngs],t._holes&&(this.latlngs=this.latlngs.concat(t._holes)),this._poly=t,this._poly.on("revert-edited",this._updateLatLngs,this)},_defaultShape:function(){return L.Polyline._flat?L.Polyline._flat(this._poly._latlngs)?this._poly._latlngs:this._poly._latlngs[0]:this._poly._latlngs},_eachVertexHandler:function(t){for(var i=0;i<this._verticesHandlers.length;i++)t(this._verticesHandlers[i])},addHooks:function(){this._initHandlers(),this._eachVertexHandler((function(t){t.addHooks()}))},removeHooks:function(){this._eachVertexHandler((function(t){t.removeHooks()}))},updateMarkers:function(){this._eachVertexHandler((function(t){t.updateMarkers()}))},_initHandlers:function(){this._verticesHandlers=[];for(var t=0;t<this.latlngs.length;t++)this._verticesHandlers.push(new L.Edit.PolyVerticesEdit(this._poly,this.latlngs[t],this._poly.options.poly))},_updateLatLngs:function(t){this.latlngs=[t.layer._latlngs],t.layer._holes&&(this.latlngs=this.latlngs.concat(t.layer._holes))}}),L.Edit.PolyVerticesEdit=L.Handler.extend({options:{icon:new L.DivIcon({iconSize:new L.Point(8,8),className:"leaflet-div-icon leaflet-editing-icon"}),touchIcon:new L.DivIcon({iconSize:new L.Point(20,20),className:"leaflet-div-icon leaflet-editing-icon leaflet-touch-icon"}),drawError:{color:"#b00b00",timeout:1e3}},initialize:function(t,i,e){L.Browser.touch&&(this.options.icon=this.options.touchIcon),this._poly=t,e&&e.drawError&&(e.drawError=L.Util.extend({},this.options.drawError,e.drawError)),this._latlngs=i,L.setOptions(this,e)},_defaultShape:function(){return L.Polyline._flat?L.Polyline._flat(this._latlngs)?this._latlngs:this._latlngs[0]:this._latlngs},addHooks:function(){var t=this._poly,i=t._path;t instanceof L.Polygon||(t.options.fill=!1,t.options.editing&&(t.options.editing.fill=!1)),i&&t.options.editing&&t.options.editing.className&&(t.options.original.className&&t.options.original.className.split(" ").forEach((function(t){L.DomUtil.removeClass(i,t)})),t.options.editing.className.split(" ").forEach((function(t){L.DomUtil.addClass(i,t)}))),t.setStyle(t.options.editing),this._poly._map&&(this._map=this._poly._map,this._markerGroup||this._initMarkers(),this._poly._map.addLayer(this._markerGroup))},removeHooks:function(){var t=this._poly,i=t._path;i&&t.options.editing&&t.options.editing.className&&(t.options.editing.className.split(" ").forEach((function(t){L.DomUtil.removeClass(i,t)})),t.options.original.className&&t.options.original.className.split(" ").forEach((function(t){L.DomUtil.addClass(i,t)}))),t.setStyle(t.options.original),t._map&&(t._map.removeLayer(this._markerGroup),delete this._markerGroup,delete this._markers)},updateMarkers:function(){this._markerGroup.clearLayers(),this._initMarkers()},_initMarkers:function(){this._markerGroup||(this._markerGroup=new L.LayerGroup),this._markers=[];var t,i,e,o,n,s,r=this._defaultShape();for(t=0,e=r.length;t<e;t++)(o=this._createMarker(r[t],t)).on("click",this._onMarkerClick,this),o.on("contextmenu",this._onContextMenu,this),this._markers.push(o);for(t=0,i=e-1;t<e;i=t++)(0!==t||L.Polygon&&this._poly instanceof L.Polygon)&&(n=this._markers[i],s=this._markers[t],this._createMiddleMarker(n,s),this._updatePrevNext(n,s))},_createMarker:function(t,i){var e=new L.Marker.Touch(t,{draggable:!0,icon:this.options.icon});return e._origLatLng=t,e._index=i,e.on("dragstart",this._onMarkerDragStart,this).on("drag",this._onMarkerDrag,this).on("dragend",this._fireEdit,this).on("touchmove",this._onTouchMove,this).on("touchend",this._fireEdit,this).on("MSPointerMove",this._onTouchMove,this).on("MSPointerUp",this._fireEdit,this),this._markerGroup.addLayer(e),e},_onMarkerDragStart:function(){this._poly.fire("editstart")},_spliceLatLngs:function(){var t=this._defaultShape(),i=[].splice.apply(t,arguments);return this._poly._convertLatLngs(t,!0),this._poly.redraw(),i},_removeMarker:function(t){var i=t._index;this._markerGroup.removeLayer(t),this._markers.splice(i,1),this._spliceLatLngs(i,1),this._updateIndexes(i,-1),t.off("dragstart",this._onMarkerDragStart,this).off("drag",this._onMarkerDrag,this).off("dragend",this._fireEdit,this).off("touchmove",this._onMarkerDrag,this).off("touchend",this._fireEdit,this).off("click",this._onMarkerClick,this).off("MSPointerMove",this._onTouchMove,this).off("MSPointerUp",this._fireEdit,this)},_fireEdit:function(){this._poly.edited=!0,this._poly.fire("edit"),this._poly._map.fire(L.Draw.Event.EDITVERTEX,{layers:this._markerGroup,poly:this._poly})},_onMarkerDrag:function(t){var i=t.target,e=this._poly,o=L.LatLngUtil.cloneLatLng(i._origLatLng);if(L.extend(i._origLatLng,i._latlng),e.options.poly){var n=e._map._editTooltip;if(!e.options.poly.allowIntersection&&e.intersects()){L.extend(i._origLatLng,o),i.setLatLng(o);var s=e.options.color;e.setStyle({color:this.options.drawError.color}),n&&n.updateContent({text:L.drawLocal.draw.handlers.polyline.error}),setTimeout((function(){e.setStyle({color:s}),n&&n.updateContent({text:L.drawLocal.edit.handlers.edit.tooltip.text,subtext:L.drawLocal.edit.handlers.edit.tooltip.subtext})}),1e3)}}i._middleLeft&&i._middleLeft.setLatLng(this._getMiddleLatLng(i._prev,i)),i._middleRight&&i._middleRight.setLatLng(this._getMiddleLatLng(i,i._next)),this._poly._bounds._southWest=L.latLng(1/0,1/0),this._poly._bounds._northEast=L.latLng(-1/0,-1/0);var r=this._poly.getLatLngs();this._poly._convertLatLngs(r,!0),this._poly.redraw(),this._poly.fire("editdrag")},_onMarkerClick:function(t){var i=L.Polygon&&this._poly instanceof L.Polygon?4:3,e=t.target;this._defaultShape().length<i||(this._removeMarker(e),this._updatePrevNext(e._prev,e._next),e._middleLeft&&this._markerGroup.removeLayer(e._middleLeft),e._middleRight&&this._markerGroup.removeLayer(e._middleRight),e._prev&&e._next?this._createMiddleMarker(e._prev,e._next):e._prev?e._next||(e._prev._middleRight=null):e._next._middleLeft=null,this._fireEdit())},_onContextMenu:function(t){var i=t.target;this._poly;this._poly._map.fire(L.Draw.Event.MARKERCONTEXT,{marker:i,layers:this._markerGroup,poly:this._poly}),L.DomEvent.stopPropagation},_onTouchMove:function(t){var i=this._map.mouseEventToLayerPoint(t.originalEvent.touches[0]),e=this._map.layerPointToLatLng(i),o=t.target;L.extend(o._origLatLng,e),o._middleLeft&&o._middleLeft.setLatLng(this._getMiddleLatLng(o._prev,o)),o._middleRight&&o._middleRight.setLatLng(this._getMiddleLatLng(o,o._next)),this._poly.redraw(),this.updateMarkers()},_updateIndexes:function(t,i){this._markerGroup.eachLayer((function(e){e._index>t&&(e._index+=i)}))},_createMiddleMarker:function(t,i){var e,o,n,s=this._getMiddleLatLng(t,i),r=this._createMarker(s);r.setOpacity(.6),t._middleRight=i._middleLeft=r,o=function(){r.off("touchmove",o,this);var n=i._index;r._index=n,r.off("click",e,this).on("click",this._onMarkerClick,this),s.lat=r.getLatLng().lat,s.lng=r.getLatLng().lng,this._spliceLatLngs(n,0,s),this._markers.splice(n,0,r),r.setOpacity(1),this._updateIndexes(n,1),i._index++,this._updatePrevNext(t,r),this._updatePrevNext(r,i),this._poly.fire("editstart")},n=function(){r.off("dragstart",o,this),r.off("dragend",n,this),r.off("touchmove",o,this),this._createMiddleMarker(t,r),this._createMiddleMarker(r,i)},e=function(){o.call(this),n.call(this),this._fireEdit()},r.on("click",e,this).on("dragstart",o,this).on("dragend",n,this).on("touchmove",o,this),this._markerGroup.addLayer(r)},_updatePrevNext:function(t,i){t&&(t._next=i),i&&(i._prev=t)},_getMiddleLatLng:function(t,i){var e=this._poly._map,o=e.project(t.getLatLng()),n=e.project(i.getLatLng());return e.unproject(o._add(n)._divideBy(2))}}),L.Polyline.addInitHook((function(){this.editing||(L.Edit.Poly&&(this.editing=new L.Edit.Poly(this),this.options.editable&&this.editing.enable()),this.on("add",(function(){this.editing&&this.editing.enabled()&&this.editing.addHooks()})),this.on("remove",(function(){this.editing&&this.editing.enabled()&&this.editing.removeHooks()})))}));