-
Notifications
You must be signed in to change notification settings - Fork 0
/
scroll.js
290 lines (240 loc) · 7.62 KB
/
scroll.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
mapboxgl.accessToken = 'pk.eyJ1IjoidHVuZHJhdGhlcmFpbXkiLCJhIjoiY2wxdzV6czg2MW02azNpcXNjOXQxYjh5cSJ9.B-hTJxiaSAX1AlG6_a7F2g';
const map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/tundratheraimy/cl2omakm2000f15qmv4jhdcz3',
center: [77.0688997, 28.5272803],
zoom: 3,
bearing: 0,
pitch: 0
});
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
map.on('load', () =>
{
// I want to show the slider, legend from the start
map.setLayoutProperty('lines-female','visibility', 'visible');
map.setLayoutProperty('lines-male', 'visibility', 'visible');
// don't show the console and slider unless the page is scrolled to part_3
document.getElementById('console').style.display = 'none';
////////////////////////////////////////////////
// Legend
Legend()
////////////////////////////////////////////////
// I want the user to be able to toggle between genders
// The gender buttons will ideally have the same colours as their legend
// show menu buttons
Menu()
}
);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Function to fill the map legend
function Legend()
{
var legendElements = ['Female', 'Male'];
var legendColors = ['#34c53c', '#2b1ba1'];
for (var i = 0; i < 2; i++)
{
var legendElement = legendElements[i];
var elementColor = legendColors[i];
var item = document.createElement('div');
var key = document.createElement('span');
key.className = 'legend-key';
key.style.backgroundColor = elementColor;
var value = document.createElement('span');
value.innerHTML = legendElement;
item.appendChild(key);
item.appendChild(value);
legend.appendChild(item);
}
}
// Function to populate the menu to toggle between filters to view male and female paths
function Menu()
{
var keys = ['lines-female', 'lines-male'];
var buttonLabels = ['Hide Paths of Females', 'Hide Paths of Males'];
for (var i = 0; i < 2; i++)
{
var id = keys[i];
var buttonLabel = buttonLabels[i];
var link = document.createElement('a');
// link properties
link.href = '#';
link.className = ''; // means it is initially inactive
link.textContent = buttonLabel;
link.id = id;
// create an event handler for each menu item
// if clicked, check the visibility of the layer
// if visibility is 'none' set it to 'visible' and vice versa
link.onclick = function(e)
{
var clickedLayer = this.id;
e.preventDefault();
e.stopPropagation();
gender = this.textContent.split(" ")[3];
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility == 'visible')
{
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
this.textContent = 'Show Paths of '.concat(gender) ;
}
else
{
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
this.textContent = 'Hide Paths of '.concat(gender);
}
};
var layers = document.getElementById('menu');
layers.appendChild(link);
}
}
// Function that effects what the slider does and console shows.
function Slider()
{
var labels = ['Pre-Partition (before 1947-08-14)','Partition (1947-08-15 - 1948-02-28)','Post-Partition (1948-03-01 - 1971-12-16)','Long Partition (after 1971-12-16)','Indeterminable', 'Temporality not coded'];
// showing the Pre-Partition condition by default
if (document.getElementById('active-temporality').innerText == '')
{
document.getElementById('active-temporality').innerText = 'Please move the slider.'
}
// creating event listener to catch slider movement
document.getElementById('slider').addEventListener('input', function(e)
{
// where is the slider now?
var step = parseInt(e.target.value, 10) // in decimal
activeLayers = visibleLayers();
console.log(activeLayers);
for (var i = 0; i < activeLayers.length; i++)
{
ID = activeLayers[i];
gender = ID.split("-")[1];
gender = gender[0].toUpperCase() + gender.substring(1).toLowerCase();
console.log(ID)
console.log(gender)
const filterTemporality = ['==', ['number', ['get', 'temporal_sequence']], step];
const filterGender = ['==', ['string', ['get', 'gender']], gender];
map.setFilter(ID, ['all', filterGender, filterTemporality]);
document.getElementById('active-temporality').innerText = labels[step - 1];
}
})
}
// naive approach, but it's guaranteed to work.
function visibleLayers()
{
var visibleLayerIds = [];
// case 1: both are visible
if (map.getLayoutProperty('lines-female', 'visibility') == 'visible' && map.getLayoutProperty('lines-male', 'visibility') == 'visible')
{
visibleLayerIds = ['lines-female', 'lines-male'];
}
// case 2: only female layer is visible
else if (map.getLayoutProperty('lines-female', 'visibility') == 'visible')
{
visibleLayerIds = ['lines-female'];
}
// case 3: only male layer is visible
else
{
visibleLayerIds = ['lines-male'];
}
return visibleLayerIds;
}
// Function to set the active chapter to active
function setActiveChapter(chapterName)
{
if (chapterName === activeChapterName) return;
map.flyTo(chapters[chapterName]);
document.getElementById(chapterName).classList.add('active');
document.getElementById(activeChapterName).classList.remove('active');
activeChapterName = chapterName;
}
// Function that tells you if the element of interest is within the bounds of the screen
function isElementOnScreen(id)
{
const element = document.getElementById(id);
const bounds = element.getBoundingClientRect();
return bounds.top < window.innerHeight && bounds.bottom > 0;
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// defining properties of chapter elements
const chapters = {
'part_1': {
bearing:0,
center: [75.0688997, 28.5272803],
zoom: 3,
pitch: 0
},
'part_2': {
duration: 7000,
center: [74.8723, 31.6340],
bearing: 0,
zoom: 8,
pitch: 0
},
'part_3': {
bearing:0,
center: [77.0688997, 28.5272803],
zoom: 3,
pitch: 0
// bearing: 45,
// center: [74.3383, 31.5767],
// zoom: 13,
// speed: 1,
// pitch: 50
},
'part_4': {
bearing: 45,
center: [0.6004, 52.2891],
zoom: 6,
pitch: 50,
speed: .2
},
'part_5': {
bearing:0,
center: [77.0688997, 28.5272803],
zoom: 3,
pitch: 0
// bearing: 180,
// center: [77.1835, 28.6239],
// zoom: 11,
// pitch: 10,
// speed: 1,
// curve:1
}
//,
// 'part_6': {
// bearing:0,
// center: [77.0688997, 28.5272803],
// zoom: 3,
// pitch: 0
// // bearing: 25,
// // center: [77.0688997, 28.5272803],
// // zoom: 7
// }
};
let activeChapterName = 'part_1';
// On every scroll event, check which element is on screen
window.onscroll = () => {
for (const chapterName in chapters)
{
if (isElementOnScreen(chapterName))
{
// we want the slider and console to show up only from chapter 3 onwards
if (chapterName != 'part_1' && chapterName != 'part_2')
{
document.getElementById('console').style.display = 'inline';
document.getElementById('slider').style.display = 'inline';
// activate slider functionality
Slider();
}
// in chapters 1, 2, we hide the slider and the console.
else
{
document.getElementById('console').style.display = 'none';
document.getElementById('slider').style.display = 'none';
}
setActiveChapter(chapterName);
break;
}
}
};