/
index.js
240 lines (188 loc) · 5.55 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
var orig_search_param;
var reload_active = false;
var data;
function update_distances() {
search_param = form_search.get_data();
for(var i = 0; i < data.data.length; i++) {
var el = data.data[i];
if(search_param.location) {
el.distance = haversine({
latitude: search_param.location.latitude,
longitude: search_param.location.longitude
}, {
latitude: parseFloat(el.LAT),
longitude: parseFloat(el.LON),
}, {unit: 'meter'});
}
else {
el.distance = '';
}
}
}
var filters = {};
function load_filters() {
for(var k in form_search.def) {
if(form_search.def[k].filter_function) {
eval("_filter = " + form_search.def[k].filter_function);
filters[k] = _filter;
}
}
}
function apply_filters(data, filter) {
for(var k in filters) {
if(filter[k] == null)
continue;
if(!filters[k](data[k], filter[k]))
return false;
}
return true;
}
function follow_link(a) {
var x = update_url(a.href);
if(x == true)
return false;
}
function update_url(url) {
var location_base = location.href.slice(0, location.href.length - location.search.length);
var query_string = url.slice(location_base.length);
if(!query_string.match(/^\?/))
return false;
var query_data = {};
var parts = query_string.slice(1).split("&");
for(var i = 0; i < parts.length; i++) {
var p = parts[i].split("=");
query_data[decodeURIComponent(p[0])] = decodeURIComponent(p[1]);
}
form_search.set_data(query_data);
form_search.set_orig_data(form_search.get_data());
history.pushState(form_search.get_data(), null, query_string);
update_location();
return true;
}
function catch_links(dom) {
var as = dom.getElementsByTagName("a");
var location_base = location.href.slice(0, location.href.length - location.search.length);
for(var i = 0; i < as.length; i++) {
var a = as[i];
if(a.href.slice(0, location_base.length) == location_base) {
a.onclick = follow_link.bind(this, a);
}
}
}
function update_table() {
if(!data.data)
return;
update_distances();
var search_param = form_search.get_data();
var filtered_data = [];
for(var i = 0; i < data.data.length; i++) {
if(apply_filters(data.data[i], search_param))
filtered_data.push(data.data[i]);
}
var t = new table(table_def, filtered_data, {
template_engine: "twig"
});
twig_render_into(document.getElementById("search_status"), "result.html", {
'count': filtered_data.length,
'max_list': max_list
});
if(filtered_data.length == 1)
t.show("html-transposed", update_table_show.bind(this, t));
else if(filtered_data.length > 0)
t.show("html", { limit: max_list }, update_table_show.bind(this, t));
else
update_table_show(t, "");
}
function update_table_show(t, table_content) {
document.getElementById("table").innerHTML = table_content;
catch_links(document.getElementById("table"));
t.resize();
}
function update_data(search_param, _data) {
reload_active = false;
document.body.className = "";
orig_search_param = search_param;
form_search.set_orig_data(search_param);
if((!_data) || (!_data.data)) {
alert("Error loading data!");
return;
}
data = _data;
update_table();
twig_render_into(document.getElementById("footer"), "footer.html", data.info);
}
function update_location(reload) {
var search_param = form_search.get_data();
history.replaceState(search_param, null, location.href);
var ajax_param = {};
if(data) {
if(search_param.location) {
if(orig_search_param.location) {
var distance = haversine({
latitude: search_param.location.latitude,
longitude: search_param.location.longitude
}, {
latitude: orig_search_param.location.latitude,
longitude: orig_search_param.location.longitude,
}, {unit: 'meter'});
if(distance > 100)
reload = true;
}
else {
reload = true;
}
}
}
else
reload = true;
if(!search_param.location) {
if(search_param.BAUMNUMMER)
ajax_param.BAUMNUMMER = search_param.BAUMNUMMER;
reload = true;
}
if(reload && (!reload_active)) {
if(search_param.location) {
ajax_param.latitude = search_param.location.latitude;
ajax_param.longitude = search_param.location.longitude;
}
ajax("data.php", ajax_param, update_data.bind(this, search_param));
document.body.className = "loading";
reload_active = true;
}
var url = [];
for(var k in search_param) {
if((k == 'location') && (search_param.location)) {
url.push('location[latitude]=' + encodeURIComponent(search_param.location.latitude));
url.push('location[longitude]=' + encodeURIComponent(search_param.location.longitude));
}
else {
if(search_param[k])
url.push(encodeURIComponent(k) + '=' + encodeURIComponent(search_param[k]));
}
}
url = '.?' + url.join('&');
history.replaceState(form_search.get_data(), null, url);
if(!data)
return;
update_table();
}
window.onload = function() {
form_search = form__;
form_search.onchange = update_location;
load_filters();
window.addEventListener('popstate', function(event) {
form_search.set_data(event.state);
form_search.set_orig_data(form_search.get_data());
update_location();
});
document.getElementById("form_search").onsubmit = function() {
update_location();
return false;
}
history.replaceState(form_search.get_data(), null, location.href);
// TODO: data may not have been loaded into JS space
//catch_links(document.getElementById("table"));
call_hooks("init");
orig_search_param = form_search.get_data();
update_location();
}