-
Notifications
You must be signed in to change notification settings - Fork 25
/
storymap.js
172 lines (122 loc) · 5.19 KB
/
storymap.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
(function ($) {
'use strict';
$.fn.storymap = function(options) {
var defaults = {
selector: '[data-place]',
breakpointPos: '33.333%',
createMap: function () {
// create a map in the "map" div, set the view to a given place and zoom
var map = L.map('map').setView([65, 18], 5);
// add an OpenStreetMap tile layer
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '© <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
return map;
},
markerOptions: {
radius: 5,
color: '#de2d26'
},
clustering: true
};
var settings = $.extend(defaults, options);
if (typeof(L) === 'undefined') {
throw new Error('Storymap requires Laeaflet');
}
if (typeof(_) === 'undefined') {
throw new Error('Storymap requires underscore.js');
}
function getDistanceToTop(elem, top) {
var docViewTop = $(window).scrollTop();
var elemTop = $(elem).offset().top;
var dist = elemTop - docViewTop;
var d1 = top - dist;
if (d1 < 0) {
return $(document).height();
}
return d1;
}
function highlightTopPara(paragraphs, top) {
var distances = _.map(paragraphs, function (element) {
var dist = getDistanceToTop(element, top);
return {el: $(element), distance: dist};
});
var closest = _.min(distances, function (dist) {
return dist.distance;
});
_.each(paragraphs, function (element) {
var paragraph = $(element);
if (paragraph[0] !== closest.el[0]) {
paragraph.trigger('notviewing');
}
});
if (!closest.el.hasClass('viewing')) {
closest.el.trigger('viewing');
}
}
function watchHighlight(element, searchfor, top) {
var paragraphs = element.find(searchfor);
highlightTopPara(paragraphs, top);
$(window).scroll(function () {
highlightTopPara(paragraphs, top);
});
}
var makeStoryMap = function (element, markers, markerOptions) {
var topElem = $('<div class="breakpoint-current"></div>')
.css('top', settings.breakpointPos);
$('body').append(topElem);
var top = topElem.offset().top - $(window).scrollTop();
var searchfor = settings.selector;
var paragraphs = element.find(searchfor);
paragraphs.on('viewing', function () {
$(this).addClass('viewing');
});
paragraphs.on('notviewing', function () {
$(this).removeClass('viewing');
});
watchHighlight(element, searchfor, top);
var map = settings.createMap();
var initPoint = map.getCenter();
var initZoom = map.getZoom();
var fg = L.featureGroup().addTo(map);
var clusteredMarkers = L.markerClusterGroup({
showCoverageOnHover: false,
maxClusterRadius: 50
});
function showMapView(key) {
fg.clearLayers();
clusteredMarkers.clearLayers();
if (key === 'overview') {
map.setView(initPoint, initZoom, true);
} else if (markers[key]) {
var marker = markers[key];
var center = marker.center;
var photos = marker.photos;
var layer = marker.layer;
if(typeof layer !== 'undefined'){
fg.addLayer(layer);
};
// change to circleMarker so marker is more easily customized
//fg.addLayer(L.circleMarker([center.lat, center.lon], markerOptions));
// add photos
for (var i = 0; i < photos.length; i++) {
if (settings.clustering == true) {
clusteredMarkers.addLayer(L.circleMarker([photos[i].lat, photos[i].lon], markerOptions)
.bindTooltip("<img class='tooltip-image' src='"+photos[i].url+"'>"));
fg.addLayer(clusteredMarkers);
} else {
fg.addLayer(L.circleMarker([photos[i].lat, photos[i].lon], markerOptions)
.bindTooltip("<img class='tooltip-image' src='"+photos[i].url+"'>"));
}
}
map.setView([center.lat, center.lon], center.zoom, 1);
}
}
paragraphs.on('viewing', function () {
showMapView($(this).data('place'));
});
};
makeStoryMap(this, settings.markers, settings.markerOptions);
return this;
}
}(jQuery));