-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
181 lines (155 loc) · 4.81 KB
/
script.js
File metadata and controls
181 lines (155 loc) · 4.81 KB
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
180
181
// downloadOptions are index fixed. Don't move them!
const downloadOptions = [
{
os:'Web',
link: 'https://fasko-app.web.app',
img: 'assets/logos/web-logo.png',
current: false
},
{
os:'Windows',
link: null,
img: 'assets/logos/windows-logo.png',
current: false
},
{
os:'Mac OS',
link: null,
img: 'assets/logos/macos-logo.png',
current: false
},
{
os:'iOS',
link: null,
img: 'assets/logos/ios-logo.png',
current: false
},
{
os:'Android',
link: 'https://github.com/fasko-app/fasko/releases/latest/download/fasko.apk',
img: 'assets/logos/android-logo.png',
current: false
},
{
os:'Linux',
link: null,
img: 'assets/logos/linux-logo.png',
current: false
},
];
const defaultLocale = "en";
const supportedLocales = ["en", "uk"];
let locale;
let translations = {};
let currentOptionId = getCurrentDownloadOptionId();
document.addEventListener('DOMContentLoaded', () => {
const initialLocale = supportedOrDefault(browserLocales());
setLocale(initialLocale);
bindLocaleSwitcher(initialLocale);
buildDownloadOptions();
});
function buildPage() {
translatePage();
buildDownloadBtn();
buildLogoImage();
}
// get user supported locales
function browserLocales() {
return navigator.languages.map((locale) => locale.split("-")[0]);
}
function isSupported(locale) {
return supportedLocales.indexOf(locale) > -1;
}
function supportedOrDefault(locales) {
return locales.find(isSupported) || defaultLocale;
}
async function setLocale(newLocale) {
if (newLocale === locale) return;
translations = await fetchTranslationsFor(newLocale);
locale = newLocale;
buildPage();
}
async function fetchTranslationsFor(newLocale) {
const response = await fetch(`langs/${newLocale}.json`);
return await response.json();
}
function translatePage() {
document
.querySelectorAll("[data-i18n-key]")
.forEach(translateElement);
}
function translateElement(element) {
const key = element.getAttribute("data-i18n-key");
element.innerText = translations[key];
}
function getCurrentDownloadOptionId() {
var userAgent = window.navigator.userAgent,
platform = window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod']
let optionIndex = -1;
if (macosPlatforms.indexOf(platform) !== -1) {
optionIndex = 0;
//optionIndex = 1;
} else if (iosPlatforms.indexOf(platform) !== -1) {
optionIndex = 0;
//optionIndex = 2;
} else if (windowsPlatforms.indexOf(platform) !== -1) {
optionIndex = 0;
//optionIndex = 1;
} else if (/Android/.test(userAgent)) {
optionIndex = 4;
} else if (!os && /Linux/.test(platform)) {
optionIndex = 0;
//optionIndex = 4;
}
if(optionIndex != -1) {
downloadOptions[optionIndex].current = true;
return optionIndex;
}
return null;
}
function buildDownloadOptions() {
let downloadOptionsDiv = document.getElementById('downloadOptionsDiv');
for (let i = 0; i < downloadOptions.length; i++) {
const element = downloadOptions[i];
if(!element.current && element.link != null) {
downloadOptionsDiv.innerHTML +=
`
<div class="col-sm-6">
<div class="card border-dark mb-3">
<div class="card-header bg-transparent border-dark">
<h6 class="m-1 text-center">${element.os}</h6>
</div>
<div class="card-body d-flex justify-content-center align-items-center">
<img src="${element.img}" class="card-img-top w-25" alt="${element.os}">
</div>
<div class="card-footer bg-transparent border-dark p-0">
<a class="btn btn-light w-100 h-100" href="${element.link}" data-i18n-key="${element.os == 'Web' ? 'visit' : 'download'}"></a>
</div>
</div>
</div>
`;
}
}
}
function buildDownloadBtn() {
let downloadBtn = document.getElementById('downloadBtn');
if(downloadOptions[currentOptionId] != null && downloadOptions[currentOptionId].link != null) {
downloadBtn.innerHTML = `<i class="bi bi-download"></i>  ${translations[currentOptionId == 0 ? 'visit' : 'download']} ${translations['for']} ${downloadOptions[currentOptionId].os}`;
downloadBtn.setAttribute('href', downloadOptions[currentOptionId].link);
} else {
downloadBtn.innerHTML = `<i class="bi bi-download"></i>  ${translations['download']}`;
downloadBtn.setAttribute('href', '#otherOptions');
}
}
function buildLogoImage() {
const logoImg = document.getElementById('logoImg');
logoImg.setAttribute("src", translations['logo']);
}
function bindLocaleSwitcher(value) {
const switcher = document.querySelector("[data-i18n-switcher]");
switcher.value = value;
switcher.onchange = e => setLocale(e.target.value);
}