diff options
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r-- | src/dumbyUtils.mjs | 86 |
1 files changed, 50 insertions, 36 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs index c878e48..d30145c 100644 --- a/src/dumbyUtils.mjs +++ b/src/dumbyUtils.mjs | |||
@@ -1,58 +1,72 @@ | |||
1 | export function focusNextMap(reverse = false) { | 1 | export function focusNextMap(reverse = false) { |
2 | const renderedList = Array.from(this.htmlHolder.querySelectorAll('[data-render=fulfilled]')) | 2 | const renderedList = Array.from( |
3 | const mapNum = renderedList.length | 3 | this.htmlHolder.querySelectorAll("[data-render=fulfilled]"), |
4 | if (mapNum === 0) return | 4 | ); |
5 | const mapNum = renderedList.length; | ||
6 | if (mapNum === 0) return; | ||
5 | 7 | ||
6 | // Get current focused map element | 8 | // Get current focused map element |
7 | const currentFocus = this.container.querySelector('.mapclay.focus') | 9 | const currentFocus = this.container.querySelector(".mapclay.focus"); |
8 | 10 | ||
9 | // Remove class name of focus for ALL candidates | 11 | // Remove class name of focus for ALL candidates |
10 | // This may trigger animation | 12 | // This may trigger animation |
11 | renderedList.forEach(ele => ele.classList.remove('focus')) | 13 | renderedList.forEach(ele => ele.classList.remove("focus")); |
12 | 14 | ||
13 | // Get next existing map element | 15 | // Get next existing map element |
14 | const padding = reverse ? -1 : 1 | 16 | const padding = reverse ? -1 : 1; |
15 | let nextIndex = currentFocus ? renderedList.indexOf(currentFocus) + padding : 0 | 17 | let nextIndex = currentFocus |
16 | nextIndex = (nextIndex + mapNum) % mapNum | 18 | ? renderedList.indexOf(currentFocus) + padding |
17 | const nextFocus = renderedList[nextIndex] | 19 | : 0; |
18 | nextFocus.classList.add("focus") | 20 | nextIndex = (nextIndex + mapNum) % mapNum; |
21 | const nextFocus = renderedList[nextIndex]; | ||
22 | nextFocus.classList.add("focus"); | ||
19 | 23 | ||
20 | return nextFocus | 24 | return nextFocus; |
21 | } | 25 | } |
22 | 26 | ||
23 | export function focusDelay() { | 27 | export function focusDelay() { |
24 | return window.getComputedStyle(this.showcase).display === 'none' ? 50 : 300 | 28 | return window.getComputedStyle(this.showcase).display === "none" ? 50 : 300; |
25 | } | 29 | } |
26 | 30 | ||
27 | export function switchToNextLayout(reverse = false) { | 31 | export function switchToNextLayout(reverse = false) { |
28 | const layouts = this.layouts | 32 | const layouts = this.layouts; |
29 | const currentLayoutName = this.container.getAttribute('data-layout') | 33 | const currentLayoutName = this.container.getAttribute("data-layout"); |
30 | const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName) | 34 | const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName); |
31 | const padding = reverse ? -1 : 1 | 35 | const padding = reverse ? -1 : 1; |
32 | const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + layouts.length) % layouts.length | 36 | const nextIndex = |
33 | const nextLayout = layouts[nextIndex] | 37 | currentIndex === -1 |
34 | this.container.setAttribute("data-layout", nextLayout.name) | 38 | ? 0 |
39 | : (currentIndex + padding + layouts.length) % layouts.length; | ||
40 | const nextLayout = layouts[nextIndex]; | ||
41 | this.container.setAttribute("data-layout", nextLayout.name); | ||
35 | } | 42 | } |
36 | 43 | ||
37 | export function focusNextBlock(reverse = false) { | 44 | export function focusNextBlock(reverse = false) { |
38 | const blocks = this.blocks.filter(b => b.checkVisibility({ | 45 | const blocks = this.blocks.filter(b => |
39 | contentVisibilityAuto: true, | 46 | b.checkVisibility({ |
40 | opacityProperty: true, | 47 | contentVisibilityAuto: true, |
41 | visibilityProperty: true, | 48 | opacityProperty: true, |
42 | })) | 49 | visibilityProperty: true, |
43 | const currentBlock = blocks.find(b => b.classList.contains('focus')) | 50 | }), |
44 | const currentIndex = blocks.indexOf(currentBlock) | 51 | ); |
45 | const padding = reverse ? -1 : 1 | 52 | const currentBlock = blocks.find(b => b.classList.contains("focus")); |
46 | const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + blocks.length) % blocks.length | 53 | const currentIndex = blocks.indexOf(currentBlock); |
47 | const nextBlock = blocks[nextIndex] | 54 | const padding = reverse ? -1 : 1; |
48 | blocks.forEach(b => b.classList.remove('focus')) | 55 | const nextIndex = |
49 | nextBlock?.classList?.add('focus') | 56 | currentIndex === -1 |
50 | const scrollBlock = nextBlock.getBoundingClientRect().height > nextBlock.parentElement.getBoundingClientRect().height * 0.8 | 57 | ? 0 |
51 | ? 'nearest' | 58 | : (currentIndex + padding + blocks.length) % blocks.length; |
52 | : 'center' | 59 | const nextBlock = blocks[nextIndex]; |
53 | nextBlock.scrollIntoView({ behavior: 'smooth', block: scrollBlock }) | 60 | blocks.forEach(b => b.classList.remove("focus")); |
61 | nextBlock?.classList?.add("focus"); | ||
62 | const scrollBlock = | ||
63 | nextBlock.getBoundingClientRect().height > | ||
64 | nextBlock.parentElement.getBoundingClientRect().height * 0.8 | ||
65 | ? "nearest" | ||
66 | : "center"; | ||
67 | nextBlock.scrollIntoView({ behavior: "smooth", block: scrollBlock }); | ||
54 | } | 68 | } |
55 | 69 | ||
56 | export function removeBlockFocus() { | 70 | export function removeBlockFocus() { |
57 | this.blocks.forEach(b => b.classList.remove('focus')) | 71 | this.blocks.forEach(b => b.classList.remove("focus")); |
58 | } | 72 | } |