This repository has been archived by the owner on May 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
472 lines (430 loc) · 15.9 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
var jsonReturned;
var thumbnailURLs = [];
var markerList = {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": []
}
};
var rightMarker;
var map;
var rightMap;
var CLIENT_ID = "TVBudDVxUkNVVU5BZFQ5QmpKZVlndzoxMjE3N2VmOTE2YzU4OTNj";
//position last time node was changed
var lastNodechange;
//bottom bar map scroll handler //this will have to change, now just removing map when scrolling
$(document).scroll(function () {
var storyPosition = $("#fullscreen-view").offset().top;
var y = $(document).scrollTop(),
footer = $("#clear-map");
if (y >= storyPosition) {
footer.css({position: "fixed", "top": "70%"});
$("#expand-map").css({display: "block", position: "fixed", "top": "95%"});
} else {
//cannot expand map if at top part of page
footer.css({position: "relative", display: "none"});
$("#expand-map").css({position: "relative", display: "none"});
}
//if scrolled more than xxx px -- hide map
if (y > lastNodechange + 400) {
footer.css({position: "relative", display: "none"});
if (y >= storyPosition) {
$("#expand-map").css({display: "block", position: "fixed", "top": "95%"});
}
} else if (y < lastNodechange - 600) {
footer.css({position: "relative", display: "none"});
if (y >= storyPosition) {
$("#expand-map").css({display: "block", position: "fixed", "top": "95%"});
}
}
});
// Click handlers
$("#open-bottom-map").click(function () {
$("#clear-map").css({position: "fixed", display: "block"});
$("#expand-map").css({display: "none"});
});
$("#close-bottom-map").click(function () {
footer = $("#clear-map");
footer.css({position: "relative", display: "none"});
$("#expand-map").css({display: "block"});
});
initPage();
initRightMap(53.0, 53.0, 'right-map');
$("html, body").animate({scrollTop: 0}, "fast");
function initAllViewers() {
$('.fullscreen-item').each(function (i, el) {
var picId = $(el).find('img').attr('src').replace("https://d1cuyjsrcm0gby.cloudfront.net/", '');
picId = picId.replace("/thumb-2048.jpg", '');
var id = "fullscreen-item-" + picId;
$(el).attr('id', id);
var fullscreenId = '#fullscreen-' + picId;
$(fullscreenId).attr('id', fullscreenId);
initViewerMapBlock($(el));
});
}
function getGistJson() {
$.ajax({
url: 'https://api.github.com/gists/' + '008b49fd6bb056ddf15c6562fb4f0a26',
type: 'GET',
dataType: 'jsonp'
}).success(function (gistdata) {
jsonReturned = JSON.parse(gistdata.data.files.sequence_list.content);
$('#mainTitle').text(jsonReturned.mainTitle);
$('#mainDescription').text(jsonReturned.frontPageDescription);
$('#description').fadeIn("slow");
$(jsonReturned.keys).each(function (index, element) {
getThumbnailForSequence(jsonReturned.keys[index].key, jsonReturned.keys.length, index);
});
});
}
function getLocalJson() {
$.getJSON("sequence_list.json", function (json) {
jsonReturned = json;
$('#mainTitle').text(jsonReturned.mainTitle);
$('#mainDescription').text(jsonReturned.frontPageDescription);
$('#description').fadeIn("slow");
$(jsonReturned.keys).each(function (index, element) {
getThumbnailForSequence(jsonReturned.keys[index].key, jsonReturned.keys.length, index);
});
});
}
function getLocalNewJson() {
$.getJSON("story.json", function (json) {
jsonReturned = json;
$('#mainTitle').text(jsonReturned.mainTitle);
$('#mainDescription').text(jsonReturned.frontPageDescription);
$('#intro').text(jsonReturned.intro);
$('#author').text(jsonReturned.author);
$('#date').text(jsonReturned.date);
$('#description').fadeIn("slow");
$('#authorDate').fadeIn("slow");
$(jsonReturned.keys).each(function (index, element) {
getThumbnailForSequence(jsonReturned.keys[index].key, jsonReturned.keys.length, index);
});
});
}
//Helper functions
function initPage() {
$('document').ready(function () {
$(window).scrollTop();
// getGistJson();
// getLocalJson();
getLocalNewJson();
});
}
//finds the description of the current element
function findDescription(currentIndex) {
for (var z = 0; z < jsonReturned.keys.length; z++) {
if (jsonReturned.keys[z].key === sequenceIds[currentIndex]) {
return jsonReturned.keys[currentIndex].description;
}
}
return "";
}
//finds the startNode of the current element
function findStartNode(currentIndex) {
if (jsonReturned.keys[currentIndex].startNode) {
return jsonReturned.keys[currentIndex].startNode;
} else {
return undefined;
}
}
function appendStoryElements(index) {
for (var i = 0; i < thumbnailURLs.length; i++) {
if (thumbnailURLs[i] != "") { //if element has sequence
var id = thumbnailURLs[i].replace('https://d1cuyjsrcm0gby.cloudfront.net/', '');
id = id.replace('/thumb-2048.jpg', '');
var description = findDescription(i);
var thumbnail = findStartNode(i);
console.log("thumbnail", thumbnail);
if(thumbnail!= undefined) {
id = thumbnail;
}
$('#fullscreen-view').append("<div id='fullscreen-description'> <div class='description-text'> <h1 id='fullscreen-title'>" + jsonReturned.keys[i].title + " </h1> <h2 class='fullscreen-body' id=\'" + jsonReturned.keys[i].title.replace(' ', '-') + "\'>" +
description +
"</h2> </div> <p id='img-description'></p> <div class='fullscreen-item'> <img src='" +
'https://d1cuyjsrcm0gby.cloudfront.net/'+id +'/thumb-2048.jpg' +
"'/> </div> </div>");
} else { //text with no sequence (wont find right description if multiple entries with no sequence)
var description = findDescription(i);
$('#fullscreen-view').append("<div id='fullscreen-description'> <div class='description-text'> <h1 id='fullscreen-title'>" + jsonReturned.keys[i].title + " </h1> <h2 class='fullscreen-body' id=\'" + jsonReturned.keys[i].title.replace(' ', '-') + "\'>" +
description +
"</h2> </div> </div>");
}
}
}
var counter = 0;
var sequenceIds = [];
function getThumbnailForSequence(sequenceId, length, currentIndex) {
sequenceIds.push(sequenceId);
if (sequenceId != "") {
var pathAppend = '/v2/s/' + sequenceId + '?client_id=' + CLIENT_ID;
var host = 'https://a.mapillary.com';
$.ajax({
url: host + pathAppend,
type: 'GET',
dataType: 'json',
success: function (data) {
var thumbnail = ("https://d1cuyjsrcm0gby.cloudfront.net/" + data.keys[0] +
"/thumb-2048.jpg");
thumbnailURLs[currentIndex] = thumbnail;
counter++;
if (counter == length) { //to make sure that all elements have been fetched before continuing
appendStoryElements(currentIndex);
//should be here, was initializing map multiple times. still dont have all locations..
initMap(53.0, 53.0, 'map');
}
initAllViewers();
// initMap(53.0, 53.0, 'map');
}, //success end
error: function () {
console.log("ERROR: when fetching the thumbnails for the sequences.");
}
});
} else {
thumbnailURLs[currentIndex] = ""; //appends empty string to the URL's..
counter++;
}
}
function initMap(lat, lon, mapContainer) {
mapboxgl.accessToken = 'pk.eyJ1IjoibWFwaWxsYXJ5IiwiYSI6ImNpanB0NmN1bDAwOTF2dG03enM3ZHRocDcifQ.Z6wgtnyRBO0TuY3Ak1tVLQ';
map = new mapboxgl.Map({
container: mapContainer, // container id
style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location
center: [lon, lat], // starting position
zoom: 12 // starting zoom
});
var mapillarySource = {
type: 'vector',
tiles: ['https://d2munx5tg0hw47.cloudfront.net/tiles/{z}/{x}/{y}.mapbox'],
minzoom: 0,
maxzoom: 16
};
var bounds = new mapboxgl.LngLatBounds();
for (var i = 0; i < jsonReturned.keys.length; i++) {
addMarker(map, bounds, jsonReturned.keys[i].key, jsonReturned.keys[i].title)
}
//error handling
map.on('error', function (err) {
console.log("An error occured while loading the map");
console.log(err);
})
map.on('load', function () {
map.addSource("points", markerList);
map.addLayer({
"id": "points",
"type": "symbol",
"source": "points",
"layout": {
"icon-image": "{icon}-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
});
map.addSource('mapillary', mapillarySource)
map.addLayer({
'id': 'mapillary',
'type': 'line',
'source': 'mapillary',
'source-layer': 'mapillary-sequences',
'layout': {
'line-cap': 'round',
'line-join': 'round'
},
'paint': {
'line-opacity': 0.6,
'line-color': 'rgb(53, 175, 109)',
'line-width': 2
}
}, 'markers')
map.on('click', function (e) {
var divToScroll = map.queryRenderedFeatures(e.point)[0].properties.title;
divToScroll = ('#' + divToScroll).toString();
divToScroll = divToScroll.replace(' ', '-');
$('html, body').animate({
scrollTop: $(divToScroll).offset().top
}, 2000);
});
});
map.scrollZoom.disable();
map.addControl(new mapboxgl.Navigation());
}
function addMarker(map, bounds, searchKey, title) {
//to exclude any keys that do not have a sequence
if (searchKey != "") {
var pathAppend = '/v2/s/' + searchKey + '?client_id=' + CLIENT_ID;
var host = 'https://a.mapillary.com';
$.ajax({
url: host + pathAppend,
type: 'GET',
dataType: 'json'
}).success(function (data) {
var coords = data.coords[0];
var feature = {
"type": "Feature",
"properties": {
"title": title,
"icon": "marker-15"
},
"geometry": {
"type": "Point",
"coordinates": [coords[0], coords[1]]
}
};
markerList.data.features.push(feature);
bounds.extend(feature.geometry.coordinates);
map.fitBounds(bounds, {padding: 100}); //padding to see all points
});
}
}
function initRightMap(lat, lon, setMap, picId) {
mapboxgl.accessToken = 'pk.eyJ1IjoibWFwaWxsYXJ5IiwiYSI6ImNpanB0NmN1bDAwOTF2dG03enM3ZHRocDcifQ.Z6wgtnyRBO0TuY3Ak1tVLQ';
rightMap = new mapboxgl.Map({
container: setMap, // container id
style: 'mapbox://styles/mapbox/streets-v8', //stylesheet location
center: [lon, lat], // starting position
zoom: 16 // starting zoom
});
var mapillarySource = {
type: 'vector',
tiles: ['https://d2munx5tg0hw47.cloudfront.net/tiles/{z}/{x}/{y}.mapbox'],
minzoom: 0,
maxzoom: 16
};
//on error
rightMap.on('error', function (err) {
console.log("An error occured while loading the right-map");
console.log(err);
})
rightMap.on('style.load', function () {
var markerSource = {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [lon, lat]
},
properties: {
title: 'You\'re here!',
'marker-symbol': 'marker'
}
}
};
rightMap.addSource('markers', markerSource);
rightMap.addLayer({
id: 'markers',
type: 'symbol',
source: 'markers',
layout: {
'icon-image': '{marker-symbol}-15',
'text-field': '{title}',
'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
'text-offset': [0, 0.6],
'text-anchor': 'top'
}
})
rightMap.addSource('mapillary', mapillarySource)
rightMap.addLayer({
'id': 'mapillary',
'type': 'line',
'source': 'mapillary',
'source-layer': 'mapillary-sequences',
'layout': {
'line-cap': 'round',
'line-join': 'round'
},
'paint': {
'line-opacity': 0.8,
'line-color': 'rgb(53, 175, 109)',
'line-width': 4
}
}, 'markers')
})
rightMap.scrollZoom.disable();
rightMap.addControl(new mapboxgl.Navigation());
}
function checkForImageDescription(el, picId) {
var imgDescription = "";
$(jsonReturned.imageDescriptions).each(function (index, element) {
if (element.key === picId) {
imgDescription = element.description;
}
});
var descriptionElement = el.parent().find('#img-description')
if (imgDescription != "") {
descriptionElement.show();
descriptionElement.show();
descriptionElement.text(imgDescription);
console.log("Image Description for this image!!");
} else {
descriptionElement.hide();
}
}
function displayBottomMap(viewer) {
//if in div with js viewers
var storyPosition = $("#fullscreen-view").offset().top;
var y = $(document).scrollTop();
if (y >= storyPosition) {
$("#clear-map").css({display: "block"});
$("#expand-map").css({display: "block"}); //display the expand btn
$(".fullscreen-item").css({height: "75%"});
$("#fullscreen-view").css("padding-bottom", "20%");
viewer.resize();
}
}
function saveCurrentPosition() {
lastNodechange = $(document).scrollTop();
}
function initViewerMapBlock(el, startNode) {
if (!startNode) {
var picId = el.find('img').attr('src').replace("https://d1cuyjsrcm0gby.cloudfront.net/", '');
picId = picId.replace("/thumb-2048.jpg", '');
el.find('img').remove();
} else {
picId = startNode;
}
var viewer = new Mapillary
.Viewer(el.attr('id'),
'TVBudDVxUkNVVU5BZFQ5QmpKZVlndzoxMjE3N2VmOTE2YzU4OTNj',
picId, {
cover: false,
renderMode: Mapillary.RenderMode.Fill,
baseImageSize: Mapillary.ImageSize.Size2048,
maxImageSize: Mapillary.ImageSize.Size2048,
sequence: {
minWidth: 200,
}
});
viewer.on('nodechanged', function (node) {
checkForImageDescription(el, picId);
displayBottomMap(viewer);
rightMap.resize();
var lnglat = [node.latLon.lon, node.latLon.lat]
var tempSource = new mapboxgl.GeoJSONSource({
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: lnglat
},
properties: {
title: 'You\'re here!',
'marker-symbol': 'marker'
}
}
})
rightMap.getSource('markers').setData(tempSource._data)
rightMap.flyTo({
center: lnglat,
zoom: 16,
speed: 1.7
})
var currDiv = el.attr('id');
document.getElementById(currDiv).scrollIntoView();
saveCurrentPosition();
});
}