aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/dumbyUtils.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'src/dumbyUtils.mjs')
-rw-r--r--src/dumbyUtils.mjs57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/dumbyUtils.mjs b/src/dumbyUtils.mjs
new file mode 100644
index 0000000..ba0bbf8
--- /dev/null
+++ b/src/dumbyUtils.mjs
@@ -0,0 +1,57 @@
1export function focusNextMap(reverse = false) {
2 const renderedList = this.renderMaps
3 .map(render => render.target)
4 .filter(ele => ele.getAttribute('data-state') === 'rendered')
5 const mapNum = renderedList.length
6 if (mapNum === 0) return
7
8 // Get current focused map element
9 const currentFocus = this.container.querySelector('.map-container.focus')
10
11 // Remove class name of focus for ALL candidates
12 // This may trigger animation
13 renderedList.forEach(ele => ele.classList.remove('focus'))
14
15 // Get next existing map element
16 const padding = reverse ? -1 : 1
17 let nextIndex = currentFocus ? renderedList.indexOf(currentFocus) + padding : 0
18 nextIndex = (nextIndex + mapNum) % mapNum
19 const nextFocus = renderedList[nextIndex]
20 nextFocus.classList.add("focus")
21
22 return nextFocus
23}
24
25export function focusDelay() {
26 return window.getComputedStyle(this.showcase).display === 'none' ? 50 : 300
27}
28
29export function switchToNextLayout(reverse = false) {
30 const layouts = this.layouts
31 const currentLayoutName = this.container.getAttribute('data-layout')
32 const currentIndex = layouts.map(l => l.name).indexOf(currentLayoutName)
33 const padding = reverse ? -1 : 1
34 const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + layouts.length) % layouts.length
35 const nextLayout = layouts[nextIndex]
36 this.container.setAttribute("data-layout", nextLayout.name)
37}
38
39export function focusNextBlock(reverse = false) {
40 const blocks = this.blocks.filter(b => b.checkVisibility({
41 contentVisibilityAuto: true,
42 opacityProperty: true,
43 visibilityProperty: true,
44 }))
45 const currentBlock = blocks.find(b => b.classList.contains('focus'))
46 const currentIndex = blocks.indexOf(currentBlock)
47 const padding = reverse ? -1 : 1
48 const nextIndex = currentIndex === -1 ? 0 : (currentIndex + padding + blocks.length) % blocks.length
49 const nextBlock = blocks[nextIndex]
50 blocks.forEach(b => b.classList.remove('focus'))
51 nextBlock?.classList?.add('focus')
52 nextBlock.scrollIntoView({ behavior: 'smooth', block: "nearest" })
53}
54
55export function removeBlockFocus() {
56 this.blocks.forEach(b => b.classList.remove('focus'))
57}