diff --git a/dist/leaflet.pm.js b/dist/leaflet.pm.js index e69707aa..6477a930 100644 --- a/dist/leaflet.pm.js +++ b/dist/leaflet.pm.js @@ -21,6 +21,11 @@ L.PM = L.PM || { + }, + enableDraw: function(options) { + + L.PM.Draw.Poly.enable(options.map); + }, addControls: function(map) { @@ -31,13 +36,12 @@ L.PM = L.PM || { }, 'afterClick': function(e) { - var newPoly; - if(this.toggled()) { - newPoly = new L.PM.Draw.Poly(map); - newPoly.enable(); + L.PM.enableDraw({ + map: map + }); } else { - newPoly.disable(); + map.disableDraw(); } }, 'doToggle': true, @@ -156,15 +160,13 @@ L.Control.PMButton = L.Control.extend({ }); -L.PM.Draw.Poly = L.Class.extend({ +L.PM.Draw.Poly = { - initialize: function(map) { - this._map = map; + enable: function(map) { + var self = this; - }, - - enable: function() { + this._map = map; this._layerGroup = new L.LayerGroup(); this._layerGroup.addTo(this._map); @@ -176,6 +178,10 @@ L.PM.Draw.Poly = L.Class.extend({ this._map.on('click', this._createPolygonPoint, this); + this._map.disableDraw = function() { + self.disable(); + }; + }, disable: function() { @@ -222,7 +228,7 @@ L.PM.Draw.Poly = L.Class.extend({ return marker; }, -}); +}; L.PM.Edit.Poly = L.Class.extend({ diff --git a/dist/leaflet.pm.min.js b/dist/leaflet.pm.min.js index e03c9ef7..6ea28750 100644 --- a/dist/leaflet.pm.min.js +++ b/dist/leaflet.pm.min.js @@ -1 +1 @@ -L.PM=L.PM||{initialize:function(){var t=function(){this.pm=new L.PM.Edit.LayerGroup(this)};L.LayerGroup.addInitHook(t);var e=function(){this.pm=new L.PM.Edit.Poly(this)};L.Polygon.addInitHook(e)},addControls:function(t){var e={iconUrl:"assets/icons/polygon.png",onClick:function(){},afterClick:function(e){var i;this.toggled()?(i=new L.PM.Draw.Poly(t),i.enable()):i.disable()},doToggle:!0,toggleStatus:!1},i=new L.Control.PMButton(e).addTo(t);return t.on("pm:create",function(){i._clicked()}),[i]},Edit:{},Draw:{}},L.Control.PMButton=L.Control.extend({options:{position:"topleft"},initialize:function(t){this._button={},this.setButton(t)},onAdd:function(t){this._map=t;var e=L.DomUtil.create("div","leaflet-control-button");return this._container=e,this._makeButton(this._button),this._container},onRemove:function(t){},setButton:function(t){var e={iconUrl:t.iconUrl,onClick:t.onClick,afterClick:t.afterClick,doToggle:t.doToggle,toggleStatus:!1};this._button=e},getText:function(){return this._button.text},getIconUrl:function(){return this._button.iconUrl},destroy:function(){this._button={},this._update()},toggle:function(t){"boolean"==typeof t?this._button.toggleStatus=t:this._button.toggleStatus=!this._button.toggleStatus},toggled:function(){return this._button.toggleStatus},onCreate:function(){this.toggle(!1)},_makeButton:function(t){var e=L.DomUtil.create("div","leaflet-buttons-control-button",this._container);t.toggleStatus&&L.DomUtil.addClass(e,"active");L.DomUtil.create("img","control-icon",e);return L.DomEvent.addListener(e,"click",L.DomEvent.stop).addListener(e,"click",t.onClick,this).addListener(e,"click",this._clicked,this).addListener(e,"click",t.afterClick,this),L.DomEvent.disableClickPropagation(e),e},_clicked:function(){this._button.doToggle&&(this._button.toggleStatus?L.DomUtil.removeClass(this._container.childNodes[0],"active"):L.DomUtil.addClass(this._container.childNodes[0],"active"),this.toggle())}}),L.PM.Draw.Poly=L.Class.extend({initialize:function(t){this._map=t},enable:function(){this._layerGroup=new L.LayerGroup,this._layerGroup.addTo(this._map),this._polyline=L.polyline([],{color:"red"}),this._layerGroup.addLayer(this._polyline),this._map._container.style.cursor="crosshair",this._map.on("click",this._createPolygonPoint,this)},disable:function(){this._map._container.style.cursor="default",this._map.off("click",this._createPolygonPoint),this._map.removeLayer(this._layerGroup)},_createPolygonPoint:function(t){var e=0===this._polyline.getLatLngs().length?!0:!1;this._polyline.addLatLng(t.latlng),this._createMarker(t.latlng,e)},_finishPolygon:function(){var t=this._polyline.getLatLngs(),e=L.polygon(t).addTo(this._map);e.pm.toggleEdit(),this.disable(),this._map.fireEvent("pm:create",e)},_createMarker:function(t,e){var i=new L.Marker(t,{draggable:!1,icon:L.divIcon({className:"marker-icon"})});return this._layerGroup.addLayer(i),e&&i.on("click",this._finishPolygon,this),i}}),L.PM.Edit.Poly=L.Class.extend({initialize:function(t){this._poly=t,this._enabled=!1},toggleEdit:function(){this.enabled()?this.disable():this.enable()},enable:function(){this.enabled()||(this._enabled=!0,this._markerGroup||(this._markerGroup=new L.LayerGroup,this._initMarkers()),this._poly._map.addLayer(this._markerGroup))},enabled:function(){return this._enabled},disable:function(){this._enabled=!1,this._poly._map.removeLayer(this._markerGroup)},_initMarkers:function(){this._markers=[];for(var t=this._poly._latlngs[0],e=0;e=t.length?0:r+1;this._createMiddleMarker(this._markers[r],this._markers[n])}},_createMarker:function(t,e){var i=new L.Marker(t,{draggable:!0,icon:L.divIcon({className:"marker-icon"})});return i._origLatLng=t,i._index=e,i.on("drag",this._onMarkerDrag,this),i.on("dragend",this._onMarkerDragEnd,this),i.on("contextmenu",this._removeMarker,this),this._markerGroup.addLayer(i),i},_createMiddleMarker:function(t,e){var i=this,r=this._calcMiddleLatLng(t,e),n=this._createMarker(r),a=L.divIcon({className:"marker-icon marker-icon-middle"});n.setIcon(a),t._middleMarkerRight=n,e._middleMarkerLeft=n,n.on("dragstart",function(){i._addMarker(n,t,e)}),n.on("click",function(){i._addMarker(n,t,e)})},_addMarker:function(t,e,i){var r=L.divIcon({className:"marker-icon"});t.setIcon(r),t.off("dragstart"),t.off("click");var n=t.getLatLng(),a=this._poly._latlngs[0],o=e._index+1;a.splice(o,0,n),t._origLatLng=a[o],this._markers.splice(o,0,t);for(var s=0;sr-1?this._markers.length-1:r-1,a=r+1>=this._markers.length?0:r+1,o=this._markers[n],s=this._markers[a];this._createMiddleMarker(o,s),this._markers.splice(r,1);for(var l=0;l=this._markers.length?0:e._index+1,r=e._index-1<0?this._markers.length-1:e._index-1;L.extend(e._origLatLng,e._latlng),this._poly.redraw();var n=this._calcMiddleLatLng(e,this._markers[i]);e._middleMarkerRight.setLatLng(n);var a=this._calcMiddleLatLng(e,this._markers[r]);e._middleMarkerLeft.setLatLng(a)},_onMarkerDragEnd:function(t){t.target;this._fireEdit()},_fireEdit:function(){this._poly.edited=!0,this._poly.fireEvent("pm:edit")},_calcMiddleLatLng:function(t,e){var i=this._poly._map,r=i.project(t.getLatLng()),n=i.project(e.getLatLng()),a=i.unproject(r._add(n)._divideBy(2));return a}}),L.PM.Edit.LayerGroup=L.Class.extend({initialize:function(t){this._layerGroup=t,this._layers=t.getLayers();for(var e=0;e=t.length?0:r+1;this._createMiddleMarker(this._markers[r],this._markers[n])}},_createMarker:function(t,e){var i=new L.Marker(t,{draggable:!0,icon:L.divIcon({className:"marker-icon"})});return i._origLatLng=t,i._index=e,i.on("drag",this._onMarkerDrag,this),i.on("dragend",this._onMarkerDragEnd,this),i.on("contextmenu",this._removeMarker,this),this._markerGroup.addLayer(i),i},_createMiddleMarker:function(t,e){var i=this,r=this._calcMiddleLatLng(t,e),n=this._createMarker(r),a=L.divIcon({className:"marker-icon marker-icon-middle"});n.setIcon(a),t._middleMarkerRight=n,e._middleMarkerLeft=n,n.on("dragstart",function(){i._addMarker(n,t,e)}),n.on("click",function(){i._addMarker(n,t,e)})},_addMarker:function(t,e,i){var r=L.divIcon({className:"marker-icon"});t.setIcon(r),t.off("dragstart"),t.off("click");var n=t.getLatLng(),a=this._poly._latlngs[0],o=e._index+1;a.splice(o,0,n),t._origLatLng=a[o],this._markers.splice(o,0,t);for(var s=0;sr-1?this._markers.length-1:r-1,a=r+1>=this._markers.length?0:r+1,o=this._markers[n],s=this._markers[a];this._createMiddleMarker(o,s),this._markers.splice(r,1);for(var l=0;l=this._markers.length?0:e._index+1,r=e._index-1<0?this._markers.length-1:e._index-1;L.extend(e._origLatLng,e._latlng),this._poly.redraw();var n=this._calcMiddleLatLng(e,this._markers[i]);e._middleMarkerRight.setLatLng(n);var a=this._calcMiddleLatLng(e,this._markers[r]);e._middleMarkerLeft.setLatLng(a)},_onMarkerDragEnd:function(t){t.target;this._fireEdit()},_fireEdit:function(){this._poly.edited=!0,this._poly.fireEvent("pm:edit")},_calcMiddleLatLng:function(t,e){var i=this._poly._map,r=i.project(t.getLatLng()),n=i.project(e.getLatLng()),a=i.unproject(r._add(n)._divideBy(2));return a}}),L.PM.Edit.LayerGroup=L.Class.extend({initialize:function(t){this._layerGroup=t,this._layers=t.getLayers();for(var e=0;e