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 ( c2f046...f09db0 )
by Florian
01:24
created

Okapi.loadBbox   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
c 1
b 1
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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
            response.map(function (site) {
84
                if (keys[site.site_name] !== undefined) {
85
                    //console.log("adding OC site: " + site.site_name);
86
                    self.m_sites.push({
87
                        siteid: self.m_sites.length,
88
                        name: site.site_name,
89
                        site_url: site.site_url,
90
                        url: site.okapi_base_url,
91
                        prefix: prefixes[site.site_name],
92
                        key: keys[site.site_name],
93
                        ignore_user: null,
94
                        markers: {},
95
                        finished: true
96
                    });
97
                }
98
            });
99
100
            self.m_ready = true;
101
            if (self.m_enabled) {
102
                self.scheduleLoad(true);
103
            }
104
            if (self.m_showCache && self.m_showCache !== "") {
105
                self.centerMap(self.m_showCache);
106
                self.m_showCache = null;
107
            }
108
        }
109
    });
110
};
111
112
113
Okapi.setupIcons = function () {
114
    'use strict';
115
116
    if (this.m_icons) {
117
        return;
118
    }
119
120
    this.m_icons = {
121
        "Other": new google.maps.MarkerImage("img/cachetype-1.png"),
122
        "Traditional": new google.maps.MarkerImage("img/cachetype-2.png"),
123
        "Multi": new google.maps.MarkerImage("img/cachetype-3.png"),
124
        "Virtual": new google.maps.MarkerImage("img/cachetype-4.png"),
125
        "Webcam": new google.maps.MarkerImage("img/cachetype-5.png"),
126
        "Event": new google.maps.MarkerImage("img/cachetype-6.png"),
127
        "Quiz": new google.maps.MarkerImage("img/cachetype-7.png"),
128
        "Math/Physics": new google.maps.MarkerImage("img/cachetype-8.png"),
129
        "Moving": new google.maps.MarkerImage("img/cachetype-9.png"),
130
        "Drive-In": new google.maps.MarkerImage("img/cachetype-10.png")
131
    };
132
};
133
134
135
Okapi.icon = function (type) {
136
    'use strict';
137
138
    if (this.m_icons[type] !== undefined) {
139
        return this.m_icons[type];
140
    }
141
142
    return this.m_icons.Other;
143
};
144
145
146
Okapi.guessSiteId = function (code) {
147
    'use strict';
148
149
    code = code.upperCase();
150
    var siteid;
151
    for (siteid = 0; siteid < this.m_sites.length; siteid += 1) {
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
        markers.map(function (m) {
263
            m.setMap(null);
264
        });
265
    }
266
};
267
268
269
Okapi.removeMarkers = function () {
270
    'use strict';
271
272
    if (!this.m_ready) {
273
        return;
274
    }
275
276
    var self = this;
277
    this.m_sites.map(function (site) {
278
        self.removeMarkersSite(site.markers);
279
        site.markers = {};
280
    });
281
282
    if (this.m_marker) {
283
        this.m_marker.setMap(null);
284
        delete this.m_marker;
285
        this.m_marker = null;
286
    }
287
};
288
289
290
Okapi.loadBboxSite = function (siteid) {
291
    'use strict';
292
293
    if (!this.m_ready) {
294
        return;
295
    }
296
297
    var self = this,
298
        site = this.m_sites[siteid],
299
        b,
300
        bbox;
301
302
    if (!this.m_enabled) {
303
        site.finished = true;
304
        return;
305
    }
306
307
    if (!site.finished) {
308
        return;
309
    }
310
311
    site.finished = false;
312
313
    b = this.m_map.getBounds();
314
    bbox = b.getSouthWest().lat() + "|" + b.getSouthWest().lng() + "|" + b.getNorthEast().lat() + "|" + b.getNorthEast().lng();
315
316
    $.ajax({
317
        url: site.url + 'services/caches/shortcuts/search_and_retrieve',
318
        dataType: 'json',
319
        data: {
320
            'consumer_key': site.key,
321
            'search_method': 'services/caches/search/bbox',
322
            'search_params': '{"bbox" : "' + bbox + '", "limit" : "500"}',
323
            'retr_method': 'services/caches/geocaches',
324
            'retr_params': '{"fields": "code|name|location|type|status|url"}',
325
            'wrap': 'false'
326
        },
327
        success: function (response) {
328
            var addedCaches = {},
329
                cache,
330
                code;
331
332
            for (code in response) {
333
                if (!response.hasOwnProperty(code)) {
334
                    continue;
335
                }
336
337
                cache = response[code];
338
                if (cache.status !== "Available") {
339
                    continue;
340
                }
341
342
                addedCaches[cache.code] = true;
343
                if (site.markers[cache.code] !== undefined) {
344
                    continue;
345
                }
346
347
                site.markers[cache.code] = new google.maps.Marker({
348
                    position: self.parseLocation(cache.location),
349
                    map: self.m_map,
350
                    icon: self.icon(cache.type)
351
                });
352
353
                self.registerPopup(site.markers[cache.code], cache.code, siteid);
354
            }
355
356
            for (code in site.markers) {
357
                if (site.markers.hasOwnProperty(code) && addedCaches[code] === undefined) {
358
                    site.markers[code].setMap(null);
359
                    delete site.markers[code];
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 self = this;
382
    this.m_sites.map(function (site) {
383
        self.loadBboxSite(site.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