burguillos.info/js-src/index.js

203 lines
6.1 KiB
JavaScript
Raw Normal View History

2023-08-21 00:58:11 +02:00
"use strict";
import Tablesort from 'tablesort';
window.Tablesort = require('tablesort');
require('tablesort/src/sorts/tablesort.number');
window.onload = () => {
const menu_expand = document.querySelector('a.menu-expand');
const mobile_foldable = document.querySelector('nav.mobile-foldable');
const tables = document.querySelectorAll('table')
loadAd()
addEasterEggAnimation()
2023-08-21 00:58:11 +02:00
if (menu_expand !== null && mobile_foldable !== null) {
menu_expand.addEventListener('click', () => {
mobile_foldable.classList.toggle('show');
});
}
for (const table of tables) {
const header = table.querySelector('tr');
if (header !== null) {
header.setAttribute('data-sort-method', 'none')
for (const th of header.querySelectorAll('th')) {
if (th.getAttribute('data-sort-method') == null) {
th.setAttribute('data-sort-method', 'thead')
}
}
}
new Tablesort(table)
}
2023-08-29 23:52:07 +02:00
if (window !== undefined && window.Android !== undefined) {
executeAndroidExclusiveCode(Android)
}
2023-09-04 01:23:27 +02:00
addListenersSearch()
2023-08-21 00:58:11 +02:00
};
2023-09-04 01:23:27 +02:00
function addListenersSearch() {
const searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
if (searchMobile !== null) {
searchMobile.addEventListener('click', onFakeSearchClick);
}
const exitSearch = document.querySelector('a.exit-search')
if (exitSearch !== null) {
2023-09-04 01:36:15 +02:00
exitSearch.addEventListener('click', onExitSearch)
}
}
function onExitSearch() {
const searchOverlay = document.querySelector('div.search-overlay');
if (searchOverlay !== null) {
searchOverlay.classList.toggle('active');
2023-09-04 01:23:27 +02:00
}
}
function onFakeSearchClick(e) {
e.preventDefault();
const searchOverlay = document.querySelector('div.search-overlay');
if (searchOverlay === null) {
return
}
searchOverlay.classList.toggle('active');
const search = searchOverlay.querySelector('div.search input');
if (search !== null) {
search.focus()
}
return false;
}
2023-08-29 23:52:07 +02:00
function absoluteToHost(imageUrl) {
if (imageUrl.match(/^\//)) {
imageUrl = window.location.protocol + "//" + window.location.host + imageUrl
}
return imageUrl.replace(/\?.*$/, '');
}
2023-08-30 23:46:49 +02:00
function addListenerOpenInBrowserButton(android) {
const openInBrowserLink = document.querySelector('a.open-in-browser')
if (openInBrowserLink === null) {
return
}
openInBrowserLink.addEventListener('click', () => {
android.openInBrowser(window.location.href)
})
}
2023-08-29 23:52:07 +02:00
function executeAndroidExclusiveCode(android) {
document.querySelectorAll('*.android').forEach((element) => {
element.classList.remove('android')
})
2023-08-30 23:46:49 +02:00
addListenerOpenInBrowserButton(android)
2023-08-29 23:52:07 +02:00
const pinToHomeUrl = document.querySelector('a.pin-to-home')
if (pinToHomeUrl === null) {
return;
}
pinToHomeUrl.addEventListener('click', () => {
const url = new URL(window.location.href)
const pathandQuery = url.pathname + url.search;
2023-08-30 01:20:23 +02:00
const label = (url.pathname.replace(/^.*\//g, '')
.replace(/(?:^|-)\w/g, function(character) {
2023-08-29 23:52:07 +02:00
return character.toUpperCase()
2023-08-30 01:20:23 +02:00
})
.replace(/-/g, ' ')) + ' - Burguillos.info';
2023-08-29 23:52:07 +02:00
const firstImg = document.querySelector('div.description img');
let iconUrl;
if (firstImg !== null) {
if (!firstImg.src.match(/\.svg(?:\?|$)/)) {
iconUrl = absoluteToHost(firstImg.src);
}
}
if (iconUrl === undefined) {
const imagePreview = document.querySelector('meta[name="image"]');
iconUrl = absoluteToHost(imagePreview.content);
}
android.pinPage(pathandQuery, label, iconUrl)
})
}
function addEasterEggAnimation() {
const logoContainer = document.querySelector('div.burguillos-logo-container')
if (logoContainer === null) {
return;
}
logoContainer.addEventListener('click', () => {
logoContainer.classList.toggle('active')
})
}
2023-08-21 00:58:11 +02:00
let current_ad_number = null
function expand_page_contents() {
const page_contents = document.querySelector('div.page-contents');
if (page_contents === null) {
return;
}
page_contents.classList.add('no-carousel');
}
function no_more_ads() {
const carousel = document.querySelector('.carousel');
if (carousel !== null) {
carousel.remove();
}
expand_page_contents();
}
2023-08-21 00:58:11 +02:00
function loadAd() {
const params = new URLSearchParams();
if (current_ad_number !== null) {
params.append('n', ""+current_ad_number);
}
fetch('/next-ad.json?' + params).then((res) => {
return res.json()
}).then((res) => {
current_ad_number = res.current_ad_number
const ad = res.ad
const must_continue = res.continue
const carousel = document.querySelector('.carousel');
if (must_continue === 0
|| carousel === null
|| carousel.offsetWidth === 0) {
no_more_ads();
2023-08-21 00:58:11 +02:00
return;
}
const a = _retrieveLinkCarousel(carousel)
a.innerHTML = ""
2023-08-21 00:58:11 +02:00
const image = document.createElement('img')
const text_container = document.createElement('div')
2023-08-21 14:10:18 +02:00
const text = document.createElement('h4')
2023-08-21 00:58:11 +02:00
const promoted = document.createElement('p')
promoted.classList.add('promoted-tag')
promoted.innerText = "Promocionado"
image.src = ad.img
2023-08-26 15:46:33 +02:00
image.alt = ""
2023-08-21 00:58:11 +02:00
text.innerText = ad.text
a.href = ad.href
a.append(image)
text_container.append(promoted)
text_container.append(text)
a.append(text_container)
window.setTimeout(() => {
loadAd()
}, ad.seconds * 1000)
}).catch(() => {
window.setTimeout(() => {
loadAd()
}, 1000)
});
2023-08-21 00:58:11 +02:00
}
function _retrieveLinkCarousel(carousel) {
const maybeA = carousel.querySelector('a')
if (maybeA !== null) {
return maybeA
}
const a = document.createElement('a')
carousel.innerHTML = ""
carousel.append(a)
return a
}