-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminimal.js
396 lines (366 loc) · 18.2 KB
/
minimal.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
//simple helped functions for calculating maximums
function getMax(arr, prop) {
var max;
for (var i = 0; i < arr.length; i++) {
if (!max || parseInt(arr[i][prop]) > parseInt(max[prop]))
max = arr[i];
}
return max;
};
//creates the color legend html object and the stylization for the maps. it relies on colors specified in the variable definition code, not on the colors that are specified in the color scheme functions above.
//that means that when the scheme needs to change, it needs to be updated in both places
function createLegend(labels, colors, div_legend) {
for (i = 0; i < labels.length; i++) {
var label = labels[i];
var color = colors[i];
var item = document.createElement('div');
var key = document.createElement('span');
key.className = 'legend-key';
key.style.backgroundColor = color;
var value = document.createElement('span');
value.innerHTML = label;
item.appendChild(key);
item.appendChild(value);
div_legend.append(item);
}
}
//creates the html object for the date slider and calls the function that gives that slider functionality. Could be updated to create sliders for other variables fairly simply.
function createSlider(location, slide_val) {
var input_div = document.createElement('div');
var h3 = document.createElement("h3");
var label = document.createElement('label');
var input = document.createElement('input');
input_div.className = 'map-overlay-inner';
h3.innerHTML = "FCC data range";
label.id = "time_chunks";
input.id = "slider";
input.type = "range";
input.min = 0;
input.max = date_ids.length - 1;
input.step = 1;
input.value = slide_val;
input_div.appendChild(h3);
input_div.appendChild(label);
input_div.appendChild(input);
location.append(input_div);
document.getElementById('time_chunks').textContent = date_names[slide_val];
createSliderAction();
}
//adds functionality to an html slider. never called outside of "createSlider"
function createSliderAction() {
document.getElementById('slider').addEventListener('input', function(time_chunk) {
var cutoffs_obj_active = $("#broadband_cutoffs")[0].className; //when there are more standalones, change this to check the div for any active standalones
var slid_ind = document.getElementById('slider').value;
var geo = $("#geo_menu")[0].value;
if (cutoffs_obj_active == "active") { //when there are more standalones, change this to check the div for any active standalones
var keep_map = date_ids[slid_ind] + "_" + geo + "_" + "broadband_cutoffs";
} else {
var attribute = $("#attribute_menu")[0].value;
var source = $("#source_menu")[0].value;
var keep_map = date_ids[slid_ind] + "_" + geo + "_" + source + "_" + attribute;
}
document.getElementById('time_chunks').textContent = date_names[slid_ind];
map.setLayoutProperty(keep_map + "_id", 'visibility', 'visible');
turnOffOtherMaps(keep_map);
});
}
//this function makes invisible data layers that aren't provided as input. That's the trick of how this app styles the map.
function turnOffOtherMaps(current_map) {
//this layer stays on top of other layers so don't remove the other maps
if (current_map.endsWith("speed_muni")) {
} else {
//turn off main maps
for (var k = 0; k < date_ids.length; k++) {
for (var i = 0; i < geo_ids.length; i++) {
for (var j = 0; j < source_ids.length; j++) {
for (var l = 0; l < attribute_ids.length; l++) {
var map_name = date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l]
if (map_name != current_map) {
map.setLayoutProperty(map_name + "_id", 'visibility', 'none')
}
}
}
}
}
//turn off cutoff maps
for (var k = 0; k < date_ids.length; k++) {
for (var i = 0; i < geo_ids.length; i++) {
var map_name = date_ids[k] + "_" + geo_ids[i] + "_broadband_cutoffs"
if (map_name != current_map) {
map.setLayoutProperty(map_name + "_id", 'visibility', 'none')
}
}
}
}
}
//creates the html toggle bars that appear on the right of the map
function makeToggle(toggleIDs, toggleableLayerNames, active_level, id_name, make_legend) {
var select = document.getElementById(id_name);
for (var i = 0; i < toggleableLayerNames.length; i++) {
var opt = toggleableLayerNames[i];
var el = document.createElement("option");
el.textContent = opt;
el.value = toggleIDs[i];
select.appendChild(el);
}
select.onchange = function() { createToggleMechanics(this, make_legend) };
}
//similar to the two functions used to create the slider, the html object and the mechanics of the object are created by two separate functions. This creates the mechanics
function createToggleMechanics(this_Toggle, make_legend) {
var cutoffs_obj_active = $("#broadband_cutoffs")[0].className;
var clickedLayer = this_Toggle.value;
date_ind = document.getElementById('slider').value
var date = date_ids[date_ind];
var geo = $("#geo_menu")[0].value;
var attribute = $("#attribute_menu")[0].value;
var source = $("#source_menu")[0].value;
if (cutoffs_obj_active == "active") { //when there are more standalones, change this to check the div for any active standalones
var keep_map = date + "_" + geo + "_" + "broadband_cutoffs";
if ($.inArray(clickedLayer, standalone_admissible_toggles["broadband_cutoffs"]) >-1) { //change the broadband_cutoffs to the name of the variable that has the active standalone
//this changes which link is active
turnOffOtherMaps(keep_map);
//this makes the newly active layer visible
map.setLayoutProperty(keep_map + "_id", 'visibility', 'visible');
this_Toggle.className = 'active';
} else {
var keep_map = date + "_" + geo + "_" + source + "_" + attribute;
var visibility = map.getLayoutProperty(keep_map + "_id", 'visibility');
//this changes which link is active
turnOffOtherMaps(keep_map);
//this makes the newly active layer visible
map.setLayoutProperty(keep_map + "_id", 'visibility', 'visible');
this_Toggle.className = 'active';
$("#broadband_cutoffs")[0].className="";
}
} else {
var keep_map = date + "_" + geo + "_" + source + "_" + attribute;
var visibility = map.getLayoutProperty(keep_map + "_id", 'visibility');
//this changes which link is active
turnOffOtherMaps(keep_map);
//this makes the newly active layer visible
map.setLayoutProperty(keep_map + "_id", 'visibility', 'visible');
this_Toggle.className = 'active';
}
//set this link to active in the zoomed case
if (make_legend == true) {
//this is the legend section
var legend_div = document.createElement("div");
legend_div.className = "map-overlay legend" + " " + clickedLayer;
legend_div.id = legend_grouping[clickedLayer];
$(".map-overlay_1").append(legend_div);
var curr_legend = $("." + clickedLayer);
var slid_ind = document.getElementById('slider').value;
var to_make_inactive_div = $('div.map-overlay')
.empty();
var to_make_inactive_div = $('div.map-overlay-inner')
.empty();
createLegend(eval(legend_dict[source + "_" + attribute] + "_" + "labels"), eval(legend_dict[source + "_" + attribute] + "_" + "colors"), curr_legend);
$('div.map-overlay')
.filter(":empty")
.remove()
$('div.map-overlay-inner')
.filter(":empty")
.remove()
createSlider(map_div, slid_ind);
}
};
function makeStandalone(_id, text, menu_name, action, make_legend) {
var id = _id
var link = document.createElement('a');
link.href = '#';
link.id = _id
link.className = '';
link.textContent = text;
if (action == true) {
link.onclick = function() { createStandaloneAction(this, _id, make_legend) };
} else {
link.onclick = function(e) {
var clickedLayer = this.id;
var visibility = map.getLayoutProperty(clickedLayer, 'visibility');
if (visibility === 'visible') {
map.setLayoutProperty(clickedLayer, 'visibility', 'none');
this.className = '';
} else {
this.className = 'active';
map.setLayoutProperty(clickedLayer, 'visibility', 'visible');
}
};
map.setLayoutProperty(_id, 'visibility', 'none');
}
var layers = document.getElementById(menu_name);
layers.appendChild(link);
}
function createStandaloneAction(this_Standalone, standalone_id, make_legend) {
var clickedLayer = this_Standalone.id
date_ind = document.getElementById('slider').value
var date = date_ids[date_ind];
var geo = $("#geo_menu")[0].value;
var keep_map = date + "_" + geo + "_" + standalone_id;
var visibility = map.getLayoutProperty(keep_map + "_id", 'visibility');
if(this_Standalone.className=="active"){
make_legend=false
}
//set this link to active in the zoomed case
if (make_legend == true) {
//this is the legend section
var legend_div = document.createElement("div");
legend_div.className = "map-overlay legend" + " " + clickedLayer;
legend_div.id = legend_grouping[clickedLayer];
$(".map-overlay_1").append(legend_div);
var curr_legend = $("." + clickedLayer);
var slid_ind = document.getElementById('slider').value;
var to_make_inactive_div = $('div.map-overlay')
.empty();
var to_make_inactive_div = $('div.map-overlay-inner')
.empty();
createLegend(eval(legend_dict[clickedLayer] + "_" + "labels"), eval(legend_dict[clickedLayer] + "_" + "colors"), curr_legend);
$('div.map-overlay')
.filter(":empty")
.remove()
$('div.map-overlay-inner')
.filter(":empty")
.remove()
createSlider(map_div, slid_ind);
}
//up until here
//this changes which link is active
turnOffOtherMaps(keep_map);
//this makes the newly active layer visible
for (var j = 0; j < geo_ids.length; j++) {
map.setLayoutProperty(keep_map + "_id", 'visibility', 'visible');
}
this_Standalone.className = 'active';
}
function loadStandalone(standalone_id, source_ind, attribute_ind) {
for (var k = 0; k < date_ids.length; k++) {
for (var i = 0; i < geo_ids.length; i++) {
if (geo_ids[i] == "state_house") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>State House district:" + e.features[0].properties.house_num + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id",
"type": "fill",
"source": "legislative_lower_state",
"source-layer": "full_speed_data_census_leg_lo-1ltr3a",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][source_ind][attribute_ind],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
} else if (geo_ids[i] == "state_senate") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>State Senate district:" + e.features[0].properties.house_num + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id",
"type": "fill",
"source": "legislative_upper_state",
"source-layer": "full_speed_data_census_leg_up-39wwkk",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][source_ind][attribute_ind],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
} else if (geo_ids[i] == "county") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>County:" + e.features[0].properties.county + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + standalone_id + "_id",
"type": "fill",
"source": "county",
"source-layer": "full_speed_data_census_base_use",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][source_ind][attribute_ind],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
}
}
}
}
//This function is used to split up layer loading so that some amount of map shows up quickly while the others load
function loadLayers(time_lower, time_upper, geo_lower, geo_upper, source_lower, source_upper, attribute_lower, attribute_upper) {
for (var k = time_lower; k < time_upper; k++) {
for (var i = geo_lower; i < geo_upper; i++) {
for (var j = source_lower; j < source_upper; j++) {
for (var l = attribute_lower; l < attribute_upper; l++) {
if (geo_ids[i] == "state_house") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>State House district:" + e.features[0].properties.house_num + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id",
"type": "fill",
"source": "legislative_lower_state",
"source-layer": "full_speed_data_census_leg_lo-1ltr3a",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][j][l],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
} else if (geo_ids[i] == "state_senate") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>State Senate district:" + e.features[0].properties.house_num + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id",
"type": "fill",
"source": "legislative_upper_state",
"source-layer": "full_speed_data_census_leg_up-39wwkk",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][j][l],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
} else if (geo_ids[i] == "county") {
map.on('click', date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id", function(e) {
new mapboxgl.Popup()
.setLngLat(e.lngLat)
.setHTML("<p>County:" + e.features[0].properties.county + "</p>")
.addTo(map);
});
map.addLayer({
"id": date_ids[k] + "_" + geo_ids[i] + "_" + source_ids[j] + "_" + attribute_ids[l] + "_id",
"type": "fill",
"source": "county",
"source-layer": "full_speed_data_census_base_use",
"visibility": "none",
"paint": {
"fill-color": colorArrayTime[k][i][j][l],
"fill-outline-color": "#cecece"
}
}, "water" //add a layer id here to insert this layer just before that one
);
}
}
}
}
}
};