Total Complexity | 142 |
Complexity/F | 3.55 |
Lines of Code | 788 |
Function Count | 40 |
Duplicated Lines | 81 |
Ratio | 10.28 % |
Changes | 33 | ||
Bugs | 2 | Features | 4 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like js/map.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /*jslint |
||
19 | var map = null; |
||
20 | var copyrightDiv = null; |
||
21 | var theMarkers = new Markers(); |
||
22 | |||
23 | var CLAT_DEFAULT = 51.163375; |
||
24 | var CLON_DEFAULT = 10.447683; |
||
25 | var ZOOM_DEFAULT = 12; |
||
26 | var MAPTYPE_DEFAULT = "OSM"; |
||
27 | var RADIUS_DEFAULT = 0; |
||
28 | |||
29 | |||
30 | function enterEditMode(id) { |
||
31 | 'use strict'; |
||
32 | |||
33 | trackMarker('edit'); |
||
34 | var m = theMarkers.getById(id); |
||
35 | |||
36 | $('#edit_name' + m.getAlpha()).val(m.getName()); |
||
37 | $('#edit_coordinates' + m.getAlpha()).val(Coordinates.toString(m.getPosition())); |
||
38 | $('#edit_circle' + m.getAlpha()).val(m.getRadius()); |
||
39 | |||
40 | $('#dynview' + id).hide(); |
||
41 | $('#dynedit' + id).show(); |
||
42 | } |
||
43 | |||
44 | |||
45 | function leaveEditMode(id, takenew) { |
||
46 | 'use strict'; |
||
47 | |||
48 | if (takenew) { |
||
49 | var m = theMarkers.getById(id), |
||
50 | name = $('#edit_name' + m.getAlpha()).val(), |
||
51 | name_ok = /^([a-zA-Z0-9-_]*)$/.test(name), |
||
52 | s_coordinates = $('#edit_coordinates' + m.getAlpha()).val(), |
||
53 | coordinates = Coordinates.fromString(s_coordinates), |
||
54 | s_radius = $('#edit_circle' + m.getAlpha()).val(), |
||
55 | radius = Conversion.getInteger(s_radius, 0, 100000000000), |
||
56 | errors = []; |
||
57 | |||
58 | if (!name_ok) { |
||
59 | errors.push(mytrans("sidebar.markers.error_badname").replace(/%1/, name)); |
||
60 | } |
||
61 | if (!coordinates) { |
||
62 | errors.push(mytrans("sidebar.markers.error_badcoordinates").replace(/%1/, s_coordinates)); |
||
63 | } |
||
64 | if (radius === null) { |
||
65 | errors.push(mytrans("sidebar.markers.error_badradius").replace(/%1/, s_radius)); |
||
66 | } |
||
67 | |||
68 | if (errors.length > 0) { |
||
69 | showAlert(mytrans("dialog.error"), errors.join("<br /><br />")); |
||
70 | } else { |
||
71 | m.setNamePositionRadius(name, coordinates, radius); |
||
72 | $('#dynview' + id).show(); |
||
73 | $('#dynedit' + id).hide(); |
||
74 | } |
||
75 | } else { |
||
76 | $('#dynview' + id).show(); |
||
77 | $('#dynedit' + id).hide(); |
||
78 | } |
||
79 | } |
||
80 | |||
81 | |||
82 | function createMarkerDiv(id) { |
||
83 | 'use strict'; |
||
84 | |||
85 | var alpha = id2alpha(id), |
||
86 | iconw = 33, |
||
87 | iconh = 37, |
||
88 | offsetx = (id % 26) * iconw, |
||
89 | offsety = Math.floor(id / 26) * iconh; |
||
90 | |||
91 | return "<div id=\"dyn" + id + "\">" + |
||
92 | "<table id=\"dynview" + id + "\" style=\"width: 100%; vertical-align: middle;\">\n" + |
||
93 | " <tr>\n" + |
||
94 | " <td rowspan=\"3\" style=\"vertical-align: top\">\n" + |
||
95 | " <span style=\"width:" + iconw + "px; height:" + iconh + "px; float: left; display: block; background-image: url(img/markers.png); background-repeat: no-repeat; background-position: -" + offsetx + "px -" + offsety + "px;\"> </span>\n" + |
||
96 | " </td>\n" + |
||
97 | " <td style=\"text-align: center\"><i class=\"fa fa-map-marker\"></i></td>\n" + |
||
98 | " <td id=\"view_name" + alpha + "\" colspan=\"2\">marker</td>\n" + |
||
99 | " </tr>\n" + |
||
100 | " <tr>\n" + |
||
101 | " <td style=\"text-align: center\"><i class=\"fa fa-globe\"></i></td>\n" + |
||
102 | " <td id=\"view_coordinates" + alpha + "\" colspan=\"2\">N 48° 00.123 E 007° 51.456</td>\n" + |
||
103 | " </tr>\n" + |
||
104 | " <tr>\n" + |
||
105 | " <td style=\"text-align: center\"><i class=\"fa fa-circle-o\"></i></td>\n" + |
||
106 | " <td id=\"view_circle" + alpha + "\">16100 m</td>\n" + |
||
107 | " <td>\n" + |
||
108 | " <div class=\"btn-group\" style=\"padding-bottom: 2px; padding-top: 2px; float: right\">\n" + |
||
109 | " <button class=\"my-button btn btn-mini btn-warning\" data-i18n=\"[title]sidebar.markers.edit_marker\" type=\"button\" onclick=\"enterEditMode(" + id + ");\"><i class=\"fa fa-edit\"></i></button>\n" + |
||
110 | " <button class=\"my-button btn btn-mini btn-danger\" data-i18n=\"[title]sidebar.markers.delete_marker\" type=\"button\" onClick=\"theMarkers.removeById(" + id + ")\"><i class=\"fa fa-trash-o\"></i></button>\n" + |
||
111 | " <button class=\"my-button btn btn-mini btn-info\" data-i18n=\"[title]sidebar.markers.move_to\" type=\"button\" onClick=\"theMarkers.goto(" + id + ")\"><i class=\"fa fa-search\"></i></button>\n" + |
||
112 | " <button class=\"my-button btn btn-mini btn-warning\" data-i18n=\"[title]sidebar.markers.center\" type=\"button\" onClick=\"theMarkers.center(" + id + ")\"><i class=\"fa fa-crosshairs\"></i></button>\n" + |
||
113 | " <button class=\"my-button btn btn-mini btn-success\" data-i18n=\"[title]sidebar.markers.project\" type=\"button\" onClick=\"projectFromMarker(" + id + ")\"><i class=\"fa fa-location-arrow\"></i></button>\n" + |
||
114 | " </div>\n" + |
||
115 | " </td>\n" + |
||
116 | " </tr>\n" + |
||
117 | "</table>\n" + |
||
118 | "<table id=\"dynedit" + id + "\" style=\"display: none; width: 100%; vertical-align: middle;\">\n" + |
||
119 | " <tr>\n" + |
||
120 | " <td rowspan=\"4\" style=\"vertical-align: top\"><span style=\"width:" + iconw + "px; height:" + iconh + "px; float: left; display: block; background-image: url(img/markers.png); background-repeat: no-repeat; background-position: -" + offsetx + "px -" + offsety + "px;\"> </span>\n" + |
||
121 | " <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-map-marker\"></i></td>\n" + |
||
122 | " <td><input id=\"edit_name" + alpha + "\" data-i18n=\"[title]sidebar.markers.name;[placeholder]sidebar.markers.name_placeholder\" class=\"form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" + |
||
123 | " </tr>\n" + |
||
124 | " <tr>\n" + |
||
125 | " <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-globe\"></i></td>\n" + |
||
126 | " <td><input id=\"edit_coordinates" + alpha + "\" data-i18n=\"[title]sidebar.markers.coordinates;[placeholder]sidebar.markers.coordinates_placeholder\" class=\"form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" + |
||
127 | " </tr>\n" + |
||
128 | " <tr>\n" + |
||
129 | " <td style=\"text-align: center; vertical-align: middle;\"><i class=\"icon-circle-blank\"></i></td>\n" + |
||
130 | " <td><input id=\"edit_circle" + alpha + "\" data-i18n=\"[title]sidebar.markers.radius;[placeholder]sidebar.markers.radius_placeholder\" class=\"form-control input-block-level\" type=\"text\" style=\"margin-bottom: 0px;\" value=\"n/a\" /></td>\n" + |
||
131 | " </tr>\n" + |
||
132 | " <tr>\n" + |
||
133 | " <td colspan=\"2\" style=\"text-align: right\">\n" + |
||
134 | " <button class=\"btn btn-small btn-primary\" type=\"button\" onclick=\"javascript: leaveEditMode(" + id + ", true);\" data-i18n=\"dialog.ok\">OK</button>\n" + |
||
135 | " <button class=\"btn btn-small\" type=\"button\" onclick=\"leaveEditMode(" + id + ", false);\" data-i18n=\"dialog.cancel\">CANCEL</button>\n" + |
||
136 | " </td>\n" + |
||
137 | " </tr>\n" + |
||
138 | "</table>" + |
||
139 | "</div>"; |
||
140 | } |
||
141 | |||
142 | |||
143 | function newMarker(coordinates, id, radius, name) { |
||
144 | 'use strict'; |
||
145 | |||
146 | if (radius < 0) { |
||
147 | radius = RADIUS_DEFAULT; |
||
148 | } |
||
149 | |||
150 | if (id < 0 || id >= theMarkers.getSize() || !theMarkers.getById(id).isFree()) { |
||
151 | id = theMarkers.getFreeId(); |
||
152 | } |
||
153 | if (id < 0) { |
||
154 | showAlert( |
||
155 | mytrans("dialog.error"), |
||
156 | mytrans("dialog.toomanymarkers_error.content").replace(/%1/, theMarkers.getSize()) |
||
157 | ); |
||
158 | return null; |
||
159 | } |
||
160 | |||
161 | var alpha = id2alpha(id), |
||
162 | marker, |
||
163 | div, |
||
164 | nextid; |
||
165 | |||
166 | if (!name || name === "") { |
||
167 | name = "marker_" + alpha; |
||
168 | } |
||
169 | |||
170 | marker = theMarkers.getById(id); |
||
171 | marker.initialize(map, name, coordinates, radius); |
||
172 | div = createMarkerDiv(id); |
||
173 | |||
174 | nextid = theMarkers.getNextUsedId(id); |
||
175 | if (nextid < 0) { |
||
176 | $('#dynMarkerDiv').append(div); |
||
177 | } else { |
||
178 | $(div).insertBefore('#dyn' + nextid); |
||
179 | } |
||
180 | |||
181 | $('#edit_name' + alpha).keydown(function (e) { |
||
182 | if (e.which === 27) { |
||
183 | leaveEditMode(id, false); |
||
184 | } else if (e.which === 13) { |
||
185 | leaveEditMode(id, true); |
||
186 | } |
||
187 | }); |
||
188 | |||
189 | $('#edit_coordinates' + alpha).keydown(function (e) { |
||
190 | if (e.which === 27) { |
||
191 | leaveEditMode(id, false); |
||
192 | } else if (e.which === 13) { |
||
193 | leaveEditMode(id, true); |
||
194 | } |
||
195 | }); |
||
196 | |||
197 | $('#edit_circle' + alpha).keydown(function (e) { |
||
198 | if (e.which === 27) { |
||
199 | leaveEditMode(id, false); |
||
200 | } else if (e.which === 13) { |
||
201 | leaveEditMode(id, true); |
||
202 | } |
||
203 | }); |
||
204 | |||
205 | $('#btnmarkers2').show(); |
||
206 | $('#btnmarkersdelete1').removeAttr('disabled'); |
||
207 | $('#btnmarkersdelete2').removeAttr('disabled'); |
||
208 | |||
209 | marker.update(); |
||
210 | theMarkers.saveMarkersList(); |
||
211 | Lines.updateLinesMarkerAdded(); |
||
212 | |||
213 | return marker; |
||
214 | } |
||
215 | |||
216 | |||
217 | function projectFromMarker(id) { |
||
218 | 'use strict'; |
||
219 | |||
220 | trackMarker('project'); |
||
221 | |||
222 | var mm = theMarkers.getById(id), |
||
223 | oldpos = mm.getPosition(); |
||
224 | |||
225 | showProjectionDialog( |
||
226 | function (data1, data2) { |
||
227 | var angle = Conversion.getFloat(data1, 0, 360), |
||
228 | dist = Conversion.getFloat(data2, 0, 100000000000), |
||
229 | newpos, |
||
230 | newmarker; |
||
231 | |||
232 | if (angle === null) { |
||
233 | showAlert( |
||
234 | mytrans("dialog.error"), |
||
235 | mytrans("dialog.projection.error_bad_bearing").replace(/%1/, data1) |
||
236 | ); |
||
237 | return; |
||
238 | } |
||
239 | |||
240 | if (dist === null) { |
||
241 | showAlert( |
||
242 | mytrans("dialog.error"), |
||
243 | mytrans("dialog.projection.error_bad_distance").replace(/%1/, data2) |
||
244 | ); |
||
245 | return; |
||
246 | } |
||
247 | |||
248 | newpos = Coordinates.projection_geodesic(oldpos, angle, dist); |
||
249 | newmarker = newMarker(newpos, -1, RADIUS_DEFAULT, null); |
||
250 | if (newmarker) { |
||
251 | showAlert( |
||
252 | mytrans("dialog.information"), |
||
253 | mytrans("dialog.projection.msg_new_marker").replace(/%1/, newmarker.getAlpha()) |
||
254 | ); |
||
255 | } |
||
256 | } |
||
257 | ); |
||
258 | } |
||
259 | |||
260 | |||
261 | function storeCenter() { |
||
262 | 'use strict'; |
||
263 | |||
264 | var c = map.getCenter(); |
||
265 | Cookies.set('clat', c.lat(), {expires: 30}); |
||
266 | Cookies.set('clon', c.lng(), {expires: 30}); |
||
267 | } |
||
268 | |||
269 | |||
270 | function storeZoom() { |
||
271 | 'use strict'; |
||
272 | |||
273 | Cookies.set('zoom', map.getZoom(), {expires: 30}); |
||
274 | } |
||
275 | |||
276 | |||
277 | function getFeaturesString() { |
||
278 | 'use strict'; |
||
279 | |||
280 | var s = ""; |
||
281 | //if ($('#boundaries').is(':checked')) { s += "b"; } |
||
282 | if ($('#geocaches').is(':checked')) { s += "g"; } |
||
283 | if ($('#hillshading').is(':checked')) { s += "h"; } |
||
284 | if ($('#npa').is(':checked')) { s += "n"; } |
||
285 | if ($('#freifunk').is(':checked')) { s += "f"; } |
||
286 | |||
287 | return s; |
||
288 | } |
||
289 | |||
290 | |||
291 | function getPermalink() { |
||
292 | 'use strict'; |
||
293 | |||
294 | var lat = map.getCenter().lat(), |
||
295 | lng = map.getCenter().lng(); |
||
296 | |||
297 | return "http://flopp.net/" + |
||
298 | "?c=" + lat.toFixed(6) + ":" + lng.toFixed(6) + |
||
299 | "&z=" + map.getZoom() + |
||
300 | "&t=" + map.getMapTypeId() + |
||
301 | "&f=" + getFeaturesString() + |
||
302 | "&m=" + theMarkers.toString() + |
||
303 | "&d=" + Lines.getLinesText(); |
||
304 | } |
||
305 | |||
306 | function generatePermalink() { |
||
307 | 'use strict'; |
||
308 | |||
309 | var link = getPermalink(); |
||
310 | showLinkDialog(link); |
||
311 | } |
||
312 | |||
313 | |||
314 | function updateCopyrights() { |
||
315 | 'use strict'; |
||
316 | |||
317 | var newMapType = map.getMapTypeId(), |
||
318 | isGoogleMap = true, |
||
319 | copyright = ""; |
||
320 | |||
321 | Cookies.set('maptype', newMapType, {expires: 30}); |
||
322 | |||
323 | if (newMapType === "OSM" || newMapType === "OSM/DE") { |
||
324 | isGoogleMap = false; |
||
325 | copyright = "Map data (C) by <a href=\"http://www.openstreetmap.org/\">OpenStreetMap.org</a> and its contributors; <a href=\"http://opendatacommons.org/licenses/odbl/\">Open Database License</a>"; |
||
326 | } else if (newMapType === "OCM") { |
||
327 | isGoogleMap = false; |
||
328 | copyright = "Map data (C) by <a href=\"http://www.openstreetmap.org/\">OpenStreetMap.org</a> and its contributors; <a href=\"http://opendatacommons.org/licenses/odbl/\">Open Database License</a>, tiles (C) by <a href=\"http://opencyclemap.org\">OpenCycleMap.org</a>"; |
||
329 | } else if (newMapType === "OUTD") { |
||
330 | isGoogleMap = false; |
||
331 | copyright = "Map data (C) by <a href=\"http://www.openstreetmap.org/\">OpenStreetMap.org</a> and its contributors; <a href=\"http://opendatacommons.org/licenses/odbl/\">Open Database License</a>, tiles (C) by <a href=\"http://www.thunderforest.com/outdoors/\">Thunderforest</a>"; |
||
332 | } else if (newMapType === "TOPO") { |
||
333 | isGoogleMap = false; |
||
334 | copyright = "Map data (C) by <a href=\"http://www.openstreetmap.org/\">OpenStreetMap.org</a> and its contributors; <a href=\"http://opendatacommons.org/licenses/odbl/\">Open Database License</a>, height data by SRTM, tiles (C) by <a href=\"http://www.opentopomap.org/\">OpenTopoMap</a>"; |
||
335 | } |
||
336 | |||
337 | if (copyrightDiv) { |
||
338 | copyrightDiv.innerHTML = copyright; |
||
339 | } |
||
340 | |||
341 | if (isGoogleMap) { |
||
342 | $(".gmnoprint a, .gmnoprint span, .gm-style-cc").css("display", "block"); |
||
343 | $("a[href*='maps.google.com/maps']").show(); |
||
344 | map.setOptions({streetViewControl: true}); |
||
345 | } else { |
||
346 | // hide logo for non-g-maps |
||
347 | $("a[href*='maps.google.com/maps']").hide(); |
||
348 | // hide term-of-use for non-g-maps |
||
349 | $(".gmnoprint a, .gmnoprint span, .gm-style-cc").css("display", "none"); |
||
350 | map.setOptions({streetViewControl: false}); |
||
351 | } |
||
352 | } |
||
353 | |||
354 | |||
355 | function repairLat(x, d) { |
||
356 | 'use strict'; |
||
357 | |||
358 | if (Coordinates.validLat(x)) { |
||
359 | return x; |
||
360 | } |
||
361 | |||
362 | return d; |
||
363 | } |
||
364 | |||
365 | |||
366 | function repairLon(x, d) { |
||
367 | 'use strict'; |
||
368 | |||
369 | if (Coordinates.validLng(x)) { |
||
370 | return x; |
||
371 | } |
||
372 | |||
373 | return d; |
||
374 | } |
||
375 | |||
376 | |||
377 | function repairRadius(x, d) { |
||
378 | 'use strict'; |
||
379 | |||
380 | if (x === null || isNaN(x) || x < 0 || x > 100000000) { |
||
381 | return d; |
||
382 | } |
||
383 | |||
384 | return x; |
||
385 | } |
||
386 | |||
387 | |||
388 | function repairZoom(x, d) { |
||
389 | 'use strict'; |
||
390 | |||
391 | if (x === null || isNaN(x) || x < 1 || x > 20) { |
||
392 | return d; |
||
393 | } |
||
394 | |||
395 | return x; |
||
396 | } |
||
397 | |||
398 | |||
399 | function repairMaptype(t, d) { |
||
400 | 'use strict'; |
||
401 | |||
402 | if (t === "OSM" || t === "OSM/DE" || t === "OCM" || t === "OUTD" || t === "TOPO" || |
||
403 | t === "satellite" || t === "hybrid" || t === "roadmap" || t === "terrain") { |
||
404 | return t; |
||
405 | } |
||
406 | |||
407 | return d; |
||
408 | } |
||
409 | |||
410 | |||
411 | function parseMarkersFromUrl(urlarg) { |
||
412 | 'use strict'; |
||
413 | |||
414 | if (urlarg === null) { |
||
415 | return []; |
||
416 | } |
||
417 | |||
418 | var markers = [], |
||
419 | data; |
||
420 | |||
421 | // ID:COODRS:R(:NAME)?|ID:COORDS:R(:NAME)? |
||
422 | // COORDS=LAT:LON or DEG or DMMM |
||
423 | if (urlarg.indexOf("*") >= 0) { |
||
424 | data = urlarg.split('*'); |
||
425 | } else { |
||
426 | /* sep is '|' */ |
||
427 | data = urlarg.split('|'); |
||
428 | } |
||
429 | |||
430 | View Code Duplication | data.map(function (dataitem) { |
|
|
|||
431 | dataitem = dataitem.split(':'); |
||
432 | if (dataitem.length < 3 || dataitem.length > 5) { |
||
433 | return; |
||
434 | } |
||
435 | |||
436 | var m = { |
||
437 | alpha: dataitem[0], |
||
438 | id: alpha2id(dataitem[0]), |
||
439 | name: null, |
||
440 | coords: null, |
||
441 | r: 0 |
||
442 | }, |
||
443 | index = 1, |
||
444 | lat, |
||
445 | lon; |
||
446 | |||
447 | if (m.id < 0) { |
||
448 | return; |
||
449 | } |
||
450 | |||
451 | lat = parseFloat(dataitem[index]); |
||
452 | lon = parseFloat(dataitem[index + 1]); |
||
453 | if (Coordinates.valid(lat, lon)) { |
||
454 | index += 2; |
||
455 | m.coords = new google.maps.LatLng(lat, lon); |
||
456 | } else { |
||
457 | m.coords = Coordinates.fromString(dataitem[index]); |
||
458 | index += 1; |
||
459 | } |
||
460 | if (!m.coords) { |
||
461 | return; |
||
462 | } |
||
463 | |||
464 | m.r = repairRadius(parseFloat(dataitem[index]), 0); |
||
465 | index = index + 1; |
||
466 | |||
467 | if (index < dataitem.length && |
||
468 | /^([a-zA-Z0-9-_]*)$/.test(dataitem[index])) { |
||
469 | m.name = dataitem[index]; |
||
470 | } |
||
471 | |||
472 | markers.push(m); |
||
473 | }); |
||
474 | |||
475 | return markers; |
||
476 | } |
||
477 | |||
478 | |||
479 | function parseCenterFromUrl(urlarg) { |
||
480 | 'use strict'; |
||
481 | |||
482 | if (urlarg === null) { |
||
483 | return null; |
||
484 | } |
||
485 | |||
486 | var data = urlarg.split(':'), |
||
487 | lat, |
||
488 | lon; |
||
489 | |||
490 | if (data.length === 1) { |
||
491 | return Coordinates.fromString(data[0]); |
||
492 | } |
||
493 | |||
494 | if (data.length === 2) { |
||
495 | lat = parseFloat(data[0]); |
||
496 | lon = parseFloat(data[1]); |
||
497 | if (Coordinates.valid(lat, lon)) { |
||
498 | return new google.maps.LatLng(lat, lon); |
||
499 | } |
||
500 | } |
||
501 | |||
502 | return null; |
||
503 | } |
||
504 | |||
505 | |||
506 | function parseLinesFromUrl(urlarg) { |
||
507 | 'use strict'; |
||
508 | |||
509 | if (urlarg === null) { |
||
510 | return []; |
||
511 | } |
||
512 | |||
513 | var lines = []; |
||
514 | |||
515 | /* be backwards compatible */ |
||
516 | if (urlarg.length === 3 |
||
517 | && alpha2id(urlarg[0]) >= 0 |
||
518 | && urlarg[1] === '*' |
||
519 | && alpha2id(urlarg[1]) >= 0) { |
||
520 | urlarg = urlarg[0] + ':' + urlarg[2]; |
||
521 | } |
||
522 | |||
523 | urlarg.split('*').map(function (pair_string) { |
||
524 | var m = {source: -1, target: -1}, |
||
525 | pair = pair_string.split(':'); |
||
526 | |||
527 | if (pair.length !== 2) { |
||
528 | return; |
||
529 | } |
||
530 | |||
531 | m.source = alpha2id(pair[0]); |
||
532 | m.target = alpha2id(pair[1]); |
||
533 | |||
534 | lines.push(m); |
||
535 | }); |
||
536 | |||
537 | return lines; |
||
538 | } |
||
539 | |||
540 | |||
541 | function parseMarkersFromCookies() { |
||
542 | 'use strict'; |
||
543 | |||
544 | var raw_ids = Cookies.get('markers'), |
||
545 | markers = []; |
||
546 | |||
547 | if (raw_ids === null || raw_ids === undefined) { |
||
548 | return markers; |
||
549 | } |
||
550 | |||
551 | View Code Duplication | raw_ids.split(':').map(function (id_string) { |
|
552 | var m = {id: null, name: null, coords: null, r: 0}, |
||
553 | raw_data, |
||
554 | data, |
||
555 | lat, |
||
556 | lon; |
||
557 | |||
558 | m.id = parseInt(id_string, 10); |
||
559 | if (m.id === null || m.id < 0 || m.id >= 26 * 10) { |
||
560 | return; |
||
561 | } |
||
562 | |||
563 | raw_data = Cookies.get('marker' + m.id); |
||
564 | if (raw_data === null || raw_data === undefined) { |
||
565 | return; |
||
566 | } |
||
567 | |||
568 | data = raw_data.split(':'); |
||
569 | if (data.length !== 4) { |
||
570 | return; |
||
571 | } |
||
572 | |||
573 | lat = parseFloat(data[0]); |
||
574 | lon = parseFloat(data[1]); |
||
575 | if (Coordinates.valid(lat, lon)) { |
||
576 | return; |
||
577 | } |
||
578 | m.coords = new google.LatLng(lat, lon); |
||
579 | |||
580 | m.r = repairRadius(parseFloat(data[2]), 0); |
||
581 | |||
582 | if (/^([a-zA-Z0-9-_]*)$/.test(data[3])) { |
||
583 | m.name = data[3]; |
||
584 | } |
||
585 | |||
586 | markers.push(m); |
||
587 | }); |
||
588 | |||
589 | return markers; |
||
590 | } |
||
591 | |||
592 | |||
593 | function parseLinesFromCookies() { |
||
594 | 'use strict'; |
||
595 | |||
596 | var raw_lines = Cookies.get('lines'), |
||
597 | lines = []; |
||
598 | |||
599 | if (raw_lines === null || raw_lines === undefined) { |
||
600 | return lines; |
||
601 | } |
||
602 | |||
603 | raw_lines.split('*').map(function (pair_string) { |
||
604 | var m = {source: -1, target: -1}, |
||
605 | pair = pair_string.split(':'); |
||
606 | |||
607 | if (pair.length !== 2) { |
||
608 | return; |
||
609 | } |
||
610 | |||
611 | m.source = alpha2id(pair[0]); |
||
612 | m.target = alpha2id(pair[1]); |
||
613 | |||
614 | lines.push(m); |
||
615 | }); |
||
616 | |||
617 | return lines; |
||
618 | } |
||
619 | |||
620 | |||
621 | function initialize(xcenter, xzoom, xmap, xfeatures, xmarkers, xlines, xgeocache) { |
||
622 | 'use strict'; |
||
623 | |||
624 | var center = null, |
||
625 | atDefaultCenter = false, |
||
626 | zoom = parseInt(xzoom, 10), |
||
627 | maptype = xmap, |
||
628 | loadfromcookies = false, |
||
629 | markerdata = parseMarkersFromUrl(xmarkers), |
||
630 | markercenter = null, |
||
631 | clat = 0, |
||
632 | clon = 0; |
||
633 | if (markerdata.length > 0) { |
||
634 | markerdata.map(function (m) { |
||
635 | clat += m.coords.lat(); |
||
636 | clon += m.coords.lng(); |
||
637 | }); |
||
638 | markercenter = new google.maps.LatLng(clat / markerdata.length, clon / markerdata.length); |
||
639 | } |
||
640 | |||
641 | if (xcenter && xcenter !== '') { |
||
642 | center = parseCenterFromUrl(xcenter); |
||
643 | } else if (markercenter) { |
||
644 | center = markercenter; |
||
645 | } else { |
||
646 | loadfromcookies = true; |
||
647 | |||
648 | /* try to read coordinats from cookie */ |
||
649 | clat = get_cookie_float('clat', CLAT_DEFAULT); |
||
650 | clon = get_cookie_float('clon', CLON_DEFAULT); |
||
651 | if (clat === CLAT_DEFAULT && clon === CLON_DEFAULT) { |
||
652 | atDefaultCenter = true; |
||
653 | } |
||
654 | |||
655 | clat = repairLat(clat, CLAT_DEFAULT); |
||
656 | clon = repairLon(clon, CLON_DEFAULT); |
||
657 | center = new google.maps.LatLng(clat, clon); |
||
658 | |||
659 | zoom = get_cookie_int('zoom', ZOOM_DEFAULT); |
||
660 | maptype = get_cookie_string('maptype', MAPTYPE_DEFAULT); |
||
661 | } |
||
662 | |||
663 | if (!center) { |
||
664 | center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT); |
||
665 | atDefaultCenter = true; |
||
666 | } |
||
667 | |||
668 | zoom = repairZoom(zoom, ZOOM_DEFAULT); |
||
669 | maptype = repairMaptype(maptype, MAPTYPE_DEFAULT); |
||
670 | map = new google.maps.Map( |
||
671 | document.getElementById("themap"), |
||
672 | { |
||
673 | zoom: zoom, |
||
674 | center: center, |
||
675 | scaleControl: true, |
||
676 | streetViewControl: false, |
||
677 | mapTypeControlOptions: { mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] }, |
||
678 | mapTypeId: google.maps.MapTypeId.ROADMAP |
||
679 | } |
||
680 | ); |
||
681 | |||
682 | map.mapTypes.set("OSM", osmProvider("OSM")); |
||
683 | map.mapTypes.set("OSM/DE", osmDeProvider("OSM/DE")); |
||
684 | map.mapTypes.set("OCM", ocmProvider("OCM")); |
||
685 | map.mapTypes.set("OUTD", thunderforestOutdoorsProvider("OUTD")); |
||
686 | map.mapTypes.set("TOPO", opentopomapProvider("TOPO")); |
||
687 | map.setMapTypeId(maptype); |
||
688 | |||
689 | Sidebar.init(map); |
||
690 | ExternalLinks.init(map); |
||
691 | Lines.init(map); |
||
692 | Geolocation.init(map); |
||
693 | Hillshading.init(map); |
||
694 | NPA.init(map); |
||
695 | CDDA.init(map); |
||
696 | Freifunk.init(map); |
||
697 | |||
698 | //boundariesLayer = new google.maps.ImageMapType({ |
||
699 | // getTileUrl: function(coord, zoom) { |
||
700 | // if (6 <= zoom && zoom <= 16) |
||
701 | // { |
||
702 | // return tileUrl("http://korona.geog.uni-heidelberg.de/tiles/adminb/?x=%x&y=%y&z=%z", ["dummy"], coord, zoom); |
||
703 | // } |
||
704 | // else |
||
705 | // { |
||
706 | // return null; |
||
707 | // } |
||
708 | // }, |
||
709 | // tileSize: new google.maps.Size(256, 256), |
||
710 | // name: "adminb", |
||
711 | // alt: "Administrative Boundaries", |
||
712 | // maxZoom: 16 }); |
||
713 | |||
714 | // Create div for showing copyrights. |
||
715 | copyrightDiv = document.createElement("div"); |
||
716 | copyrightDiv.id = "map-copyright"; |
||
717 | copyrightDiv.style.fontSize = "11px"; |
||
718 | copyrightDiv.style.fontFamily = "Arial, sans-serif"; |
||
719 | copyrightDiv.style.margin = "0 2px 2px 0"; |
||
720 | copyrightDiv.style.whiteSpace = "nowrap"; |
||
721 | copyrightDiv.style.background = "#FFFFFF"; |
||
722 | map.controls[google.maps.ControlPosition.BOTTOM_RIGHT].push(copyrightDiv); |
||
723 | |||
724 | map.setCenter(center, zoom); |
||
725 | |||
726 | google.maps.event.addListener(map, "center_changed", function () { |
||
727 | storeZoom(); |
||
728 | storeCenter(); |
||
729 | okapi_schedule_load_caches(); |
||
730 | }); |
||
731 | google.maps.event.addListener(map, "zoom_changed", function () { |
||
732 | storeZoom(); |
||
733 | storeCenter(); |
||
734 | okapi_schedule_load_caches(); |
||
735 | }); |
||
736 | google.maps.event.addListener(map, "maptypeid_changed", function () { |
||
737 | updateCopyrights(); |
||
738 | }); |
||
739 | |||
740 | if (loadfromcookies) { |
||
741 | parseMarkersFromCookies().map(function (m) { |
||
742 | newMarker(m.coords, m.id, m.r, m.name); |
||
743 | }); |
||
744 | |||
745 | parseLinesFromCookies().map(function (m) { |
||
746 | if (m.source >= 0 && theMarkers.getById(m.source).isFree()) { |
||
747 | m.source = -1; |
||
748 | } |
||
749 | if (m.target >= 0 && theMarkers.getById(m.target).isFree()) { |
||
750 | m.target = -1; |
||
751 | } |
||
752 | Lines.newLine(m.source, m.target); |
||
753 | }); |
||
754 | } else { |
||
755 | markerdata.map(function (m) { |
||
756 | newMarker(m.coords, m.id, m.r, m.name); |
||
757 | }); |
||
758 | |||
759 | parseLinesFromUrl(xlines).map(function (m) { |
||
760 | if (m.source >= 0 && theMarkers.getById(m.source).isFree()) { |
||
761 | m.source = -1; |
||
762 | } |
||
763 | if (m.target >= 0 && theMarkers.getById(m.target).isFree()) { |
||
764 | m.target = -1; |
||
765 | } |
||
766 | Lines.newLine(m.source, m.target); |
||
767 | }); |
||
768 | } |
||
769 | |||
770 | okapi_show_cache = xgeocache; |
||
771 | Sidebar.restore(true); |
||
772 | xfeatures = xfeatures.toLowerCase(); |
||
773 | if (xfeatures === '[default]') { |
||
774 | Hillshading.restore(false); |
||
775 | //restoreBoundaries(false); |
||
776 | restoreGeocaches(false); |
||
777 | NPA.toggle(false); |
||
778 | CDDA.toggle(false); |
||
779 | Freifunk.toggle(false); |
||
780 | } else { |
||
781 | Hillshading.toggle(xfeatures.indexOf('h') >= 0); |
||
782 | //toggleBoundaries(xfeatures.indexOf('b') >= 0); |
||
783 | okapi_toggle_load_caches(xfeatures.indexOf('g') >= 0); |
||
784 | NPA.toggle(xfeatures.indexOf('n') >= 0); |
||
785 | Freifunk.toggle(xfeatures.indexOf('f') >= 0); |
||
786 | } |
||
787 | restoreCoordinatesFormat(0); |
||
788 | |||
789 | if (xgeocache !== "") { |
||
790 | okapi_toggle_load_caches(true); |
||
791 | atDefaultCenter = false; |
||
792 | } |
||
793 | |||
794 | // update copyrights + gmap-stuff now, once the map is fully loaded, and in 1s - just to be sure! |
||
795 | updateCopyrights(); |
||
796 | google.maps.event.addListenerOnce(map, 'idle', function () { |
||
797 | updateCopyrights(); |
||
798 | }); |
||
799 | setTimeout(function () { |
||
800 | updateCopyrights(); |
||
801 | }, 1000); |
||
802 | |||
803 | //if (atDefaultCenter) { |
||
804 | // Geolocation.whereAmI(); |
||
805 | //} |
||
806 | } |
||
807 |