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

Last change on this file since 9811 was 9782, checked in by rick, 13 years ago

Some tricks to start making permalinks for custom filters to be able to share more interesing views right from a single link. As building those filters can sometimes be a burden...

File size: 14.8 KB
RevLine 
[9302]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 */
[9269]5function getNodeList(zoomlevel, mousepos){
[9358]6 $.getJSON("nodelist/" + zoomlevel + "," + mousepos.lat + "," + mousepos.lon,
[9269]7 function(json){
[9234]8 content = 'Wireless Leiden nodes on mouseposition:<br /><b>';
9 $.each(json, function(i,json){
[9244]10 content += json.fields.ssid + '<br />';
[9234]11 });
[9244]12 content += '</b>';
[9234]13 $("#node_list").html(content);
[9223]14 }
[9234]15 );
[9223]16}
17
[9302]18/**
19 * Right way to get url parameters?
20 * http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery/1404100#1404100
21 */
[9269]22function getURLParameter(name) {
23 return decodeURI(
24 (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
25 );
26}
27
[9779]28
[9651]29$(window).load(function() {
[9267]30 var signallayer = '';
[9263]31 var userlist = new Array();
32 var nodelist = new Array();
[9237]33
[9779]34 function update_raw_filter() {
35 $("#raw_filter").val(get_raw_filter());
36 }
37
[9302]38 /**
39 * Adds colourpicker for layer colour.
40 * Function set in: 'colourpicker.js'
41 */
[9267]42 $("#colour").colorPicker();
43
[9302]44 /**
45 * Adds datepicker for start & end date
46 * Function set in: 'jquery-ui-1.8.13.custom.min.js'
47 */
[9267]48 $("#start_date").datepicker({
49 dateFormat: "dd/mm/yy",
50 onSelect: function(dateValue, inst){
51 $("#end_date").datepicker("option", "minDate", dateValue);
[9779]52 update_raw_filter();
[9267]53 }
54 });
55 $("#end_date").datepicker({
[9779]56 dateFormat: "dd/mm/yy",
57 onSelect: function(dateValue, inst){
58 update_raw_filter();
59 }
[9267]60 });
[9272]61
[9302]62 /**
63 * Adds slider for signal strength layer.
64 * Function set in: 'jquery-ui-1.8.13.custom.min.js'
65 */
[9570]66 function update_slider(min,max) {
[9575]67 $("#sig_value").text("All AccessPoints Signal Strength: " + min + " - " + max);
68 $("#signallayer font").text("All AccessPoints Signal: " + min + " - " + max);
[9570]69 };
70
[9578]71 function update_sig_slider(min,max) {
72 $("#select_sig_value").text("Signal Strength: " + min + " - " + max);
[9779]73 update_raw_filter();
[9578]74 };
75
[9267]76 $("#slider").slider({
77 range: true,
78 min: 0,
79 max: 100,
80 values: [0, 100],
81 // while sliding, it changes the text above the slider to show the values realtime
82 slide: function(event, sig){
[9570]83 update_slider(sig.values[0], sig.values[1]);
[9267]84 },
[9297]85 // on slider stop, it sets a new URL with updated values and redraws the layer
[9267]86 stop: function(event, sig){
[9275]87 signallayer = map.getLayersByName('signallayer');
[9570]88 signallayer[0].setUrl("tile/${z}/${x},${y}.png?colour=90,90,90&signaal__gte=" + sig.values[0] + "&signaal__lte=" + sig.values[1]);
[9275]89 signallayer[0].redraw();
[9648]90 }
[9267]91 });
[9578]92 $("#select_sig_slider").slider({
93 range: true,
94 min: 0,
95 max: 100,
96 values: [0,100],
97 // while sliding, it changes the text above the slider to show the values realtime
98 slide: function(event, sig){
99 update_sig_slider(sig.values[0], sig.values[1]);
[9648]100 }
[9578]101 });
[9267]102
[9570]103
104 function init_slider(min, max) {
105 $("#slider").slider( "option", "values", [min,max] );
106 signallayer = map.getLayersByName('signallayer');
107 signallayer[0].setUrl("tile/${z}/${x},${y}.png?colour=90,90,90&signaal__gte=" + min + "&signaal__lte=" + max);
108 signallayer[0].redraw();
109 update_slider(min,max);
[9578]110
111 $("#select_sig_slider").slider( "option", "values", [min,max] );
112 update_sig_slider(min,max);
[9570]113 };
114
115
116
[9302]117 /**
118 * Toggles layer visibility on checkbox use from layerswitcher.
119 */
[9297]120 $("#togglelayer").live('click', function(){
[9272]121 layername = $(this).parents('span').attr('id');
122 layer = map.getLayersByName(layername);
[9405]123 if(this.checked){
124 layer[0].setVisibility(true);
125 }
126 else{
127 layer[0].setVisibility(false);
128 }
[9272]129 });
130
[9302]131 /**
132 * Toggles layer visibility per category on checkbox use from layerswitcher.
133 */
[9297]134 $("#togglecat").live("click", function(){
135 layertype = $(this).attr('class');
136 layers = new Array();
137 layers = $('#' + layertype).children('span');
[9405]138 if(this.checked){
139 $.each(layers, function(l, layer){
140 layer = map.getLayersByName(layer.id);
141 layer[0].setVisibility(true);
142 $(this).children('input').attr('checked', true);
143 });
144 }
145 else{
146 $.each(layers, function(l, layer){
147 layer = map.getLayersByName(layer.id);
148 layer[0].setVisibility(false);
149 $(this).children('input').attr('checked', false);
150 });
151 }
[9276]152 });
153
[9302]154 /**
155 * Removes layer from map and layerswitcher.
156 */
[9272]157 $("#rmvlayer").live("click", function(){
[9274]158 layername = $(this).closest('span').attr('id');
[9273]159 layer = map.getLayersByName(layername);
[9272]160 map.removeLayer(layer[0]);
[9274]161 $(this).closest('span').remove();
[9272]162 });
163
[9302]164 /**
165 * Toggles text shown on website.
166 * 'info' shows info text.
167 * 'filter' page shows text on filters & filter options.
168 */
[9267]169 $("#menu_filter").click(function(){
170 $("#filter").slideToggle("fast");
[9272]171 $("#info").slideUp("fast");
[9267]172 });
173 $("#menu_info").click(function(){
174 $("#filter").slideUp("fast");
175 $("#info").slideToggle("fast");
176 });
[9347]177 $("#hide_parent").live("click",function(){
178 parent = $(this).parent("div");
179 $(parent).slideUp("fast");
180 });
181
[9267]182
[9347]183
[9302]184 /**
185 * Toggles layerswitcher visibility.
186 * On 'off', toggler shows a '+'.
187 * On 'on', toggler shows a '-'.
188 */
[9274]189 $("#layer_switcher_toggle").click(function(){
190 $("#layer_switcher").toggle();
191 if($(this).text() == '+'){
192 $(this).html('-');
193 }
194 else{
195 $(this).html('+');
196 }
197 });
198
[9302]199 /**
[9317]200 * Toggles category visibility.
201 * On 'visible', toggler shows a '^'.
202 * On 'invisible', toggler shows a 'v'.
[9316]203 */
204 $("#cat_hide").live("click", function(){
205 cat = $(this).parent('div').next('p').children('span')
206 cat.toggle();
207 if(cat.is(":visible")){
208 //FIXME: images are getting loaded the hard way. Replace '/static/'. '{{ STATIC_URL }}' doesn't seem to work here.
209 $(this).css('background-image', "url(/static/img/up.png)");
210 }
211 else{
212 $(this).css('background-image', "url(/static/img/down.png)");
213 }
214 });
215
216 /**
[9302]217 * Fastest?
218 * http://stackoverflow.com/questions/170986/what-is-the-best-way-to-add-options-to-a-select-from-an-array-with-jquery
219 */
[9358]220 $.getJSON("filters/",
[9235]221 function(json) {
222 $.each(json, function(i,json){
[9241]223 $("#select_user").append($("<option/>").attr("value",json.gebruiker).text(json.gebruiker));
[9263]224 userlist.push(json.gebruiker);
[9235]225 $.each(json.meetrondje, function(m, meetrondje){
[9577]226 $("#select_dataset").append($("<option/>").attr({"class":json.gebruiker, "value": json.gebruiker + "-" + meetrondje.naam}).text(meetrondje.naam));
[9235]227 $.each(meetrondje.nodes, function(n, nodes){
[9577]228 $("#select_node").append($("<option/>").attr({"class":json.gebruiker + "-" + meetrondje.naam, "value":nodes}).text(nodes));
[9263]229 if(jQuery.inArray(nodes, nodelist)<0){
230 nodelist.push(nodes);
231 }
[9235]232 });
233 });
234 });
[9243]235 // Initiate the chain
[9239]236 $("#select_dataset").chained("#select_user");
237 $("#select_node").chained("#select_dataset");
[9229]238 }
[9235]239 );
[9223]240
[9302]241 /**
242 * Earlier process got all users in JSON and put them in array.
243 * For each user in array, new layer is added with username as parameter.
244 * Layercolour is randomized.
245 * If button is pressed again, layers will be deleted.
246 */
[9577]247 function update_lname() {
248 $("#lname").val($("#select_user option:selected").text() + " - " + $("#select_dataset option:selected").text() + " - " + $("#select_node option:selected").text());
249 }
250 $("#select_user").change(function(){
251 update_lname();
[9779]252 update_raw_filter();
[9577]253 });
254 $("#select_dataset").change(function(){
255 update_lname();
[9779]256 update_raw_filter();
[9577]257 });
258 $("#select_node").change(function(){
259 update_lname();
[9779]260 update_raw_filter();
[9577]261 });
[9779]262 $("#colour").change(function(){
263 update_raw_filter();
264 });
[9577]265
[9263]266 $('#user_filter').click(function(){
[9276]267 if(this.text=='(Add all users)'){
[9264]268 $.each(userlist, function(u, user){
[9273]269 colour = Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);
[9264]270 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
271 initialize: function(name, options) {
272 var url = [
[9358]273 "tile/${z}/${x},${y}.png?&colour=" + colour + "&meetrondje__gebruiker__naam=" + user
[9264]274 ];
275 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
276 var newArguments = [name, url, options];
277 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
278 },
279 CLASS_NAME: "OpenLayers.Layer.Overlay"
280 });
[9274]281 filterlayer = new OpenLayers.Layer.OSM.Overlay(user, {isBaseLayer: false, visibility: true});
[9264]282 map.addLayer(filterlayer);
[9406]283 $("#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>");
[9263]284 });
[9276]285 $(this).text('(Del all users');
[9264]286 }
287 else{
[9276]288 $(this).text('(Add all users)');
[9317]289 $.each($('#user span'), function(s, span){
290 layer = map.getLayersByName(span.id);
[9264]291 map.removeLayer(layer[0]);
[9317]292 $(span).remove();
[9264]293 });
294 }
[9263]295 });
296
[9302]297 /**
298 * Earlier process got all nodes in JSON and put them in array.
299 * For each node in array, new layer is added with node as parameter.
300 * Layercolour is randomized.
301 * If button is pressed again, layers will be deleted.
302 */
[9263]303 $('#node_filter').click(function(){
[9276]304 if(this.text=='(Add all nodes)'){
[9264]305 $.each(nodelist, function(n, node){
[9273]306 colour = Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);
[9264]307 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
308 initialize: function(name, options) {
309 var url = [
[9358]310 "tile/${z}/${x},${y}.png?&colour=" + colour + "&accespoint__ssid=" + node
[9264]311 ];
312 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
313 var newArguments = [name, url, options];
314 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
315 },
316 CLASS_NAME: "OpenLayers.Layer.Overlay"
317 });
[9274]318 nodefilterlayer = new OpenLayers.Layer.OSM.Overlay(node, {isBaseLayer: false, visibility: true});
[9264]319 map.addLayer(nodefilterlayer);
[9406]320 $("#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>");
[9263]321 });
[9276]322 $(this).text('(Del all nodes)');
[9264]323 }
324 else{
[9276]325 $(this).text('(Add all nodes)');
326 $.each($('#node span'), function(s, span){
327 layer = map.getLayersByName(span.id);
[9264]328 map.removeLayer(layer[0]);
[9276]329 $(span).remove();
[9264]330 });
331 }
[9263]332 });
333
[9779]334 function get_raw_filter() {
[9235]335 user = $('#select_user option:selected').text();
336 dataset = $('#select_dataset option:selected').text();
337 wlnode = $('#select_node option:selected').text();
[9251]338 start_date = $('#start_date').val();
339 end_date = $('#end_date').val();
[9577]340 colour = $("#colour").val();
[9223]341
[9302]342 /**
343 * On HEX to RGB:
344 * http://stackoverflow.com/questions/5798868/need-some-tips-with-how-to-convert-a-hexadecimal-color-value-to-a-rgb-one/5798900#5798900
345 */
[9655]346 colour = parseInt((colour.substr(1, 2)),16) + ',' + parseInt((colour.substr(3, 2)),16) + ',' + parseInt((colour.substr(5, 2)),16);
[9246]347
[9263]348 // some checks if values are correct, and adding them to parts of the link
[9578]349 if (user == 'all') {
350 user = '';
351 } else {
352 user='&meetrondje__gebruiker__naam='+user;
353 }
354 if (dataset == 'all') {
355 dataset = '';
356 } else {
357 dataset='&meetrondje__naam='+dataset;
358 }
359 if (wlnode == 'all'){
360 wlnode='';
361 } else {
362 wlnode='&accespoint__ssid='+wlnode;
363 }
[9251]364 if (start_date && end_date){
365 sd_d=start_date.substr(0,2);
366 sd_m=start_date.substr(3,2);
367 sd_y=start_date.substr(6,4);
368
369 ed_d=end_date.substr(0,2);
370 ed_m=end_date.substr(3,2);
371 ed_y=end_date.substr(6,4);
372
373 start_date='&meetrondje__datum__gte=' + sd_y +'-'+ sd_m +'-'+ sd_d;
374 end_date='&meetrondje__datum__lte=' + ed_y +'-'+ ed_m +'-'+ ed_d;
375 }
[9253]376 if (start_date && !end_date){
377 sd_d=start_date.substr(0,2);
378 sd_m=start_date.substr(3,2);
379 sd_y=start_date.substr(6,4);
380 start_date='&meetrondje__datum__gte=' + sd_y +'-'+ sd_m +'-'+ sd_d;
381 end_date='';
382 }
383 if (!start_date && end_date){
384 ed_d=end_date.substr(0,2);
385 ed_m=end_date.substr(3,2);
386 ed_y=end_date.substr(6,4);
387 start_date='';
388 end_date='&meetrondje__datum__lte=' + ed_y +'-'+ ed_m +'-'+ ed_d;
389 }
[9578]390 if (colour != 'NaN,NaN,NaN'){
391 colour=colour;
392 } else {
393 colour = Math.floor(Math.random()*256)+','+Math.floor (Math.random()*256)+','+Math.floor(Math.random()*256);
394 }
[9223]395
[9578]396 var min = $("#select_sig_slider").slider("values")[0];
397 var max = $("#select_sig_slider").slider("values")[1];
398 signal = '&signaal__gte=' + min + '&signaal__lte=' + max;
399
[9779]400 var raw_filter = "colour=" + colour + user + dataset + wlnode + start_date + end_date + signal
401 return raw_filter;
402 }
403
404 /**
405 * Adds a new layer when clicked.
406 * Filters set by user are used as layer parameters.
407 * Colour is either chosen or randomized.
408 * Blank parameters are ignored.
409 */
[9781]410 function add_filter(lname, filter) {
[9263]411 // setting layer
[9235]412 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
413 initialize: function(name, options) {
414 var url = [
[9781]415 "tile/${z}/${x},${y}.png?" + filter,
[9235]416 ];
417 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
418 var newArguments = [name, url, options];
419 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
420 },
[9274]421
[9235]422 CLASS_NAME: "OpenLayers.Layer.Overlay"
423 });
[9223]424
[9263]425 // adding layer
[9235]426 filterlayer = new OpenLayers.Layer.OSM.Overlay(lname, {isBaseLayer: false, visibility: true});
427 map.addLayer(filterlayer);
[9274]428
[9406]429 $("#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>");
[9781]430 }
431
432 $('#add_filter').click(function(){
433 lname = $("#lname").val();
434 if (lname == ''){
435 lname= 'Custom Filter';
436 }
437 add_filter(lname, $("#raw_filter").val());
438
[9223]439 });
[9570]440 init_heatmap();
441 init_slider(35,75);
[9781]442
443 // Initiate the custom filters
444 var items = window.location.hash.split('#').slice(1);
445 for (var i=0; i < items.length; i++) {
446 var y = items[i].split('!');
[9782]447 var type = y[0];
448 if ( type == 'filter') {
449 add_filter(y[1], y[2]);
450 } else if ( type == 'focus') {
451 var lonLat = new OpenLayers.LonLat(y[1], y[2]).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
452 map.setCenter (lonLat, y[3]);
453 }
[9781]454 }
[9235]455});
Note: See TracBrowser for help on using the repository browser.