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 ( 45f4d7...bcf64e )
by Florian
01:13
created

js/map.js   F

Complexity

Total Complexity 96
Complexity/F 3

Size

Lines of Code 555
Function Count 32

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 17
Bugs 1 Features 4
Metric Value
cc 0
c 17
b 1
f 4
nc 1
dl 0
loc 555
rs 3.12
wmc 96
mnd 3
bc 82
fnc 32
bpm 2.5625
cpm 3
noi 0

19 Functions

Rating   Name   Duplication   Size   Complexity  
A map.js ➔ storeCenter 0 7 1
A map.js ➔ storeMapType 0 5 1
A map.js ➔ storeZoom 0 5 1
B map.js ➔ projectFromMarker 0 42 1
A map.js ➔ parseLinesFromCookies 0 19 2
A map.js ➔ getPermalink 0 18 2
A map.js ➔ generatePermalink 0 6 1
A map.js ➔ repairLon 0 9 2
A map.js ➔ parseMarkersFromCookies 0 50 3
B map.js ➔ repairRadius 0 9 5
B map.js ➔ repairZoom 0 9 5
A map.js ➔ repairLat 0 9 2
A map.js ➔ parseCenterFromUrl 0 19 4
A map.js ➔ repairMaptype 0 21 2
A map.js ➔ createMap 0 67 1
F map.js ➔ initialize 0 99 12
B map.js ➔ parseLinesFromUrl 0 27 6
B map.js ➔ getFeaturesString 0 20 5
A map.js ➔ parseMarkersFromUrl 0 73 3

How to fix   Complexity   

Complexity

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