burguillos.info/js-src/index.js

439 lines
15 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');
2023-09-04 17:32:27 +02:00
let fakeSearchInput
let searchMobile
2023-09-28 21:40:18 +02:00
document.addEventListener("DOMContentLoaded", function () {
2023-08-21 00:58:11 +02:00
const menu_expand = document.querySelector('a.menu-expand');
const mobile_foldable = document.querySelector('nav.mobile-foldable');
2023-09-28 16:32:33 +02:00
const transparentFullscreenHide = document.querySelector('div.transparent-fullscreen-hide');
2023-09-28 21:40:18 +02:00
const contentsWithoutMenu = document.querySelector('div.contents-without-menu')
2023-08-21 00:58:11 +02:00
const tables = document.querySelectorAll('table')
2023-09-07 18:12:28 +02:00
fillFarmaciaGuardia();
2023-08-21 00:58:11 +02:00
loadAd()
addEasterEggAnimation()
2023-08-21 00:58:11 +02:00
2023-09-28 21:40:18 +02:00
if (menu_expand !== null && mobile_foldable !== null && transparentFullscreenHide !== null && contentsWithoutMenu !== null) {
mobile_foldable.toggleAttribute('aria-hidden')
if (mobile_foldable.getAttribute('aria-hidden') !== null) {
mobile_foldable.setAttribute('aria-hidden', true);
}
2023-09-28 18:08:58 +02:00
transparentFullscreenHide.addEventListener('click', () => {
mobile_foldable.classList.remove('show');
transparentFullscreenHide.classList.remove('show');
2023-09-28 19:49:41 +02:00
menu_expand.classList.remove('active');
2023-09-28 21:40:18 +02:00
contentsWithoutMenu.removeAttribute('aria-hidden')
mobile_foldable.setAttribute('aria-hidden', true)
2023-09-28 18:08:58 +02:00
});
2023-08-21 00:58:11 +02:00
menu_expand.addEventListener('click', () => {
2023-09-28 19:49:41 +02:00
menu_expand.classList.toggle('active');
2023-08-21 00:58:11 +02:00
mobile_foldable.classList.toggle('show');
2023-09-28 16:32:33 +02:00
transparentFullscreenHide.classList.toggle('show');
2023-09-28 21:40:18 +02:00
contentsWithoutMenu.toggleAttribute('aria-hidden')
if (contentsWithoutMenu.getAttribute('aria-hidden') !== null) {
contentsWithoutMenu.setAttribute('aria-hidden', true);
}
mobile_foldable.toggleAttribute('aria-hidden')
if (mobile_foldable.getAttribute('aria-hidden') !== null) {
mobile_foldable.setAttribute('aria-hidden', true);
}
2023-08-21 00:58:11 +02:00
});
}
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 17:32:27 +02:00
}
searchMobile = document.querySelector('nav.mobile-shortcuts div.search')
fakeSearchInput = searchMobile.querySelector('input')
2023-09-04 01:23:27 +02:00
addListenersSearch()
2023-09-28 21:40:18 +02:00
}, false);
2023-08-21 00:58:11 +02:00
2023-09-07 18:12:28 +02:00
function fillFarmaciaGuardia() {
const farmaciaName = document.querySelector('#farmacia-name');
const farmaciaAddress = document.querySelector('#farmacia-address');
if (farmaciaName !== null || farmaciaAddress !== null) {
const port = _port()
const url = new URL(window.location.protocol
+ "//"
+ window.location.hostname
+ port
+ '/farmacia-guardia.json');
fetch(url).then(async (res) => {
const farmacia = await res.json()
if (farmaciaName !== null) {
farmaciaName.innerText = farmacia.name;
farmaciaAddress.innerText = farmacia.address;
}
})
}
}
2023-09-04 01:23:27 +02:00
function addListenersSearch() {
2023-09-10 19:33:28 +02:00
const searchInPage = document.querySelector('div.search-in-page')
2023-09-04 01:23:27 +02:00
if (searchMobile !== null) {
2023-09-10 17:11:07 +02:00
const searchIcon = searchMobile.querySelector('a.search-icon')
searchIcon.addEventListener('click', (e) => {
const searchOverlay = document.querySelector('div.search-overlay');
const searchInput = searchOverlay.querySelector('div.search input');
searchInput.value = fakeSearchInput.value;
onSearchChange(e)
onFakeSearchClick(e)
return true;
})
2023-09-10 17:20:19 +02:00
fakeSearchInput.addEventListener('keyup', (e) => {
2023-09-10 19:33:28 +02:00
if (searchInPage === null) {
return;
}
if (fakeSearchInput.value === "") {
searchInPage.classList.remove('active')
} else {
searchInPage.classList.add('active')
}
2023-09-10 17:20:19 +02:00
if (e.keyCode !== 13) {
return false;
}
2023-09-05 19:36:17 +02:00
const searchOverlay = document.querySelector('div.search-overlay');
const searchInput = searchOverlay.querySelector('div.search input');
searchInput.value = fakeSearchInput.value;
onSearchChange(e)
2023-09-04 17:32:27 +02:00
onFakeSearchClick(e)
return true;
});
}
2023-09-10 19:33:28 +02:00
const nextResult = searchInPage.querySelector('a.down');
const prevResult = searchInPage.querySelector('a.up');
if (nextResult !== null && prevResult !== null) {
nextResult.addEventListener('click', () => {
searchInWebsite(fakeSearchInput.value, true);
});
prevResult.addEventListener('click', () => {
searchInWebsite(fakeSearchInput.value, false);
2023-09-04 17:32:27 +02:00
});
2023-09-04 01:23:27 +02:00
}
const exitSearch = document.querySelector('a.exit-search')
2023-09-05 19:36:17 +02:00
const searchOverlay = document.querySelector('div.search-overlay');
const searchInput = searchOverlay.querySelector('div.search input');
fakeSearchInput.value = searchInput.value;
exitSearch.addEventListener('click', onExitSearch)
const search = document.querySelector('div.search-overlay div.search input');
if (search !== null) {
search.addEventListener('change', onSearchChange);
}
2023-09-08 01:43:38 +02:00
const searchIconDesktop = document.querySelector('nav.desktop a.search-icon');
if (searchIconDesktop !== null) {
searchIconDesktop.addEventListener('click', (e) => {
onFakeSearchClick(e)
})
}
}
function searchInWebsite(value, isToBottom) {
2023-09-10 16:56:20 +02:00
window.find(value, false, !isToBottom, true)
const selection = window.getSelection()
2023-09-11 02:27:30 +02:00
if (selection.anchorNode === null) {
const pageContents = document.querySelector('div.page-contents');
pageContents.focus()
searchInWebsite(value, isToBottom)
}
2023-09-10 16:56:20 +02:00
const anchorNode = selection.anchorNode.parentNode
2023-09-11 02:27:30 +02:00
if (anchorNode.tagName !== null
&& anchorNode.tagName === "INPUT") {
const pageContents = document.querySelector('div.page-contents');
pageContents.focus()
searchInWebsite(value, isToBottom)
}
2023-09-10 16:56:20 +02:00
if (anchorNode !== null) {
2023-09-10 19:47:00 +02:00
const pageContents = document.querySelector('div.page-contents');
2023-09-10 21:22:23 +02:00
const offsetTop = _getOffsetTopWithNParent(anchorNode, pageContents);
pageContents.scroll(0, offsetTop - 150)
2023-09-10 16:56:20 +02:00
}
}
2023-09-10 21:22:23 +02:00
function _getOffsetTopWithNParent(element, nParent, _carry = 0) {
if (element === null) {
return null;
}
if (element === nParent) {
return _carry;
}
_carry += element.offsetTop
return _getOffsetTopWithNParent(element.offsetParent, nParent, _carry)
}
2023-09-07 18:12:28 +02:00
function _port() {
let port = window.location.port;
if (port !== '') {
port = ':' + port
}
return port;
}
function onSearchChange() {
const search = document.querySelector('div.search-overlay div.search input');
const searchResults = document.querySelector('div.search-overlay div.search-results');
if (search === null || searchResults === null) {
return;
}
const query = search.value;
2023-09-04 17:32:27 +02:00
fakeSearchInput.value = search.value
2023-09-07 18:12:28 +02:00
const port = _port()
const url = new URL(window.location.protocol
+ "//"
+ window.location.hostname
2023-09-05 16:42:58 +02:00
+ port
+ '/search.json');
url.searchParams.set('q', query);
fetch(url).then(async (res) => {
const json = await res.json()
if (!json.ok) {
noResults(searchResults);
return
}
console.log(json.searchObjects.length)
if (json.searchObjects.length < 1) {
noResults(searchResults);
return;
}
showResults(searchResults, json.searchObjects);
})
search.focus()
}
function showResults(searchResults, searchObjects) {
2023-09-04 15:49:54 +02:00
searchResults.innerHTML = "";
for (let searchObject of searchObjects) {
const searchResultContainer = document.createElement('div')
searchResultContainer.classList.add('search-result')
2023-09-08 03:03:52 +02:00
const rowTitleUrlImageDiv = document.createElement('div');
rowTitleUrlImageDiv.classList.add('row-title-url-image');
const columnTitleUrl = document.createElement('div');
columnTitleUrl.classList.add('column-title-url');
const img = document.createElement('img')
const title = document.createElement('b')
const url = document.createElement('a')
const content = document.createElement('p')
title.innerText = searchObject.title
2023-09-05 16:42:58 +02:00
let port = window.location.port;
if (port !== '') {
port = ':' + port
}
if (searchObject.url.match(/^\//)) {
searchObject.url = window.location.protocol
+ "//" + window.location.hostname
2023-09-05 16:42:58 +02:00
+ port
+ searchObject.url
}
2023-09-08 03:03:52 +02:00
let urlImage = searchObject.urlImage;
if (urlImage !== null && urlImage.match(/^\//)) {
urlImage = window.location.protocol
+ "//" + window.location.hostname
+ port
+ urlImage
}
if (urlImage !== null) {
img.alt = ""
img.src = urlImage
}
url.href = searchObject.url
url.innerText = searchObject.url
content.innerText = searchObject.content
2023-09-08 03:03:52 +02:00
if (urlImage !== null) {
rowTitleUrlImageDiv.appendChild(img)
}
columnTitleUrl.appendChild(title);
columnTitleUrl.appendChild(document.createElement('br'))
columnTitleUrl.appendChild(url)
rowTitleUrlImageDiv.appendChild(columnTitleUrl)
searchResultContainer.appendChild(rowTitleUrlImageDiv)
searchResultContainer.appendChild(content)
searchResults.appendChild(searchResultContainer)
}
}
function noResults(searchResults) {
searchResults.innerHTML = ""
const p = document.createElement('p')
p.innerText = 'No se han encontrado resultados.'
searchResults.appendChild(p)
2023-09-04 01:36:15 +02:00
}
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')
})
document.querySelectorAll('*.no-android-app').forEach((element) => {
element.style.display = 'none';
})
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() {
2023-09-10 19:47:00 +02:00
const pageContents = document.querySelector('div.page-contents');
if (pageContents === null) {
return;
}
2023-09-10 19:47:00 +02:00
pageContents.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
}