marc-leopold/components/GalleryThumbs.vue

441 lines
12 KiB
Vue
Raw Normal View History

<template>
2019-01-04 13:55:30 +00:00
<div>
<div class="gallery-thumbs__spacer"></div>
2019-01-09 17:34:01 +00:00
<ul ref="galleryThumbsList"
class="gallery-thumbs__list">
2019-01-09 12:56:10 +00:00
<li v-for="(gallery, galleryIndex) in galleries"
:key="galleryIndex"
:class="{ 'is-active': activeRow === galleryIndex }"
class="gallery-thumbs__row-container">
2019-01-12 20:08:07 +00:00
<ul ref="galleryThumbsRows"
2019-01-09 12:56:10 +00:00
class="gallery-thumbs__row"
2019-01-09 17:34:01 +00:00
:style="{ 'transform': 'translate3d(' + thumbRowOffsets[galleryIndex] + 'px, 0, 0)' }">
2019-01-09 12:56:10 +00:00
<li class="featured-image-spacer mobile-only"
2019-01-04 13:55:30 +00:00
:style="{ 'height': featuredHeight }">
2019-01-09 17:34:01 +00:00
<ThumbNav class="thumb-nav thumb-nav--large mobile-only"
2019-01-09 12:56:10 +00:00
direction="right"
@navClick="handleNavClick(galleryIndex, 'right')" />
2019-01-04 13:55:30 +00:00
</li>
2019-01-09 12:56:10 +00:00
<li v-for="(image, imageIndex) in gallery.images"
:ref="galleryIndex == 0 && imageIndex == 0 ? 'thumbContainer' : null"
class="thumb-container thumb-overlay selected-indicator"
:class="{ 'is-active': imageIndex === activeIndex }"
:style="{ 'background-image': 'url(' + image.thumbUrl + ')' }"
2019-01-09 21:23:03 +00:00
:key="galleryIndex + '.' + imageIndex + '.' + image.url"
@click="$emit('thumbClick', galleryIndex, imageIndex)">
2019-01-09 17:34:01 +00:00
<ThumbNav class="thumb-nav thumb-nav--left mobile-only"
2019-01-09 12:56:10 +00:00
direction="left"
@navClick="handleNavClick(galleryIndex, 'left')" />
2019-01-09 12:56:10 +00:00
<ThumbNav v-if="imageIndex < gallery.images.length - 1"
2019-01-09 17:34:01 +00:00
class="thumb-nav mobile-only"
2019-01-09 12:56:10 +00:00
direction="right"
@navClick="handleNavClick(galleryIndex, 'right')" />
</li>
</ul>
</li>
2019-01-09 17:34:01 +00:00
<ThumbNav class="thumb-nav thumb-nav--left mobile-hide"
:class="{ 'is-active': isLeftDesktopNavActive }"
2019-01-09 17:34:01 +00:00
direction="left"
@navClick="handleNavClickDesktop(activeRow, 'left')"/>
<ThumbNav class="thumb-nav thumb-nav--right mobile-hide"
2019-01-13 11:47:43 +00:00
:class="{ 'is-active': isRightDesktopNavActive }"
2019-01-09 17:34:01 +00:00
direction="right"
@navClick="handleNavClickDesktop(activeRow, 'right')"/>
</ul>
</div>
</template>
<script>
2019-01-04 16:18:45 +00:00
import ThumbNav from '@/components/ThumbNav'
export default {
components: {
2019-01-04 16:18:45 +00:00
ThumbNav
},
props: {
galleries: {
type: Array,
required: true
2019-01-04 13:55:30 +00:00
},
featuredHeight: {
type: String,
required: true
},
activeRow: {
type: Number,
required: true
},
2019-01-29 10:30:05 +00:00
// index of image to be highlighted
activeIndex: {
type: Number,
required: false,
default () {
return 0
},
2019-01-29 10:30:05 +00:00
},
// scroll this image into view
showImage: {
2019-01-29 10:30:05 +00:00
type: Number,
required: false,
default () {
return 0
},
},
},
data () {
return {
2019-01-09 12:56:10 +00:00
thumbRowOffsets: new Array(this.galleries.length),
isLeftDesktopNavActive: false,
2019-01-13 11:47:43 +00:00
resizeIsThrottling: false,
2019-01-13 14:16:48 +00:00
isThumbsScrolling: false,
touchDownPosition: null
}
},
computed: {
thumbsVerticalOffset () {
let rowHeight = this.$refs.thumbContainer ? this.$refs.thumbContainer[0].clientHeight : 0;
return rowHeight * this.activeRow
2019-01-13 11:47:43 +00:00
},
isRightDesktopNavActive () {
return this.thumbRowOffsets[this.activeRow] > 0
}
},
watch: {
activeRow: function(val) {
this.$nextTick(() => {
2019-01-12 20:08:07 +00:00
this.updateElements(false)
})
},
showImage () {
this.scrollToThumb(this.showImage)
}
},
2019-01-04 16:18:45 +00:00
mounted () {
window.addEventListener('resize', this.handleResize)
2019-01-13 14:16:48 +00:00
this.$refs.galleryThumbsList.addEventListener('touchstart', this.handleTouch)
this.$refs.galleryThumbsList.addEventListener('touchend', this.handleTouch)
2019-01-13 11:47:43 +00:00
this.$refs.galleryThumbsList.addEventListener('wheel', this.handleThumbsScroll)
// firefox
this.$refs.galleryThumbsList.addEventListener('mousewheel', this.handleThumbsScroll)
this.$nextTick(function () {
this.updateElements()
this.scrollToThumb(this.activeIndex)
})
2019-01-04 16:18:45 +00:00
},
methods: {
2019-01-29 10:30:05 +00:00
// TOOD move logic of getting sizes of elements into getter methods
scrollToThumb (thumbIndex) {
if (thumbIndex >= this.galleries[this.activeRow].images.length || thumbIndex < 0) { return }
2019-01-29 10:30:05 +00:00
const containerWidth = this.$refs.galleryThumbsList.clientWidth
const thumbWidth = this.$refs.thumbContainer[0].clientWidth
let offset = this.thumbRowOffsets[this.activeRow]
2019-01-29 10:30:05 +00:00
const thumbPosition = thumbWidth * (this.galleries[this.activeRow].images.length - thumbIndex)
const rowWidth = this.$refs.galleryThumbsRows[this.activeRow].clientWidth
2019-01-29 10:30:05 +00:00
if (containerWidth < thumbPosition - offset) {
this.$set(this.thumbRowOffsets, this.activeRow, thumbPosition - containerWidth + 1)
this.isLeftDesktopNavActive = containerWidth + offset > rowWidth
} else if (thumbPosition <= offset) {
this.$set(this.thumbRowOffsets, this.activeRow, thumbPosition - thumbWidth)
this.isLeftDesktopNavActive = containerWidth + offset > rowWidth
2019-01-29 10:30:05 +00:00
}
},
2019-01-12 20:08:07 +00:00
updateElements (resetOffsets=true) {
if (resetOffsets) {this.resetOffsets()}
if (this.$refs.galleryThumbsRows[this.activeRow] && this.$refs.galleryThumbsList) {
let totalRowWidth = this.$refs.galleryThumbsRows[this.activeRow].clientWidth
2019-01-09 21:23:03 +00:00
let visibleRowWidth = this.$refs.galleryThumbsList.clientWidth
2019-01-12 20:08:07 +00:00
this.initLeftNav(totalRowWidth, visibleRowWidth)
if (visibleRowWidth > totalRowWidth) {
this.centerThumbs(totalRowWidth, visibleRowWidth)
}
2019-01-09 21:23:03 +00:00
}
},
2019-01-12 20:08:07 +00:00
initLeftNav (totalRowWidth, visibleRowWidth) {
let offset = this.thumbRowOffsets[this.activeRow]
let maxOffset = totalRowWidth - visibleRowWidth
this.isLeftDesktopNavActive = (totalRowWidth > visibleRowWidth)
&& (offset < maxOffset)
},
2019-01-09 12:56:10 +00:00
resetOffsets() {
for (let index = 0; index < this.thumbRowOffsets.length; index++) {
this.$set(this.thumbRowOffsets, index, 0)
}
},
2019-01-12 20:08:07 +00:00
centerThumbs(rowWidth, visibleWidth) {
let offset = (visibleWidth - rowWidth) / -2
this.$set(this.thumbRowOffsets, [this.activeRow], offset)
},
2019-01-09 12:56:10 +00:00
handleNavClick(source, direction) {
let offset = this.thumbRowOffsets[source]
2019-01-29 10:30:05 +00:00
const thumbContainerWidth = this.$refs.thumbContainer[0].clientWidth
2019-01-09 12:56:10 +00:00
if (direction === 'left') {
2019-01-09 17:34:01 +00:00
offset += thumbContainerWidth
2019-01-09 12:56:10 +00:00
} else if (direction === 'right') {
2019-01-09 17:34:01 +00:00
offset -= thumbContainerWidth
}
offset = offset > 0 ? 0 : offset
this.$set(this.thumbRowOffsets, source, offset)
},
handleNavClickDesktop(source, direction) {
let offset = this.thumbRowOffsets[source]
let thumbContainerWidth = this.$refs.thumbContainer[0].clientWidth
let visibleRowWidth = this.$refs.galleryThumbsList.clientWidth
2019-01-12 20:08:07 +00:00
let totalRowWidth = this.$refs.galleryThumbsRows[source].clientWidth
2019-01-09 17:34:01 +00:00
let maxOffset = totalRowWidth - visibleRowWidth
if (direction === 'left') {
2019-01-09 12:56:10 +00:00
offset += thumbContainerWidth
2019-01-09 17:34:01 +00:00
} else if (direction === 'right') {
offset -= thumbContainerWidth
if (offset != 0) {
this.isLeftDesktopNavActive = true
}
2019-01-09 17:34:01 +00:00
}
if (offset < 0) {
offset = 0
} else if (offset >= maxOffset) {
2019-01-09 17:34:01 +00:00
offset = maxOffset
this.isLeftDesktopNavActive = false
2019-01-09 12:56:10 +00:00
}
this.$set(this.thumbRowOffsets, source, offset)
},
handleResize() {
if (!this.resizeIsThrottling) {
this.resizeIsThrottling = true
this.$nextTick(() => {
2019-01-12 20:08:07 +00:00
this.updateElements()
this.resizeIsThrottling = false
})
}
2019-01-13 11:47:43 +00:00
},
handleThumbsScroll(event) {
if (event.wheelDeltaX) {
if (event.wheelDeltaX > 0) {
2019-01-13 14:16:48 +00:00
this.doThumbScroll('right')
2019-01-13 11:47:43 +00:00
} else if (event.wheelDeltaX < 0) {
this.doThumbScroll('left')
}
} else if (event.deltaY) {
if (event.deltaY > 0) {
this.doThumbScroll('left')
} else if (event.deltaY < 0) {
this.doThumbScroll('right')
}
}
},
2019-01-13 14:16:48 +00:00
handleTouch(event) {
if (event.type === 'touchstart') {
this.touchDownPosition = event.changedTouches[0].clientX
} else if (event.type === 'touchmove') {
event.preventDefault()
} else if (event.type === 'touchend') {
let xPos = event.changedTouches[0].clientX
let direction = this.touchDownPosition - xPos < 0 ? 'left' : 'right'
this.doThumbScroll(direction)
}
},
2019-01-13 11:47:43 +00:00
doThumbScroll(direction) {
if (this.isThumbsScrolling) { return }
2019-01-14 10:17:10 +00:00
2019-01-13 11:47:43 +00:00
this.isThumbsScrolling = true
if ((direction === 'left' && this.isLeftDesktopNavActive) || (direction === 'right' && this.isRightDesktopNavActive)) {
this.handleNavClickDesktop(this.activeRow, direction)
}
this.$nextTick(() => {
this.isThumbsScrolling = false
})
2019-01-04 15:23:13 +00:00
}
}
}
</script>
<style lang="scss" scoped>
.gallery-thumbs__row {
display: flex;
flex-direction: row;
list-style: none;
padding: 0;
margin: 0;
}
.thumb-container {
background-size: cover;
background-position: center center;
2019-01-12 20:08:07 +00:00
overflow: hidden;
2019-01-23 11:29:48 +00:00
padding: 0 2px;
background-clip: content-box;
2019-01-27 13:34:32 +00:00
cursor: pointer;
}
@media (max-width: $bp__layout) {
2019-01-09 17:34:01 +00:00
.mobile-hide {
display: none;
}
.gallery-thumbs__spacer {
display:none;
}
.gallery-thumbs__row {
position: relative;
2019-01-09 12:56:10 +00:00
transition : transform .5s;
}
2019-01-09 12:56:10 +00:00
.featured-image-spacer {
position: relative;
height: calc(50vh - #{$site-menu__header-height / 2}) !important; // must override inline style set with prop
width: 100vw;
flex: 0 0 100vw;
}
.thumb-container {
2019-01-09 12:56:10 +00:00
position: relative;
height: calc(50vh - #{$site-menu__header-height / 2});
2019-01-04 16:18:45 +00:00
width: 100vw;
background-size: cover;
background-position: center center;
}
2019-01-09 12:56:10 +00:00
.thumb-nav {
2019-01-27 16:27:02 +00:00
z-index: 20;
2019-01-09 12:56:10 +00:00
position: absolute;
width: 8rem;
height: 8rem;
bottom: 1rem;
right: 1rem;
&--left {
left: 1rem;
right: auto;
}
&--large {
width: 12rem;
height: 12rem;
}
}
}
@media (min-width: $bp__layout) {
.mobile-only {
display: none;
}
2019-01-09 17:34:01 +00:00
.mobile-hide {
display: initial;
}
2019-01-09 12:56:10 +00:00
.featured-image-spacer {
width: 100%;
}
.thumb-container {
2019-01-27 20:35:24 +00:00
position: relative;
z-index: 1;
2019-01-17 12:05:25 +00:00
height: $gallery-thumbs-height--compact;
width: $gallery-thumbs-height--compact * 1.6;
flex: 0 0 $gallery-thumbs-height--compact * 1.6;
}
.gallery__thumbs {
width: 100%;
}
2019-01-04 13:55:30 +00:00
.gallery-thumbs__list {
2019-01-09 17:34:01 +00:00
position: relative;
2019-01-17 12:05:25 +00:00
height: $gallery-thumbs-height--compact;
flex: 0 0 $gallery-thumbs-height--compact;
2019-01-12 20:08:07 +00:00
bottom: 8px;
2019-01-04 13:55:30 +00:00
overflow: hidden;
2019-01-10 17:17:15 +00:00
transition: opacity .3s; // TEMP
}
.gallery-thumbs__row-container {
2019-01-09 17:34:01 +00:00
position: absolute;
width: 100%;
height: 100%;
transition: opacity .5s;
opacity: 0;
2019-01-09 21:23:03 +00:00
pointer-events: none;
&.is-active {
transition: opacity .5s .3s;
opacity: 1;
2019-01-09 21:23:03 +00:00
pointer-events: auto;
}
}
2019-01-04 13:55:30 +00:00
.gallery-thumbs__row {
position: absolute;
height: 100%;
top: 0;
2019-01-09 17:34:01 +00:00
right: 0;
transition: transform .5s;
2019-01-04 13:55:30 +00:00
}
2019-01-04 16:18:45 +00:00
.thumb-nav {
2019-01-09 17:34:01 +00:00
position: absolute;
top: 1rem;
2019-01-17 12:05:25 +00:00
width: $gallery-thumbs-height--compact - 2rem;
height: $gallery-thumbs-height--compact - 2rem;
2019-01-09 17:34:01 +00:00
transition: opacity .5s;
opacity: 0;
pointer-events: none;
&.is-active {
opacity: 1;
pointer-events: auto;
}
&--left {
left: 1rem,;
}
&--right {
right: 1rem,;
}
background-color: rgba(red, .3);
2019-01-04 16:18:45 +00:00
}
}
2019-01-17 12:05:25 +00:00
@media (min-width: $bp__gallery-compact) {
.gallery-thumbs__list {
height: $gallery-thumbs-height;
flex: 0 0 $gallery-thumbs-height;
}
.thumb-container {
height: $gallery-thumbs-height;
width: $gallery-thumbs-height * 1.6;
flex: 0 0 $gallery-thumbs-height * 1.6;
}
.gallery-thumbs__list {
height: $gallery-thumbs-height;
flex: 0 0 $gallery-thumbs-height;
}
.thumb-nav {
width: $gallery-thumbs-height - 2rem;
height: $gallery-thumbs-height - 2rem;
}
}
</style>