-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
330 lines (277 loc) · 9.23 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
// ----------- MAPBOX SETUP CODE BELOW -----------
mapboxgl.accessToken =
"pk.eyJ1IjoiY29tbW9ua25vd2xlZGdlIiwiYSI6ImNqc3Z3NGZxcDA4NGo0OXA2dzd5eDJvc2YifQ.f68VZ1vlc6s3jg3JgShd0A";
// Create empty locations GeoJSON object
let mapLocations = {
type: "FeatureCollection",
features: [],
};
let selectedMapLocations = [];
let map = new mapboxgl.Map({
container: "map",
//style: "mapbox://styles/mapbox/light-v11",
style: "mapbox://styles/commonknowledge/clqfcx8dc00i301nw55apf380/draft",
center: [-3.205, 54.437],
zoom: 5.36,
});
let listLocations;
let numLocations = document.getElementById("location-list").childNodes.length;
// Adjust zoom of map for mobile and desktop
let mq = window.matchMedia("(min-width: 480px)");
map.setZoom(mq.matches ? 5.36 : 4.8);
// Add zoom and rotation controls to the map
map.addControl(new mapboxgl.NavigationControl());
// ----------- GEOCODING CODE BELOW -----------
const geocoder = new MapboxGeocoder({
accessToken: mapboxgl.accessToken,
mapboxgl: mapboxgl,
marker: true,
bbox: [-8.943386, 49.908511, 1.95694, 61.233906],
});
// Set hover popup
const popup = new mapboxgl.Popup({
closeButton: false,
closeOnClick: false,
});
geocoder.on("result", (event) => {
const searchResult = event.result.geometry;
const options = { units: "miles" };
for (const loc of mapLocations.features) {
loc.properties.distance = turf.distance(
searchResult,
loc.geometry,
options
);
}
mapLocations.features.sort(
(a, b) => a.properties.distance - b.properties.distance
);
function updateOrderList() {
mapLocations.features.forEach(function (location, i) {
mapLocations.features[i].properties.dynOrder = i;
});
$(".locations-map_item").each(function (i) {
let dynOrder = mapLocations.features[i].properties.dynOrder;
let arrayID = mapLocations.features[i].properties.arrayID;
$(".array-id-num").eq(arrayID).text(arrayID);
$(".dyn-order-num").eq(arrayID).text(dynOrder);
});
$("html").animate(
{
scrollTop: $(".locations-map_item").eq(0).offset().top,
},
800
);
}
updateOrderList();
getGeoData();
$(function () {
var theContainer = $("#location-list"),
theRows = $(".locations-map_item").get();
theRows = theRows.sort(function (a, b) {
var aLoc = $(".dyn-order-num", a).text(),
bLoc = $(".dyn-order-num", b).text();
return aLoc.localeCompare(bLoc, undefined, {
numeric: true,
sensitivity: "base",
});
});
theContainer.append(theRows);
});
});
map.addControl(geocoder, "top-left");
// Mutation observer
var targetNode = document.getElementById("location-list");
const config = { childList: true, subTree: false };
const callback = function (mutationsList, observer) {
if (
numLocations !== document.getElementById("location-list").childNodes.length
) {
populateMap();
}
numLocations = document.getElementById("location-list").childNodes.length;
};
const observer = new MutationObserver(callback);
observer.observe(targetNode, config);
// When map is loaded, initialize with data
map.on("load", function (e) {
populateMap();
});
async function populateMap() {
mapLocations = {
type: "FeatureCollection",
features: [],
};
selectedMapLocations = [];
listLocations = [];
// ----------- WEBFLOW CMS CODE BELOW -----------
// Get CMS items
listLocations = document.getElementById("location-list").childNodes;
await getGeoData(listLocations, mapLocations);
// On load, add this data to the relevant sections
$(".locations-map_item").each(function (i) {
let dynOrder = mapLocations.features[i].properties.dynOrder;
let arrayID = mapLocations.features[i].properties.arrayID;
$("#array-id-num").eq(arrayID).text(arrayID);
$("#dyn-order-num").eq(arrayID).text(dynOrder);
});
// ----------- ADDING POINTS CODE BELOW -----------
// When map is loaded, initialize with data
addMapPoints();
}
// For each collection item, grab hidden fields and convert to GeoJSON property
async function getGeoData() {
listLocations.forEach(async function (location, i) {
let locationLat = location.querySelector("#locationLatitude").value;
let locationLong = location.querySelector("#locationLongitude").value;
//let locationLong = location.querySelector("#locationLongitude").value;
let locationInfo = location.querySelector(".locations-map_card").innerHTML;
let siteType = location.querySelector(".sitetype").innerHTML;
let coordinates = [locationLong, locationLat];
let locationID = location.querySelector("#locationID").value;
// This is so every item has an initial ID
let arrayID = i;
// This is the dyn Order of the CMS items if geocoding is used
let dynOrder = i;
// Add to the array
let geoData = {
type: "Feature",
geometry: {
type: "Point",
coordinates: coordinates,
},
properties: {
id: locationID,
description: locationInfo,
arrayID: arrayID,
dynOrder: dynOrder,
siteType: siteType,
},
};
if (
!mapLocations.features.some(
(feature) => feature.properties.id === locationID
)
) {
mapLocations.features.push(geoData);
}
});
}
// Define mapping function to be invoked later
function addMapPoints() {
let nodeColors = [
"match",
["get", "siteType"] /* Name of options in cms
/* Name of options in cms, color */,
"Immigration Removal Centre",
"#2D19A9",
"Short Term Holding Facility",
"#57B598",
"Prison",
"#FFE27A",
/* other */ "#ccc",
];
/* Add the data to your map as a layer */
try {
$(".locations-map_list .locations-map_item").off("click");
map.off("click", "locations");
map.off("mouseenter", "locations");
map.off("mouseleave", "locations");
map.removeLayer("locations");
map.removeSource("locations");
} catch (e) {
console.log("Layer not found");
}
map.addLayer({
id: "locations",
type: "circle",
source: {
type: "geojson",
data: mapLocations,
},
paint: {
"circle-radius": 8,
"circle-stroke-width": 3,
"circle-stroke-opacity": 0.4,
"circle-opacity": 1,
"circle-stroke-color": "white",
"circle-color": nodeColors,
},
});
// ----------- INTERACTIVITY CODE BELOW -----------
$(".locations-map_list .locations-map_item").click(function () {
var index = $(this).index();
var arrayID = mapLocations.features[index].properties.arrayID;
let selectedCoords = mapLocations.features[index].geometry.coordinates;
let description = mapLocations.features[index].properties.description;
if ($(".location-map_card-wrap.is--selected").length) {
$(".location-map_card-wrap").removeClass("is--selected");
}
$(".location-map_card-wrap").eq(index).toggleClass("is--selected");
map.flyTo({
center: selectedCoords,
speed: 4,
curve: 1,
zoom: 14,
easing(t) {
return t;
},
});
const popup = new mapboxgl.Popup({ focusAfterOpen: false })
.setLngLat(selectedCoords)
.setHTML(description);
const popups = document.getElementsByClassName("mapboxgl-popup");
if (popups.length) {
popups[0].remove();
}
popup.addTo(map);
});
// When a click event occurs on a feature in the places layer, open a popup
map.on("click", "locations", (e) => {
var featureID = e.features[0].properties.id; // Use a unique identifier from CMS
var dynOrder = e.features[0].properties.dynOrder;
console.log("Feature ID: " + featureID);
console.log("New Order: " + dynOrder);
// Add your logic to find the corresponding CMS item using the unique identifier
// For example, find the index in the CMS list where the item has the matching ID
var indexInCMSList = findIndexInCMSListByID(featureID);
// Add logic to toggle class or perform other actions based on the CMS list index
// For example:
if (indexInCMSList !== -1) {
$(".location-map_card-wrap.is--selected").removeClass("is--selected");
$(".location-map_card-wrap")
.eq(indexInCMSList)
.toggleClass("is--selected");
}
$("html").animate(
{
scrollTop: $(".locations-map_item").eq(indexInCMSList).offset().top,
},
800
);
});
// Function to find the index in the CMS list based on the unique identifier
function findIndexInCMSListByID(featureID) {
// Replace this with the actual logic to find the index
// For example, if you have an array of CMS items with unique IDs:
for (var i = 0; i < mapLocations.features.length; i++) {
if (mapLocations.features[i].properties.id === featureID) {
return i;
}
}
return -1; // Return -1 if not found
}
map.on("mouseenter", "locations", (e) => {
map.getCanvas().style.cursor = "pointer";
const coordinates = e.features[0].geometry.coordinates.slice();
const description = e.features[0].properties.description;
while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360;
}
popup.setLngLat(coordinates).setHTML(description).addTo(map);
});
map.on("mouseleave", "locations", () => {
map.getCanvas().style.cursor = "";
popup.remove();
});
}