Move getting indexes to utils
This commit is contained in:
@@ -8,6 +8,9 @@
|
|||||||
const id = generateId()
|
const id = generateId()
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
store.setItem(id)
|
store.setItem(id)
|
||||||
|
return () => {
|
||||||
|
store.removeItem(id)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,11 @@
|
|||||||
*/
|
*/
|
||||||
export let arrows = true;
|
export let arrows = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Infinite looping
|
||||||
|
*/
|
||||||
|
export let infinite = true;
|
||||||
|
|
||||||
let contentContainerElement
|
let contentContainerElement
|
||||||
let children
|
let children
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
@@ -15,10 +20,10 @@
|
|||||||
})
|
})
|
||||||
|
|
||||||
function handlePrevClick() {
|
function handlePrevClick() {
|
||||||
store.prev()
|
store.prev({ infinite })
|
||||||
}
|
}
|
||||||
function handleNextClick() {
|
function handleNextClick() {
|
||||||
store.next()
|
store.next({ infinite })
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|||||||
21
src/store.js
21
src/store.js
@@ -1,5 +1,5 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
|
import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index'
|
||||||
|
|
||||||
function createStore() {
|
function createStore() {
|
||||||
const { subscribe, set, update } = writable({
|
const { subscribe, set, update } = writable({
|
||||||
@@ -18,13 +18,17 @@ function createStore() {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
function next() {
|
function removeItem(id) {
|
||||||
// TODO: move to utils functions and cover eoth tests
|
update(store => ({
|
||||||
|
...store,
|
||||||
|
items: store.items.filter(item => item !== id)
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
function next({ infinite }) {
|
||||||
update(store => {
|
update(store => {
|
||||||
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
||||||
// TODO: infinite
|
const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, store.items)
|
||||||
// const newCurrentItemIndex = currentItemIndex + 1 > store.items.length - 1 ? 0 : currentItemIndex + 1
|
|
||||||
const newCurrentItemIndex = Math.min(currentItemIndex + 1, store.items.length - 1)
|
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemId: store.items[newCurrentItemIndex]
|
currentItemId: store.items[newCurrentItemIndex]
|
||||||
@@ -32,10 +36,10 @@ function createStore() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function prev() {
|
function prev({ infinite }) {
|
||||||
update(store => {
|
update(store => {
|
||||||
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
const currentItemIndex = store.items.findIndex(item => item === store.currentItemId)
|
||||||
const newCurrentItemIndex = Math.max(0, currentItemIndex - 1)
|
const newCurrentItemIndex = getPrevItemIndexFn(infinite)(currentItemIndex, store.items)
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemId: store.items[newCurrentItemIndex]
|
currentItemId: store.items[newCurrentItemIndex]
|
||||||
@@ -46,6 +50,7 @@ function createStore() {
|
|||||||
return {
|
return {
|
||||||
subscribe,
|
subscribe,
|
||||||
setItem,
|
setItem,
|
||||||
|
removeItem,
|
||||||
next,
|
next,
|
||||||
prev
|
prev
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -5,11 +5,19 @@
|
|||||||
/**
|
/**
|
||||||
* Enable Next/Previos arrows
|
* Enable Next/Previos arrows
|
||||||
*/
|
*/
|
||||||
export let arrows = true;
|
export let arrows = true;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Infinite looping
|
||||||
|
*/
|
||||||
|
export let infinite = true;
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="main-container">
|
<div class="main-container">
|
||||||
<ImageCarousel {arrows}>
|
<ImageCarousel
|
||||||
|
{arrows}
|
||||||
|
{infinite}
|
||||||
|
>
|
||||||
<CarouselChild>
|
<CarouselChild>
|
||||||
<div class="child-content-container">
|
<div class="child-content-container">
|
||||||
<h1>Element 1</h1>
|
<h1>Element 1</h1>
|
||||||
|
|||||||
25
src/utils/item-index.js
Normal file
25
src/utils/item-index.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export function getNextItemIndexLimited(currentItemIndex, items) {
|
||||||
|
return Math.min(currentItemIndex + 1, items.length - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNextItemIndexInfinte(currentItemIndex, items) {
|
||||||
|
const newCurrentItemIndex = currentItemIndex + 1
|
||||||
|
return newCurrentItemIndex > items.length - 1 ? 0 : newCurrentItemIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNextItemIndexFn(infinite) {
|
||||||
|
return infinite ? getNextItemIndexInfinte : getNextItemIndexLimited
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevItemIndexLimited(currentItemIndex, items) {
|
||||||
|
return Math.max(currentItemIndex - 1, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevItemIndexInfinte(currentItemIndex, items) {
|
||||||
|
const newCurrentItemIndex = currentItemIndex - 1
|
||||||
|
return newCurrentItemIndex >= 0 ? newCurrentItemIndex : items.length - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevItemIndexFn(infinite) {
|
||||||
|
return infinite ? getPrevItemIndexInfinte : getPrevItemIndexLimited
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user