Option to ignore old CDRs, RT#81480
[freeside.git] / httemplate / elements / polygon.html
1 <%init>
2 my %opt = @_;
3 my $field = $opt{'field'};
4 my $id = $opt{'id'} || $opt{'field'};
5 my $div_id = "div_$id";
6
7 my $vertices_json = $opt{'curr_value'} || '[]';
8
9 my $apikey = FS::Conf->new->config('google_maps_api_key');
10
11 </%init>
12 <& hidden.html, %opt &>
13 <div id="<% $div_id %>" style="height: 600px; width: 600px"></div>
14 <div id="<% $div_id %>_hint" style="width: 100%; border: 2px solid black; text-align: center; box-sizing: border-box; padding: 4px">&nbsp;</div>
15
16 <script src="https://maps.googleapis.com/maps/api/js?libraries=drawing&v=3.22<% $apikey ? "&key=$apikey" : '' %>"></script>
17 <script>
18 var map;
19 var drawingManager;
20
21 function updateFormInput(event) {
22   var path = window.polygon.getPath();
23   var vertices = []; // array of arrays, geoJSON style
24   for (var i =0; i < path.getLength(); i++) {
25     var xy = path.getAt(i);
26     vertices[i] = [ xy.lat(), xy.lng() ];
27   }
28   if (console) console.log(vertices); //XXX
29   $('#<% $field %>').prop('value', JSON.stringify(vertices));
30 }
31
32 $(function() {
33   mapOptions = {
34     zoom: 4,
35     center: {lat: 39.40114, lng: -96.57127}, // continental U.S.
36     mapTypeId: google.maps.MapTypeId.ROADMAP,
37     panControl: true,
38     scaleControl: true,
39     streetViewControl: false,
40   };
41   var div_map = $('#<% $div_id %>');
42   var div_hint = $('#<% $div_id %>_hint');
43   map = new google.maps.Map(div_map[0], mapOptions);
44
45   var set_hint = function(txt) {
46     div_hint.text(txt);
47   }
48
49   var polygonComplete = function(p) {
50     window.polygon = p;
51     if (drawingManager) {
52       drawingManager.setDrawingMode(null);
53       drawingManager.setOptions({ drawingControl: false });
54     }
55     // double click to delete a vertex (so long as it remains a polygon)
56     p.addListener('dblclick', function (mev) {
57       if (mev.vertex != null && window.polygon.getPath().length > 3) {
58         p.getPath().removeAt(mev.vertex);
59       }
60     });
61     // any time the polygon is modified, update the vertex list
62     p.getPath().addListener('set_at', updateFormInput);
63     p.getPath().addListener('insert_at', updateFormInput);
64     p.getPath().addListener('remove_at', updateFormInput);
65
66     // and also now
67     updateFormInput();
68
69     set_hint('Edit the zone by dragging the markers. Double-click to remove a vertex.');
70
71   };
72
73   var polygonOptions = {
74     fillColor: '#0000a0',
75     fillOpacity: 0.2,
76     strokeColor: '#0000a0',
77     strokeWeight: 2,
78     clickable: false,
79     editable: true,
80     zIndex: 1,
81     map: map,
82   };
83
84   var vertex_array = <% $vertices_json %>;
85   if ( vertex_array.length > 2 ) {
86     // then we already have a polygon. make it acceptable to google maps,
87     // and also create a bounding box for it and fit the map to that.
88
89     var path = [];
90     var bounds = new google.maps.LatLngBounds();
91     for (var i = 0; i < vertex_array.length; i++) {
92       var xy = new google.maps.LatLng(vertex_array[i][0], vertex_array[i][1]);
93       path.push(xy);
94       bounds.extend(xy);
95     }
96
97     polygonOptions.paths = [ path ];
98     polygonComplete(new google.maps.Polygon(polygonOptions));
99     map.fitBounds(bounds);
100
101   } else {
102     // there are no vertices, or not enough to make a polygon, so 
103     // enable drawing mode to create a new one
104
105     drawingManager = new google.maps.drawing.DrawingManager({
106       drawingMode: google.maps.drawing.OverlayType.POLYGON,
107       drawingControl: true,
108       drawingControlOptions: {
109         position: google.maps.ControlPosition.TOP_CENTER,
110         drawingModes: [
111           google.maps.drawing.OverlayType.POLYGON,
112         ]
113       },
114       polygonOptions: polygonOptions,
115     });
116
117     // after a single polygon is drawn: remember it, add a listener to let
118     // nodes be deleted, and exit drawing mode
119     drawingManager.addListener('polygoncomplete', polygonComplete);
120     drawingManager.setMap(map);
121
122     // center the map on the user (for lack of a better choice)
123     if (navigator.geolocation) {
124       navigator.geolocation.getCurrentPosition(function(position) {
125         var pos = {
126           lat: position.coords.latitude,
127           lng: position.coords.longitude
128         };
129
130         map.setCenter(pos);
131         map.setZoom(12);
132       });
133     } // on error, or if geolocation isn't available, do nothing
134
135     set_hint('Click to place the corners of the zone.');
136   }
137
138 });
139
140     </script>
141   </body>
142 </html>