-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
323 lines (254 loc) · 8.39 KB
/
script.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
let mapData = [];
let countryData = await loadData('./countries.json');
let secondaryLabel = null;
let scale = 1;
let originX = 0;
let originY = 0;
let startX = 0;
let startY = 0;
let offsetX = 0;
let offsetY = 0;
const container = document.querySelector('.map-container');
const content = document.querySelector('.map-svg');
const countries = document.querySelectorAll('path[name], path[class]');
const svg = document.querySelector('.map-svg');
/**
* @typedef {Object} MapData
* @param {string} name - The name of the country
* @param {string} url - The url to the country's flag image
* @param {number} count - The number of counts in the country
*/
/**
* Load data from the external map file
* @returns {Promise<Array<MapData>>} - A promise that resolves to an array of MapData objects
*/
async function loadData(path='./data.json') {
try {
const request = await fetch(path);
if (!request.ok) {
throw new Error('Failed to fetch map data');
}
const data = await request.json();
return data;
} catch(error) {
throw new Error(error.message);
}
}
/**
* Populate the tooltip container
* @param {string} name
* @param {number} count
* @param {string} flag
* @param {string} label
*/
const updateTooltipData = (name, count, flag, label) => {
const title = document.querySelector('.map-tooltip #wmcName');
const wmcCount = document.querySelector('.map-tooltip #wmcCount');
const countryFlag = document.querySelector('.map-tooltip .wmcFlag');
const wmcLabel = document.querySelector('.map-tooltip #wmcLabel');
const country = countryData.find(c => c.name.toLowerCase() === name.toLowerCase());
title.textContent = name;
wmcCount.textContent = count;
wmcLabel.textContent = label;
countryFlag.textContent = country.emoji;
}
/**
* Clicking on a map pin
* @returns {void}
*/
const openTooltipHandler = function(e) {
const country = this.getAttribute('data-name');
const tooltip = document.querySelector('.map-tooltip');
if (country === null) return;
const match = mapData.find(countryData => {
return countryData.name.toLowerCase() === country.toLowerCase();
});
if (!match) return;
updateTooltipData(match.name, match.count, match.url, match?.label ?? secondaryLabel)
showAllHiddenPins();
this.classList.add('hide');
tooltip.classList.add('show');
tooltip.style.left = `${e.pageX}px`;
tooltip.style.top = `${e.pageY}px`;
clearHighlightedMapArea();
}
const clearHighlightedMapArea = () => {
[...countries].forEach(el => el.style.fill = 'inherit');
}
const showAllHiddenPins = () => {
const mapPinsEl = document.querySelectorAll('.map-pin');
[...mapPinsEl].forEach(el => el.classList.remove('hide'));
}
/**
* Clicking outside of the map should close the map
* @returns {void}
*/
const closeTooltipHandler = ({ target }) => {
if (Array.from(target.classList).includes('map-pin')) {
return;
}
const tooltip = document.querySelector('.map-tooltip');
tooltip.classList.remove('show');
showAllHiddenPins();
clearHighlightedMapArea();
}
/**
* For countries with multiple locations on the map, return the largest location
* @param {Array<object>} data - An array containing all countries data
* @returns {Array<HTMLElement>} - Unique largest area of a country
*/
const getLargestAreasByCountry = (data) => {
const countries = {};
const countryNamesInData = mapData.map(country => country.name.toLowerCase());
Array.from(data).forEach(el => {
const countryName = el.getAttribute('name');
const countryNameByClass = el.getAttribute('class');
const country = countryName ?? countryNameByClass;
const { width: landAreaWidth } = el.getBoundingClientRect();
if (!countryDataExists(country)) return;
if (!Object.keys(countries).includes(country.toLowerCase())) {
countries[country.toLowerCase()] = el;
} else {
const name = countryNamesInData.filter(countryItem => countryItem === country.toLowerCase())[0];
if (name !== country.toLowerCase()) return;
if (landAreaWidth > countries[name].getBoundingClientRect().width) {
countries[name] = el;
}
}
});
return Object.values(countries);
}
/**
* Place pin on largest area of a country
* @param {Array<HTMLElement>} locations
* @param {string} eventType
* @returns {void}
*/
const mapPins = (locations, eventType = 'click') => {
locations.map(el => {
const ns = 'http://www.w3.org/2000/svg';
const foreignObject = document.createElementNS(ns, 'foreignObject');
const span = document.createElement('span');
const countryName = el.getAttribute('name');
const countryNameByClass = el.getAttribute('class');
const {x, y, height, width} = el.getBBox();
const attrOptions = {
x: x + width/3,
y: y + height/3,
height: 24,
width: 24
}
span.className = 'map-pin';
const country = countryName ?? countryNameByClass;
const [countryItem] = mapData.filter(dataItem => {
return dataItem.name === country;
});
span.setAttribute('data-name', country);
span.setAttribute('data-count', countryItem.count);
span.addEventListener(eventType, openTooltipHandler);
span.addEventListener(eventType, () => el.style.fill = '#bfc8d2');
document.addEventListener('click', closeTooltipHandler);
setForeignObjectAttributes(foreignObject, attrOptions);
foreignObject.appendChild(span);
svg.appendChild(foreignObject);
});
highlightTopThreePins();
}
const highlightTopThreePins = () => {
const [high, higher, highest] = getTopThreeValues();
const mapPinsEl = Array.from(document.querySelectorAll('.map-pin'));
mapPinsEl.forEach(el => {
const count = parseInt(el.getAttribute('data-count'));
if (count === highest) el.classList.add('map-pin--highest');
if (count === higher) el.classList.add('map-pin--higher');
if (count === high) el.classList.add('map-pin--high');
})
}
/**
* Get top 3 values from the countries dataset
* @returns {Array<number>}
*/
const getTopThreeValues = () => {
const sortedData = mapData.sort((a, b) => {
if (a.count < b.count) return -1;
if (a.count > b.count) return 1;
return 0;
});
const sortedValues = sortedData.map(dataItem => {
return dataItem.count;
});
const uniqueValues = [...new Set(sortedValues)];
return uniqueValues.splice(uniqueValues.length - 3, 3);
}
/**
*
* @param {HTMLElement} el - The DOM element to set properties on
* @param {Object} options - The attributes to set
*/
const setForeignObjectAttributes = (el, options) => {
for (let key in options) {
el.setAttribute(key, options[key])
}
}
/**
* Check if a country exists in list of countries data
* @param {string} country
* @returns {boolean}
*/
const countryDataExists = (country) => {
const index = mapData.findIndex(item => {
return item.name.toLowerCase() === country.toLowerCase();
});
return index >= 0;
}
const handleWheel = (e) => {
e.preventDefault();
const zoomIntensity = 0.1;
const newScale = e.deltaY < 0 ? scale + zoomIntensity : scale - zoomIntensity;
if (newScale < 0.5 || newScale > 3) return; // Limit zoom levels
scale = newScale;
updateTransform();
};
const handleMouseDown = (e) => {
startX = e.clientX - originX;
startY = e.clientY - originY;
content.style.cursor = 'grabbing';
const handleMouseMove = (e) => {
originX = e.clientX - startX;
originY = e.clientY - startY;
offsetX = originX;
offsetY = originY;
updateTransform();
};
const handleMouseUp = () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
content.style.cursor = 'grab';
};
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
};
const updateTransform = () => {
content.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(${scale})`;
};
/**
* Initialize map logic
* @param {Object} options - User options to customize map behaviour
* @returns {void}
*/
const init = async (options = {}) => {
const { event, label, data, allowZoom } = options;
try {
mapData = data;
secondaryLabel = label;
const largestAreas = getLargestAreasByCountry(countries);
mapPins(largestAreas, event);
if(allowZoom) {
container.addEventListener('wheel', handleWheel);
content.addEventListener('mousedown', handleMouseDown);
}
} catch(error) {
throw new Error(error.message);
}
}
export default init;