L.Clipper.min.js 4.4 KB

1
  1. L.Clipper=L.Evented.extend({options:{selectedPathOptions:{color:"#FF3399"}},initialize:function(e,t){L.Handler.prototype.initialize.call(this,e);L.Util.setOptions(this,t);if(!(this.options.featureGroup instanceof L.FeatureGroup)){throw new Error("options.featureGroup must be a L.FeatureGroup")}},enable:function(){if(this._enabled||!this._hasAvailableLayers()){return}this.fire("enabled",{handler:this.type});this._map.fire("draw:joinstart",{handler:this.type});L.Handler.prototype.enable.call(this);this.options.featureGroup.on("layeradd",this._enableLayerEdit,this).on("layerremove",this._disableLayerEdit,this)},disable:function(){if(!this._enabled){return}this.options.featureGroup.off("layeradd",this._enableLayerJoin,this).off("layerremove",this._disableLayerJoin,this);L.Handler.prototype.disable.call(this);this._map.fire("draw:editstop",{handler:this.type});this.fire("disabled",{handler:this.type})},addHooks:function(){var e=this._map;if(e){e.getContainer().focus();this.options.featureGroup.eachLayer(this._enableLayerEdit,this)}},removeHooks:function(){if(this._map){this.options.featureGroup.eachLayer(this._disableLayerEdit,this);this._uneditedLayerProps={};this._tooltip.dispose();this._tooltip=null;this._map.off("mousemove",this._onMouseMove,this)}},revertLayers:function(){this.options.featureGroup.eachLayer(function(e){this._revertLayer(e)},this)},save:function(){var e=new L.LayerGroup;this.options.featureGroup.eachLayer(function(t){if(t.edited){e.addLayer(t);t.edited=false}});this._map.fire("draw:edited",{layers:e})},_revertLayer:function(e){var t=L.Util.stamp(e);e.edited=false;if(this._uneditedLayerProps.hasOwnProperty(t)){if(e instanceof L.Polyline||e instanceof L.Polygon||e instanceof L.Rectangle){e.setLatLngs(this._uneditedLayerProps[t].latlngs)}else if(e instanceof L.Circle){e.setLatLng(this._uneditedLayerProps[t].latlng);e.setRadius(this._uneditedLayerProps[t].radius)}else if(e instanceof L.Marker){e.setLatLng(this._uneditedLayerProps[t].latlng)}}},_enableLayerEdit:function(e){var t=e.layer||e.target||e;t.on("click",this._subjectLayer,this)},_disableLayerEdit:function(e){var t=e.layer||e.target||e;t.off("click")},_onMouseMove:function(e){this._tooltip.updatePosition(e.latlng)},_hasAvailableLayers:function(){return this.options.featureGroup.getLayers().length!==0},_subjectLayer:function(e){var t=e.layer||e.target||e;if(!this._subject){this._subject=t}else{this._clipperLayer(e)}t.setStyle(this.options.selectedPathOptions)},_clipperLayer:function(e){var t=e.layer||e.target||e;this._clipper=t;t.setStyle(this.options.selectedPathOptions);this._layerClip()},_layerClip:function(){var e=this._subject.getLatLngs();var t=this._clipper.getLatLngs();var i=this._coordsToPoints(e);var r=this._coordsToPoints(t);var s=ClipperLib.Paths();var o=new ClipperLib.Clipper;for(var a=0,n=i.length;a<n;a++){o.AddPaths(i[a],ClipperLib.PolyType.ptSubject,true)}for(var p=0,l=r.length;p<l;p++){o.AddPaths(r[p],ClipperLib.PolyType.ptClip,true)}o.Execute(this._cliptype,s);this._solution=L.polygon(this._pointsToCoords([s],true));this.options.featureGroup.addLayer(this._solution);this.options.featureGroup.removeLayer(this._subject);this.options.featureGroup.removeLayer(this._clipper);this._subject=null;this._clipper=null},_pointAmplifier:function(){return Math.pow(10,13)},_coordsToPoints:function(e,t){var i=[],r=this._pointAmplifier();for(var s=0,o=e.length;s<o;s++){i.push([]);for(var a=0,n=e[s].length;a<n;a++){var p=e[s][a];i[s].push([]);for(var l=0,h=p.length;l<h;l++){var u=p[l];if(Array.isArray(u)){if(t){i[s][a].push({X:Math.round(u[1]*r),Y:Math.round(u[0]*r)})}else{i[s][a].push({X:Math.round(u[0]*r),Y:Math.round(u[1]*r)})}}else{i[s][a].push({X:Math.round(u.lng*r),Y:Math.round(u.lat*r)})}}}}return i},_pointsToCoords:function(e,t){var i=[],r=this._pointAmplifier();for(var s=0,o=e.length;s<o;s++){i.push([]);for(var a=0,n=e[s].length;a<n;a++){var p=e[s][a];i[s].push([]);for(var l=0,h=p.length;l<h;l++){var u=p[l];if(t){i[s][a].push([u.Y/r,u.X/r])}else{i[s][a].push([u.X/r,u.Y/r])}}}}return i}});L.Clipper.AND=L.Clipper.extend({_cliptype:ClipperLib.ClipType.ctIntersection});L.Clipper.Intersection=L.Clipper.AND;L.Clipper.OR=L.Clipper.extend({_cliptype:ClipperLib.ClipType.ctUnion});L.Clipper.Union=L.Clipper.OR;L.Clipper.NOT=L.Clipper.extend({_cliptype:ClipperLib.ClipType.ctDifference});L.Clipper.Difference=L.Clipper.NOT;L.Clipper.XOR=L.Clipper.extend({_cliptype:ClipperLib.ClipType.ctXor});L.Clipper.Xor=L.Clipper.XOR;