primevue-mirror/components/lib/galleria/GalleriaThumbnails.vue

538 lines
21 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-31 22:28:41 +00:00
<div :class="cx('thumbnailWrapper')" v-bind="ptm('thumbnailWrapper')">
<div :class="cx('thumbnailContainer')" v-bind="ptm('thumbnailContainer')">
2023-05-04 08:29:52 +00:00
<button
v-if="showThumbnailNavigators"
v-ripple
2023-05-31 22:28:41 +00:00
:class="cx('previousThumbnailButton')"
2023-05-04 08:29:52 +00:00
:disabled="isNavBackwardDisabled()"
type="button"
:aria-label="ariaPrevButtonLabel"
@click="navBackward($event)"
v-bind="{ ...prevButtonProps, ...ptm('previousThumbnailButton') }"
2023-08-14 13:08:45 +00:00
data-pc-group-section="thumbnailnavigator"
2023-05-04 08:29:52 +00:00
>
2023-05-31 22:28:41 +00:00
<component :is="templates.previousthumbnailicon || (isVertical ? 'ChevronUpIcon' : 'ChevronLeftIcon')" :class="cx('previousThumbnailIcon')" v-bind="ptm('previousThumbnailIcon')" />
2022-09-06 12:03:37 +00:00
</button>
2023-05-31 22:28:41 +00:00
<div :class="cx('thumbnailItemsContainer')" :style="{ height: isVertical ? contentHeight : '' }" v-bind="ptm('thumbnailItemsContainer')">
<div ref="itemsContainer" :class="cx('thumbnailItems')" role="tablist" @transitionend="onTransitionEnd" @touchstart="onTouchStart($event)" @touchmove="onTouchMove($event)" @touchend="onTouchEnd($event)" v-bind="ptm('thumbnailItems')">
2022-09-14 11:26:01 +00:00
<div
v-for="(item, index) of value"
:key="`p-galleria-thumbnail-item-${index}`"
2023-06-09 11:49:47 +00:00
:class="cx('thumbnailItem', { index, activeIndex })"
2022-12-08 11:04:25 +00:00
role="tab"
2023-05-31 23:19:04 +00:00
:data-p-active="activeIndex === index"
2022-12-08 11:04:25 +00:00
:aria-selected="activeIndex === index"
:aria-controls="containerId + '_item_' + index"
@keydown="onThumbnailKeydown($event, index)"
2023-05-04 08:29:52 +00:00
v-bind="ptm('thumbnailItem')"
2023-06-09 11:49:47 +00:00
:data-p-galleria-thumbnail-item-current="activeIndex === index"
:data-p-galleria-thumbnail-item-active="isItemActive(index)"
:data-p-galleria-thumbnail-item-start="firstItemAciveIndex() === index"
:data-p-galleria-thumbnail-item-end="lastItemActiveIndex() === index"
2022-09-14 11:26:01 +00:00
>
2023-05-04 08:29:52 +00:00
<div
2023-05-31 22:28:41 +00:00
:class="cx('thumbnailItemContent')"
2023-05-04 08:29:52 +00:00
:tabindex="activeIndex === index ? '0' : '-1'"
:aria-label="ariaPageLabel(index + 1)"
:aria-current="activeIndex === index ? 'page' : undefined"
@click="onItemClick(index)"
v-bind="ptm('thumbnailItemContent')"
>
2022-09-14 11:26:01 +00:00
<component v-if="templates.thumbnail" :is="templates.thumbnail" :item="item" />
2022-09-06 12:03:37 +00:00
</div>
</div>
</div>
</div>
2023-05-04 08:29:52 +00:00
<button
v-if="showThumbnailNavigators"
v-ripple
2023-05-31 22:28:41 +00:00
:class="cx('nextThumbnailButton')"
2023-05-04 08:29:52 +00:00
:disabled="isNavForwardDisabled()"
type="button"
:aria-label="ariaNextButtonLabel"
@click="navForward($event)"
v-bind="{ ...nextButtonProps, ...ptm('nextThumbnailButton') }"
2023-08-14 13:08:45 +00:00
data-pc-group-section="thumbnailnavigator"
2023-05-04 08:29:52 +00:00
>
2023-05-31 22:28:41 +00:00
<component :is="templates.nextthumbnailicon || (isVertical ? 'ChevronDownIcon' : 'ChevronRightIcon')" :class="cx('nextThumbnailIcon')" v-bind="ptm('nextThumbnailIcon')" />
2022-09-06 12:03:37 +00:00
</button>
</div>
</div>
</template>
<script>
2023-05-04 08:29:52 +00:00
import BaseComponent from 'primevue/basecomponent';
import ChevronDownIcon from 'primevue/icons/chevrondown';
import ChevronLeftIcon from 'primevue/icons/chevronleft';
import ChevronRightIcon from 'primevue/icons/chevronright';
import ChevronUpIcon from 'primevue/icons/chevronup';
2022-09-06 12:03:37 +00:00
import Ripple from 'primevue/ripple';
import { DomHandler, ObjectUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'GalleriaThumbnails',
2023-07-19 12:46:57 +00:00
hostName: 'Galleria',
2023-05-04 08:29:52 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
emits: ['stop-slideshow', 'update:activeIndex'],
props: {
containerId: {
type: String,
2022-09-14 11:26:01 +00:00
default: null
2022-09-06 12:03:37 +00:00
},
value: {
type: Array,
default: null
},
numVisible: {
type: Number,
default: 3
},
activeIndex: {
type: Number,
default: 0
},
isVertical: {
type: Boolean,
default: false
},
slideShowActive: {
type: Boolean,
default: false
},
circular: {
type: Boolean,
default: false
},
responsiveOptions: {
type: Array,
default: null
},
contentHeight: {
type: String,
2022-09-14 11:26:01 +00:00
default: '300px'
2022-09-06 12:03:37 +00:00
},
showThumbnailNavigators: {
type: Boolean,
default: true
},
templates: {
type: null,
default: null
2022-12-08 11:04:25 +00:00
},
prevButtonProps: {
type: null,
default: null
},
nextButtonProps: {
type: null,
default: null
2022-09-06 12:03:37 +00:00
}
},
startPos: null,
thumbnailsStyle: null,
sortedResponsiveOptions: null,
data() {
return {
d_numVisible: this.numVisible,
d_oldNumVisible: this.numVisible,
d_activeIndex: this.activeIndex,
d_oldActiveItemIndex: this.activeIndex,
totalShiftedItems: 0,
page: 0
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
watch: {
numVisible(newValue, oldValue) {
2022-09-14 11:26:01 +00:00
this.d_numVisible = newValue;
this.d_oldNumVisible = oldValue;
2022-09-06 12:03:37 +00:00
},
activeIndex(newValue, oldValue) {
2022-09-14 11:26:01 +00:00
this.d_activeIndex = newValue;
this.d_oldActiveItemIndex = oldValue;
2022-09-06 12:03:37 +00:00
}
},
mounted() {
2022-09-14 11:26:01 +00:00
this.createStyle();
this.calculatePosition();
2022-09-06 12:03:37 +00:00
2022-09-14 11:26:01 +00:00
if (this.responsiveOptions) {
this.bindDocumentListeners();
}
2022-09-06 12:03:37 +00:00
},
updated() {
let totalShiftedItems = this.totalShiftedItems;
if (this.d_oldNumVisible !== this.d_numVisible || this.d_oldActiveItemIndex !== this.d_activeIndex) {
if (this.d_activeIndex <= this.getMedianItemIndex()) {
totalShiftedItems = 0;
2022-09-14 11:26:01 +00:00
} else if (this.value.length - this.d_numVisible + this.getMedianItemIndex() < this.d_activeIndex) {
2022-09-06 12:03:37 +00:00
totalShiftedItems = this.d_numVisible - this.value.length;
2022-09-14 11:26:01 +00:00
} else if (this.value.length - this.d_numVisible < this.d_activeIndex && this.d_numVisible % 2 === 0) {
totalShiftedItems = this.d_activeIndex * -1 + this.getMedianItemIndex() + 1;
} else {
totalShiftedItems = this.d_activeIndex * -1 + this.getMedianItemIndex();
2022-09-06 12:03:37 +00:00
}
if (totalShiftedItems !== this.totalShiftedItems) {
this.totalShiftedItems = totalShiftedItems;
}
2022-09-14 11:26:01 +00:00
this.$refs.itemsContainer.style.transform = this.isVertical ? `translate3d(0, ${totalShiftedItems * (100 / this.d_numVisible)}%, 0)` : `translate3d(${totalShiftedItems * (100 / this.d_numVisible)}%, 0, 0)`;
2022-09-06 12:03:37 +00:00
if (this.d_oldActiveItemIndex !== this.d_activeIndex) {
document.body.setAttribute('data-p-items-hidden', 'false');
2023-05-31 23:19:04 +00:00
!this.isUnstyled && DomHandler.removeClass(this.$refs.itemsContainer, 'p-items-hidden');
2022-09-06 12:03:37 +00:00
this.$refs.itemsContainer.style.transition = 'transform 500ms ease 0s';
}
this.d_oldActiveItemIndex = this.d_activeIndex;
this.d_oldNumVisible = this.d_numVisible;
}
},
beforeUnmount() {
2022-09-14 11:26:01 +00:00
if (this.responsiveOptions) {
this.unbindDocumentListeners();
2022-09-06 12:03:37 +00:00
}
if (this.thumbnailsStyle) {
this.thumbnailsStyle.parentNode.removeChild(this.thumbnailsStyle);
}
2022-09-14 11:26:01 +00:00
},
2022-09-06 12:03:37 +00:00
methods: {
step(dir) {
let totalShiftedItems = this.totalShiftedItems + dir;
2022-09-14 11:26:01 +00:00
if (dir < 0 && -1 * totalShiftedItems + this.d_numVisible > this.value.length - 1) {
2022-09-06 12:03:37 +00:00
totalShiftedItems = this.d_numVisible - this.value.length;
2022-09-14 11:26:01 +00:00
} else if (dir > 0 && totalShiftedItems > 0) {
2022-09-06 12:03:37 +00:00
totalShiftedItems = 0;
}
if (this.circular) {
if (dir < 0 && this.value.length - 1 === this.d_activeIndex) {
totalShiftedItems = 0;
2022-09-14 11:26:01 +00:00
} else if (dir > 0 && this.d_activeIndex === 0) {
2022-09-06 12:03:37 +00:00
totalShiftedItems = this.d_numVisible - this.value.length;
}
}
if (this.$refs.itemsContainer) {
document.body.setAttribute('data-p-items-hidden', 'false');
2023-05-31 23:19:04 +00:00
!this.isUnstyled && DomHandler.removeClass(this.$refs.itemsContainer, 'p-items-hidden');
2022-09-14 11:26:01 +00:00
this.$refs.itemsContainer.style.transform = this.isVertical ? `translate3d(0, ${totalShiftedItems * (100 / this.d_numVisible)}%, 0)` : `translate3d(${totalShiftedItems * (100 / this.d_numVisible)}%, 0, 0)`;
2022-09-06 12:03:37 +00:00
this.$refs.itemsContainer.style.transition = 'transform 500ms ease 0s';
}
this.totalShiftedItems = totalShiftedItems;
},
stopSlideShow() {
if (this.slideShowActive && this.stopSlideShow) {
this.$emit('stop-slideshow');
}
},
getMedianItemIndex() {
let index = Math.floor(this.d_numVisible / 2);
2022-09-14 11:26:01 +00:00
return this.d_numVisible % 2 ? index : index - 1;
2022-09-06 12:03:37 +00:00
},
navBackward(e) {
this.stopSlideShow();
let prevItemIndex = this.d_activeIndex !== 0 ? this.d_activeIndex - 1 : 0;
let diff = prevItemIndex + this.totalShiftedItems;
2022-09-14 11:26:01 +00:00
if (this.d_numVisible - diff - 1 > this.getMedianItemIndex() && (-1 * this.totalShiftedItems !== 0 || this.circular)) {
2022-09-06 12:03:37 +00:00
this.step(1);
}
let activeIndex = this.circular && this.d_activeIndex === 0 ? this.value.length - 1 : prevItemIndex;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.$emit('update:activeIndex', activeIndex);
if (e.cancelable) {
e.preventDefault();
}
},
navForward(e) {
this.stopSlideShow();
2022-12-08 11:04:25 +00:00
let nextItemIndex = this.d_activeIndex === this.value.length - 1 ? this.value.length - 1 : this.d_activeIndex + 1;
2022-09-14 11:26:01 +00:00
if (nextItemIndex + this.totalShiftedItems > this.getMedianItemIndex() && (-1 * this.totalShiftedItems < this.getTotalPageNumber() - 1 || this.circular)) {
2022-09-06 12:03:37 +00:00
this.step(-1);
}
2022-09-14 11:26:01 +00:00
let activeIndex = this.circular && this.value.length - 1 === this.d_activeIndex ? 0 : nextItemIndex;
2022-09-06 12:03:37 +00:00
this.$emit('update:activeIndex', activeIndex);
if (e.cancelable) {
e.preventDefault();
}
},
onItemClick(index) {
this.stopSlideShow();
let selectedItemIndex = index;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (selectedItemIndex !== this.d_activeIndex) {
const diff = selectedItemIndex + this.totalShiftedItems;
let dir = 0;
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (selectedItemIndex < this.d_activeIndex) {
2022-09-14 11:26:01 +00:00
dir = this.d_numVisible - diff - 1 - this.getMedianItemIndex();
if (dir > 0 && -1 * this.totalShiftedItems !== 0) {
2022-09-06 12:03:37 +00:00
this.step(dir);
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
dir = this.getMedianItemIndex() - diff;
2022-09-14 11:26:01 +00:00
if (dir < 0 && -1 * this.totalShiftedItems < this.getTotalPageNumber() - 1) {
2022-09-06 12:03:37 +00:00
this.step(dir);
}
}
this.$emit('update:activeIndex', selectedItemIndex);
}
},
2022-12-08 11:04:25 +00:00
onThumbnailKeydown(event, index) {
if (event.code === 'Enter' || event.code === 'Space') {
this.onItemClick(index);
event.preventDefault();
}
switch (event.code) {
case 'ArrowRight':
this.onRightKey();
break;
case 'ArrowLeft':
this.onLeftKey();
break;
case 'Home':
this.onHomeKey();
event.preventDefault();
break;
case 'End':
this.onEndKey();
event.preventDefault();
break;
case 'ArrowUp':
case 'ArrowDown':
event.preventDefault();
break;
case 'Tab':
this.onTabKey();
break;
default:
break;
}
},
onRightKey() {
2023-05-31 23:19:04 +00:00
const indicators = DomHandler.find(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"]');
2022-12-08 11:04:25 +00:00
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, activeIndex + 1 === indicators.length ? indicators.length - 1 : activeIndex + 1);
},
onLeftKey() {
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, activeIndex - 1 <= 0 ? 0 : activeIndex - 1);
},
onHomeKey() {
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, 0);
},
onEndKey() {
2023-05-31 23:19:04 +00:00
const indicators = DomHandler.find(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"]');
2022-12-08 11:04:25 +00:00
const activeIndex = this.findFocusedIndicatorIndex();
this.changedFocusedIndicator(activeIndex, indicators.length - 1);
},
onTabKey() {
2023-05-31 23:19:04 +00:00
const indicators = [...DomHandler.find(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"]')];
const highlightedIndex = indicators.findIndex((ind) => DomHandler.getAttribute(ind, 'data-p-active') === true);
const activeIndicator = DomHandler.findSingle(this.$refs.itemsContainer, '[tabindex="0"]');
2022-12-08 11:04:25 +00:00
const activeIndex = indicators.findIndex((ind) => ind === activeIndicator.parentElement);
indicators[activeIndex].children[0].tabIndex = '-1';
indicators[highlightedIndex].children[0].tabIndex = '0';
},
findFocusedIndicatorIndex() {
2023-05-31 23:19:04 +00:00
const indicators = [...DomHandler.find(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"]')];
const activeIndicator = DomHandler.findSingle(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"] > [tabindex="0"]');
2022-12-08 11:04:25 +00:00
return indicators.findIndex((ind) => ind === activeIndicator.parentElement);
},
changedFocusedIndicator(prevInd, nextInd) {
2023-05-31 23:19:04 +00:00
const indicators = DomHandler.find(this.$refs.itemsContainer, '[data-pc-section="thumbnailitem"]');
2022-12-08 11:04:25 +00:00
indicators[prevInd].children[0].tabIndex = '-1';
indicators[nextInd].children[0].tabIndex = '0';
indicators[nextInd].children[0].focus();
},
2022-09-06 12:03:37 +00:00
onTransitionEnd() {
if (this.$refs.itemsContainer) {
document.body.setAttribute('data-p-items-hidden', 'true');
2023-05-31 23:19:04 +00:00
!this.isUnstyled && DomHandler.addClass(this.$refs.itemsContainer, 'p-items-hidden');
2022-09-06 12:03:37 +00:00
this.$refs.itemsContainer.style.transition = '';
}
},
onTouchStart(e) {
let touchobj = e.changedTouches[0];
this.startPos = {
x: touchobj.pageX,
y: touchobj.pageY
};
},
onTouchMove(e) {
if (e.cancelable) {
e.preventDefault();
}
},
onTouchEnd(e) {
let touchobj = e.changedTouches[0];
if (this.isVertical) {
2022-09-14 11:26:01 +00:00
this.changePageOnTouch(e, touchobj.pageY - this.startPos.y);
} else {
this.changePageOnTouch(e, touchobj.pageX - this.startPos.x);
2022-09-06 12:03:37 +00:00
}
},
changePageOnTouch(e, diff) {
2022-09-14 11:26:01 +00:00
if (diff < 0) {
// left
2022-09-06 12:03:37 +00:00
this.navForward(e);
2022-09-14 11:26:01 +00:00
} else {
// right
2022-09-06 12:03:37 +00:00
this.navBackward(e);
}
},
getTotalPageNumber() {
2022-09-14 11:26:01 +00:00
return this.value.length > this.d_numVisible ? this.value.length - this.d_numVisible + 1 : 0;
2022-09-06 12:03:37 +00:00
},
createStyle() {
if (!this.thumbnailsStyle) {
this.thumbnailsStyle = document.createElement('style');
this.thumbnailsStyle.type = 'text/css';
DomHandler.setAttribute(this.thumbnailsStyle, 'nonce', this.$primevue?.config?.csp?.nonce);
2022-09-06 12:03:37 +00:00
document.body.appendChild(this.thumbnailsStyle);
}
let innerHTML = `
2023-06-09 11:49:47 +00:00
#${this.containerId} [data-pc-section="thumbnailitem"] {
2022-09-14 11:26:01 +00:00
flex: 1 0 ${100 / this.d_numVisible}%
2022-09-06 12:03:37 +00:00
}
`;
if (this.responsiveOptions && !this.isUnstyled) {
2022-09-06 12:03:37 +00:00
this.sortedResponsiveOptions = [...this.responsiveOptions];
const comparer = ObjectUtils.localeComparator();
2023-08-18 12:52:27 +00:00
2022-09-06 12:03:37 +00:00
this.sortedResponsiveOptions.sort((data1, data2) => {
const value1 = data1.breakpoint;
const value2 = data2.breakpoint;
return ObjectUtils.sort(value1, value2, -1, comparer);
2022-09-06 12:03:37 +00:00
});
for (let i = 0; i < this.sortedResponsiveOptions.length; i++) {
let res = this.sortedResponsiveOptions[i];
innerHTML += `
@media screen and (max-width: ${res.breakpoint}) {
#${this.containerId} .p-galleria-thumbnail-item {
2022-09-14 11:26:01 +00:00
flex: 1 0 ${100 / res.numVisible}%
2022-09-06 12:03:37 +00:00
}
}
2022-09-14 11:26:01 +00:00
`;
2022-09-06 12:03:37 +00:00
}
}
this.thumbnailsStyle.innerHTML = innerHTML;
},
calculatePosition() {
if (this.$refs.itemsContainer && this.sortedResponsiveOptions) {
let windowWidth = window.innerWidth;
let matchedResponsiveData = {
numVisible: this.numVisible
};
for (let i = 0; i < this.sortedResponsiveOptions.length; i++) {
let res = this.sortedResponsiveOptions[i];
if (parseInt(res.breakpoint, 10) >= windowWidth) {
matchedResponsiveData = res;
}
}
if (this.d_numVisible !== matchedResponsiveData.numVisible) {
this.d_numVisible = matchedResponsiveData.numVisible;
}
}
},
bindDocumentListeners() {
if (!this.documentResizeListener) {
this.documentResizeListener = () => {
this.calculatePosition();
};
window.addEventListener('resize', this.documentResizeListener);
}
},
unbindDocumentListeners() {
2022-09-14 11:26:01 +00:00
if (this.documentResizeListener) {
2022-09-06 12:03:37 +00:00
window.removeEventListener('resize', this.documentResizeListener);
this.documentResizeListener = null;
}
},
isNavBackwardDisabled() {
2022-09-14 11:26:01 +00:00
return (!this.circular && this.d_activeIndex === 0) || this.value.length <= this.d_numVisible;
2022-09-06 12:03:37 +00:00
},
isNavForwardDisabled() {
2022-09-14 11:26:01 +00:00
return (!this.circular && this.d_activeIndex === this.value.length - 1) || this.value.length <= this.d_numVisible;
2022-09-06 12:03:37 +00:00
},
firstItemAciveIndex() {
return this.totalShiftedItems * -1;
},
lastItemActiveIndex() {
return this.firstItemAciveIndex() + this.d_numVisible - 1;
},
isItemActive(index) {
return this.firstItemAciveIndex() <= index && this.lastItemActiveIndex() >= index;
2022-12-08 11:04:25 +00:00
},
ariaPageLabel(value) {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.pageLabel.replace(/{page}/g, value) : undefined;
2022-09-06 12:03:37 +00:00
}
},
computed: {
2022-12-08 11:04:25 +00:00
ariaPrevButtonLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.prevPageLabel : undefined;
},
ariaNextButtonLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.nextPageLabel : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
ChevronLeftIcon: ChevronLeftIcon,
ChevronRightIcon: ChevronRightIcon,
ChevronUpIcon: ChevronUpIcon,
ChevronDownIcon: ChevronDownIcon
},
2022-09-06 12:03:37 +00:00
directives: {
2022-09-14 11:26:01 +00:00
ripple: Ripple
2022-09-06 12:03:37 +00:00
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>