-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdocs.js
128 lines (117 loc) · 3.85 KB
/
docs.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
const loadJSON = (path, callback) => {
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);
};
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");
colDiv.id = country.code;
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);
// Divider
const dividerSpan = document.createElement("span");
const divider = document.createTextNode(" ");
dividerSpan.appendChild(divider);
//Country
const countryDiv = document.createElement("div");
countryDiv.classList.add("flag-country");
countryDiv.classList.add("no-wrap");
countryDiv.title = country.name;
const countrySpan = document.createElement("span");
const countryName = document.createTextNode(country.name);
countrySpan.appendChild(countryName);
countryDiv.appendChild(codeSpan);
countryDiv.appendChild(dividerSpan);
countryDiv.appendChild(countrySpan);
const flagImg = document.createElement("img");
flagImg.classList.add("flag-img");
flagImg.src = country.flag_4x3;
flagImg.alt = `Flag of ${country.name}`;
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}`;
colDiv.appendChild(flagDiv);
flagDiv.appendChild(countryDiv);
flagDiv.appendChild(flagImg);
flagDiv.appendChild(flagImgSquare);
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");
}
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");
}
gtag("event", "switch", {
event_category: "flags",
event_label: "1x1",
});
};
window.onload = function () {
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();
});
loadJSON("country.json", (response) => {
const countries = JSON.parse(response);
for (country of countries) {
if (country.iso) {
addFlag(country, isoFlagsRow);
} else {
addFlag(country, nonIsoFlagsRow);
}
}
});
};