manifold: now managing update queries
[myslice.git] / plugins / googlemaps / static / js / googlemaps.js
1 /**
2  * Description: display a query result in a Google map
3  * Copyright (c) 2012-2013 UPMC Sorbonne Universite - INRIA
4  * License: GPLv3
5  */
6
7 (function($){
8
9    var GoogleMaps = Plugin.extend({
10
11         init: function(options, element)
12         {
13             this._super(options, element);
14
15             /* Member variables */
16             // query status
17             this.received_all = false;
18             this.received_set = false;
19             this.in_set_buffer = Array();
20
21             /* XXX Events XXX */
22             this.$element.on('show.Datatables', this.on_show);
23             // TODO in destructor
24             // $(window).unbind('Hazelnut');
25
26             /* Setup query and record handlers */
27             this.listen_query(options.query_uuid);
28             this.listen_query(options.query_all_uuid, 'all');
29
30             /* GUI setup and event binding */
31             this.initialize_map();
32         }, // init
33
34         /* PLUGIN EVENTS */
35
36         on_show: function()
37         {
38             google.maps.event.trigger(map, 'resize');
39         }, // on_show
40
41         /* GUI EVENTS */
42
43         /* GUI MANIPULATION */
44
45         /**
46          */
47         initialize_map: function()
48         {
49             this.map = null;
50             this.markerCluster = null;
51             this.markers = [];
52             this.coords = new Array();
53
54             var myLatlng = new google.maps.LatLng(this.options.latitude, this.options.longitude);
55             var myOptions = {
56                 zoom: this.options.zoom,
57                 center: myLatlng,
58                 mapTypeId: google.maps.MapTypeId.ROADMAP
59             }
60       
61             this.map = new google.maps.Map(document.getElementById("map"), myOptions);
62             this.infowindow = new google.maps.InfoWindow();
63         }, // initialize_map
64
65         /**
66          */
67         new_record: function(record)
68         {
69             // get the coordinates
70             var latitude=get_value(record['latitude']);
71             var longitude=get_value(record['longitude']);
72             var hash = latitude + longitude;
73
74             // check to see if we've seen this hash before
75             if(this.coords[hash] == null) {
76                 // get coordinate object
77                 var myLatlng = new google.maps.LatLng(latitude, longitude);
78                 // store an indicator that we've seen this point before
79                 this.coords[hash] = 1;
80             } else {
81                 // add some randomness to this point 1500 = 100 meters, 15000 = 10 meters
82                 var lat = latitude + (Math.random() -.5) / 1500; 
83                 var lng = longitude + (Math.random() -.5) / 1500; 
84
85                 // get the coordinate object
86                 var myLatlng = new google.maps.LatLng(lat, lng);
87             }
88             // If the node is attached to the slice, action will be Remove; else action will be add to slice
89             if (typeof(record['sliver']) != 'undefined') {
90                 data.current_resources.push(record['urn']);
91                 action="del";
92                 action_class="ui-icon-circle-minus";
93                 action_message="Remove from slice";
94             }else{
95                 action="add";
96                 action_class="ui-icon-circle-plus";
97                 action_message="Add to slice";
98             }
99             // XXX not working
100             if (!(record['latitude'])) {
101                 return true;
102             }
103
104             //jQuery(".map-button").click(button_click);
105             //if(jQuery.inArray(record, rows)>-1){
106                 var marker = new google.maps.Marker({
107                     position: myLatlng,
108                     title: get_value(record['hostname']),
109                     // This should be done by the rendering
110                     content: '<p>Agent: ' + get_value(record['ip']) + ' (' + get_value(record['resource_hrn']) + ')<br/>Platform: ' + get_value(record['platform'])+'</p>' +
111                             '<div class="map-button" id="'+action+'/'+get_value(record['resource_hrn'])+'" style="cursor:pointer;">'+
112                             '<span class="ui-icon '+action_class+'" style="clear:both;float:left;"></span>'+action_message+
113                             '</div>'
114                 }); 
115
116                 this.addInfoWindow(marker, this.map);
117                 this.markers.push(marker);
118             //}
119
120         }, // new_record
121
122         addInfoWindow: function(marker, map)
123         {
124             var self = this;
125             google.maps.event.addListener(marker, 'click', function () {     
126                 if(self.infowindow){
127                     self.infowindow.close();
128                 }
129                 self.infowindow.setContent(marker.content);// = new google.maps.InfoWindow({ content: marker.content });
130                 self.infowindow.open(map, marker);
131                 // onload of the infowindow on the map, bind a click on a button
132                 google.maps.event.addListener(self.infowindow, 'domready', function() {
133                     jQuery('.map-button').unbind('click');
134 //                    jQuery(".map-button").click({instance: instance_, infoWindow: object.infowindow}, button_click);                     
135                 });
136             });
137         }, // addInfoWindow
138
139         set_checkbox: function(record)
140         {
141             // XXX urn should be replaced by the key
142             // XXX we should enforce that both queries have the same key !!
143             //checkbox_id = "#hazelnut-checkbox-" + object.options.plugin_uuid + "-" + unfold.escape_id(record[ELEMENT_KEY].replace(/\\/g, ''))
144             //$(checkbox_id, object.table.fnGetNodes()).attr('checked', true);
145         }, // set_checkbox
146
147
148         /*************************** QUERY HANDLER ****************************/
149
150         /*************************** RECORD HANDLER ***************************/
151
152         on_new_record: function(record)
153         {
154             if (this.received_all)
155                 // update checkbox for record
156                 this.set_checkbox(record);
157             else
158                 // store for later update of checkboxes
159                 this.in_set_buffer.push(record);
160         },
161
162         on_clear_records: function(record)
163         {
164
165         },
166
167         // Could be the default in parent
168         on_query_in_progress: function()
169         {
170             this.spin();
171         },
172
173         on_query_done: function()
174         {
175             if (this.received_all)
176                 this.unspin();
177             this.received_set = true;
178         },
179
180         // all
181
182         on_all_new_record: function(record)
183         {
184             this.new_record(record);
185         },
186
187         on_all_clear_records: function()
188         {
189         },
190
191         on_all_query_in_progress: function()
192         {
193             // XXX parent
194             this.spin();
195         },
196
197         on_all_query_done: function()
198         {
199
200             // MarkerClusterer
201             this.markerCluster = new MarkerClusterer(this.map, this.markers, {zoomOnClick: false});
202             google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
203                 var markers = cluster.getMarkers();
204                 var bounds  = new google.maps.LatLngBounds();
205               /* 
206               * date: 24/05/2012
207               * author: lbaron
208               * Firefox JS Error - replaced $.each by JQuery.each
209               */                  
210               jQuery.each(markers, function(i, marker){
211                   bounds.extend(marker.getPosition()); 
212               });
213
214               //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
215               this.map.fitBounds(bounds);
216             });
217
218             var self = this;
219             if (this.received_set) {
220                 /* XXX needed ? XXX We uncheck all checkboxes ... */
221                 $("[id^='datatables-checkbox-" + this.options.plugin_uuid +"']").attr('checked', false);
222
223                 /* ... and check the ones specified in the resource list */
224                 $.each(this.in_set_buffer, function(i, record) {
225                     self.set_checkbox(record);
226                 });
227
228                 this.unspin();
229             }
230             this.received_all = true;
231
232         }, // on_all_query_done
233
234         /************************** PRIVATE METHODS ***************************/
235
236         _button_click: function(e)
237         {
238             var op_value=this.id.split("/");
239             if(op_value.length>0) {
240                 var value = op_value[1];
241                 manifold.raise_event(this.options.query_uuid, (op_value[0] == 'add')?SET_ADD:SET_REMOVED, value);
242             }
243         } // button_click
244
245     });
246
247     $.plugin('GoogleMaps', GoogleMaps);
248
249 })(jQuery);