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

Last change on this file since 9394 was 9358, checked in by rick, 13 years ago

Starting to make production ready:

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