0
0
mirror of https://github.com/lipis/flag-icons.git synced 2024-11-24 19:36:55 +01:00
flags/assets/docs.js

65 lines
1.9 KiB
JavaScript
Raw Normal View History

2021-10-23 11:43:30 +02:00
const loadJSON = (path, callback) => {
2021-10-22 22:26:29 +02:00
var xobj = new XMLHttpRequest();
xobj.overrideMimeType('application/json');
xobj.open('GET', path, true);
xobj.onreadystatechange = function () {
if (xobj.readyState == 4 && xobj.status == '200') {
callback(xobj.responseText);
}
};
xobj.send(null);
};
2021-10-23 11:43:30 +02:00
const addFlag = (country, rowDiv) => {
const colDiv = document.createElement('div');
colDiv.classList.add('col-xl-2');
colDiv.classList.add('col-lg-3');
colDiv.classList.add('col-md-4');
colDiv.classList.add('col-6');
2021-10-24 11:51:14 +02:00
colDiv.id = country.code;
2021-10-23 11:43:30 +02:00
const flagDiv = document.createElement('div');
flagDiv.classList.add('flag');
// Code
const codeSpan = document.createElement('span');
codeSpan.classList.add('flag-code');
const code = document.createTextNode(country.code);
codeSpan.appendChild(code);
//Country
const countryDiv = document.createElement('div');
countryDiv.classList.add('flag-country');
countryDiv.classList.add('no-wrap');
countryDiv.title = country.name;
2021-10-24 11:51:14 +02:00
const countrySpan = document.createElement('span');
2021-10-23 11:43:30 +02:00
const countryName = document.createTextNode(country.name);
2021-10-24 11:51:14 +02:00
countrySpan.appendChild(countryName)
2021-10-23 11:43:30 +02:00
countryDiv.appendChild(codeSpan);
2021-10-24 11:51:14 +02:00
countryDiv.appendChild(countrySpan);
2021-10-23 11:43:30 +02:00
const flagImg = document.createElement('img');
flagImg.classList.add('flag-img');
flagImg.src = country.flag_4x3;
2021-10-23 20:36:36 +02:00
flagImg.alt = `Flag of ${country.name}`;
2021-10-23 11:43:30 +02:00
colDiv.appendChild(flagDiv);
flagDiv.appendChild(countryDiv);
flagDiv.appendChild(flagImg);
rowDiv.appendChild(colDiv);
};
2013-09-27 00:40:54 +02:00
window.onload = function () {
2021-10-23 11:43:30 +02:00
const isoFlagsRow = document.getElementById('iso-flags');
const nonIsoFlagsRow = document.getElementById('non-iso-flags');
2021-10-22 22:26:29 +02:00
loadJSON('country.json', response => {
const countries = JSON.parse(response);
for (country of countries) {
console.log(country);
2021-10-23 11:43:30 +02:00
if (country.iso) {
addFlag(country, isoFlagsRow);
} else {
addFlag(country, nonIsoFlagsRow);
}
2015-08-31 03:07:13 +02:00
}
});
2021-10-22 22:26:29 +02:00
};