@@ -5,7 +5,8 @@ L.Polyline.plotter = L.Polyline.extend({
5
5
_existingLatLngs : [ ] ,
6
6
options : {
7
7
weight : 2 ,
8
- color : '#000'
8
+ color : '#000' ,
9
+ readOnly : false ,
9
10
} ,
10
11
initialize : function ( latlngs , options ) {
11
12
this . _setExistingLatLngs ( latlngs ) ;
@@ -15,14 +16,39 @@ L.Polyline.plotter = L.Polyline.extend({
15
16
L . Polyline . prototype . onAdd . call ( this , map ) ;
16
17
this . _map = map ;
17
18
this . _plotExisting ( ) ;
18
- this . _bindMapClick ( ) ;
19
+ if ( ! this . options . readOnly ) {
20
+ this . _bindMapClick ( ) ;
21
+ }
19
22
} ,
20
23
setLatLngs : function ( latlngs ) {
21
24
L . Polyline . prototype . setLatLngs . call ( this , latlngs ) ;
22
25
} ,
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
+ } ,
23
46
_bindMapClick : function ( ) {
24
47
this . _map . on ( 'click' , this . _addNewMarker , this ) ;
25
48
} ,
49
+ _unbindMapClick : function ( ) {
50
+ this . _map . off ( 'click' , this . _addNewMarker , this ) ;
51
+ } ,
26
52
_setExistingLatLngs : function ( latlngs ) {
27
53
this . _existingLatLngs = latlngs ;
28
54
} ,
@@ -31,13 +57,29 @@ L.Polyline.plotter = L.Polyline.extend({
31
57
this . _redrawHalfwayPoints ( ) ;
32
58
} ,
33
59
_getNewMarker : function ( latlng , options ) {
34
- options . draggable = true ;
35
60
return new L . marker ( latlng , options ) ;
36
61
} ,
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
+ } ,
37
78
_addToMapAndBindMarker : function ( newMarker ) {
38
79
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
+ }
41
83
} ,
42
84
_removePoint : function ( e ) {
43
85
this . _map . removeLayer ( this . _lineMarkers [ this . _lineMarkers . indexOf ( e . target ) ] ) ;
@@ -52,11 +94,10 @@ L.Polyline.plotter = L.Polyline.extend({
52
94
} ,
53
95
_redrawHalfwayPoints : function ( ) {
54
96
for ( index in this . _halfwayPointMarkers ) {
55
- index = parseInt ( index ) ;
56
97
this . _map . removeLayer ( this . _halfwayPointMarkers [ index ] ) ;
57
98
}
99
+ this . _halfwayPointMarkers = [ ] ;
58
100
for ( index in this . _lineMarkers ) {
59
- index = parseInt ( index ) ;
60
101
if ( typeof this . _lineMarkers [ index + 1 ] === 'undefined' ) {
61
102
return ;
62
103
}
@@ -65,7 +106,9 @@ L.Polyline.plotter = L.Polyline.extend({
65
106
( this . _lineMarkers [ index ] . getLatLng ( ) . lng + this . _lineMarkers [ index + 1 ] . getLatLng ( ) . lng ) / 2 ,
66
107
] , { icon : this . _editIcon , opacity : 0.5 } ) . addTo ( this . _map ) ;
67
108
halfwayMarker . index = index ;
68
- halfwayMarker . on ( 'click' , this . _addHalfwayPoint , this ) ;
109
+ if ( ! this . options . readOnly ) {
110
+ this . _bindHalfwayMarker ( halfwayMarker ) ;
111
+ }
69
112
this . _halfwayPointMarkers . push ( halfwayMarker ) ;
70
113
}
71
114
} ,
@@ -96,5 +139,5 @@ L.Polyline.plotter = L.Polyline.extend({
96
139
} ) ;
97
140
98
141
L . Polyline . Plotter = function ( latlngs , options ) {
99
- return new L . Polyline . plotter ( latlngs , options ) ;
142
+ return new L . Polyline . plotter ( latlngs , options ) ;
100
143
} ;
0 commit comments