melawy-en-site/public/assets/js/script.js

300 lines
8.9 KiB
JavaScript
Raw Normal View History

2023-11-01 02:32:48 +05:00
// Экран ожидания страницы
function showPage() {
document.getElementById("loader").style.display = "none";
document.getElementById("body").style.display = "block";
}
function hidePage() {
document.getElementById("loader").style.display = "block";
document.getElementById("body").style.display = "none";
}
2023-12-10 01:21:40 +05:00
function openInNewTab(url) {
window.open(url, '_blank').focus();
}
2023-11-02 01:08:03 +05:00
function showFullscreenImage(element) {
imageSrc = element.getAttribute('src');
imageAlt = element.getAttribute('alt');
myModalImg = document.getElementById('myModalImg');
myModalImg.setAttribute('src', imageSrc);
myModalHeader = document.getElementById('myModalHeader');
myModalHeader.innerText = imageAlt;
const myModal = new bootstrap.Modal(document.getElementById('myModal'), {})
myModal.show();
}
2023-11-01 02:32:48 +05:00
let showPage_var;
function ready() {
showPage_var = setTimeout(showPage, 10);
2023-11-02 01:08:03 +05:00
// let fullscreenImages = document.getElementsByClassName("fullscreen");
//
// Array.from(fullscreenImages).forEach(function(fullscreenImage) {
// fullscreenImage.addEventListener('click', showFullscreenImage);
// });
2023-11-01 02:32:48 +05:00
}
document.addEventListener("DOMContentLoaded", ready);
2023-12-10 01:21:40 +05:00
async function fetchData() {
2023-12-10 01:39:44 +05:00
let log = document.getElementById('log');
2023-12-10 01:21:40 +05:00
var serverAddress = "techhub.social";
const response = await fetch(`https://${serverAddress}/api/v1/accounts/111518136987654656/statuses`);
const data = await response.json();
data.forEach(obj => {
Object.entries(obj).forEach(([key, value]) => {
if (key == "id") {
2023-12-10 01:58:35 +05:00
log.innerHTML = log.innerHTML + `<iframe src="https://${serverAddress}/@Melawy/${value}/embed" class="mastodon-embed" style="max-width: 100%; min-height: 75vh; border: 0" width="60%" height="100%" allowfullscreen="allowfullscreen"></iframe>` + '<br>';
2023-12-10 01:21:40 +05:00
}
});
log.innerHTML = log.innerHTML + '<br>';
});
}
2023-11-02 01:08:03 +05:00
2023-11-01 02:32:48 +05:00
// Загрузка HTML из файла при загрузке URL
async function bodyFetch(url) {
if (!url) {
2023-12-10 01:26:41 +05:00
var tplUrl = 'news.tpl.html';
2023-11-01 02:32:48 +05:00
} else {
2023-12-10 01:26:41 +05:00
var tplUrl = url + ".tpl.html";
2023-11-01 02:32:48 +05:00
}
let response = await fetch(tplUrl);
let body = document.getElementById('body');
2023-12-10 01:46:27 +05:00
if (tplUrl == 'news.tpl.html') {fetchData();}
2023-12-10 01:37:11 +05:00
2023-11-01 02:32:48 +05:00
if (response.ok) { // если HTTP-статус в диапазоне 200-299
// получаем тело ответа (см. про этот метод ниже)
let text = await response.text();
body.innerHTML = text;
2023-10-31 14:50:36 +05:00
} else {
2023-11-01 02:32:48 +05:00
body.innerHTML = "Ой всё сломалось :(";
2023-10-31 14:50:36 +05:00
}
2023-11-01 02:32:48 +05:00
}
let query = window.location.pathname;
switch (query) {
2023-12-10 01:21:40 +05:00
case "/news":
bodyFetch(query);
break;
2023-11-03 13:58:43 +05:00
2023-12-10 01:21:40 +05:00
case "/overview":
bodyFetch(query);
break;
2023-11-01 02:32:48 +05:00
2023-12-10 01:21:40 +05:00
case "/download":
bodyFetch(query);
break;
2023-11-01 02:32:48 +05:00
2023-12-10 01:21:40 +05:00
case "/donate":
bodyFetch(query);
break;
2023-11-01 02:32:48 +05:00
2023-12-10 01:21:40 +05:00
case "/cooperation":
bodyFetch(query);
break;
2023-11-01 02:32:48 +05:00
2023-12-10 01:21:40 +05:00
case "/about":
bodyFetch(query);
break;
2023-11-01 02:32:48 +05:00
2023-12-10 01:21:40 +05:00
default:
bodyFetch();
2023-11-01 02:32:48 +05:00
}
// Загрузка HTML из файла при ПЕРЕХОДЕ ПО ССЫЛКЕ
let elements = document.getElementsByClassName("menu");
2023-11-02 01:08:03 +05:00
let fetchPage = async function(evt) {
2023-11-01 02:32:48 +05:00
evt.preventDefault();
const controller = new AbortController();
const signal = controller.signal;
// Cancel the fetch request in 500ms
setTimeout(() => controller.abort(), 500);
var body = document.getElementById('body');
let text;
const url = this.getAttribute("href");
const tplUrl = url + '.tpl.html';
const linkText = this.text.trim();
const title = document.title + ": " + linkText;
2023-12-10 01:46:27 +05:00
if (tplUrl == 'news.tpl.html') {fetchData();}
2023-12-10 01:37:11 +05:00
2023-11-01 02:32:48 +05:00
try {
const response = await fetch(tplUrl, { signal });
text = await response.text();
2023-12-10 01:37:11 +05:00
2023-11-01 02:32:48 +05:00
} catch (error) {
// DOMException: The user aborted a request.
console.log('Error: ', error)
body.innerHTML = "<p>Ой всё сломалось :(</p>";
}
if (text) {
const stateObj = {"html":text,"pageTitle":title};
// window.history.replaceState({}, title, url);
// window.history.replaceState(null, title, url)
// window.history.replaceState(stateObj, title, url)
// window.history.pushState({}, title, url);
window.history.pushState(stateObj, title, url);
2023-10-31 14:50:36 +05:00
2023-11-01 02:32:48 +05:00
body.innerHTML = text;
} else {
body.innerHTML = "<p>Ой всё сломалось :(</p>";
}
// let attribute = this.getAttribute("href");
// let url = attribute + '.tpl.html';
// let response = await fetch(url);
// let body = document.getElementById('body');
// let status = response.status
// isSuccess = status >= 200 && status < 300 || status === 304;
//
// if (isSuccess) { // если HTTP-статус в диапазоне 200-299
// // получаем тело ответа (см. про этот метод ниже)
// let text = await response.text();
// body.innerHTML = text;
// } else {
// body.innerHTML = "<p>Ой всё сломалось :(</p>";
// }
};
Array.from(elements).forEach(function(element) {
2023-11-02 01:08:03 +05:00
element.addEventListener('click', fetchPage);
2023-11-01 02:32:48 +05:00
});
// АКТИВАЦИЯ ПОДСКАЗОК
2023-10-31 14:50:36 +05:00
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
2023-11-01 02:32:48 +05:00
// ПЕРЕХОД НАВЕРХ СТРАНИЦЫ
2023-10-31 14:50:36 +05:00
//Get the button
let mybutton = document.getElementById("btn-back-to-top");
// When the user scrolls down 20px from the top of the document, show the button
window.onscroll = function () {
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
mybutton.style.display = "block";
} else {
mybutton.style.display = "none";
}
}
// When the user clicks on the button, scroll to the top of the document
mybutton.addEventListener("click", backToTop);
function backToTop() {
document.body.scrollTop = 0;
document.documentElement.scrollTop = 0;
}
2023-11-01 02:32:48 +05:00
/*!
* Color mode toggler for Bootstrap's docs (https://getbootstrap.com/)
* Copyright 2011-2023 The Bootstrap Authors
* Licensed under the Creative Commons Attribution 3.0 Unported License.
*/
(() => {
'use strict'
const getStoredTheme = () => localStorage.getItem('theme')
const setStoredTheme = theme => localStorage.setItem('theme', theme)
const getPreferredTheme = () => {
const storedTheme = getStoredTheme()
if (storedTheme) {
return storedTheme
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
}
const setTheme = theme => {
if (theme === 'auto' && window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.setAttribute('data-bs-theme', 'dark')
} else {
document.documentElement.setAttribute('data-bs-theme', theme)
}
}
setTheme(getPreferredTheme())
const showActiveTheme = (theme, focus = false) => {
const themeSwitcher = document.querySelector('#bd-theme')
if (!themeSwitcher) {
return
}
const themeSwitcherText = document.querySelector('#bd-theme-text')
const activeThemeIcon = document.querySelector('.theme-icon-active use')
const btnToActive = document.querySelector(`[data-bs-theme-value="${theme}"]`)
const svgOfActiveBtn = btnToActive.querySelector('svg use').getAttribute('href')
document.querySelectorAll('[data-bs-theme-value]').forEach(element => {
element.classList.remove('active')
element.setAttribute('aria-pressed', 'false')
})
btnToActive.classList.add('active')
btnToActive.setAttribute('aria-pressed', 'true')
activeThemeIcon.setAttribute('href', svgOfActiveBtn)
const themeSwitcherLabel = `${themeSwitcherText.textContent} (${btnToActive.dataset.bsThemeValue})`
themeSwitcher.setAttribute('aria-label', themeSwitcherLabel)
if (focus) {
themeSwitcher.focus()
}
}
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', () => {
const storedTheme = getStoredTheme()
if (storedTheme !== 'light' && storedTheme !== 'dark') {
setTheme(getPreferredTheme())
}
})
window.addEventListener('DOMContentLoaded', () => {
showActiveTheme(getPreferredTheme())
document.querySelectorAll('[data-bs-theme-value]')
.forEach(toggle => {
toggle.addEventListener('click', () => {
const theme = toggle.getAttribute('data-bs-theme-value')
setStoredTheme(theme)
setTheme(theme)
showActiveTheme(theme, true)
console.log(theme)
})
})
})
})()