Repository navigation
Expand file tree
/
Copy pathcountries.html
More file actions
166 lines (143 loc) · 4.6 KB
/
Copy pathcountries.html
File metadata and controls
166 lines (143 loc) · 4.6 KB
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
<hmtl>
<head>
<title>Countries of the World</title>
<style>
path:hover {
fill: rgba(252, 141, 89, 1.0) !important;
}
path.selected {
fill: rgb(255, 255, 0) !important;
}
path.complete {
fill: rgb(0, 0, 255) !important;
}
</style>
</head>
<body>
<script src="./dist/d3.js"></script>
<script src="./dist/topojson.js"></script>
<script src="./dist/datamaps.world.hires.js"></script>
<div id="info">
<span id="info-options">
<label for="input-show-country-names">Show names: </label>
<input id="input-show-country-names" name="input-show-country-names" type="checkbox" />
</span>
<br />
<span id="info-banner">
<span id="info-score"></span> |
<span id="info-missed"></span>
</span>
<br />
<span id="identify">
<label for="identify-input">Country name: </label>
<input id="identify-input" name="identify-input" />
<span id="identify-input-cancel"><a href="#">Cancel</a></span>
</span>
</div>
<div id="map-container" style="position: relative; width: 900px; height: 600px;" />
<script>
var countries = Datamap.prototype.worldTopo.objects.world.geometries
.map((geom) => ({ id: geom.id, name: geom.properties.name, identified: false }))
.reduce((a, c) => {
a[c.id] = c;
return a;
}, {});
var state = {
selectedCountry: null,
completeCount: -1,
missedCount: -1,
showCountryNames: false
};
document.getElementById("input-show-country-names").addEventListener('click', function(event) {
state.showCountryNames = event.target.checked;
});
function addScore () {
state.completeCount += 1;
document.getElementById("info-score").innerHTML = 'Score: ' + state.completeCount + '/' + Object.keys(countries).length;
}
function addMissed () {
state.missedCount += 1;
document.getElementById("info-missed").innerHTML = 'Missed: ' + state.missedCount;
}
var identifyInputElement = document.getElementById("identify-input");
function focusIdentifyInput () {
identifyInputElement.disabled = false;
identifyInputElement.focus();
}
function unfocusIdentifyInput () {
identifyInputElement.value = '';
identifyInputElement.disabled = true;
}
document.getElementById("identify-input-cancel").onclick = function () {
unfocusIdentifyInput();
resetCountrySelection(map);
}
function selectCountry (countryId, element, datamap) {
resetCountrySelection(datamap);
state.selectedCountry = countryId;
element.classList.add('selected');
}
function resetCountrySelection (datamap) {
state.selectedCountry = null;
datamap.svg.selectAll('.datamaps-subunit')[0].forEach((path)=>{
path.classList.remove('selected');
});
}
function identifyCountry (countryId, value) {
if(countries[countryId].name == value) {
countries[countryId].identified = true;
addScore();
// Color path accordingly
let subunits = Object.entries(map.svg.selectAll('.datamaps-subunit')[0]);
for(let i = 0; i < subunits.length; i++) {
if(subunits[i][1].__data__.id == countryId) {
subunits[i][1].classList.add('complete');
unfocusIdentifyInput(map);
return;
}
}
} else {
addMissed();
}
}
function identifyInputKeypress (event) {
if(event.code == "Enter") {
identifyCountry(state.selectedCountry, identifyInputElement.value);
}
}
identifyInputElement.onkeypress = identifyInputKeypress;
var map = new Datamap({
element: document.getElementById('map-container'),
data: {},
geographyConfig: {
highlightOnHover: false,
popupOnHover: true,
popupTemplate: function(geo, data) {
let name = ((countries[geo.id].identified || state.showCountryNames) ? geo.properties.name : '???');
return '<div class="hoverinfo"> Name: ' + name + ' </div>';
}
},
done: function(datamap) {
// Initialize
unfocusIdentifyInput();
addScore();
addMissed();
// Options
// Zoom
datamap.svg.call(d3.behavior.zoom().on("zoom", redraw));
function redraw() {
// Floor the values to avoid bluriness in rendering
datamap.svg.selectAll("g").attr("transform", "translate(" + d3.event.translate + ")scale(" + d3.event.scale + ")");
}
// Click
datamap.svg.selectAll('.datamaps-subunit').on('click', function(geography) {
// If we are actually clicking and dragging, cancel selection
if(d3.event.defaultPrevented) return;
selectCountry(geography.id, this, datamap);
focusIdentifyInput();
});
}
});
</script>
</body>
</html>