forked from antiboredom/walkin-tour
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main-v3.js
185 lines (156 loc) · 5.53 KB
/
main-v3.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
var tour = $('#tour-title').text();
var filename = filename(tour);
function get_hash_int() {
return parseInt(window.location.hash.substring(1)) || 0;
}
$(document).ready(function () {
var data;
var tourMap = initializeMap();
loadFile();
function loadFile() {
d3.csv(filename, function (data) {
var tourData = loadTour(data);
populateMap(tourMap, tourData);
var initialLocation = L.latLng(tourData[0].lat, tourData[0].lng);
tourMap.setView(initialLocation);
go(window.location.hash.substring(1));
$(window).on('hashchange', function () {
var locationNumber = parseInt(window.location.hash.substring(1));
var currentLocation = L.latLng(tourData[locationNumber].lat, tourData[locationNumber].lng);
tourMap.setView(currentLocation);
$('.hereIcon').hide();
setHereMarker(tourMap, tourData);
go(window.location.hash.substring(1));
});
});
}
$('#forward').on('click', function (e) {
nextPlace();
});
$('#backward').on('click', function (e) {
prevPlace();
});
$('#read-more').on('click', function (e) {
expand();
});
function prevPlace() {
var prev = get_hash_int() - 1;
if (prev < 0) {
prev = tourData.length - 1;
}
go(prev);
}
function nextPlace() {
var next = get_hash_int() + 1;
if (next > tourData.length - 1) {
next = 0;
}
go(next);
}
});
function initializeMap() {
var ctrLat = 40.768243;
var ctrLng = -73.975664;
var tourMap = L.map('tour-map').setView([ctrLat, ctrLng], 13);
tourMap.scrollWheelZoom.disable();
L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(tourMap);
return tourMap;
}
function populateMap(tourMap, tourData) {
var redCircleIcon = L.icon({
iconUrl: '../images/capital-red-circle-map.png',
iconSize: [18, 18],
className: 'locationIcon'
});
tourData.map(function (d) {
if (d['Streetview Embed Code']) {
var lat = parseFloat(d['Streetview Embed Code'].match(/1d(.*?)!/)[1]);
var lng = parseFloat(d['Streetview Embed Code'].match(/2d(.*?)!/)[1]);
if (lat && lng) {
var marker = L.marker([lat, lng], {icon: redCircleIcon}).addTo(tourMap);
marker.on('click', function () {
go(d['Index'])
});
}
}
});
setHereMarker(tourMap, tourData);
}
function setHereMarker(tourMap, tourData) {
var locationNumber = get_hash_int();
var currentLocation = L.latLng(tourData[locationNumber].lat, tourData[locationNumber].lng);
var hereIcon = L.icon({
iconUrl: '../images/capital-walker.png',
iconSize: [30, 30],
className: 'hereIcon'
});
L.marker(currentLocation, {icon: hereIcon, zIndexOffset: 1000}).addTo(tourMap);
}
function loadTour(data) {
var tourIndex = 0;
tourData = data.map(function (d) {
d.embed = d['Streetview Embed Code'].match(/src="(.*?)"/)[1];
d.lat = d['Streetview Embed Code'].match(/1d(.*?)!/)[1];
d.lng = d['Streetview Embed Code'].match(/2d(.*?)!/)[1];
d['Context'] = d['Context'].replace('\n', '<br>');
d['Preview'] = d['Preview'].replace('\n', '<br>');
d['Quote'] = d['Quote'].replace('\n', '<br>');
d['Source'] = d['Source'].replace('\n', '<br>');
d['Index'] = tourIndex;
tourIndex++;
return d;
});
return tourData;
}
function go(i) {
if (typeof i != 'number') i = +i;
index = i;
var link = "http://capital-newyork.com/" + fileize(tourData[index]['Tour'].replace(/[^a-zA-Z0-9]/g, ' ')) + "/tour.html#" + i;
var encoded = "http%3A%2F%2Fcapital-newyork.com%2F" + fileize(tourData[index]['Tour'].replace(/[^a-zA-Z0-9]/g, ' ')) + "%2Ftour.html%23" + i;
var location = tourData[index]['Location'] + " via";
var source = tourData[index]['Source'];
var preview = tourData[index]['Preview'];
$("#map").attr('src', tourData[index].embed);
$("#quote").html(tourData[index]['Quote']);
$("#preview").html(tourData[index]['Preview'] + '...');
$("#context").html(tourData[index]['Context']);
$("#source").html(tourData[index]['Source']);
$("#location").html(tourData[index]['Location']);
$("#location-link").attr("href", link);
$("#tweet").attr("href", "https://twitter.com/intent/tweet?text="+ location + "&url=" + encoded);
$("#tumblr").attr("href",
"https://www.tumblr.com/widgets/share/tool?shareSource=legacy&canonicalUrl=" +
"&url=" + encoded + "&posttype=quote" +
"&caption=" + encodeURIComponent("<a href='" + link + "'>" + source + "</a>") +
"&content=" + encodeURI(preview));
$("#facebook").attr("href", "https://www.facebook.com/sharer/sharer.php?u=" + encoded);
$('meta[property="og:title"]').attr('content', tourData[index]['Location']);
$('meta[property="og:url"]').attr('content', link);
$("#quote").hide();
$("#preview").show();
$("#read-more").html("Read more");
($("#quote").html() == '') ? $("#read-more").hide() : $("#read-more").show();
window.location.hash = '#' + index;
}
function expand() {
if ($("#quote").text() != "") {
$("#read-more").show();
if ($("#read-more").html() == "Read more") {
$("#preview").hide();
$("#quote").show();
$("#read-more").html("Read less");
} else {
$("#quote").hide();
$("#preview").show();
$("#read-more").html("Read more");
}
}
}
function fileize(title) {
return title.replace(/\s+/g, '-').toLowerCase();
}
function filename(title) {
return "../data/" + fileize(title) + ".csv";
}