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