|
| 1 | +<script> |
| 2 | + import {createEventDispatcher} from "svelte" |
| 3 | + import {scrollTo} from "../utils" |
| 4 | +
|
| 5 | + export let title |
| 6 | + export let items |
| 7 | + export let height = 360 |
| 8 | + export let itemsShow = 3 |
| 9 | + /** |
| 10 | + * Выбранный элемент |
| 11 | + * @type {number} |
| 12 | + */ |
| 13 | + export let chosen = 0 |
| 14 | +
|
| 15 | + const dispatch = createEventDispatcher() |
| 16 | +
|
| 17 | + let itemHeight = height / itemsShow |
| 18 | +
|
| 19 | + /** |
| 20 | + * ID таймаута скролла |
| 21 | + * @type {number} |
| 22 | + */ |
| 23 | + let scrollTimeout |
| 24 | +
|
| 25 | + /** |
| 26 | + * ID таймаута выбора элемента |
| 27 | + * @type {number} |
| 28 | + */ |
| 29 | + let selectTimeout |
| 30 | + let containerNode |
| 31 | +
|
| 32 | + /** |
| 33 | + * Предыдущий выбранный элемент. Нужен чтобы понимать произошли ли изменения |
| 34 | + * @type {number} |
| 35 | + */ |
| 36 | + let lastChosen = 0 |
| 37 | +
|
| 38 | + function scroll(e) { |
| 39 | + let scrollTop = e.target.scrollTop |
| 40 | + let new_chosen = |
| 41 | + Math.round((scrollTop + height / 2 + itemHeight / 2) / itemHeight) - 2 |
| 42 | +
|
| 43 | + if (new_chosen !== chosen) { |
| 44 | + lastChosen = chosen |
| 45 | + chosen = new_chosen |
| 46 | + if (selectTimeout) clearTimeout(selectTimeout) |
| 47 | + selectTimeout = setTimeout(() => { |
| 48 | + dispatch("select", {index: chosen}) |
| 49 | + }, 500) |
| 50 | + } |
| 51 | +
|
| 52 | + if (scrollTimeout) clearTimeout(scrollTimeout) |
| 53 | + scrollTimeout = setTimeout(() => { |
| 54 | + let scroll = itemHeight * chosen |
| 55 | + scrollTo(containerNode, 0, scroll) |
| 56 | + }, 300) |
| 57 | + } |
| 58 | +
|
| 59 | + function bezelRotate(e) { |
| 60 | + if (e.detail.direction === "CW" && chosen < items.length - 1) { |
| 61 | + chosen = chosen + 1 |
| 62 | + scrollTo(containerNode, 0, itemHeight * chosen) |
| 63 | + } |
| 64 | + if (e.detail.direction === "CCW" && chosen > 0) { |
| 65 | + chosen = chosen - 1 |
| 66 | + scrollTo(containerNode, 0, itemHeight * chosen) |
| 67 | + } |
| 68 | + } |
| 69 | +</script> |
| 70 | + |
| 71 | +<svelte:window on:rotarydetent={bezelRotate} /> |
| 72 | + |
| 73 | +<div |
| 74 | + bind:this={containerNode} |
| 75 | + class="cont" |
| 76 | + on:scroll={scroll} |
| 77 | + style="height: {height}px;" |
| 78 | +> |
| 79 | + <div class="item fake-item" style="height: {itemHeight}px;"> |
| 80 | + {#if title} |
| 81 | + <div class="title" style="color: #14b6ff">{title}</div> |
| 82 | + {/if} |
| 83 | + </div> |
| 84 | + <!-- TODO: добавить id --> |
| 85 | + {#each items as item, i (item.title || item)} |
| 86 | + <div |
| 87 | + on:click={() => dispatch("click", item)} |
| 88 | + class:active={chosen === i} |
| 89 | + class="item" |
| 90 | + style="height: {itemHeight}px;" |
| 91 | + > |
| 92 | + <slot {item} active={chosen === i} /> |
| 93 | + </div> |
| 94 | + {/each} |
| 95 | + <div class="fake-item" style="height: {itemHeight}px;" /> |
| 96 | +</div> |
| 97 | + |
| 98 | +<style> |
| 99 | + .item { |
| 100 | + text-align: center; |
| 101 | + margin: 0; |
| 102 | + box-sizing: border-box; |
| 103 | + border-radius: 99px; |
| 104 | + transition: background ease-in-out 0.5s; |
| 105 | + justify-content: center; |
| 106 | + display: flex; |
| 107 | + flex-direction: column; |
| 108 | + } |
| 109 | +
|
| 110 | + .cont { |
| 111 | + overflow-y: scroll; |
| 112 | + scroll-behavior: smooth; |
| 113 | + } |
| 114 | +
|
| 115 | + .active { |
| 116 | + background: #3a3a3a; |
| 117 | + } |
| 118 | +
|
| 119 | + .title { |
| 120 | + font-weight: bold; |
| 121 | + font-size: 2em; |
| 122 | + } |
| 123 | +
|
| 124 | + .active .title { |
| 125 | + font-size: 2.2em; |
| 126 | + } |
| 127 | +
|
| 128 | + .subtitle { |
| 129 | + font-size: 1.8em; |
| 130 | + /* TODO: сделать плавающий текст */ |
| 131 | + max-height: 72px; |
| 132 | + overflow: hidden; |
| 133 | + word-break: break-all; |
| 134 | + } |
| 135 | +
|
| 136 | + .title { |
| 137 | + max-width: 100%; |
| 138 | + margin: 0 auto; |
| 139 | + white-space: nowrap; |
| 140 | + overflow: hidden; |
| 141 | + } |
| 142 | +
|
| 143 | + .title span { |
| 144 | + display: inline-block; |
| 145 | + } |
| 146 | +
|
| 147 | + .title :global(span.animated) { |
| 148 | + animation: marquee 10s ease-in-out alternate infinite; |
| 149 | + } |
| 150 | +
|
| 151 | + @keyframes marquee { |
| 152 | + 0% { |
| 153 | + transform: translateX(20px); |
| 154 | + } |
| 155 | + 100% { |
| 156 | + transform: translateX(calc(-100% + 340px)); |
| 157 | + } |
| 158 | + } |
| 159 | +</style> |
0 commit comments