GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 776483...6e9dec )
by Florian
01:16
created

map.js ➔ ... ➔ google.maps.event.addListener   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $, google, Lines, Markers, Conversion, Cookies, Coordinates, trackMarker, mytrans, showAlert,
7
  id2alpha, alpha2id,
8
  showProjectionDialog, showLinkDialog,
9
  osmProvider, osmDeProvider, thunderforestProvider, opentopomapProvider,
10
  get_cookie_int, get_cookie_float, get_cookie_string,
11
  Attribution, Sidebar, ExternalLinks, Hillshading, Geolocation, NPA, CDDA, Freifunk, Okapi,
12
  DownloadGPX, API_KEY_THUNDERFOREST,
13
  restoreCoordinatesFormat,
14
  document
15
*/
16
17
//var boundariesLayer = null;
18
//var boundariesLayerShown = false;
19
var map = null;
20
var CLAT_DEFAULT = 51.163375;
21
var CLON_DEFAULT = 10.447683;
22
var ZOOM_DEFAULT = 12;
23
var MAPTYPE_DEFAULT = "OSM";
24
25
function projectFromMarker(id) {
26
    'use strict';
27
28
    trackMarker('project');
29
30
    var mm = Markers.getById(id),
31
        oldpos = mm.getPosition();
32
33
    showProjectionDialog(
34
        function (data1, data2) {
35
            var angle = Conversion.getFloat(data1, 0, 360),
36
                dist = Conversion.getFloat(data2, 0, 100000000000),
37
                newpos,
38
                newmarker;
39
40
            if (angle === null) {
41
                showAlert(
42
                    mytrans("dialog.error"),
43
                    mytrans("dialog.projection.error_bad_bearing").replace(/%1/, data1)
44
                );
45
                return;
46
            }
47
48
            if (dist === null) {
49
                showAlert(
50
                    mytrans("dialog.error"),
51
                    mytrans("dialog.projection.error_bad_distance").replace(/%1/, data2)
52
                );
53
                return;
54
            }
55
56
            newpos = Coordinates.projection_geodesic(oldpos, angle, dist);
57
            newmarker = Markers.newMarker(newpos, -1, 0, null);
58
            if (newmarker) {
59
                showAlert(
60
                    mytrans("dialog.information"),
61
                    mytrans("dialog.projection.msg_new_marker").replace(/%1/, newmarker.getAlpha())
62
                );
63
            }
64
        }
65
    );
66
}
67
68
69
function storeCenter() {
70
    'use strict';
71
72
    var c = map.getCenter();
73
    Cookies.set('clat', c.lat(), {expires: 30});
74
    Cookies.set('clon', c.lng(), {expires: 30});
75
}
76
77
78
function storeZoom() {
79
    'use strict';
80
81
    Cookies.set('zoom', map.getZoom(), {expires: 30});
82
}
83
84
85
function storeMapType() {
86
    'use strict';
87
88
    Cookies.set('maptype', map.getMapTypeId(), {expires: 30});
89
}
90
91
92
function getFeaturesString() {
93
    'use strict';
94
95
    var s = "";
96
    //if ($('#boundaries').is(':checked')) { s += "b"; }
97
    if ($('#geocaches').is(':checked')) { s += "g"; }
98
    if ($('#hillshading').is(':checked')) { s += "h"; }
99
    if ($('#npa').is(':checked')) { s += "n"; }
100
    if ($('#freifunk').is(':checked')) { s += "f"; }
101
102
    return s;
103
}
104
105
106
function getPermalink() {
107
    'use strict';
108
109
    var lat = map.getCenter().lat(),
110
        lng = map.getCenter().lng(),
111
        geocache = Okapi.popupCacheCode(),
112
        url = "https://flopp.net/" +
113
            "?c=" + lat.toFixed(6) + ":" + lng.toFixed(6) +
114
            "&z=" + map.getZoom() +
115
            "&t=" + map.getMapTypeId() +
116
            "&f=" + getFeaturesString() +
117
            "&m=" + Markers.toString() +
118
            "&d=" + Lines.getLinesText();
119
    if (geocache) {
120
        url += "&g=" + geocache;
121
    }
122
    return url;
123
}
124
125
function generatePermalink() {
126
    'use strict';
127
128
    var link = getPermalink();
129
    showLinkDialog(link);
130
}
131
132
133
function repairLat(x, d) {
134
    'use strict';
135
136
    if (Coordinates.validLat(x)) {
137
        return x;
138
    }
139
140
    return d;
141
}
142
143
144
function repairLon(x, d) {
145
    'use strict';
146
147
    if (Coordinates.validLng(x)) {
148
        return x;
149
    }
150
151
    return d;
152
}
153
154
155
function repairRadius(x, d) {
156
    'use strict';
157
158
    if (x === null || isNaN(x) || x < 0 || x > 100000000) {
159
        return d;
160
    }
161
162
    return x;
163
}
164
165
166
function repairZoom(x, d) {
167
    'use strict';
168
169
    if (x === null || isNaN(x) || x < 1 || x > 20) {
170
        return d;
171
    }
172
173
    return x;
174
}
175
176
177
function repairMaptype(t, d) {
178
    'use strict';
179
180
    var mapTypes = {
181
        "OSM": 1, "OSM/DE": 1, "OCM": 1, "OUTD": 1, "TOPO": 1, "satellite": 1, "hybrid": 1, "roadmap": 1, "terrain": 1
182
    };
183
184
    if (mapTypes[t]) {
185
        return t;
186
    }
187
188
    return d;
189
}
190
191
192
function parseMarkersFromUrl(urlarg) {
193
    'use strict';
194
195
    if (urlarg === null) {
196
        return [];
197
    }
198
199
    var markers = [],
200
        data;
201
202
    // ID:COODRS:R(:NAME)?|ID:COORDS:R(:NAME)?
203
    // COORDS=LAT:LON or DEG or DMMM
204
    if (urlarg.indexOf("*") >= 0) {
205
        data = urlarg.split('*');
206
    } else {
207
        /* sep is '|' */
208
        data = urlarg.split('|');
209
    }
210
211
    data.map(function (dataitem) {
212
        dataitem = dataitem.split(':');
213
        if (dataitem.length < 3 || dataitem.length > 5) {
214
            return;
215
        }
216
217
        var m = {
218
                alpha: dataitem[0],
219
                id: alpha2id(dataitem[0]),
220
                name: null,
221
                coords: null,
222
                r: 0
223
            },
224
            index = 1,
225
            lat,
226
            lon;
227
228
        if (m.id < 0) {
229
            return;
230
        }
231
232
        lat = parseFloat(dataitem[index]);
233
        lon = parseFloat(dataitem[index + 1]);
234
        if (Coordinates.valid(lat, lon)) {
235
            index += 2;
236
            m.coords = new google.maps.LatLng(lat, lon);
237
        } else {
238
            m.coords = Coordinates.fromString(dataitem[index]);
239
            index += 1;
240
        }
241
        if (!m.coords) {
242
            return;
243
        }
244
245
        m.r = repairRadius(parseFloat(dataitem[index]), 0);
246
        index = index + 1;
247
248
        if (index < dataitem.length &&
249
                /^([a-zA-Z0-9-_]*)$/.test(dataitem[index])) {
250
            m.name = dataitem[index];
251
        }
252
253
        markers.push(m);
254
    });
255
256
    return markers;
257
}
258
259
260
function parseCenterFromUrl(urlarg) {
261
    'use strict';
262
263
    if (urlarg === null) {
264
        return null;
265
    }
266
267
    var data = urlarg.split(':');
268
269
    if (data.length === 1) {
270
        return Coordinates.fromString(data[0]);
271
    }
272
273
    if (data.length === 2) {
274
        return Coordinates.toLatLng(parseFloat(data[0]), parseFloat(data[1]));
275
    }
276
277
    return null;
278
}
279
280
281
function parseLinesFromUrl(urlarg) {
282
    'use strict';
283
284
    if (urlarg === null) {
285
        return [];
286
    }
287
288
    var lines = [];
289
290
    /* be backwards compatible */
291
    if (urlarg.length === 3
292
            && alpha2id(urlarg[0]) >= 0
293
            && urlarg[1] === '*'
294
            && alpha2id(urlarg[1]) >= 0) {
295
        urlarg = urlarg[0] + ':' + urlarg[2];
296
    }
297
298
    urlarg.split('*').map(function (pair_string) {
299
        var pair = pair_string.split(':');
300
        if (pair.length === 2) {
301
            lines.push({source: alpha2id(pair[0]), target: alpha2id(pair[1])});
302
        }
303
304
    });
305
306
    return lines;
307
}
308
309
310
function parseMarkersFromCookies() {
311
    'use strict';
312
313
    var raw_ids = Cookies.get('markers'),
314
        markers = [];
315
316
    if (raw_ids === null || raw_ids === undefined) {
317
        return markers;
318
    }
319
320
    raw_ids.split(':').map(function (id_string) {
321
        var m = {id: null, name: null, coords: null, r: 0},
322
            raw_data,
323
            data;
324
325
        m.id = parseInt(id_string, 10);
326
        if (m.id === null || m.id < 0 || m.id >= 26 * 10) {
327
            return;
328
        }
329
330
        raw_data = Cookies.get('marker' + m.id);
331
        if (!raw_data) {
332
            return;
333
        }
334
335
        data = raw_data.split(':');
336
        if (data.length !== 4) {
337
            return;
338
        }
339
340
        m.coords = Coordinates.toLatLng(parseFloat(data[0]), parseFloat(data[1]));
341
        if (!m.coords) {
342
            return;
343
        }
344
345
        m.r = repairRadius(parseFloat(data[2]), 0);
346
347
        if (/^([a-zA-Z0-9-_]*)$/.test(data[3])) {
348
            m.name = data[3];
349
        }
350
351
        markers.push(m);
352
    });
353
354
    return markers;
355
}
356
357
358
function parseLinesFromCookies() {
359
    'use strict';
360
361
    var raw_lines = Cookies.get('lines'),
362
        lines = [];
363
364
    if (!raw_lines) {
365
        return lines;
366
    }
367
368
    raw_lines.split('*').map(function (pair_string) {
369
        var pair = pair_string.split(':');
370
        if (pair.length === 2) {
371
            lines.push({source: alpha2id(pair[0]), target: alpha2id(pair[1])});
372
        }
373
    });
374
375
    return lines;
376
}
377
378
379
function createMap(id, center, zoom, maptype) {
380
    'use strict';
381
382
    var m = new google.maps.Map(
383
        document.getElementById(id),
384
        {
385
            zoom: zoom,
386
            center: center,
387
            scaleControl: true,
388
            streetViewControl: false,
389
            mapTypeControlOptions: { mapTypeIds: ['OSM', 'OSM/DE', 'OCM', 'OUTD', 'TOPO', google.maps.MapTypeId.ROADMAP, google.maps.MapTypeId.SATELLITE, google.maps.MapTypeId.HYBRID, google.maps.MapTypeId.TERRAIN] },
390
            mapTypeId: google.maps.MapTypeId.ROADMAP
391
        }
392
    );
393
394
    m.mapTypes.set("OSM", osmProvider("OSM"));
395
    m.mapTypes.set("OSM/DE", osmDeProvider("OSM/DE"));
396
    m.mapTypes.set("OCM", thunderforestProvider("OCM", "cycle", API_KEY_THUNDERFOREST));
397
    m.mapTypes.set("OUTD", thunderforestProvider("OUTD", "outdoors", API_KEY_THUNDERFOREST));
398
    m.mapTypes.set("TOPO", opentopomapProvider("TOPO"));
399
    m.setMapTypeId(maptype);
400
401
    Attribution.init(m);
402
    Sidebar.init(m);
403
    ExternalLinks.init(m);
404
    Markers.init(m);
405
    Lines.init(m);
406
    Geolocation.init(m);
407
    Hillshading.init(m);
408
    NPA.init(m);
409
    CDDA.init(m);
410
    Freifunk.init(m);
411
    Okapi.init(m);
412
    DownloadGPX.init(m);
413
414
    //boundariesLayer = new google.maps.ImageMapType({
415
    //  getTileUrl: function(coord, zoom) {
416
    //    if (6 <= zoom && zoom <= 16)
417
    //    {
418
    //      return tileUrl("http://korona.geog.uni-heidelberg.de/tiles/adminb/?x=%x&y=%y&z=%z", ["dummy"], coord, zoom);
419
    //    }
420
    //    else
421
    //    {
422
    //      return null;
423
    //    }
424
    //  },
425
    //  tileSize: new google.maps.Size(256, 256),
426
    //  name: "adminb",
427
    //  alt: "Administrative Boundaries",
428
    //  maxZoom: 16 });
429
430
    m.setCenter(center, zoom);
431
432
    google.maps.event.addListener(m, "center_changed", function () {
433
        storeZoom();
434
        storeCenter();
435
    });
436
    google.maps.event.addListener(m, "zoom_changed", function () {
437
        storeZoom();
438
        storeCenter();
439
    });
440
    google.maps.event.addListener(m, "maptypeid_changed", function () {
441
        storeMapType();
442
    });
443
444
    return m;
445
}
446
447
448
function initialize(xcenter, xzoom, xmap, xfeatures, xmarkers, xlines, xgeocache) {
449
    'use strict';
450
451
    var center,
452
        atDefaultCenter = false,
453
        zoom = parseInt(xzoom, 10),
454
        maptype = xmap,
455
        loadfromcookies = false,
456
        markerdata = parseMarkersFromUrl(xmarkers),
457
        markercenter = null,
458
        clat = 0,
459
        clon = 0;
460
    if (markerdata.length > 0) {
461
        markerdata.map(function (m) {
462
            clat += m.coords.lat();
463
            clon += m.coords.lng();
464
        });
465
        markercenter = new google.maps.LatLng(clat / markerdata.length, clon / markerdata.length);
466
    }
467
468
    if (xcenter && xcenter !== '') {
469
        center = parseCenterFromUrl(xcenter);
470
    } else if (markercenter) {
471
        center = markercenter;
472
    } else {
473
        loadfromcookies = true;
474
475
        /* try to read coordinats from cookie */
476
        clat = get_cookie_float('clat', CLAT_DEFAULT);
477
        clon = get_cookie_float('clon', CLON_DEFAULT);
478
        if (clat === CLAT_DEFAULT && clon === CLON_DEFAULT) {
479
            atDefaultCenter = true;
480
        }
481
482
        clat = repairLat(clat, CLAT_DEFAULT);
483
        clon = repairLon(clon, CLON_DEFAULT);
484
        center = new google.maps.LatLng(clat, clon);
485
486
        zoom = get_cookie_int('zoom', ZOOM_DEFAULT);
487
        maptype = get_cookie_string('maptype', MAPTYPE_DEFAULT);
488
    }
489
490
    if (!center) {
491
        center = new google.maps.LatLng(CLAT_DEFAULT, CLON_DEFAULT);
492
        atDefaultCenter = true;
493
    }
494
495
    zoom = repairZoom(zoom, ZOOM_DEFAULT);
496
    maptype = repairMaptype(maptype, MAPTYPE_DEFAULT);
497
    map = createMap("themap", center, zoom, maptype);
498
499
    if (loadfromcookies) {
500
        parseMarkersFromCookies().map(function (m) {
501
            Markers.newMarker(m.coords, m.id, m.r, m.name);
502
        });
503
504
        parseLinesFromCookies().map(function (m) {
505
            Lines.newLine(m.source, m.target);
506
        });
507
    } else {
508
        markerdata.map(function (m) {
509
            Markers.newMarker(m.coords, m.id, m.r, m.name);
510
        });
511
512
        parseLinesFromUrl(xlines).map(function (m) {
513
            Lines.newLine(m.source, m.target);
514
        });
515
    }
516
517
    Okapi.setShowCache(xgeocache);
518
    Sidebar.restore(true);
519
    xfeatures = xfeatures.toLowerCase();
520
    if (xfeatures === '[default]') {
521
        Hillshading.restore(false);
522
        //restoreBoundaries(false);
523
        Okapi.restore(false);
524
        NPA.toggle(false);
525
        CDDA.toggle(false);
526
        Freifunk.toggle(false);
527
    } else {
528
        Hillshading.toggle(xfeatures.indexOf('h') >= 0);
529
        //toggleBoundaries(xfeatures.indexOf('b') >= 0);
530
        Okapi.toggle(xfeatures.indexOf('g') >= 0);
531
        NPA.toggle(xfeatures.indexOf('n') >= 0);
532
        Freifunk.toggle(xfeatures.indexOf('f') >= 0);
533
    }
534
    restoreCoordinatesFormat("DM");
535
536
    if (xgeocache !== "") {
537
        Okapi.toggle(true);
538
        atDefaultCenter = false;
539
    }
540
541
    Attribution.forceUpdate();
542
543
    if (atDefaultCenter) {
544
        Geolocation.whereAmI();
545
    }
546
}
547