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 ( 67de7b...c2f046 )
by Florian
01:44
created

Okapi.centerMap   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 16
rs 9.4285
1
/*jslint
2
  indent: 4
3
*/
4
5
/*global
6
  $, google, mytrans, window, Cookies, Coordinates, get_cookie_string
7
*/
8
9
10
var Okapi = {};
11
Okapi.m_map = null;
12
Okapi.m_sites = null;
13
Okapi.m_ready = false;
14
Okapi.m_showCache = null;
15
Okapi.m_enabled = false;
16
Okapi.m_popup = null;
17
Okapi.m_marker = null;
18
Okapi.m_icons = null;
19
Okapi.m_timer = null;
20
21
22
Okapi.init = function (themap) {
23
    'use strict';
24
25
    this.m_map = themap;
26
};
27
28
29
Okapi.parseLocation = function (s) {
30
    'use strict';
31
32
    var loc = s.split("|"),
33
        lat = parseFloat(loc[0]),
34
        lng = parseFloat(loc[1]);
35
36
    if (Coordinates.valid(lat, lng)) {
37
        return new google.maps.LatLng(lat, lng);
38
    }
39
40
    return null;
41
};
42
43
44
Okapi.setShowCache = function (code) {
45
    'use strict';
46
47
    this.m_showCache = code;
48
};
49
50
51
Okapi.setupSites = function () {
52
    'use strict';
53
54
    if (this.m_sites) {
55
        return;
56
    }
57
58
    var self = this,
59
        main_url = "http://www.opencaching.pl/okapi/services/apisrv/installations",
60
        keys = {
61
            "Opencaching.DE" : "YSqPufH82encfJ67ZxV2",
62
            "Opencaching.PL" : "jhRyc6rGmT6XEvxva29B",
63
            "Opencaching.NL" : "gcwaesuq3REu8RtCgLDj",
64
            "Opencaching.US" : "GvgyCMvwfH42GqJGL494",
65
            "Opencaching.ORG.UK" : "7t7VfpkCd4HuxPabfbHd",
66
            "Opencaching.RO" : "gqSWmVJhZGDwc4sRhyy7"
67
        },
68
        prefixes = {
69
            "Opencaching.DE" : "OC",
70
            "Opencaching.PL" : "OP",
71
            "Opencaching.NL" : "OB",
72
            "Opencaching.US" : "OU",
73
            "Opencaching.ORG.UK" : "OK",
74
            "Opencaching.RO" : "OR"
75
        };
76
77
    this.m_sites = {};
78
79
    $.ajax({
80
        url: main_url,
81
        dataType: 'json',
82
        success: function (response) {
83
            var siteid, site;
84
            for (siteid in response) {
85
                site = response[siteid];
86
                if (site.site_name in keys) {
87
                    //console.log("adding OC site: " + site.site_name);
88
                    self.m_sites[siteid] = {
89
                        siteid: siteid,
90
                        name: site.site_name,
91
                        site_url: site.site_url,
92
                        url: site.okapi_base_url,
93
                        prefix: prefixes[site.site_name],
94
                        key: keys[site.site_name],
95
                        ignore_user: null,
96
                        markers: {},
97
                        finished: true
98
                    };
99
                }
100
            }
101
102
            self.m_ready = true;
103
            if (self.m_enabled) {
104
                self.scheduleLoad(true);
105
            }
106
            if (self.m_showCache && self.m_showCache !== "") {
107
                self.centerMap(self.m_showCache);
108
                self.m_showCache = null;
109
            }
110
        }
111
    });
112
};
113
114
115
Okapi.setupIcons = function () {
116
    'use strict';
117
118
    if (this.m_icons) {
119
        return;
120
    }
121
122
    this.m_icons = {};
123
    this.m_icons["Other"] = new google.maps.MarkerImage("img/cachetype-1.png");
124
    this.m_icons["Traditional"] = new google.maps.MarkerImage("img/cachetype-2.png");
125
    this.m_icons["Multi"] = new google.maps.MarkerImage("img/cachetype-3.png");
126
    this.m_icons["Virtual"] = new google.maps.MarkerImage("img/cachetype-4.png");
127
    this.m_icons["Webcam"] = new google.maps.MarkerImage("img/cachetype-5.png");
128
    this.m_icons["Event"] = new google.maps.MarkerImage("img/cachetype-6.png");
129
    this.m_icons["Quiz"] = new google.maps.MarkerImage("img/cachetype-7.png");
130
    this.m_icons["Math/Physics"] = new google.maps.MarkerImage("img/cachetype-8.png");
131
    this.m_icons["Moving"] = new google.maps.MarkerImage("img/cachetype-9.png");
132
    this.m_icons["Drive-In"] = new google.maps.MarkerImage("img/cachetype-10.png");
133
};
134
135
136
Okapi.icon = function (type) {
137
    'use strict';
138
139
    if (type in this.m_icons) {
140
        return this.m_icons[type];
141
    }
142
143
    return this.m_icons["Other"];
144
};
145
146
147
Okapi.guessSiteId = function (code) {
148
    'use strict';
149
150
    code = code.upperCase();
151
    for (siteid in this.m_sites) {
0 ignored issues
show
Bug introduced by
The variable siteid seems to be never declared. Assigning variables without defining them first makes them global. If this was intended, consider making it explicit like using window.siteid.
Loading history...
152
        if (code.startsWith(this.m_sites[siteid].prefix)) {
153
            return siteid;
154
        }
155
    }
156
157
    return -1;
158
};
159
160
161
Okapi.centerMap = function (code) {
162
    'use strict';
163
164
    if (!this.m_ready) {
165
        //console.log("okapi not ready");
166
        return;
167
    }
168
169
    var siteid = this.guessSiteId(code);
170
    if (siteid < 0) {
171
        //console.log("bad code. cannot determine okapi site");
172
        return;
173
    }
174
175
    this.showPopup(null, code.toUpperCase(), siteid);
176
};
177
178
179
Okapi.createPopupContent = function (code, response) {
180
    'use strict';
181
182
    var content =
183
        '<a href="' + response.url + '" target="_blank">' + code + ' <b>' + response.name + '</b></a><br />'
184
        + '<table>'
185
        + '<tr><td>' + mytrans("geocache.owner") + '</td><td>' + '<a href="' + response.owner.profile_url + '" target="_blank"><b>' + response.owner.username + '</b></a></td></tr>'
186
        + '<tr><td>' + mytrans("geocache.type") + '</td><td>' + response.type + '</td></tr>'
187
        + '<tr><td>' + mytrans("geocache.size") + '</td><td>' + response.size2 + '</td></tr>'
188
        + '<tr><td>' + mytrans("geocache.status") + '</td><td>' + response.status + '</td></tr>'
189
        + '<tr><td>' + mytrans("geocache.difficulty") + '</td><td>' + response.difficulty + '/5</td></tr>'
190
        + '<tr><td>' + mytrans("geocache.terrain") + '</td><td>' + response.terrain + '/5</td></tr>'
191
        + '<tr><td>' + mytrans("geocache.finds") + '</td><td>' + response.founds + '</td></tr>'
192
        + '</table>';
193
    return content;
194
};
195
196
197
Okapi.showPopup = function (m, code, siteid) {
198
    'use strict';
199
200
    if (!this.m_popup) {
201
        this.m_popup = new google.maps.InfoWindow();
202
    }
203
204
    var self = this,
205
        site = this.m_sites[siteid];
206
207
    $.ajax({
208
        url: site.url + 'services/caches/geocache',
209
        dataType: 'json',
210
        data: {
211
            'consumer_key': site.key,
212
            'cache_code': code,
213
            'fields' : 'name|type|status|url|owner|founds|size2|difficulty|terrain|location'
214
        },
215
        success: function (response) {
216
            var coords = self.parseLocation(response.location);
217
            self.m_map.setCenter(coords);
218
219
            if (!m) {
220
                m = new google.maps.Marker({
221
                    position: coords,
222
                    map: self.m_map,
223
                    icon: self.icon(response.type)
224
                });
225
                if (self.m_maker) {
226
                    self.m_marker.setMap(null);
227
                }
228
                self.registerPopup(m, code, siteid);
229
                self.m_marker = m;
230
            }
231
232
            self.m_popup.setContent(self.createPopupContent(code, response));
233
            self.m_popup.open(self.m_map, m);
234
        }
235
    });
236
};
237
238
239
Okapi.registerPopup = function (m, code, siteid) {
240
    'use strict';
241
242
    if (!this.m_ready) {
243
        return;
244
    }
245
246
    var self = this;
247
248
    google.maps.event.addListener(m, 'click', function () {
249
        self.showPopup(m, code, siteid);
250
    });
251
};
252
253
254
Okapi.removeMarkersSite = function (markers) {
255
    'use strict';
256
257
    if (!this.m_ready) {
258
        return;
259
    }
260
261
    if (markers) {
262
        var m;
263
        for (m in markers) {
264
            markers[m].setMap(null);
265
            delete markers[m];
266
        }
267
    }
268
};
269
270
271
Okapi.removeMarkers = function () {
272
    'use strict';
273
274
    if (!this.m_ready) {
275
        return;
276
    }
277
278
    var siteid;
279
    for (siteid in this.m_sites) {
280
        this.removeMarkersSite(this.m_sites[siteid].markers);
281
        this.m_sites[siteid].markers = {};
282
    }
283
284
    if (this.m_marker) {
285
        this.m_marker.setMap(null);
286
        delete this.m_marker;
287
        this.m_marker = null;
288
    }
289
};
290
291
292
Okapi.loadBboxSite = function (siteid) {
293
    'use strict';
294
295
    if (!this.m_ready) {
296
        return;
297
    }
298
299
    var self = this,
300
        site = this.m_sites[siteid],
301
        b,
302
        bbox;
303
304
    if (!this.m_enabled) {
305
        site.finished = true;
306
        return;
307
    }
308
309
    if (!site.finished) {
310
        return;
311
    }
312
313
    site.finished = false;
314
315
    b = this.m_map.getBounds();
316
    bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng();
317
318
    $.ajax({
319
        url: site.url + 'services/caches/shortcuts/search_and_retrieve',
320
        dataType: 'json',
321
        data: {
322
            'consumer_key': site.key,
323
            'search_method': 'services/caches/search/bbox',
324
            'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}',
325
            'retr_method': 'services/caches/geocaches',
326
            'retr_params': '{"fields": "code|name|location|type|status|url"}',
327
            'wrap': 'false'
328
        },
329
        success: function (response) {
330
            var addedCaches = {},
331
                code,
332
                cache,
333
                m;
334
335
            for (code in response) {
336
                cache = response[code];
337
338
                if (cache.status !== "Available") {
339
                    continue;
340
                }
341
                addedCaches[cache.code] = true;
342
                if (cache.code in site.markers) {
343
                    continue;
344
                }
345
346
                m = new google.maps.Marker({
347
                    position: self.parseLocation(cache.location),
348
                    map: self.m_map,
349
                    icon: self.icon(cache.type)
350
                });
351
352
                site.markers[cache.code] = m;
353
                self.registerPopup(m, cache.code, siteid);
354
            }
355
356
            for (m in site.markers) {
357
                if (!(m in addedCaches)) {
358
                    site.markers[m].setMap(null);
359
                    delete site.markers[m];
360
                }
361
            }
362
            site.finished = true;
363
        },
364
        error: function () {
365
            //console.log("okapi request failed: " + site.name);
366
            self.removeMarkersSite(site.markers);
367
            site.markers = {};
368
            site.finished = true;
369
        }
370
    });
371
};
372
373
374
Okapi.loadBbox = function () {
375
    'use strict';
376
377
    if (!this.m_ready) {
378
        return;
379
    }
380
381
    var siteid;
382
    for (siteid in this.m_sites) {
383
        this.loadBboxSite(siteid);
384
    }
385
};
386
387
388
Okapi.unscheduleLoad = function () {
389
    'use strict';
390
391
    if (!this.m_ready) {
392
        return;
393
    }
394
395
    if (this.m_timer) {
396
        window.clearTimeout(this.m_timer);
397
        this.m_timer = null;
398
    }
399
};
400
401
402
Okapi.scheduleLoad = function () {
403
    'use strict';
404
405
    if (!this.m_ready) {
406
        return;
407
    }
408
409
    var self = this;
410
411
    this.unscheduleLoad();
412
    this.m_timer = window.setTimeout(function () {
413
        self.loadBbox();
414
    }, 500);
415
};
416
417
418
Okapi.toggle = function (t) {
419
    'use strict';
420
421
    Cookies.set('load_caches', t ? "1" : "0", {expires: 30});
422
    if ($('#geocaches').is(':checked') !== t) {
423
        $('#geocaches').attr('checked', t);
424
    }
425
426
    if (this.m_enabled !== t) {
427
        this.m_enabled = t;
428
    }
429
430
    if (this.m_enabled) {
431
        this.setupIcons();
432
        this.setupSites();
433
        this.scheduleLoad();
434
    } else {
435
        this.unscheduleLoad();
436
        this.removeMarkers();
437
    }
438
};
439
440
441
Okapi.restore = function (defaultValue) {
442
    'use strict';
443
444
    var state = get_cookie_string("load_caches", "invalid");
445
446
    if (state === "0") {
447
        this.toggle(false);
448
    } else if (state === "1") {
449
        this.toggle(true);
450
    } else {
451
        this.toggle(defaultValue);
452
    }
453
};
454