@@ -5,7 +5,8 @@ L.Polyline.plotter = L.Polyline.extend({
55 _existingLatLngs : [ ] ,
66 options : {
77 weight : 2 ,
8- color : '#000'
8+ color : '#000' ,
9+ readOnly : false ,
910 } ,
1011 initialize : function ( latlngs , options ) {
1112 this . _setExistingLatLngs ( latlngs ) ;
@@ -15,14 +16,39 @@ L.Polyline.plotter = L.Polyline.extend({
1516 L . Polyline . prototype . onAdd . call ( this , map ) ;
1617 this . _map = map ;
1718 this . _plotExisting ( ) ;
18- this . _bindMapClick ( ) ;
19+ if ( ! this . options . readOnly ) {
20+ this . _bindMapClick ( ) ;
21+ }
1922 } ,
2023 setLatLngs : function ( latlngs ) {
2124 L . Polyline . prototype . setLatLngs . call ( this , latlngs ) ;
2225 } ,
26+ setReadOnly : function ( readOnly ) {
27+ if ( readOnly && ! this . options . readOnly ) {
28+ var markerFunction = '_unbindMarkerEvents' ;
29+ var halfwayMarkerFunction = '_unbindHalfwayMarker' ;
30+ this . _unbindMapClick ( ) ;
31+ } else if ( ! readOnly && this . options . readOnly ) {
32+ var markerFunction = '_bindMarkerEvents' ;
33+ var halfwayMarkerFunction = '_bindMarkerEvents' ;
34+ this . _bindMapClick ( ) ;
35+ }
36+ if ( typeof markerFunction !== 'undefined' ) {
37+ this . options . readOnly = readOnly ;
38+ for ( index in this . _halfwayPointMarkers ) {
39+ this [ halfwayMarkerFunction ] ( this . _halfwayPointMarkers [ index ] ) ;
40+ }
41+ for ( index in this . _lineMarkers ) {
42+ this [ markerFunction ] ( this . _lineMarkers [ index ] ) ;
43+ }
44+ }
45+ } ,
2346 _bindMapClick : function ( ) {
2447 this . _map . on ( 'click' , this . _addNewMarker , this ) ;
2548 } ,
49+ _unbindMapClick : function ( ) {
50+ this . _map . off ( 'click' , this . _addNewMarker , this ) ;
51+ } ,
2652 _setExistingLatLngs : function ( latlngs ) {
2753 this . _existingLatLngs = latlngs ;
2854 } ,
@@ -31,13 +57,29 @@ L.Polyline.plotter = L.Polyline.extend({
3157 this . _redrawHalfwayPoints ( ) ;
3258 } ,
3359 _getNewMarker : function ( latlng , options ) {
34- options . draggable = true ;
3560 return new L . marker ( latlng , options ) ;
3661 } ,
62+ _unbindMarkerEvents : function ( marker ) {
63+ marker . off ( 'click' , this . _removePoint , this ) ;
64+ marker . off ( 'drag' , this . _replot , this ) ;
65+ marker . dragging . disable ( )
66+ } ,
67+ _bindMarkerEvents : function ( marker ) {
68+ marker . on ( 'click' , this . _removePoint , this ) ;
69+ marker . on ( 'drag' , this . _replot , this ) ;
70+ marker . dragging . enable ( )
71+ } ,
72+ _bindHalfwayMarker : function ( marker ) {
73+ marker . on ( 'click' , this . _addHalfwayPoint , this ) ;
74+ } ,
75+ _unbindHalfwayMarker : function ( marker ) {
76+ marker . off ( 'click' , this . _addHalfwayPoint , this ) ;
77+ } ,
3778 _addToMapAndBindMarker : function ( newMarker ) {
3879 newMarker . addTo ( this . _map ) ;
39- newMarker . on ( 'click' , this . _removePoint , this ) ;
40- newMarker . on ( 'drag' , this . _replot , this ) ;
80+ if ( ! this . options . readOnly ) {
81+ this . _bindMarkerEvents ( newMarker ) ;
82+ }
4183 } ,
4284 _removePoint : function ( e ) {
4385 this . _map . removeLayer ( this . _lineMarkers [ this . _lineMarkers . indexOf ( e . target ) ] ) ;
@@ -52,11 +94,10 @@ L.Polyline.plotter = L.Polyline.extend({
5294 } ,
5395 _redrawHalfwayPoints : function ( ) {
5496 for ( index in this . _halfwayPointMarkers ) {
55- index = parseInt ( index ) ;
5697 this . _map . removeLayer ( this . _halfwayPointMarkers [ index ] ) ;
5798 }
99+ this . _halfwayPointMarkers = [ ] ;
58100 for ( index in this . _lineMarkers ) {
59- index = parseInt ( index ) ;
60101 if ( typeof this . _lineMarkers [ index + 1 ] === 'undefined' ) {
61102 return ;
62103 }
@@ -65,7 +106,9 @@ L.Polyline.plotter = L.Polyline.extend({
65106 ( this . _lineMarkers [ index ] . getLatLng ( ) . lng + this . _lineMarkers [ index + 1 ] . getLatLng ( ) . lng ) / 2 ,
66107 ] , { icon : this . _editIcon , opacity : 0.5 } ) . addTo ( this . _map ) ;
67108 halfwayMarker . index = index ;
68- halfwayMarker . on ( 'click' , this . _addHalfwayPoint , this ) ;
109+ if ( ! this . options . readOnly ) {
110+ this . _bindHalfwayMarker ( halfwayMarker ) ;
111+ }
69112 this . _halfwayPointMarkers . push ( halfwayMarker ) ;
70113 }
71114 } ,
@@ -96,5 +139,5 @@ L.Polyline.plotter = L.Polyline.extend({
96139} ) ;
97140
98141L . Polyline . Plotter = function ( latlngs , options ) {
99- return new L . Polyline . plotter ( latlngs , options ) ;
142+ return new L . Polyline . plotter ( latlngs , options ) ;
100143} ;
0 commit comments