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