source: src/django_gheat/website/static/heatmap_extensions_test.js@ 9229

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

Progress on filters.

File size: 5.8 KB
Line 
1// Base stolen from http://java.sun.com/developer/technicalArticles/J2EE/AJAX/RealtimeValidation/
2var json;
3function AjaxRequest(url, callback, filter, field) {
4
5 var req = init();
6 req.onreadystatechange = processRequest;
7
8 function init() {
9 if (window.XMLHttpRequest) {
10 return new XMLHttpRequest();
11 } else if (window.ActiveXObject) {
12 return new ActiveXObject("Microsoft.XMLHTTP");
13 }
14 }
15
16 function processRequest () {
17 if (req.readyState == 4) {
18 if (req.status == 200) {
19 if (callback) {
20 callback(req.responseText);
21 }
22 }
23 }
24 }
25
26 this.doGet = function() {
27 req.open("GET", url, true);
28 req.send(null);
29 }
30}
31
32
33// Uses zoomlevel en mouseposition to call the nodelist.py view. View returns nodes in json format which is read and printed in document element.
34function getNodeList(zoomlevel, mousepos) {
35 var url = "/website/nodelist/" + zoomlevel + "," + mousepos.lat + "," + mousepos.lon;
36 var ajax = new AjaxRequest(url, setNodeList);
37 ajax.doGet();
38}
39
40
41function setNodeList(responseText) {
42 json_nodelist=eval('(' + responseText + ')');
43 if (json_nodelist.length>0){
44 list = 'Wireless Leiden nodes on mouseposition:<br /><b>';
45 for (var i=0; i<json_nodelist.length; i++){
46 list += json_nodelist[i].fields.ssid + '<br />';
47 }
48 list += '</b>';
49 }
50 else{
51 list = 'No nodes to display.';
52 }
53 document.getElementById('node_list').innerHTML=list;
54}
55
56
57function get_filters() {
58
59 // Create form
60 var formstart = '<form method="get" action="">';
61 var formend = '<input type="button" value="Add filter" onClick="addFilter()"/></form>';
62 var layername = '<input type="text" id="lname" name="lname" size="10"/> Layername<br />';
63 var colour = '<input type="text" id="colour" name="colour" size="10"/> Colour<br />';
64 var user = '<select id="user" style="width:104px" onchange=update_filters(this.id)><option></option></select> User<br />';
65 var dataset = '<select id="dataset" style="width:104px" onchange=update_filters(this.id)><option></option></select> Dataset<br />';
66 var node = '<select id="node" style="width:104px" onchange=update_filters(this.id)><option></option></select> Node<br />';
67 var enc = '<select id="enc" style="width:104px"><option></option></select> Encryption<br />';
68 var date = '<select id="date" style="width:104px" onchange=update_filters(this.id)><option></option></select> Date<br />';
69
70 // Write form to document
71 document.getElementById('filter_list').innerHTML+=(formstart + date + user + dataset + node + enc + colour + layername + formend);
72
73 // Request JSON with filter values
74 var url = "/website/foofilter";
75 var ajax = new AjaxRequest(url, set_filters);
76 ajax.doGet();
77}
78
79function set_filters(responseText) {
80 json=eval('(' + responseText + ')');
81 var user = '<option value="empty"></option>';
82 var dataset = '<option value="empty"></option>';
83 var date = '<option value="empty"></option>';
84 for (var i=0; i<json.length; i++){
85 user += '<option value="user">' + json[i].gebruiker + '</option>';
86 for (m in json[i].meetrondje){
87 dataset += '<option value="dataset">' + json[i].meetrondje[m].naam + '</option>';
88 date += '<option value="date">' + json[i].meetrondje[m].datum + '</option>';
89 }
90 }
91 document.getElementById('user').innerHTML=user;
92 document.getElementById('dataset').innerHTML=dataset;
93 document.getElementById('date').innerHTML=date;
94}
95
96
97
98
99function update_filters(filter_id){
100 var user_text = $('#user option:selected').text();
101 var user_val = $('#user option:selected').val();
102 var dataset_text = $('#dataset option:selected').text();
103 var user = '<option value="empty"></option>';
104 var dataset = '<option value="empty"></option>';
105
106 if (filter_id=='user'){
107 if (!user_text){
108 for (var i=0; i<json.length; i++){
109 for (m in json[i].meetrondje){
110 dataset += '<option value="dataset">' + json[i].meetrondje[m].naam + '</option>';
111 }
112 }
113 }
114 else{
115 for (var i=0; i<json.length; i++){
116 for (m in json[i].meetrondje){
117 if (json[i].gebruiker == user_text){
118 dataset += '<option value="dataset">' + json[i].meetrondje[m].naam + '</option>';
119 }
120 }
121 }
122 }
123 document.getElementById('dataset').innerHTML=dataset;
124 }
125}
126
127
128
129
130
131function addFilter(){
132
133 var user = $('#user option:selected').text();
134 var dataset = $('#dataset option:selected').text();
135 var wlnode = $('#wlnode option:selected').text();
136 var enc = $('#enc option:selected').text();
137 var date = $('#date option:selected').text();
138 var colour = encodeURIComponent(document.getElementById("colour").value);
139 var lname = encodeURIComponent(document.getElementById("lname").value);
140
141 if (user != ''){user='&meetrondje__gebruiker__naam='+user;}
142 if (dataset != ''){dataset='&meetrondje__naam='+dataset;}
143 if (wlnode != ''){wlnode='&accespoint__ssid='+wlnode;}
144 if (enc != ''){enc='&accespoint__encryptie='+enc;}
145 if (date != ''){date='&meetrondje__datum='+date;}
146 if (colour != ''){colour='colour='+colour;}
147 else {colour = '&colour='+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256)+','+Math.floor(Math.random()*256);}
148 if (lname != ''){lname=lname;}
149 else {lname = 'Custom Filter';}
150
151 var baseurl = "/website/tile/${z}/${x},${y}.png?";
152
153 OpenLayers.Layer.OSM.Overlay = OpenLayers.Class(OpenLayers.Layer.OSM, {
154 initialize: function(name, options) {
155 var url = [
156 baseurl + colour + user + dataset + wlnode + enc + date
157 ];
158 options = OpenLayers.Util.extend({ numZoomLevels: 21 }, options);
159 var newArguments = [name, url, options];
160 OpenLayers.Layer.OSM.prototype.initialize.apply(this, newArguments);
161 },
162 CLASS_NAME: "OpenLayers.Layer.Overlay"
163 });
164
165 filterlayer = new OpenLayers.Layer.OSM.Overlay(lname, {isBaseLayer: false, visibility: true});
166 map.addLayer(filterlayer);
167}
Note: See TracBrowser for help on using the repository browser.