« get me outta code hell

lyrics-switcher.js « client « js « static « src - hsmusic-wiki - HSMusic - static wiki software cataloguing collaborative creation
about summary refs log tree commit diff
path: root/src/static/js/client/lyrics-switcher.js
blob: b350ea50127d0da4bf66882b5d4a714205db3e7a (plain)
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
/* 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);
      }
    });
}