Spaces:
Running
Running
File size: 5,716 Bytes
64aaca8 |
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 |
// Event listener helper function
function addEvent(element, event, handler) {
if (element?.attachEvent) {
return element.attachEvent('on' + event, handler);
}
return element?.addEventListener(event, handler, false);
}
// DOM element getters
const getElement = id => document.getElementById(id);
const getElements = selector => document.querySelectorAll(selector);
// Logotype width calculation
function recalcLogotypeWidth() {
const logotype = getElement('logotype');
const logotypeText = getElement('logotype__text');
logotype?.setAttribute('width', `${logotypeText.getBoundingClientRect().width}px`);
}
// Viewport adaptation
function adaptViewport() {
// fix logotype when font loading delayed
document.fonts.ready.then(() => {
recalcLogotypeWidth();
});
if (window.innerWidth < 640) {
getElement('has-search')?.setAttribute('open', 'open');
getElement('has-search')?.removeAttribute('name');
getElement('has-more-menu')?.setAttribute('open', 'open');
} else {
getElement('top-nav')?.setAttribute('open', 'open')
addEvent(visualViewport, 'resize', adaptViewport);
}
}
addEvent(window, 'DOMContentLoaded', adaptViewport);
// Node collapse handlers
const collapseParentNode = getElements('.js-cpn');
const collapseGrandParentNode = getElements('.js-cgpn');
const detailsElements = getElements('details.js-details');
collapseParentNode.forEach(element => {
const handler = () => element.parentNode?.removeAttribute('open');
addEvent(element, 'click', handler);
});
collapseGrandParentNode.forEach(element => {
const handler = () => element.parentNode?.parentNode?.removeAttribute('open');
addEvent(element, 'click', handler);
});
if (window.innerWidth > 640) {
// Details element handler for firefox based browsers which do not respect the same name attribute
detailsElements.forEach(detail => {
const handler = (e) => {
const name = detail.getAttribute('name');
if (name) {
getElements(`details.js-details[name="${name}"]`).forEach(otherDetail => {
if (otherDetail !== detail && otherDetail.hasAttribute('open')) {
otherDetail.removeAttribute('open');
}
});
}
};
addEvent(detail, 'click', handler);
});
}
// Share functionality
if (typeof navigatorShare !== 'undefined') {
getElement('navigatorShare')?.setAttribute(
'href',
'javascript:navigator.share({title: document.title, url: window.location.href})'
);
if (location.protocol === 'https:') {
getElement('copyPermalink')?.removeAttribute('class');
}
}
getElement('print-button')?.removeAttribute('class');
getElement('back')?.removeAttribute('class');
// Date handling
const date = new Date();
// Mastodon and QR code functionality
if (typeof mastodonInstance !== 'undefined') {
getElement('has-mastodon').className = 'active';
const mastodonHandler = () => {
mastodonTitle.disabled = true;
mastodonPermalink.disabled = true;
mastodonText.disabled = false;
mastodon?.setAttribute('action', `${mastodonInstance.value}/share`);
};
addEvent(mastodonInstance, 'input', mastodonHandler);
if (typeof QRCode !== 'undefined') {
getElement('colophon').removeAttribute('style');
qr?.appendChild(
QRCode({
msg: window.location.href,
ecl: 'M',
pal: ['#000', '#fff'],
pad: 2,
dim: 96,
})
);
const isoTime = date.toISOString();
const timeStamp = getElement('time-stamp');
timeStamp.innerHTML = isoTime;
timeStamp?.setAttribute('datetime', isoTime);
}
}
// Digital well-being clock
const hour = date.getHours();
const isDaytime = hour > 6 && hour < 21;
function toggleNightElements(hidden) {
const elements = ['grain', 'dwclock'];
elements.forEach(id => {
const element = getElement(id);
element?.[hidden ? 'setAttribute' : 'removeAttribute']('hidden', 'hidden');
});
}
if (isDaytime) {
toggleNightElements(true);
} else {
toggleNightElements(false);
let clockInterval;
function updateClock() {
const minutes = date.getMinutes();
const seconds = date.getSeconds();
const minutesDegrees = ((minutes / 60) * 360) + ((seconds/60)*6);
const hourDegrees = ((hour / 12) * 360) + ((minutes/60)*30);
const transforms = ['transform', 'webkitTransform', 'mozTransform', 'msTransform', 'oTransform'];
const hands = {
'#min': minutesDegrees,
'#hour': hourDegrees
};
Object.entries(hands).forEach(([selector, degrees]) => {
const hand = document.querySelector(selector);
transforms.forEach(transform => {
hand.style[transform] = `rotate(${degrees}deg)`;
});
});
}
updateClock();
clockInterval = setInterval(updateClock, 10000);
}
// Overwrite browser search bar (canceled)
// addEvent(document, 'keydown', function(e) {
// if (e.ctrlKey && e.key === 'k') {
// e.preventDefault();
// getElement('has-search')?.setAttribute('open', 'open');
// const searchInput = document.querySelector('.pagefind-ui__search-input');
// searchInput?.focus();
// }
// });
// clashes with details handler, need workaround
addEvent(window, 'beforeprint', function() {
getElements('[name="redaction-history"]')?.forEach(e => {
e.setAttribute('open', 'open');
});
});
|