diff options
author | (quasar) nebula <qznebula@protonmail.com> | 2024-07-25 16:35:02 -0300 |
---|---|---|
committer | (quasar) nebula <qznebula@protonmail.com> | 2025-04-13 22:54:14 -0300 |
commit | 48dde4a388fd4c31dd5680f7535419874124e554 (patch) | |
tree | 50c0fce8ef19f3bae856e79b1dc92e257e0db4ab /src/static/js/client/lyrics-switcher.js | |
parent | b1ff1444c47f6bd8c532e3a76eb2a5b92ed82a0e (diff) |
wip
Diffstat (limited to 'src/static/js/client/lyrics-switcher.js')
-rw-r--r-- | src/static/js/client/lyrics-switcher.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/static/js/client/lyrics-switcher.js b/src/static/js/client/lyrics-switcher.js new file mode 100644 index 00000000..b350ea50 --- /dev/null +++ b/src/static/js/client/lyrics-switcher.js @@ -0,0 +1,70 @@ +/* eslint-env browser */ + +import {stitchArrays} from '../../shared-util/sugar.js'; + +import {cssProp} from '../client-util.js'; + +export const info = { + id: 'lyricsSwitcherInfo', + + entries: null, + switchLinks: null, + currentLinks: null, +}; + +export function getPageReferences() { + const content = document.getElementById('content'); + + if (!content) return; + + const switcher = content.querySelector('.lyrics-switcher'); + + if (!switcher) return; + + info.entries = + Array.from(content.querySelectorAll('.lyrics-entry')); + + info.currentLinks = + Array.from(switcher.querySelectorAll('a.current')); + + info.switchLinks = + Array.from(switcher.querySelectorAll('a:not(.current)')); +} + +export function addPageListeners() { + if (!info.switchLinks) return; + + for (const {switchLink, entry} of stitchArrays({ + switchLink: info.switchLinks, + entry: info.entries, + })) { + switchLink.addEventListener('click', domEvent => { + domEvent.preventDefault(); + showLyricsEntry(entry); + }); + } +} + +function showLyricsEntry(entry) { + const entryToShow = entry; + + stitchArrays({ + entry: info.entries, + currentLink: info.currentLinks, + switchLink: info.switchLinks, + }).forEach(({ + entry, + currentLink, + switchLink, + }) => { + if (entry === entryToShow) { + cssProp(entry, 'display', null); + cssProp(currentLink, 'display', null); + cssProp(switchLink, 'display', 'none'); + } else { + cssProp(entry, 'display', 'none'); + cssProp(currentLink, 'display', 'none'); + cssProp(switchLink, 'display', null); + } + }); +} |