-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavbar.js
179 lines (154 loc) · 5.82 KB
/
navbar.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
"use strict";
let prevScrollpos = window.pageYOffset;
window.onscroll = () => {
const currentScrollPos = window.pageYOffset;
if (prevScrollpos > currentScrollPos) {
$("#navbar").css({ top: "0" });
} else {
$("#navbar").css({ top: "-116px" });
}
prevScrollpos = currentScrollPos;
};
/* CHANGE THEME */
const setTheme = (theme) => (document.documentElement.className = theme);
/* PALETTE DROPDOWN */
$(".palette-select").each(function () {
const classes = $(this).attr("class");
let template = `<div class="${classes}">`;
template += `<span class="palette-select-trigger">${$(this).attr("placeholder")}</span>`;
template += '<div class="palette-options">';
let counter = 0;
$(this)
.find("option")
.each(function () {
template += `<span class="palette-option ${$(this).attr("class")}" data-value="${$(this).attr("value")}" data-localization-key="${$(this).attr("value")}">${$(this).html()}</span>`;
});
template += "</div></div>";
$(this).wrap('<div class="palette-select-wrapper"></div>');
$(this).hide();
$(this).after(template);
});
$(".palette-option:first-of-type").hover(
() => {
$(this).parents(".palette-options").addClass("option-hover");
},
() => {
$(this).parents(".palette-options").removeClass("option-hover");
}
);
$(".palette-select-trigger").on("click", function () {
$("html").one("click", () => {
$(".palette-select").removeClass("opened");
});
$(this).parents(".palette-select").toggleClass("opened");
event.stopPropagation();
});
$(".palette-option").on("click", function () {
$(this).parents(".palette-select-wrapper").find("select").val($(this).data("value"));
$(this).parents(".palette-options").find(".palette-option").removeClass("palette-selection");
$(this).addClass("palette-selection");
$(this).parents(".palette-select").removeClass("opened");
$(this).parents(".palette-select").find(".palette-select-trigger").text($(this).text()).attr("data-localization-key", $(this).data("value"));
setTheme($(this).data("value"));
localStorage.setItem("theme", $(this).data("value"));
});
const getTheme = () => {
const theme = localStorage.getItem("theme");
if (theme) {
setTheme(theme);
$(".palette-select-wrapper").find("select").val(theme);
$(".palette-options").find(`[data-value='${theme}']`).addClass("palette-selection");
$(".palette-select").find(".palette-select-trigger").text($(".palette-selection").first().text()).attr("data-localization-key", theme);
} else {
$(".palette-options").find("span").first().addClass("palette-selection");
}
};
getTheme();
/* CHANGE LANGUAGE */
const localizationData = {};
const loadLocalizationData = (language) => {
return new Promise((resolve, reject) => {
fetch(`../assets/languages/${language}.json`)
.then((response) => {
if (!response.ok) {
throw new Error(`Error loading localization data for ${language}: ${response.status}`);
}
return response.json();
})
.then((data) => {
localizationData[language] = data;
resolve();
})
.catch((error) => {
reject(`Error loading localization data for ${language}: ${error}`);
});
});
};
const updateLocalization = (language) => {
const elementsToUpdate = document.querySelectorAll("[data-localization-key]");
elementsToUpdate.forEach((element) => {
const key = element.getAttribute("data-localization-key");
if (localizationData[language] && localizationData[language][key]) {
element.innerHTML = localizationData[language][key];
}
});
};
const setLanguage = (language) => {
localStorage.setItem("language", language);
updateLocalization(language);
$(".language-select-wrapper").find("select").val(language);
$(".language-options").find(`[data-value='${language}']`).addClass("language-selection");
$(".language-select").find(".language-select-trigger").text($(".language-selection").first().text());
};
Promise.all([loadLocalizationData("hr"), loadLocalizationData("en")])
.then(() => {
const savedLanguage = localStorage.getItem("language");
if (savedLanguage && localizationData[savedLanguage]) {
setLanguage(savedLanguage);
} else {
setLanguage("en");
}
})
.catch((error) => {
console.error(error);
});
/* LANGUAGE DROPDOWN */
$(".language-select").each(function () {
const classes = $(this).attr("class");
let template = `<div class="${classes}">`;
template += `<span class="language-select-trigger">${$(this).attr("placeholder")}</span>`;
template += '<div class="language-options">';
$(this)
.find("option")
.each(function () {
template += `<span class="language-option ${$(this).attr("class")}" data-value="${$(this).attr("value")}">${$(this).html()}</span>`;
});
template += "</div></div>";
$(this).wrap('<div class="language-select-wrapper"></div>');
$(this).hide();
$(this).after(template);
});
$(".language-option:first-of-type").hover(
() => {
$(this).parents(".language-options").addClass("option-hover");
},
() => {
$(this).parents(".language-options").removeClass("option-hover");
}
);
$(".language-select-trigger").on("click", function () {
$("html").one("click", () => {
$(".language-select").removeClass("opened");
});
$(this).parents(".language-select").toggleClass("opened");
event.stopPropagation();
});
$(".language-option").on("click", function () {
$(this).parents(".language-select-wrapper").find("select").val($(this).data("value"));
$(this).parents(".language-options").find(".language-option").removeClass("language-selection");
$(this).addClass("language-selection");
$(this).parents(".language-select").removeClass("opened");
$(this).parents(".language-select").find(".language-select-trigger").text($(this).text());
updateLocalization($(this).data("value"));
localStorage.setItem("language", $(this).data("value"));
});