Merge pull request #9 from vadimkorr/feature/#1_Rename-contentContainerWidth
Feature/#1 Rename, code cleanup
This commit is contained in:
@@ -26,7 +26,6 @@
|
|||||||
"svelte-loader": "^2.13.6"
|
"svelte-loader": "^2.13.6"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"sirv-cli": "^1.0.0",
|
"sirv-cli": "^1.0.0"
|
||||||
"uuid": "^8.3.2"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
<script>
|
<script>
|
||||||
// TODO: rename image carousel to just carousel
|
// TODO: rename image carousel to just carousel
|
||||||
// TODO: seems CarouselChild component can be removed
|
|
||||||
// TODO: subscribe on mount and unsubscribe on destroy to
|
// TODO: subscribe on mount and unsubscribe on destroy to
|
||||||
// $store.currentItemIndex to avoid multiple subscriptions
|
// $store.currentPageIndex to avoid multiple subscriptions
|
||||||
import { onMount } from 'svelte'
|
import { onMount } from 'svelte'
|
||||||
import { store } from '../store'
|
import { store } from '../store'
|
||||||
import {
|
import {
|
||||||
@@ -33,7 +32,7 @@
|
|||||||
/**
|
/**
|
||||||
* Page to start on
|
* Page to start on
|
||||||
*/
|
*/
|
||||||
export let initialPage = 1
|
export let initialPageIndex = 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transition speed (ms)
|
* Transition speed (ms)
|
||||||
@@ -61,14 +60,14 @@
|
|||||||
export let dots = true
|
export let dots = true
|
||||||
|
|
||||||
let pagesCount = 0
|
let pagesCount = 0
|
||||||
let contentContainerWidth = 0
|
let pageWidth = 0
|
||||||
let offset
|
let offset
|
||||||
let contentContainerElement
|
let contentContainerElement
|
||||||
let innerContentContainerElement
|
let innerContentContainerElement
|
||||||
|
|
||||||
function applySlideSizes() {
|
function applySlideSizes() {
|
||||||
const children = innerContentContainerElement.children
|
const children = innerContentContainerElement.children
|
||||||
contentContainerWidth = contentContainerElement.clientWidth
|
pageWidth = contentContainerElement.clientWidth
|
||||||
|
|
||||||
const slidesCount = children.length
|
const slidesCount = children.length
|
||||||
pagesCount = getPagesCount({ slidesCount, slidesToShow })
|
pagesCount = getPagesCount({ slidesCount, slidesToShow })
|
||||||
@@ -77,11 +76,10 @@
|
|||||||
for (let slideIndex=0; slideIndex<children.length; slideIndex++) {
|
for (let slideIndex=0; slideIndex<children.length; slideIndex++) {
|
||||||
const pageIndex = getPageIndex({ slideIndex, slidesToShow })
|
const pageIndex = getPageIndex({ slideIndex, slidesToShow })
|
||||||
const isNotCompletePage = getIsNotCompletePage({ pageIndex, pagesCount })
|
const isNotCompletePage = getIsNotCompletePage({ pageIndex, pagesCount })
|
||||||
const slideSizePx = getSlideSize({ isNotCompletePage, contentContainerWidth, slidesToShow, slidesToShowTail })
|
const slideSizePx = getSlideSize({ isNotCompletePage, pageWidth, slidesToShow, slidesToShowTail })
|
||||||
children[slideIndex].style.minWidth = `${slideSizePx}px`
|
children[slideIndex].style.minWidth = `${slideSizePx}px`
|
||||||
children[slideIndex].style.maxWidth = `${slideSizePx}px`
|
children[slideIndex].style.maxWidth = `${slideSizePx}px`
|
||||||
}
|
}
|
||||||
showPage(initialPage)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyAutoplay() {
|
function applyAutoplay() {
|
||||||
@@ -103,7 +101,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
store.reset() // to init after hot reload
|
store.init(initialPageIndex)
|
||||||
applySlideSizes()
|
applySlideSizes()
|
||||||
|
|
||||||
const { teardownAutoplay } = applyAutoplay()
|
const { teardownAutoplay } = applyAutoplay()
|
||||||
@@ -120,7 +118,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
function applyOffset() {
|
function applyOffset() {
|
||||||
offset = -$store.currentItemIndex * contentContainerWidth
|
offset = -$store.currentPageIndex * pageWidth
|
||||||
}
|
}
|
||||||
|
|
||||||
function showPage(pageIndex) {
|
function showPage(pageIndex) {
|
||||||
@@ -171,13 +169,13 @@
|
|||||||
{#if dots}
|
{#if dots}
|
||||||
<slot
|
<slot
|
||||||
name="dots"
|
name="dots"
|
||||||
currentPage={$store.currentItemIndex}
|
currentPageIndex={$store.currentPageIndex}
|
||||||
{pagesCount}
|
{pagesCount}
|
||||||
{showPage}
|
{showPage}
|
||||||
>
|
>
|
||||||
<Dots
|
<Dots
|
||||||
{pagesCount}
|
{pagesCount}
|
||||||
currentPageIndex={$store.currentItemIndex}
|
currentPageIndex={$store.currentPageIndex}
|
||||||
on:pageChange={handlePageChange}
|
on:pageChange={handlePageChange}
|
||||||
></Dots>
|
></Dots>
|
||||||
</slot>
|
</slot>
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
/**
|
/**
|
||||||
* Page to start on
|
* Page to start on
|
||||||
*/
|
*/
|
||||||
export let initialPage = 1
|
export let initialPageIndex = 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transition speed (ms)
|
* Transition speed (ms)
|
||||||
@@ -65,7 +65,7 @@
|
|||||||
{arrows}
|
{arrows}
|
||||||
{infinite}
|
{infinite}
|
||||||
{slidesToShow}
|
{slidesToShow}
|
||||||
{initialPage}
|
{initialPageIndex}
|
||||||
{speed}
|
{speed}
|
||||||
{autoplay}
|
{autoplay}
|
||||||
{autoplaySpeed}
|
{autoplaySpeed}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
/**
|
/**
|
||||||
* Page to start on
|
* Page to start on
|
||||||
*/
|
*/
|
||||||
export let initialPage = 1
|
export let initialPageIndex = 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transition speed (ms)
|
* Transition speed (ms)
|
||||||
@@ -46,10 +46,6 @@
|
|||||||
*/
|
*/
|
||||||
export let dots = true
|
export let dots = true
|
||||||
|
|
||||||
function onPageChange(event, showPage) {
|
|
||||||
showPage(event.target.value)
|
|
||||||
}
|
|
||||||
|
|
||||||
const colors = [
|
const colors = [
|
||||||
'#e5f9f0',
|
'#e5f9f0',
|
||||||
'#ccf3e2',
|
'#ccf3e2',
|
||||||
@@ -69,7 +65,7 @@
|
|||||||
{arrows}
|
{arrows}
|
||||||
{infinite}
|
{infinite}
|
||||||
{slidesToShow}
|
{slidesToShow}
|
||||||
{initialPage}
|
{initialPageIndex}
|
||||||
{speed}
|
{speed}
|
||||||
{autoplay}
|
{autoplay}
|
||||||
{autoplaySpeed}
|
{autoplaySpeed}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
/**
|
/**
|
||||||
* Page to start on
|
* Page to start on
|
||||||
*/
|
*/
|
||||||
export let initialPage = 1
|
export let initialPageIndex = 1
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Transition speed (ms)
|
* Transition speed (ms)
|
||||||
@@ -69,13 +69,13 @@
|
|||||||
{arrows}
|
{arrows}
|
||||||
{infinite}
|
{infinite}
|
||||||
{slidesToShow}
|
{slidesToShow}
|
||||||
{initialPage}
|
{initialPageIndex}
|
||||||
{speed}
|
{speed}
|
||||||
{autoplay}
|
{autoplay}
|
||||||
{autoplaySpeed}
|
{autoplaySpeed}
|
||||||
{autoplayDirection}
|
{autoplayDirection}
|
||||||
{dots}
|
{dots}
|
||||||
let:currentPage
|
let:currentPageIndex
|
||||||
let:pagesCount
|
let:pagesCount
|
||||||
let:showPage
|
let:showPage
|
||||||
>
|
>
|
||||||
@@ -90,12 +90,12 @@
|
|||||||
<div slot="dots">
|
<div slot="dots">
|
||||||
<div class="select-container">
|
<div class="select-container">
|
||||||
<select
|
<select
|
||||||
value={currentPage}
|
value={currentPageIndex}
|
||||||
on:change="{(event) => onPageChange(event, showPage)}"
|
on:change="{(event) => onPageChange(event, showPage)}"
|
||||||
on:blur="{(event) => onPageChange(event, showPage)}"
|
on:blur="{(event) => onPageChange(event, showPage)}"
|
||||||
>
|
>
|
||||||
{#each Array(pagesCount) as _, pageIndex (pageIndex)}
|
{#each Array(pagesCount) as _, pageIndex (pageIndex)}
|
||||||
<option value={pageIndex} class:active={currentPage === pageIndex}>
|
<option value={pageIndex} class:active={currentPageIndex === pageIndex}>
|
||||||
{pageIndex}
|
{pageIndex}
|
||||||
</option>
|
</option>
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -1,2 +1,2 @@
|
|||||||
// TODO:
|
// TODO:
|
||||||
export default null;
|
export default null;
|
||||||
|
|||||||
36
src/store.js
36
src/store.js
@@ -1,24 +1,24 @@
|
|||||||
import { writable } from 'svelte/store';
|
import { writable } from 'svelte/store';
|
||||||
import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index'
|
import { getNextPageIndexFn, getPrevPageIndexFn } from './utils/page-index'
|
||||||
|
|
||||||
// TODO: try to split writable store items
|
|
||||||
// or try to use immer
|
|
||||||
|
|
||||||
const initState = {
|
const initState = {
|
||||||
currentItemIndex: null,
|
currentPageIndex: 0,
|
||||||
}
|
}
|
||||||
|
|
||||||
function createStore() {
|
function createStore() {
|
||||||
const { subscribe, set, update } = writable(initState);
|
const { subscribe, set, update } = writable(initState);
|
||||||
|
|
||||||
function reset() {
|
function init(initialPageIndex) {
|
||||||
set(initState)
|
set({
|
||||||
|
...initState,
|
||||||
|
currentPageIndex: initialPageIndex
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function setCurrentItemIndex(index) {
|
function setCurrentPageIndex(index) {
|
||||||
update(store => ({
|
update(store => ({
|
||||||
...store,
|
...store,
|
||||||
currentItemIndex: index,
|
currentPageIndex: index,
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -26,29 +26,27 @@ function createStore() {
|
|||||||
update(store => {
|
update(store => {
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemIndex: pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1),
|
currentPageIndex: pageIndex < 0 ? 0 : Math.min(pageIndex, pagesCount - 1),
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function next({ infinite, pagesCount }) {
|
function next({ infinite, pagesCount }) {
|
||||||
update(store => {
|
update(store => {
|
||||||
const currentItemIndex = store.currentItemIndex
|
const newCurrentPageIndex = getNextPageIndexFn(infinite)(store.currentPageIndex, pagesCount)
|
||||||
const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, pagesCount)
|
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemIndex: newCurrentItemIndex,
|
currentPageIndex: newCurrentPageIndex,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function prev({ infinite, pagesCount }) {
|
function prev({ infinite, pagesCount }) {
|
||||||
update(store => {
|
update(store => {
|
||||||
const currentItemIndex = store.currentItemIndex
|
const newCurrentPageIndex = getPrevPageIndexFn(infinite)(store.currentPageIndex, pagesCount)
|
||||||
const newCurrentItemIndex = getPrevItemIndexFn(infinite)(currentItemIndex, pagesCount)
|
|
||||||
return {
|
return {
|
||||||
...store,
|
...store,
|
||||||
currentItemIndex: newCurrentItemIndex,
|
currentPageIndex: newCurrentPageIndex,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -57,10 +55,10 @@ function createStore() {
|
|||||||
subscribe,
|
subscribe,
|
||||||
next,
|
next,
|
||||||
prev,
|
prev,
|
||||||
setCurrentItemIndex,
|
setCurrentPageIndex,
|
||||||
reset,
|
init,
|
||||||
moveToPage,
|
moveToPage,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export const store = createStore();
|
export const store = createStore();
|
||||||
|
|||||||
@@ -1,5 +0,0 @@
|
|||||||
import { v4 as uuid } from 'uuid'
|
|
||||||
|
|
||||||
export function generateId() {
|
|
||||||
return uuid()
|
|
||||||
}
|
|
||||||
@@ -1,25 +0,0 @@
|
|||||||
export function getNextItemIndexLimited(currentItemIndex, length) {
|
|
||||||
return Math.min(currentItemIndex + 1, length - 1)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getNextItemIndexInfinte(currentItemIndex, length) {
|
|
||||||
const newCurrentItemIndex = currentItemIndex + 1
|
|
||||||
return newCurrentItemIndex > length - 1 ? 0 : newCurrentItemIndex
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getNextItemIndexFn(infinite) {
|
|
||||||
return infinite ? getNextItemIndexInfinte : getNextItemIndexLimited
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getPrevItemIndexLimited(currentItemIndex, length) {
|
|
||||||
return Math.max(currentItemIndex - 1, 0)
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getPrevItemIndexInfinte(currentItemIndex, length) {
|
|
||||||
const newCurrentItemIndex = currentItemIndex - 1
|
|
||||||
return newCurrentItemIndex >= 0 ? newCurrentItemIndex : length - 1
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getPrevItemIndexFn(infinite) {
|
|
||||||
return infinite ? getPrevItemIndexInfinte : getPrevItemIndexLimited
|
|
||||||
}
|
|
||||||
25
src/utils/page-index.js
Normal file
25
src/utils/page-index.js
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
export function getNextPageIndexLimited(currentPageIndex, pagesCount) {
|
||||||
|
return Math.min(currentPageIndex + 1, pagesCount - 1)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNextPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||||
|
const newCurrentPageIndex = currentPageIndex + 1
|
||||||
|
return newCurrentPageIndex > pagesCount - 1 ? 0 : newCurrentPageIndex
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getNextPageIndexFn(infinite) {
|
||||||
|
return infinite ? getNextPageIndexInfinte : getNextPageIndexLimited
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevPageIndexLimited(currentPageIndex, pagesCount) {
|
||||||
|
return Math.max(currentPageIndex - 1, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevPageIndexInfinte(currentPageIndex, pagesCount) {
|
||||||
|
const newCurrentPageIndex = currentPageIndex - 1
|
||||||
|
return newCurrentPageIndex >= 0 ? newCurrentPageIndex : pagesCount - 1
|
||||||
|
}
|
||||||
|
|
||||||
|
export function getPrevPageIndexFn(infinite) {
|
||||||
|
return infinite ? getPrevPageIndexInfinte : getPrevPageIndexLimited
|
||||||
|
}
|
||||||
@@ -18,14 +18,14 @@ export function getIsNotCompletePage({
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function getSlideSize({
|
export function getSlideSize({
|
||||||
contentContainerWidth,
|
pageWidth,
|
||||||
slidesToShow,
|
slidesToShow,
|
||||||
slidesToShowTail,
|
slidesToShowTail,
|
||||||
isNotCompletePage
|
isNotCompletePage
|
||||||
}) {
|
}) {
|
||||||
return isNotCompletePage
|
return isNotCompletePage
|
||||||
? Math.round(contentContainerWidth/slidesToShowTail)
|
? Math.round(pageWidth/slidesToShowTail)
|
||||||
: Math.round(contentContainerWidth/slidesToShow)
|
: Math.round(pageWidth/slidesToShow)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getPageIndex({ slideIndex, slidesToShow }) {
|
export function getPageIndex({ slideIndex, slidesToShow }) {
|
||||||
|
|||||||
@@ -9492,7 +9492,7 @@ uuid@^3.3.2:
|
|||||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
|
resolved "https://registry.yarnpkg.com/uuid/-/uuid-3.4.0.tgz#b23e4358afa8a202fe7a100af1f5f883f02007ee"
|
||||||
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
|
integrity sha512-HjSDRw6gZE5JMggctHBcjVak08+KEVhSIiDzFnT9S9aegmp85S/bReBVTb4QTFaRNptJ9kuYaNhnbNEOkbKb/A==
|
||||||
|
|
||||||
uuid@^8.0.0, uuid@^8.3.2:
|
uuid@^8.0.0:
|
||||||
version "8.3.2"
|
version "8.3.2"
|
||||||
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
|
resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
|
||||||
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
|
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==
|
||||||
|
|||||||
Reference in New Issue
Block a user