-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathyear_country.js
71 lines (58 loc) · 2.09 KB
/
year_country.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
var inputKeys = document.getElementById('input-key');
var resultsTable = document.getElementById('results');
window.currentDataset = "";
var getDatasetOptions = function() {
if (window.datasets[inputKeys.value] &&
inputKeys.value != window.currentDataset) {
clearResultsTable();
window.currentDataset = inputKeys.value;
var years = document.getElementById('year');
var countries = document.getElementById('country');
years.disabled = true;
countries.disabled = true;
var displayOptions = function() {
var yearCountry = document.getElementById('year-country');
if (yearCountry.style.display === "inline") {
while(years.firstChild) {
years.removeChild(years.firstChild);
}
years.appendChild(document.createElement('option'));
while(countries.firstChild) {
countries.removeChild(countries.firstChild);
}
countries.appendChild(document.createElement('option'));
}
var data = JSON.parse(dataRequest.responseText);
for (var i = 0; i < data.years.data.length; i++) {
var year = data.years.data[i];
var option = document.createElement('option');
option.innerHTML = year;
years.appendChild(option);
}
for (var i = 0; i < data.countries.data.length; i++) {
var country = data.countries.data[i];
var option = document.createElement('option');
option.innerHTML = country;
countries.appendChild(option);
}
years.disabled = false;
countries.disabled = false;
yearCountry.style.display = "inline";
};
var dataRequest = new XMLHttpRequest();
dataRequest.onload = displayOptions;
dataRequest.open( "GET", 'http://gapminder-api.herokuapp.com/data/help/?key=' +
window.datasets[window.currentDataset]);
dataRequest.send();
}
};
var clearResultsTable = function() {
clearNode(resultsTable);
};
var clearNode = function(element) {
while(element.firstChild) {
clearNode(element.firstChild);
element.removeChild(element.firstChild);
}
};
setInterval(getDatasetOptions, 100);