source: src/django_gheat/wlheatmap/static/heatmap_extensions.js@ 9571

Last change on this file since 9571 was 9570, checked in by rick, 13 years ago

Make signal dynamic default and redo some init calls.

File size: 12.8 KB
Line 
1/**
2 * Uses zoomleven and mouseposition to call nodelist.py view.
3 * View returns nodes in json format, values are printed in doc element.
4 */
5function getNodeList(zoomlevel, mousepos){
6 $.getJSON("nodelist/" + zoomlevel + "," + mousepos.lat + "," + mousepos.lon,
7 function(json){
8 content = 'Wireless Leiden nodes on mouseposition:<br /><b>';
9 $.each(json, function(i,json){
10 content += json.fields.ssid + '<br />';
11 });
12 content += '</b>';
13 $("#node_list").html(content);
14 }
15 );
16}
17
18/**
19 * Right way to get url parameters?
20 * http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery/1404100#1404100
21 */
22function getURLParameter(name) {
23 return decodeURI(
24 (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
25 );
26}
27
28$(document).ready(function() {
29 var signallayer = '';
30 var userlist = new Array();
31 var nodelist = new Array();
32
33 /**
34 * Adds colourpicker for layer colour.
35 * Function set in: 'colourpicker.js'
36 */
37 $("#colour").colorPicker();
38
39 /**
40 * Adds datepicker for start & end date
41 * Function set in: 'jquery-ui-1.8.13.custom.min.js'
42 */
43 $("#start_date").datepicker({
44 dateFormat: "dd/mm/yy",
45 onSelect: function(dateValue, inst){
46 $("#end_date").datepicker("option", "minDate", dateValue);
47 }
48 });
49 $("#end_date").datepicker({
50 dateFormat: "dd/mm/yy",
51 });
52
53 /**
54 * Adds slider for signal strength layer.
55 * Function set in: 'jquery-ui-1.8.13.custom.min.js'
56 */
57 function update_slider(min,max) {
58 $("#sig_value").text("Signal strength: " + min + " - " + max);
59 $("#signallayer font").text("All Signal strength: " + min + " - " + max);
60 };
61
62 $("#slider").slider({
63 range: true,
64 min: 0,
65 max: 100,
66 values: [0, 100],
67 // while sliding, it changes the text above the slider to show the values realtime
68 slide: function(event, sig){
69 update_slider(sig.values[0], sig.values[1]);
70 },
71 // on slider stop, it sets a new URL with updated values and redraws the layer
72 stop: function(event, sig){
73 signallayer = map.getLayersByName('signallayer');
74 signallayer[0].setUrl("tile/${z}/${x},${y}.png?colour=90,90,90&signaal__gte=" + sig.values[0] + "&signaal__lte=" + sig.values[1]);
75 signallayer[0].redraw();
76 },
77 });
78
79
80 function init_slider(min, max) {
81 $("#slider").slider( "option", "values", [min,max] );
82 signallayer = map.getLayersByName('signallayer');
83 signallayer[0].setUrl("tile/${z}/${x},${y}.png?colour=90,90,90&signaal__gte=" + min + "&signaal__lte=" + max);
84 signallayer[0].redraw();
85 update_slider(min,max);
86 };
87
88
89
90 /**
91 * Toggles layer visibility on checkbox use from layerswitcher.
92 */
93 $("#togglelayer").live('click', function(){
94 layername = $(this).parents('span').attr('id');
95 layer = map.getLayersByName(layername);
96 if(this.checked){
97 layer[0].setVisibility(true);
98 }
99 else{
100 layer[0].setVisibility(false);
101 }
102 });
103
104 /**
105 * Toggles layer visibility per category on checkbox use from layerswitcher.
106 */
107 $("#togglecat").live("click", function(){
108 layertype = $(this).attr('class');
109 layers = new Array();
110 layers = $('#' + layertype).children('span');
111 if(this.checked){
112 $.each(layers, function(l, layer){
113 layer = map.getLayersByName(layer.id);
114 layer[0].setVisibility(true);
115 $(this).children('input').attr('checked', true);
116 });
117 }
118 else{
119 $.each(layers, function(l, layer){
120 layer = map.getLayersByName(layer.id);
121 layer[0].setVisibility(false);
122 $(this).children('input').attr('checked', false);
123 });
124 }
125 });
126
127 /**
128 * Removes layer from map and layerswitcher.
129 */
130 $("#rmvlayer").live("click", function(){
131 layername = $(this).closest('span').attr('id');
132 layer = map.getLayersByName(layername);
133 map.removeLayer(layer[0]);
134 $(this).closest('span').remove();
135 });
136
137 /**
138 * Toggles text shown on website.
139 * 'info' shows info text.
140 * 'filter' page shows text on filters & filter options.
141 */
142 $("#menu_filter").click(function(){
143 $("#filter").slideToggle("fast");
144 $("#info").slideUp("fast");
145 });
146 $("#menu_info").click(function(){
147 $("#filter").slideUp("fast");
148 $("#info").slideToggle("fast");
149 });
150 $("#hide_parent").live("click",function(){
151 parent = $(this).parent("div");
152 $(parent).slideUp("fast");
153 });
154
155
156
157 /**
158 * Toggles layerswitcher visibility.
159 * On 'off', toggler shows a '+'.
160 * On 'on', toggler shows a '-'.
161 */
162 $("#layer_switcher_toggle").click(function(){
163 $("#layer_switcher").toggle();
164 if($(this).text() == '+'){
165 $(this).html('-');
166 }
167 else{
168 $(this).html('+');
169 }
170 });
171
172 /**
173 * Toggles category visibility.
174 * On 'visible', toggler shows a '^'.
175 * On 'invisible', toggler shows a 'v'.
176 */
177 $("#cat_hide").live("click", function(){
178 cat = $(this).parent('div').next('p').children('span')
179 cat.toggle();
180 if(cat.is(":visible")){
181 //FIXME: images are getting loaded the hard way. Replace '/static/'. '{{ STATIC_URL }}' doesn't seem to work here.
182 $(this).css('background-image', "url(/static/img/up.png)");
183 }
184 else{
185 $(this).css('background-image', "url(/static/img/down.png)");
186 }
187 });
188
189 /**
190 * Fastest?
191 * http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery
192 */
193 $.getJSON("filters/",
194 function(json) {
195 $.each(json, function(i,json){
196 $("#select_user").append($("<option/>").attr("value",json.gebruiker).text(json.gebruiker));
197 userlist.push(json.gebruiker);
198 $.each(json.meetrondje, function(m, meetrondje){
199 $("#select_dataset").append($("<option/>").attr({"class":json.gebruiker, "value":meetrondje.naam}).text(meetrondje.naam));
200 $.each(meetrondje.nodes, function(n, nodes){
201 $("#select_node").append($("<option/>").attr({"class":meetrondje.naam, "value":nodes}).text(nodes));
202 if(jQuery.inArray(nodes, nodelist)<0){
203 nodelist.push(nodes);
204 }
205 });
206 });
207 });
208 // Initiate the chain
209 $("#select_dataset").chained("#select_user");
210 $("#select_node").chained("#select_dataset");
211 }
212 );
213
214 /**
215 * Earlier process got all users in JSON and put them in array.
216 * For each user in array, new layer is added with username as parameter.
217 * Layercolour is randomized.
218 * If button is pressed again, layers will be deleted.
219 */
220 $('#user_filter').click(function(){
221 if(this.text=='(Add all users)'){
222 $.each(userlist, function(u, user){
223 colour = Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);
224 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
225 initialize: function(name, options) {
226 var url = [
227 "tile/${z}/${x},${y}.png?&colour=" + colour + "&meetrondje__gebruiker__naam=" + user
228 ];
229 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
230 var newArguments = [name, url, options];
231 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
232 },
233 CLASS_NAME: "OpenLayers.Layer.Overlay"
234 });
235 filterlayer = new OpenLayers.Layer.OSM.Overlay(user, {isBaseLayer: false, visibility: true});
236 map.addLayer(filterlayer);
237 $("#user").append("<span id=\"" + user + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + colour + ")' /><input type=checkbox id=togglelayer checked /><font>" + user + " </font><a id=rmvlayer>X</a><br /></span>");
238 });
239 $(this).text('(Del all users');
240 }
241 else{
242 $(this).text('(Add all users)');
243 $.each($('#user span'), function(s, span){
244 layer = map.getLayersByName(span.id);
245 map.removeLayer(layer[0]);
246 $(span).remove();
247 });
248 }
249 });
250
251 /**
252 * Earlier process got all nodes in JSON and put them in array.
253 * For each node in array, new layer is added with node as parameter.
254 * Layercolour is randomized.
255 * If button is pressed again, layers will be deleted.
256 */
257 $('#node_filter').click(function(){
258 if(this.text=='(Add all nodes)'){
259 $.each(nodelist, function(n, node){
260 colour = Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);
261 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
262 initialize: function(name, options) {
263 var url = [
264 "tile/${z}/${x},${y}.png?&colour=" + colour + "&accespoint__ssid=" + node
265 ];
266 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
267 var newArguments = [name, url, options];
268 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
269 },
270 CLASS_NAME: "OpenLayers.Layer.Overlay"
271 });
272 nodefilterlayer = new OpenLayers.Layer.OSM.Overlay(node, {isBaseLayer: false, visibility: true});
273 map.addLayer(nodefilterlayer);
274 $("#node").append("<span class='node' id=\"" + node + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + colour + ")' /><input type=checkbox id=togglelayer checked /><font>" + node + " </font><a id=rmvlayer>X</a><br /></span>");
275 });
276 $(this).text('(Del all nodes)');
277 }
278 else{
279 $(this).text('(Add all nodes)');
280 $.each($('#node span'), function(s, span){
281 layer = map.getLayersByName(span.id);
282 map.removeLayer(layer[0]);
283 $(span).remove();
284 });
285 }
286 });
287
288 /**
289 * Adds a new layer when clicked.
290 * Filters set by user are used as layer parameters.
291 * Colour is either chosen or randomized.
292 * Blank parameters are ignored.
293 */
294 $('#add_filter').click(function(){
295 // get values from elements
296 user = $('#select_user option:selected').text();
297 dataset = $('#select_dataset option:selected').text();
298 wlnode = $('#select_node option:selected').text();
299 start_date = $('#start_date').val();
300 end_date = $('#end_date').val();
301 colour = encodeURIComponent(document.getElementById("colour").value);
302 lname = encodeURIComponent(document.getElementById("lname").value);
303
304 /**
305 * On HEX to RGB:
306 * http://stackoverflow.com/questions/5798868/need-some-tips-with-how-to-convert-a-hexadecimal-color-value-to-a-rgb-one/5798900#5798900
307 * Note that '#' comes out as '%23', so substr() starts at char 3
308 */
309 colour = parseInt((colour.substr(3, 2)),16) + ',' + parseInt((colour.substr(5, 2)),16) + ',' + parseInt((colour.substr(7, 2)),16);
310
311 // some checks if values are correct, and adding them to parts of the link
312 if (user != ''){user='&meetrondje__gebruiker__naam='+user;}
313 if (dataset != ''){dataset='&meetrondje__naam='+dataset;}
314 if (wlnode != ''){wlnode='&accespoint__ssid='+wlnode;}
315 if (start_date && end_date){
316 sd_d=start_date.substr(0,2);
317 sd_m=start_date.substr(3,2);
318 sd_y=start_date.substr(6,4);
319
320 ed_d=end_date.substr(0,2);
321 ed_m=end_date.substr(3,2);
322 ed_y=end_date.substr(6,4);
323
324 start_date='&meetrondje__datum__gte=' + sd_y +'-'+ sd_m +'-'+ sd_d;
325 end_date='&meetrondje__datum__lte=' + ed_y +'-'+ ed_m +'-'+ ed_d;
326 }
327 if (start_date && !end_date){
328 sd_d=start_date.substr(0,2);
329 sd_m=start_date.substr(3,2);
330 sd_y=start_date.substr(6,4);
331 start_date='&meetrondje__datum__gte=' + sd_y +'-'+ sd_m +'-'+ sd_d;
332 end_date='';
333 }
334 if (!start_date && end_date){
335 ed_d=end_date.substr(0,2);
336 ed_m=end_date.substr(3,2);
337 ed_y=end_date.substr(6,4);
338 start_date='';
339 end_date='&meetrondje__datum__lte=' + ed_y +'-'+ ed_m +'-'+ ed_d;
340 }
341 if (colour != 'NaN,NaN,NaN'){colour=colour;}
342 else {colour = Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);}
343 if (lname != ''){lname=lname;}
344 else {lname = 'Custom Filter';}
345
346 // setting layer
347 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
348 initialize: function(name, options) {
349 var url = [
350 "tile/${z}/${x},${y}.png?&colour=" + colour + user + dataset + wlnode + start_date + end_date
351 ];
352 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
353 var newArguments = [name, url, options];
354 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
355 },
356
357 CLASS_NAME: "OpenLayers.Layer.Overlay"
358 });
359
360 // adding layer
361 filterlayer = new OpenLayers.Layer.OSM.Overlay(lname, {isBaseLayer: false, visibility: true});
362 map.addLayer(filterlayer);
363
364 $("#custom").append("<span id=\"" + lname + "\"><div id='layer_switcher_colour' style='background-color:rgb(" + colour + ")' /><input type=checkbox id=togglelayer checked /><font>" + lname + " </font><a id=rmvlayer>X</a><br /></span>");
365 });
366 init_heatmap();
367 init_slider(35,75);
368});
Note: See TracBrowser for help on using the repository browser.