-
Notifications
You must be signed in to change notification settings - Fork 2
/
page.js
204 lines (181 loc) · 6.41 KB
/
page.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
var navIcons = document.getElementsByClassName("navIcons");
for (var i = 0; i < navIcons.length; i++) {
navIcons[i].addEventListener("click", function () {
openTab(this);
});
}
function removeActive(elements, activeName) {
// toggle elements active class
for (var i = 0; i < elements.length; i++) {
var elementClasses = elements[i].classList;
for (var x = 0; x < elementClasses.length; x++) {
if (elementClasses[x] == activeName) {
elementClasses.remove(activeName);
break;
}
}
}
}
function openTab(element) {
// toggle navIcons
var navIcons = document.getElementsByClassName("navIcons");
removeActive(navIcons, "active-navIcon");
element.classList.add("active-navIcon");
// toggle tabDivs
var tabDivs = document.getElementsByClassName("tabContent");
removeActive(tabDivs, "active-tabContent");
var contentID = element.id + "Content";
document.getElementById(contentID).classList.add("active-tabContent");
// reset map view
resetMapView();
}
function filterSubSector(subSectorValue, action) {
// update filter based on request -> set filter -> built location list -> add markers
currentFilter = map.getFilter("locations");
// update filter
if (action == "ADD") {
// if there is no filter -> set one
if (currentFilter == null) {
var filter = [
"any",
["in", subSectorValue, ["get", "subsectorsJoined"]],
];
} else {
currentFilter.push(["in", subSectorValue, ["get", "subsectorsJoined"]]);
var filter = currentFilter;
}
} else if(action == "REMOVE") {
// go thru filters, skip fist value 'any' -> remove requested subsector from filter
for (x = 1; currentFilter.length; x++) {
var filterValue = currentFilter[x][1];
if (filterValue == subSectorValue) {
currentFilter.splice(x, 1);
if (currentFilter.length == 1) {
currentFilter = null;
}
var filter = currentFilter;
break;
}
}
}
// set filter
map.setFilter("locations", filter);
// build location list
if (map.getFilter("locations") == null) {
// if there is no filter -> get all features
var features = map.getSource("locations")._data.features;
} else {
// go thru each feature -> if any conditions are true return feature to list
var subSectorFilter = filter.slice(1).map((element) => element[1]);
var features = map
.getSource("locations")
._data.features.filter((x) =>
subSectorFilter.some((y) => x.properties.subsectorsJoined.includes(y))
);
}
buildLocationList(features, map);
// add makers
addMarkers(features);
}
function toggleSubSector(element) {
var action = "REMOVE";
for (var x = 0; x < element.classList.length; x++) {
// if class is btn--stroke -> set action to add and remove class
if (element.classList[x] == "btn--stroke") {
action = "ADD";
element.classList.remove("btn--stroke");
break;
}
}
// if action still is REMOVE -> add the btn--stroke class
if (action == "REMOVE") {
element.classList.add("btn--stroke");
}
filterSubSector(element.innerText, action);
}
function primarySectorChange() {
// reset map view
resetMapView();
map.setFilter('locations', null)
addMarkers(points.features)
// get selection
selectedSector = document.getElementById("PrimarySector").value;
// clear div
subSectorDiv = document.getElementById("subSectorsDiv");
subSectorDiv.innerHTML = "";
// if All is selected ->
if (selectedSector == "All") {
} else {
// if All is not selected
// build heading
subSectorDiv.innerHTML = "<br>";
var subSectorHeading = document.createElement("div");
subSectorHeading.classList.add("txt-bold");
subSectorHeading.innerText = "Sub-Sectors:";
subSectorDiv.appendChild(subSectorHeading);
// generate subsectors
subSectorList = sectors[selectedSector]["sub_sectors"];
for (subSector in subSectorList) {
var sectorButton = document.createElement("button");
sectorButton.classList.add("btn", "btn--stroke", "btn--green", "mx3", "my3");
sectorButton.textContent = subSectorList[subSector];
sectorButton.addEventListener("click", function () {
toggleSubSector(this);
});
subSectorDiv.appendChild(sectorButton);
}
}
}
function buildLocationList(data, map) {
// clear previous list
var listings = document.getElementById("listings");
listings.innerHTML = "";
data.forEach(function (point, i) {
var prop = point.properties;
// Add a new listing section to the sidebar
var listing = listings.appendChild(document.createElement("div"));
listing.id = "listing-" + prop.id;
listing.className = "item";
// Add the link to the individual listing created above
var link = listing.appendChild(document.createElement("a"));
link.href = "#";
link.className = "title";
link.id = "link-" + prop.id;
link.innerHTML = prop.organizationName;
// Add details to the individual listing
var details = listing.appendChild(document.createElement("div"));
details.classList.add("txt-s","txt-uppercase");
details.innerText = prop.organizationStreetAddress + ', ' + prop.organizationCitytown;
// Listen to the element and when it is clicked, do four things:
// 1. Update the `currentFeature` to the point associated with the clicked link
// 2. Fly to the point
// 3. Close all other popups and display popup for clicked point
// 4. Highlight listing in sidebar (and remove highlight for all other listings)
link.addEventListener("click", function (e) {
for (var i = 0; i < data.length; i++) {
if (this.id === "link-" + data[i].properties.id) {
var clickedListing = data[i];
flyTopoint(clickedListing.geometry.coordinates, 15);
createPopUp(clickedListing);
}
}
var activeItem = document.getElementsByClassName("active");
if (activeItem[0]) {
activeItem[0].classList.toggle("active");
}
this.parentNode.classList.add("active");
});
});
}
function refreshSelection(){
map.setFilter('locations',null);
addMarkers(points.features);
var subSectorDiv = document.getElementById('subSectorsDiv');
var subSectorBtns = subSectorDiv.getElementsByClassName('btn');
for (var i=0; i<subSectorBtns.length; i++){
if (!('btn--stroke' in subSectorBtns[i].classList)){
subSectorBtns[i].classList.add('btn--stroke');
}
}
resetMapView();
}