/
script.js
202 lines (163 loc) · 7.55 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
var imageContainerMargin = 70; // Margin + padding
var currentBox = 0;
var scrollPosition = 0;
// This watches for the scrollable container
$('div#contents').scroll(function() {
scrollPosition = $(this).scrollTop();
});
// GeoJSON fill color = 1, 2... default
function getFillColor(d) {
return d == 1 ? 'red' :
d == 2 ? 'blue' :
d == 3 ? 'green' :
d == 4 ? 'white' :
'white';
}
function style(feature) {
return {
color: 'blue',
fillColor: getFillColor(feature.properties.fill),
weight: 2,
fillOpacity: 0.2
};
}
// This adds data as a new layer to the map
function refreshLayer(data, map, coord, zoom) {
var dataLayer = L.geoJson(data, {
style: style,
//onEachFeature: onEachFeature // future option to show town name on click or hover
});
dataLayer.addTo(map);
map.setView([coord[1], coord[0]], zoom);
}
function initMap() {
// Although all coordinates and zoom are pulled from map.geojson, insert initial data here for smooth startup
var map = L.map('map', {
center: [41.79, -72.717],
zoom: 10,
scrollWheelZoom: false
});
// This customizes link to view source code; add your own GitHub repository
map.attributionControl
.setPrefix('View <a href="http://github.com/ontheline/otl-town-borders" target="_blank">code on GitHub</a> with <a href="http://leafletjs.com" title="A JS library for interactive maps">Leaflet</a>');
// Legend control layers - global variable with (null, null) allows indiv layers to be added inside functions below
// See style.css code that intentionally hides legend control in this version
var controlLayers = L.control.layers( null, null, {
position: "bottomright",
collapsed: false // false = open by default
}).addTo(map);
// tileLayer.WMS as a baselayer - see http://leafletjs.com/reference.html#tilelayer-wms
// UConn MAGIC WMS settings - see http://geoserver.lib.uconn.edu:8080/geoserver/web/?wicket:bookmarkablePage=:org.geoserver.web.demo.MapPreviewPage
var map1625 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:Connecticut_Griswold_1625',
attribution: 'c1625 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1625, '1625 map');
var map1766 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:Connecticut_Park_1766',
attribution: '1766 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1766, '1766 map');
var map1780 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:Covens_1780',
attribution: '1780 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1780, '1780 map');
var map1795 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:Connecticut_Doolittle_1795',
attribution: '1795 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1795, '1795 map');
var map1811 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:1811_Warren',
attribution: '1811 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1811, '1811 map');
var map1855 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:HartfordCounty_Woodford_1855',
attribution: '1855 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1855, '1855 map');
var map1893 = new L.tileLayer.wms("http://geoserver.lib.uconn.edu:8080/geoserver/MAGIC/wms?", {
layers: 'MAGIC:Hurd_1893_page_12_13',
attribution: '1893 <a href="http://magic.library.uconn.edu">MAGIC UConn</a>'
});
controlLayers.addBaseLayer(map1893, '1893 map');
// This displays the default tile layer
var lightAll = new L.tileLayer('https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}{r}.png', {
attribution: '© <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, © <a href="http://cartodb.com/attributions">CartoDB</a>'
}).addTo(map);
controlLayers.addBaseLayer(lightAll, 'present');
// This loads the GeoJSON map data file from a local folder
$.getJSON('map.geojson', function(data) {
var geojson = L.geoJson(data, {
onEachFeature: function (feature, layer) {
(function(layer, properties) {
// This creates the contents of each chapter from the GeoJSON data. Unwanted items can be removed, and new ones can be added
var chapter = $('<p></p>', {
text: feature.properties['chapter'],
class: 'chapter-header'
});
var image = $('<img>', {
src: feature.properties['image'],
});
var source = $('<a>', {
text: feature.properties['source-credit'],
href: feature.properties['source-link'],
target: "_blank",
class: 'source'
});
var description = $('<p></p>', {
text: feature.properties['description'],
class: 'description'
});
var container = $('<div></div>', {
id: 'container' + feature.properties['id'],
class: 'image-container'
});
var imgHolder = $('<div></div', {
class: 'img-holder'
});
imgHolder.append(image);
container.append(chapter).append(description).append(imgHolder).append(source);
$('#contents').append(container);
var i;
var areaTop = -100;
var areaBottom = 0;
// Calculating total height of blocks above active
for (i = 1; i < feature.properties['id']; i++) {
areaTop += $('div#container' + i).height() + imageContainerMargin;
}
areaBottom = areaTop + $('div#container' + feature.properties['id']).height();
$('div#contents').scroll(function() {
if ($(this).scrollTop() >= areaTop && $(this).scrollTop() < areaBottom) {
if (feature.properties['id'] != currentBox) {
currentBox = feature.properties['id'];
$('.image-container').removeClass("inFocus").addClass("outFocus");
$('div#container' + feature.properties['id']).addClass("inFocus").removeClass("outFocus");
// This removes all layers except the default base layer; change name if necessary
map.eachLayer(function (layer) {
if (layer != lightAll) {
map.removeLayer(layer);
}
});
// This adds another data layer
$.getJSON(feature.properties['layer'], function(data) {
var coord = feature.geometry['coordinates'];
var zoom = feature.properties['zoom'];
refreshLayer(data, map, coord, zoom);
});
// This loads the tile layer from map.geojson by imitating a click to the legend control span
var tile = feature.properties['tile'];
$($('span:contains(" ' + tile + '")').get(0)).siblings("input").click();
}
}
});
})(layer, feature.properties);
}
});
$('#contents').append("<div class='space-at-the-bottom'><a href='#space-at-the-top'><i class='fa fa-chevron-up'></i></br><small>Top</small></a></div>");
});
}
initMap();
$("div#contents").animate({ scrollTop: 5 });