-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·89 lines (73 loc) · 2.66 KB
/
index.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
function displayPhoneNumber(event) {
event.preventDefault();
const phoneLink = document.getElementById('phone-number');
const reversedPhoneNumber = '698395386';
const phoneNumber = '+34-' + reversedPhoneNumber.split('').reverse().join('');
const phoneSpan = document.createElement('span');
phoneSpan.innerText = phoneNumber;
phoneLink.parentNode.replaceChild(phoneSpan, phoneLink);
}
window.displayPhoneNumber = displayPhoneNumber;
const currentAge = () => {
var today = new Date();
var birthDate = new Date("1994-08-04");
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
};
const currentYear = () => {
return new Date().getFullYear();
};
const navLinks = document.querySelectorAll("a");
navLinks.forEach(function (link) {
link.addEventListener("click", function (e) {
const href = e.target.getAttribute("href");
if (href && href.includes("#")) {
const attr = href.toString().replace("#", "");
const target = document.getElementById(attr);
target.scrollIntoView({
behavior: "smooth",
block: "start",
});
const url = window.location.href;
history.pushState(null, null, url);
e.preventDefault();
}
});
});
const loadJSON = async () => {
const response = await fetch("./assets/lang.json");
const json = await response.json();
return json;
};
async function switchLanguage(preloadLanguage = null) {
var check = document.querySelector(".check");
const json_data = await loadJSON();
const id_nodes = document.querySelectorAll("*[id]");
const navbar_ids = Array.from(id_nodes).map((element) => element.id);
var language = preloadLanguage || (check.checked ? "en" : "es");
for (const value of navbar_ids) {
if (json_data[language][value] !== undefined) {
document.getElementById(value).innerHTML = json_data[language][value];
}
}
document.getElementById("current-age").textContent = currentAge();
document.getElementById("current-year").textContent = currentYear();
}
async function preloadDefaultLanguage() {
const userPreferredLanguage = navigator.language || navigator.userLanguage;
let defaultLanguage = "es";
if (userPreferredLanguage.startsWith("en")) {
defaultLanguage = "en";
}
await switchLanguage(defaultLanguage);
}
document.querySelector(".check").addEventListener("change", () => {
switchLanguage();
});
window.addEventListener("load", preloadDefaultLanguage);
document.getElementById("current-age").textContent = currentAge();
document.getElementById("current-year").textContent = currentYear();