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

129 lines
3.8 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();
2022-06-21 10:00:43 +02:00
xobj.overrideMimeType("application/json");
xobj.open("GET", path, true);
2021-10-22 22:26:29 +02:00
xobj.onreadystatechange = function () {
2022-06-21 10:00:43 +02:00
if (xobj.readyState == 4 && xobj.status == "200") {
2021-10-22 22:26:29 +02:00
callback(xobj.responseText);
}
};
xobj.send(null);
};
2021-10-23 11:43:30 +02:00
const addFlag = (country, rowDiv) => {
2022-06-21 10:00:43 +02:00
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;
2022-06-21 10:00:43 +02:00
const flagDiv = document.createElement("div");
flagDiv.classList.add("flag");
2021-10-23 11:43:30 +02:00
// Code
2022-06-21 10:00:43 +02:00
const codeSpan = document.createElement("span");
codeSpan.classList.add("flag-code");
2021-10-23 11:43:30 +02:00
const code = document.createTextNode(country.code);
codeSpan.appendChild(code);
2022-06-08 21:08:02 +02:00
// Divider
2022-06-21 10:00:43 +02:00
const dividerSpan = document.createElement("span");
const divider = document.createTextNode(" ");
2022-06-08 21:08:02 +02:00
dividerSpan.appendChild(divider);
2021-10-23 11:43:30 +02:00
//Country
2022-06-21 10:00:43 +02:00
const countryDiv = document.createElement("div");
countryDiv.classList.add("flag-country");
countryDiv.classList.add("no-wrap");
2021-10-23 11:43:30 +02:00
countryDiv.title = country.name;
2022-06-21 10:00:43 +02:00
const countrySpan = document.createElement("span");
2021-10-23 11:43:30 +02:00
const countryName = document.createTextNode(country.name);
2021-12-20 14:54:20 +01:00
countrySpan.appendChild(countryName);
2021-10-23 11:43:30 +02:00
countryDiv.appendChild(codeSpan);
2022-06-08 21:08:02 +02:00
countryDiv.appendChild(dividerSpan);
2021-10-24 11:51:14 +02:00
countryDiv.appendChild(countrySpan);
2022-06-21 10:00:43 +02:00
const flagImg = document.createElement("img");
flagImg.classList.add("flag-img");
2021-10-23 11:43:30 +02:00
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
const flagImgSquare = document.createElement("img");
flagImgSquare.classList.add("flag-img-square");
flagImgSquare.classList.add("hide");
flagImgSquare.src = country.flag_1x1;
flagImgSquare.alt = `Flag of ${country.name}`;
2021-10-23 11:43:30 +02:00
colDiv.appendChild(flagDiv);
flagDiv.appendChild(countryDiv);
flagDiv.appendChild(flagImg);
flagDiv.appendChild(flagImgSquare);
2021-10-23 11:43:30 +02:00
rowDiv.appendChild(colDiv);
};
const show4x3 = () => {
const click4x3 = document.getElementById("click-4x3");
const click1x1 = document.getElementById("click-1x1");
click1x1.classList.remove("hide");
click4x3.classList.add("hide");
const flags = document.getElementsByClassName("flag-img");
for (flag of flags) {
flag.classList.remove("hide");
}
const flagsSquared = document.getElementsByClassName("flag-img-square");
for (flag of flagsSquared) {
flag.classList.add("hide");
}
2022-06-25 10:35:29 +02:00
gtag("event", "switch", {
event_category: "flags",
event_label: "4x3",
});
};
const show1x1 = () => {
const click4x3 = document.getElementById("click-4x3");
const click1x1 = document.getElementById("click-1x1");
click4x3.classList.remove("hide");
click1x1.classList.add("hide");
const flagsSquared = document.getElementsByClassName("flag-img-square");
for (flag of flagsSquared) {
flag.classList.remove("hide");
}
const flags = document.getElementsByClassName("flag-img");
for (flag of flags) {
flag.classList.add("hide");
}
2022-06-25 10:35:29 +02:00
gtag("event", "switch", {
event_category: "flags",
event_label: "1x1",
});
};
2013-09-27 00:40:54 +02:00
window.onload = function () {
2022-06-21 10:00:43 +02:00
const isoFlagsRow = document.getElementById("iso-flags");
const nonIsoFlagsRow = document.getElementById("non-iso-flags");
const click4x3 = document.getElementById("click-4x3");
click4x3.addEventListener("click", (event) => {
event.stopPropagation();
event.preventDefault();
show4x3();
});
const click1x1 = document.getElementById("click-1x1");
click1x1.addEventListener("click", (event) => {
event.stopPropagation();
event.preventDefault();
show1x1();
});
2022-06-21 10:00:43 +02:00
loadJSON("country.json", (response) => {
2021-10-22 22:26:29 +02:00
const countries = JSON.parse(response);
for (country of countries) {
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
};