aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dumbyUtils.mjs
blob: cfac00b91a5f9f3430514802312f0f75e713acc5 (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
71
72
export function focusNextMap(reverse = false) {
  const renderedList = Array.from(
    this.htmlHolder.querySelectorAll('[data-render=fulfilled]'),
  );
  const mapNum = renderedList.length;
  if (mapNum === 0) return;

  // Get current focused map element
  const currentFocus = this.container.querySelector('.mapclay.focus');

  // Remove class name of focus for ALL candidates
  // This may trigger animation
  renderedList.forEach(ele => ele.classList.remove('focus'));

  // Get next existing map element
  const padding = reverse ? -1 : 1;
  let nextIndex = currentFocus
    ? renderedList.indexOf(currentFocus) + padding
    : 0;
  nextIndex = (nextIndex + mapNum) % mapNum;
  const nextFocus = renderedList[nextIndex];
  nextFocus.classList.add('focus');

  return nextFocus;
}

export function focusDelay() {
  return window.getComputedStyle(this.showcase).display === 'none' ? 50 : 300;
}

export function switchToNextLayout(reverse = false) {
  const layouts = this.layouts;
  const currentLayoutName = this.container.getAttribute('data-layout');
  const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName);
  const padding = reverse ? -1 : 1;
  const nextIndex =
    currentIndex === -1
      ? 0
      : (currentIndex + padding + layouts.length) % layouts.length;
  const nextLayout = layouts[nextIndex];
  this.container.setAttribute('data-layout', nextLayout.name);
}

export function focusNextBlock(reverse = false) {
  const blocks = this.blocks.filter(b =>
    b.checkVisibility({
      contentVisibilityAuto: true,
      opacityProperty: true,
      visibilityProperty: true,
    }),
  );
  const currentBlock = blocks.find(b => b.classList.contains('focus'));
  const currentIndex = blocks.indexOf(currentBlock);
  const padding = reverse ? -1 : 1;
  const nextIndex =
    currentIndex === -1
      ? 0
      : (currentIndex + padding + blocks.length) % blocks.length;
  const nextBlock = blocks[nextIndex];
  blocks.forEach(b => b.classList.remove('focus'));
  nextBlock?.classList?.add('focus');
  const scrollBlock =
    nextBlock.getBoundingClientRect().height >
    nextBlock.parentElement.getBoundingClientRect().height * 0.8
      ? 'nearest'
      : 'center';
  nextBlock.scrollIntoView({ behavior: 'smooth', block: scrollBlock });
}

export function removeBlockFocus() {
  this.blocks.forEach(b => b.classList.remove('focus'));
}