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

Last change on this file since 9563 was 9406, checked in by dennisw, 13 years ago

Fixed layout issues with colourboxes.

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