-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcards-prototype.js
313 lines (255 loc) · 12 KB
/
cards-prototype.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
document.addEventListener("DOMContentLoaded", function () {
CardListeners();
HeartIconListeners();
SettingsFlyoutListeners();
HelpFlyoutListeners();
window.cardListeners = [];
});
function CardListeners() {
/*When the page loads, get all cards and add the card open event listener to each one*/
let cards = document.getElementsByClassName("small-card");
addEventListenerList(cards, 'click', CardOpen);
}
function HeartIconListeners() {
let hearts = document.getElementsByClassName("heart-icon");
for (var i = 0; i < hearts.length; i++) {
hearts[i].addEventListener('click', function (e) {
e = e || window.event;
HeartIconClickHandler(e);
}, false);
}
}
function CardOpen() {
var occupationsColumn = this.closest(".occupations-columns").querySelector(".omaps-occupations--empty-column");
HandleClicks(occupationsColumn);
/*Add listeners back into any other cards that were closed above*/
let cards = document.getElementsByClassName("small-card");
addEventListenerList(cards, 'click', CardOpen);
/*Set omaps-occupations column (flex column) to expand*/
this.parentElement.parentElement.classList.toggle("omaps-occupations__wide");
/*Set padding on the occupations-text paragraph so that it doesn't take up the new larger width of the column*/
var occupationsContainer = this.closest(".occupations-text-container");
var occupationsText = occupationsContainer.getElementsByClassName("occupations-text")[0];
occupationsText.firstElementChild.classList.toggle("occupations-text__padding-right");
this.classList.toggle("large-card");
/*Remove listeners to open card (since it's now open)*/
this.removeEventListener("click", CardOpen);
this.removeEventListener("click", CardOpen);
this.removeEventListener("click", window.cardListeners[0]);
/*Add event listener to svg (close icon) for closing card*/
this.firstElementChild.addEventListener("click", CardClose);
}
function CardClose() {
/*Get current card*/
var smallCard = this.closest(".small-card");
/*Set padding on the occupations-text paragraph so that it doesn't take up the new larger width of the column*/
var openCardsParagraph = document.querySelectorAll(".occupations-text__padding-right");
for (i = 0; i < openCardsParagraph.length; i++) {
openCardsParagraph[i].classList.remove("occupations-text__padding-right");
}
var wideColumns = document.querySelectorAll(".omaps-occupations__wide");
for (i = 0; i < wideColumns.length; i++) {
wideColumns[i].classList.remove("omaps-occupations__wide");
}
/*show the last empty column*/
smallCard.closest(".occupations-columns").querySelector(".omaps-occupations--empty-column").classList.toggle("omaps-occupations--empty-column__hidden");
/*Set styles to shrink column*/
smallCard.classList.toggle("large-card");
smallCard.parentElement.parentElement.classList.toggle("omaps-occupations__narrow");
/*Remove any leftover open listener on card and set-up a new one so we can bind 'this'
- we also need to store the new bound instance of this listener function so we can later remove it on open*/
smallCard.removeEventListener("click", CardOpen);
smallCard.removeEventListener("click", CardOpen);
for (var l = 0; l < window.cardListeners.length; l++) {
smallCard.removeEventListener("click", window.cardListeners[l]);
}
window.cardListeners.splice(0);
//Remove leftover event listeners
var new_card = smallCard.cloneNode(true);
smallCard.parentNode.replaceChild(new_card, smallCard);
setTimeout(function () {
var temp;
new_card.addEventListener("click", temp = CardOpen.bind(new_card));
window.cardListeners.push(temp);
}.bind(new_card), 100);
}
function HandleClicks(occupationsColumn) {
/*When any card is opened, all other cards should close (only one open at a time)*/
var openCards = document.querySelectorAll(".large-card");
var openCardsParagraph = document.querySelectorAll(".occupations-text__padding-right");
var wideColumns = document.querySelectorAll(".omaps-occupations__wide");
for (i = 0; i < openCards.length; i++) {
openCards[i].classList.remove("large-card");
}
for (i = 0; i < openCardsParagraph.length; i++) {
openCardsParagraph[i].classList.remove("occupations-text__padding-right");
}
for (i = 0; i < wideColumns.length; i++) {
wideColumns[i].classList.remove("omaps-occupations__wide");
}
/*Hide the last empty column if it's not already hidden*/
if (!occupationsColumn.classList.contains("omaps-occupations--empty-column__hidden")) {
occupationsColumn.classList.toggle("omaps-occupations--empty-column__hidden");
}
}
function HeartIconClickHandler(e) {
// var testy = e.currentTarget.querySelector(".heart-icon--path");
e.currentTarget.querySelector(".heart-icon--path").classList.toggle("heart-icon__clicked");
}
function SettingsFlyoutListeners() {
/*Set pathways to show by default*/
var pathwaysCheckbox = document.querySelector("input[name=pathways-toggle]");
pathwaysCheckbox.checked = true;
var closeButton = document.getElementById("flyout-close-button");
var settingsButton = document.getElementById("settings-button");
var settingsTextLinks = document.getElementsByClassName("filters-text-link");
for (var i = 0; i < settingsTextLinks.length; i++) {
settingsTextLinks[i].addEventListener('click', function () {
toggleSettingsVisibility();
}, false);
}
closeButton.addEventListener('click', function () {
toggleSettingsVisibility();
});
settingsButton.addEventListener('click', function () {
toggleSettingsVisibility();
});
var descrptionsCheckbox = document.querySelector("input[name=descriptions-toggle]");
/*Check whether they should show or not on load*/
toggleDescriptions(descrptionsCheckbox.checked);
descrptionsCheckbox.addEventListener('change', function () {
toggleDescriptions(descrptionsCheckbox.checked);
});
togglePathways(pathwaysCheckbox.checked);
pathwaysCheckbox.addEventListener('change', function () {
togglePathways(pathwaysCheckbox.checked);
});
}
function toggleSettingsVisibility() {
document.getElementById("settings-flyout").classList.toggle("settings-flyout__not-visible");
}
function toggleHelpVisibility() {
document.getElementById("help-flyout").classList.toggle("settings-flyout__not-visible");
}
function HelpFlyoutListeners() {
var helpButton = document.getElementById("help-button");
helpButton.addEventListener('click', function () {
toggleHelpVisibility();
});
var mapKeysTextLinks = document.getElementsByClassName("map-keys-links");
for (var i = 0; i < mapKeysTextLinks.length; i++) {
mapKeysTextLinks[i].addEventListener('click', function () {
toggleHelpVisibility();
}, false);
}
var closeButton = document.getElementById("flyout-help-close-button");
closeButton.addEventListener('click', function () {
toggleHelpVisibility();
});
}
function toggleDescriptions(checked) {
var columnHeadings = document.getElementsByClassName("occupations-heading");
var occupationsText = document.getElementsByClassName("occupations-text");
var headerContainer = document.getElementById("omaps-header-container");
var pathwaysCheckbox = document.querySelector("input[name=pathways-toggle]");
handleIntroText(checked, pathwaysCheckbox.checked);
if (checked) {
for (var i = 0; i < columnHeadings.length; i++) {
columnHeadings[i].classList.remove("hide");
}
for (var i = 0; i < occupationsText.length; i++) {
occupationsText[i].classList.remove("hide");
}
headerContainer.classList.remove("hide");
} else {
for (var i = 0; i < columnHeadings.length; i++) {
columnHeadings[i].classList.add("hide");
}
for (var i = 0; i < occupationsText.length; i++) {
occupationsText[i].classList.add("hide");
}
headerContainer.classList.add("hide");
}
}
function togglePathways(checked) {
var pathwayHeadings = document.getElementsByClassName("pathway-heading");
var clusterHeadings = document.getElementsByClassName("cluster-name");
var headerClusterContainers = document.getElementsByClassName("header-cluster-container");
var descrptionsCheckbox = document.querySelector("input[name=descriptions-toggle]");
handleIntroText(descrptionsCheckbox.checked, checked);
if (checked) {
for (var i = 0; i < pathwayHeadings.length; i++) {
pathwayHeadings[i].classList.remove("hide");
}
for (var i = 0; i < clusterHeadings.length; i++) {
clusterHeadings[i].firstElementChild.classList.remove("hide");
clusterHeadings[i].classList.remove("cluster-name--hidden");
}
for (var i = 0; i < headerClusterContainers.length; i++) {
headerClusterContainers[i].classList.remove("header-cluster-container--pathways-hidden");
}
} else {
for (var i = 0; i < pathwayHeadings.length; i++) {
pathwayHeadings[i].classList.add("hide");
}
for (var i = 0; i < clusterHeadings.length; i++) {
clusterHeadings[i].firstElementChild.classList.add("hide");
clusterHeadings[i].classList.add("cluster-name--hidden");
}
for (var i = 0; i < headerClusterContainers.length; i++) {
headerClusterContainers[i].classList.add("header-cluster-container--pathways-hidden");
}
}
}
function handleIntroText(descriptionsChecked, pathwaysChecked) {
var introNoDescriptions = document.getElementById("intro-no-descriptions");
var introWithDescriptions = document.getElementById("intro-with-descriptions");
var introWithDescriptionsAndPathways = document.getElementById("intro-with-descriptions-and-pathways");
var introWithoutDescriptionsWithPathways = document.getElementById("intro-without-descriptions-with-pathways");
if (descriptionsChecked && !pathwaysChecked) {
/*Show with descriptions only*/
introNoDescriptions.classList.add("hide");
introWithDescriptionsAndPathways.classList.add("hide");
introWithoutDescriptionsWithPathways.classList.add("hide");
introWithDescriptions.classList.remove("hide");
} else if (!descriptionsChecked && !pathwaysChecked) {
/*Show no descriptions only*/
introNoDescriptions.classList.remove("hide");
introWithDescriptions.classList.add("hide");
introWithDescriptionsAndPathways.classList.add("hide");
introWithoutDescriptionsWithPathways.classList.add("hide");
}
else if (descriptionsChecked && pathwaysChecked) {
/*Show with descriptions and pathways*/
introWithDescriptionsAndPathways.classList.remove("hide");
introNoDescriptions.classList.add("hide");
introWithDescriptions.classList.add("hide");
introWithoutDescriptionsWithPathways.classList.add("hide");
}
else if (!descriptionsChecked && pathwaysChecked) {
/*Show without descriptions and with pathways*/
introWithDescriptionsAndPathways.classList.add("hide");
introNoDescriptions.classList.add("hide");
introWithDescriptions.classList.add("hide");
introWithoutDescriptionsWithPathways.classList.remove("hide");
}
}
/*Utilities START*/
function addEventListenerList(list, event, fn) {
for (var i = 0, len = list.length; i < len; i++) {
list[i].addEventListener(event, fn, false);
}
}
function getNextSibling(elem, selector) {
// Get the next sibling element
var sibling = elem.nextElementSibling;
// If there's no selector, return the first sibling
if (!selector) return sibling;
// If the sibling matches our selector, use it
// If not, jump to the next sibling and continue the loop
while (sibling) {
if (sibling.matches(selector)) return sibling;
sibling = sibling.nextElementSibling
}
};
/*Utilities END*/