/* Master3 custom js */ /** * @typedef {import('uikit').default} UIkit */ // @ts-check document.addEventListener('DOMContentLoaded', () => { /** * Open external links in new window by adding target */ const externalTarget = 'external' for (const link of document.links) { // Compare (alternatives: link.origin, link.host, link.href, document.baseURI) if (link.host !== window.location.host) { link.setAttribute('target', externalTarget) } } /** * Open offcanvas on top menu click */ if (true) { const $offcanvas = document.querySelector('#offcanvas-menu') const offcanvas = UIkit.offcanvas($offcanvas) document.querySelectorAll('#navbar .uk-navbar-nav > li > a').forEach($anchor => $anchor.addEventListener('click', event => { event.preventDefault() offcanvas.show() }) ) } /* // Add lazy loading to images const main = document.getElementById('main') if (main) { for (const img of Array.from(main.querySelectorAll('img'))) { if (img.loading !== 'lazy') { img.loading = 'lazy' } } } */ /** * Wrap aligned content images */ Array .from(document.querySelectorAll( [ '.mp-article-body img.uk-align-left', '.mp-article-body img.uk-align-right', // '.mp-section img.mp-image-left', // '.mp-section img.mp-image-right', ].join(', ') )) .forEach($img => { const $wrapper = document.createElement('aside') const className = $img.getAttribute('class') $img.removeAttribute('class') $wrapper.setAttribute('class', className) $wrapper.appendChild($img.cloneNode(true)) $img.replaceWith($wrapper) }) /** * Auto non-breaking space (sierotki) * Poor man's hypens implementation https://developer.mozilla.org/en-US/docs/Web/CSS/hyphens * @see https://stackoverflow.com/questions/29467319/non-breaking-space-after-a-one-letter-words * Should be implemented at backend, but then one would have to parse HTML tags * * Tweaks: * - Use textContent instead of innnerHTML as it's faster * - Replace each node once */ const nbspWords = [ "a", "A", "i", "I", "o", "O", "u", "U", "w", "W", "z", "Z", "e", "E", "aż", "Aż", "bo", "Bo", "by", "By", "iż", "Iż", "ni", "Ni", "że", "Że", "do", "Do", "ku", "Ku", "na", "Na", "od", "Od", "po", "Po", "we", "We", "za", "Za", "ze", "Ze", "hę", "Hę", "no", "No", "ot", "Ot", "aa", "Aa", "aj", "Aj", "am", "Am", "au", "Au", "ba", "Ba", "ee", "Ee", "eh", "Eh", "ej", "Ej", "fe", "Fe", "fi", "Fi", "fi", "Fi", "fu", "Fu", "ha", "Ha", "he", "He", "ho", "Ho", "ii", "Ii", "oj", "Oj", "oo", "Oo", "ot", "Ot", "oż", "Oż", "pa", "Pa", "uf", "Uf", "uu", "Uu", "co", "Co", "ki", "Ki", "se", "Se", "ja", "Ja", "ty", "Ty", "ci", "Ci", "on", "On", "mu", "Mu", "go", "Go", "oń", "Oń", "ją", "Ją", "my", "My", "wy", "Wy", "im", "Im", "je", "Je", "ma", "Ma", "mą", "Mą", "me", "Me", "ów", "Ów", "ta", "Ta", "tę", "Tę", "tą", "Tą", "to", "To", "te", "Te", "tu", "Tu", "al", "Al", "ar", "Ar", "as", "Aa", "at", "At", "bi", "Bi", "er", "Er", "es", "Es", "ew", "Ew", "ez", "Ez", "go", "Go", "id", "Id", "ił", "Ił", "iw", "Iw", "li", "Li", "ok", "Ok", "om", "Om", "op", "Op", "or", "Or", "os", "Os", "oś", "Oś", "oz", "Oz", "ód", "Ód", "ós", "Ós", "su", "Su", "ud", "Ud", "ul", "Ul", "ut", "Ut", "je", "Je", "ma", "Ma", "są", "Są", "lub", "Lub", "ale", "Ale", "czy", "Czy", "nad", "Nad", "pod", "Pod", "bez", "Bez", "nie", "Nie", "tak", "Tak", "albo", "Albo", "więc", "Więc", "lecz", "Lecz", "przez", "Przez", "niech", "Niech", "tylko", "Tylko" ]; const pElements = document.querySelectorAll('p') const nbsp = String.fromCharCode(160) for (const pElement of pElements) { for (const child of getChildNodes(pElement)) { child.textContent = nbspWords.reduce( (text, nbspWord) => text.replace( new RegExp(`(\\s${nbspWord}) `, 'g'), `$1${nbsp}` ), child.textContent ) } } /** * Get child nodes as flat array * @param {ChildNode} element * @return {ChildNode[]} */ function getChildNodes(element) { return !element.hasChildNodes() ? [element] : Array.from(element.childNodes).reduce( (children, child) => children.concat(getChildNodes(child)), [] ) } return /** * Set section background height to be same as intro image height * There was idea to set background for intro text, however component works with article text as a whole * and content plugins as well */ // Detect layout if ( document.body.classList.contains('option--com_content') && (document.body.classList.contains('view--article') || document.body.classList.contains('view--category')) ) { /** @type {HTMLElement} */ const $main = document.getElementById('main') /** @type {HTMLDivElement} */ const $itemImage = $main.querySelector('.item-image') if ($itemImage) { const backgroundHeight = $itemImage.offsetHeight $main.style.setProperty('--section-height', backgroundHeight + 'px') } else { console.error('Cannot find item image') } } // Add style display: flow-root to paragraphs that are next to item image }, { once: true })