2023-08-03 21:52:50 +03:00
|
|
|
import { routes } from './routes.js';
|
|
|
|
|
2023-08-06 03:29:14 +03:00
|
|
|
let DEBUG = false;
|
2023-08-03 23:43:38 +03:00
|
|
|
window.DEBUG = DEBUG;
|
|
|
|
|
2023-08-06 00:44:27 +03:00
|
|
|
let SERVER_HOST;
|
2023-08-06 03:55:37 +03:00
|
|
|
if (DEBUG) SERVER_HOST = 'http://localhost:50009';
|
|
|
|
else SERVER_HOST = 'https://a2s.su';
|
2023-08-06 00:44:27 +03:00
|
|
|
window.SERVER_HOST = SERVER_HOST;
|
2023-08-04 15:48:48 +03:00
|
|
|
|
2023-08-03 21:18:50 +03:00
|
|
|
/* main */
|
|
|
|
window.onload = function () {
|
|
|
|
let app = document.documentElement;
|
|
|
|
|
2023-08-06 01:49:23 +03:00
|
|
|
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
|
2023-08-03 21:18:50 +03:00
|
|
|
switch_theme('dark');
|
|
|
|
} else {
|
|
|
|
switch_theme('light');
|
|
|
|
}
|
|
|
|
|
|
|
|
// theme switcher
|
2023-08-06 01:49:23 +03:00
|
|
|
document.getElementById('app_theme_switcher').addEventListener('click', switch_theme, false);
|
2023-08-03 21:18:50 +03:00
|
|
|
|
|
|
|
// routes handler
|
|
|
|
window.addEventListener('hashchange', location_handler);
|
|
|
|
location_handler();
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Изменяет тему приложения */
|
|
|
|
function switch_theme() {
|
|
|
|
let app = document.documentElement;
|
|
|
|
let theme = app.getAttribute('data-bs-theme');
|
|
|
|
let theme_switcher = document.getElementById('app_theme_switcher');
|
|
|
|
|
|
|
|
let theme_icon = {
|
|
|
|
dark: '☀️',
|
|
|
|
light: '🌙',
|
|
|
|
};
|
|
|
|
|
|
|
|
if (theme == 'auto' || theme == 'light') {
|
|
|
|
theme_switcher.innerText = theme_icon['dark'];
|
|
|
|
app.setAttribute('data-bs-theme', 'dark');
|
|
|
|
} else {
|
|
|
|
theme_switcher.innerText = theme_icon['light'];
|
|
|
|
app.setAttribute('data-bs-theme', 'light');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const location_handler = async () => {
|
|
|
|
let location = window.location.hash.replace('#', '');
|
|
|
|
if (location.length == 0) {
|
|
|
|
location = '/';
|
|
|
|
}
|
|
|
|
const route = routes[location] || routes['404'];
|
|
|
|
const html = await fetch(route.template).then((response) => response.text());
|
|
|
|
document.getElementById('content').innerHTML = html;
|
|
|
|
document.title = route.title;
|
2023-08-06 01:49:23 +03:00
|
|
|
document.querySelector('meta[name="description"]').setAttribute('content', route.description);
|
2023-08-03 23:39:02 +03:00
|
|
|
|
|
|
|
if (route.script) {
|
|
|
|
let script = document.getElementById('content').querySelector('script');
|
|
|
|
document.querySelector('#autorun').innerHTML = script.innerHTML;
|
|
|
|
script.remove();
|
|
|
|
|
|
|
|
eval(document.querySelector('#autorun').innerHTML);
|
|
|
|
} else {
|
|
|
|
document.querySelector('#autorun').innerHTML = '';
|
|
|
|
}
|
2023-08-03 21:18:50 +03:00
|
|
|
};
|
2023-08-04 15:48:48 +03:00
|
|
|
|
|
|
|
function get_from_api(callback, api_method = '', params = {}) {
|
|
|
|
if (api_method == '') {
|
|
|
|
console.log('wrong method');
|
|
|
|
} else {
|
2023-08-06 03:55:37 +03:00
|
|
|
let url = `${SERVER_HOST}/api/v1.0/${api_method}`;
|
2023-08-06 00:44:27 +03:00
|
|
|
let opts = {
|
|
|
|
method: 'get',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
fetch(url, opts)
|
2023-08-04 15:48:48 +03:00
|
|
|
.then((response) => {
|
|
|
|
return response.json();
|
|
|
|
})
|
|
|
|
.then((data) => {
|
|
|
|
callback(data);
|
|
|
|
})
|
|
|
|
.catch((err) => console.warn('Something went wrong.', err));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.get_from_api = get_from_api;
|