-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscript.js
264 lines (207 loc) · 6.93 KB
/
script.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
var data = {};
var groups = {};
var map;
/*
* Given a string `str`, replaces whitespaces with dashes,
* and removes nonalphanumeric characters. Used in URL hash.
*/
var slugify = function(str) {
return str.replace(/[^\w ]+/g,'').replace(/ +/g,'-');
}
/*
* Resets map view to originally defined `mapCenter` and `mapZoom` in settings.js
*/
var resetView = function() {
map.flyTo( mapCenter, mapZoom );
resetSidebar();
}
/*
* Resets sidebar, clearing out place info and leaving title+footer only
*/
var resetSidebar = function() {
// Make the map title original color
$('header').removeClass('black-50');
// Clear placeInfo containers
$('#placeInfo').addClass('dn');
$('#placeInfo h2, #placeInfo h3').html('');
$('#placeInfo div').html('');
$('#googleMaps').addClass('dn').removeClass('dt');
// Reset hash
location.hash = '';
}
/*
* Given a `marker` with data bound to it, update text and images in sidebar
*/
var updateSidebar = function(marker) {
// Get data bound to the marker
var d = marker.options.placeInfo;
if (L.DomUtil.hasClass(marker._icon, 'markerActive')) {
// Deselect current icon
L.DomUtil.removeClass(marker._icon, 'markerActive');
resetSidebar();
} else {
location.hash = d.slug;
// Dim map's title
$('header').addClass('black-50');
$('#placeInfo').removeClass('dn');
// Clear out active markers from all markers
$('.markerActive').removeClass('markerActive');
// Make clicked marker the new active marker
L.DomUtil.addClass(marker._icon, 'markerActive');
// Populate place information into the sidebar
$('#placeInfo').animate({opacity: 0.5}, 300).promise().done(function() {
$('#placeInfo h2').html(d.Name);
$('#placeInfo h3').html(d.Subtitle);
$('#description').html(d.Description);
if (d.GoogleMapsLink) {
$('#googleMaps').removeClass('dn').addClass('dt').attr('href', d.GoogleMapsLink);
} else {
$('#googleMaps').addClass('dn').removeClass('dt');
}
$('#gallery').html('');
$('#galleryIcon').hide();
// Load up to 5 images
for (var i = 1; i <= 5; i++) {
var idx = 'Image' + i;
if (d[idx]) {
var source = "<em class='normal'>" + d[idx + 'Source'] + '</em>';
if (source && d[idx + 'SourceLink']) {
source = "<a href='" + d[idx + 'SourceLink'] + "' target='_blank'>" + source + "</a>";
}
var a = $('<a/>', {
href: d[idx],
'data-lightbox': 'gallery',
'data-title': ( d[idx + 'Caption'] + ' ' + source ) || '',
'data-alt': d.Name,
'class': i === 1 ? '' : 'dn'
});
var img = $('<img/>', { src: d[idx], alt: d.Name, class: 'dim br1' });
$('#gallery').append( a.append(img) );
if (i === 1) {
$('#gallery').append(
$('<p/>', { class: 'f6 black-50 mt1', html: d[idx + 'Caption'] + ' ' + source })
);
}
if (i === 2) {
$('#gallery > a:first-child').append('<span class="material-icons arrow arrow-right white-90">navigate_next</span>')
$('#gallery > a:first-child').append('<span class="material-icons arrow arrow-left white-90">navigate_before</span>')
}
} else {
break;
}
}
$('#placeInfo').animate({ opacity: 1 }, 300);
// Scroll sidebar to focus on the place's title
$('#sidebar').animate({
scrollTop: $('header').height() + 20
}, 800);
})
}
}
/*
* Main function that generates Leaflet markers from read CSV data
*/
var addMarkers = function(data) {
var activeMarker;
var hashName = decodeURIComponent( location.hash.substr(1) );
for (var i in data) {
var d = data[i];
// Create a slug for URL hash, and add to marker data
d['slug'] = slugify(d.Name);
// Add an empty group if doesn't yet exist
if (!groups[d.Group]) { groups[d.Group] = []; }
// Create a new place marker
var m = L.marker(
[d.Latitude, d.Longitude],
{
icon: L.icon({
iconUrl: d.Icon,
iconSize: [ iconWidth, iconHeight ],
iconAnchor: [ iconWidth/2, iconHeight/2 ], // middle of icon represents point center
className: 'br1',
}),
// Pass place data
placeInfo: d
},
).on('click', function(e) {
map.flyTo(this._latlng, 11);
updateSidebar(this);
});
// Add this new place marker to an appropriate group
groups[d.Group].push(m);
if (d.slug === hashName) { activeMarker = m; }
}
// Transform each array of markers into layerGroup
for (var g in groups) {
groups[g] = L.layerGroup(groups[g]);
// By default, show all markers
groups[g].addTo(map);
}
L.control.layers({}, groups, {collapsed: false}).addTo(map);
$('.leaflet-control-layers-overlays').prepend('<h3 class="mt0 mb1 f5 black-30">Themes</h3>');
// If name in hash, activate it
if (activeMarker) { activeMarker.fire('click') }
}
/*
* Loads and parses data from a CSV (either local, or published
* from Google Sheets) using PapaParse
*/
var loadData = function(loc) {
Papa.parse(loc, {
header: true,
download: true,
complete: function(results) {
addMarkers(results.data);
}
});
}
/*
* Add home button
*/
var addHomeButton = function() {
var homeControl = L.Control.extend({
options: {
position: 'bottomright'
},
onAdd: function(map) {
var container = L.DomUtil.create('span');
container.className = 'db material-icons home-button black-80';
container.innerText = 'map';
container.onclick = function() {
resetView();
}
return container;
}
})
map.addControl(new homeControl);
}
/*
* Main function to initialize the map, add baselayer, and add markers
*/
var initMap = function() {
map = L.map('map', {
center: mapCenter,
zoom: mapZoom,
tap: false, // to avoid issues in Safari, disable tap
zoomControl: false,
});
// Add zoom control to the bottom-right corner
L.control.zoom({ position: 'bottomright' }).addTo(map);
L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors © <a href="https://carto.com/attributions">CARTO</a>',
subdomains: 'abcd',
maxZoom: 19
}).addTo(map);
loadData(dataLocation);
// Add data & GitHub links
map.attributionControl.setPrefix('Download <a href="'
+ dataLocation + '" target="_blank">data</a> or \
view <a href="http://github.com/handsondataviz/leaflet-point-map-sidebar" target="_blank">code on\
GitHub</a> | created with <a href="http://leafletjs.com" title="A JS library\
for interactive maps">Leaflet</a>');
// Add custom `home` control
addHomeButton();
$('#closeButton').on('click', resetView);
}
// When DOM is loaded, initialize the map
$('document').ready(initMap);