e97d1f5f5bd7bca26434dd32d548b16a11fd6bad
[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             google.maps.event.addListener(marker, 'click', function () {     
125                 if(object.infowindow){
126                     object.infowindow.close();
127                 }
128                 object.infowindow.setContent(marker.content);// = new google.maps.InfoWindow({ content: marker.content });
129                 object.infowindow.open(map, marker);
130                 // onload of the infowindow on the map, bind a click on a button
131                 google.maps.event.addListener(object.infowindow, 'domready', function() {
132                     jQuery('.map-button').unbind('click');
133 //                    jQuery(".map-button").click({instance: instance_, infoWindow: object.infowindow}, button_click);                     
134                 });
135             });
136         }, // addInfoWindow
137
138         set_checkbox: function(record)
139         {
140             // XXX urn should be replaced by the key
141             // XXX we should enforce that both queries have the same key !!
142             //checkbox_id = "#hazelnut-checkbox-" + object.options.plugin_uuid + "-" + unfold.escape_id(record[ELEMENT_KEY].replace(/\\/g, ''))
143             //$(checkbox_id, object.table.fnGetNodes()).attr('checked', true);
144         }, // set_checkbox
145
146
147         /*************************** QUERY HANDLER ****************************/
148
149         /*************************** RECORD HANDLER ***************************/
150
151         on_new_record: function(record)
152         {
153             if (this.received_all)
154                 // update checkbox for record
155                 this.set_checkbox(record);
156             else
157                 // store for later update of checkboxes
158                 this.in_set_buffer.push(record);
159         },
160
161         on_clear_records: function(record)
162         {
163
164         },
165
166         // Could be the default in parent
167         on_query_in_progress: function()
168         {
169             this.spin();
170         },
171
172         on_query_done: function()
173         {
174             if (this.received_all)
175                 this.unspin();
176             this.received_set = true;
177         },
178
179         // all
180
181         on_all_new_record: function(record)
182         {
183             this.new_record(record);
184         },
185
186         on_all_clear_records: function()
187         {
188         },
189
190         on_all_query_in_progress: function()
191         {
192             // XXX parent
193             this.spin();
194         },
195
196         on_all_query_done: function()
197         {
198
199             // MarkerClusterer
200             this.markerCluster = new MarkerClusterer(this.map, this.markers, {zoomOnClick: false});
201             google.maps.event.addListener(this.markerCluster, "clusterclick", function (cluster) {
202                 var markers = cluster.getMarkers();
203                 var bounds  = new google.maps.LatLngBounds();
204               /* 
205               * date: 24/05/2012
206               * author: lbaron
207               * Firefox JS Error - replaced $.each by JQuery.each
208               */                  
209               jQuery.each(markers, function(i, marker){
210                   bounds.extend(marker.getPosition()); 
211               });
212
213               //map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));
214               this.map.fitBounds(bounds);
215             });
216
217             var self = this;
218             if (this.received_set) {
219                 /* XXX needed ? XXX We uncheck all checkboxes ... */
220                 $("[id^='datatables-checkbox-" + this.options.plugin_uuid +"']").attr('checked', false);
221
222                 /* ... and check the ones specified in the resource list */
223                 $.each(this.in_set_buffer, function(i, record) {
224                     self.set_checkbox(record);
225                 });
226
227                 this.unspin();
228             }
229             this.received_all = true;
230
231         }, // on_all_query_done
232
233         /************************** PRIVATE METHODS ***************************/
234
235         _button_click: function(e)
236         {
237             var op_value=this.id.split("/");
238             if(op_value.length>0) {
239                 var value = op_value[1];
240                 manifold.raise_event(this.options.query_uuid, (op_value[0] == 'add')?SET_ADD:SET_REMOVED, value);
241             }
242         } // button_click
243
244     });
245
246     $.plugin('GoogleMaps', GoogleMaps);
247
248 })(jQuery);