primevue-mirror/components/lib/image/Image.vue

321 lines
9.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2023-05-04 07:45:01 +00:00
<span :class="containerClass" :style="style" v-bind="ptm('root')">
<slot name="image" :class="imageClass" :style="imageStyle" :onError="onError">
2023-05-04 07:45:01 +00:00
<img :style="imageStyle" :class="imageClass" @error="onError" v-bind="{ ...$attrs, ...ptm('image') }" />
</slot>
2023-05-04 07:45:01 +00:00
<button v-if="preview" ref="previewButton" class="p-image-preview-indicator" @click="onImageClick" v-bind="{ ...previewButtonProps, ...ptm('button') }">
<slot name="indicatoricon">
2023-05-04 07:45:01 +00:00
<component :is="indicatorIcon ? 'i' : 'EyeIcon'" class="p-image-preview-icon" v-bind="ptm('icon')" />
2022-09-06 12:03:37 +00:00
</slot>
2022-12-08 11:04:25 +00:00
</button>
2022-09-06 12:03:37 +00:00
<Portal>
2023-05-04 07:45:01 +00:00
<div v-if="maskVisible" :ref="maskRef" v-focustrap role="dialog" :class="maskClass" :aria-modal="maskVisible" @click="onMaskClick" @keydown="onMaskKeydown" v-bind="ptm('mask')">
<div class="p-image-toolbar" v-bind="ptm('toolbar')">
<button class="p-image-action p-link" @click="rotateRight" type="button" :aria-label="rightAriaLabel" v-bind="ptm('rotateRightButton')">
<slot name="refresh">
2023-05-04 07:45:01 +00:00
<RefreshIcon v-bind="ptm('rotateRightIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
2023-05-04 07:45:01 +00:00
<button class="p-image-action p-link" @click="rotateLeft" type="button" :aria-label="leftAriaLabel" v-bind="ptm('rotateLeftButton')">
<slot name="undo">
2023-05-04 07:45:01 +00:00
<UndoIcon v-bind="ptm('rotateLeftIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
2023-05-04 07:45:01 +00:00
<button class="p-image-action p-link" @click="zoomOut" type="button" :disabled="zoomDisabled" :aria-label="zoomOutAriaLabel" v-bind="ptm('zoomOutButton')">
<slot name="zoomout">
2023-05-04 07:45:01 +00:00
<SearchMinusIcon v-bind="ptm('zoomOutIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
2023-05-04 07:45:01 +00:00
<button class="p-image-action p-link" @click="zoomIn" type="button" :disabled="zoomDisabled" :aria-label="zoomInAriaLabel" v-bind="ptm('zoomInButton')">
<slot name="zoomin">
2023-05-04 07:45:01 +00:00
<SearchPlusIcon v-bind="ptm('zoomInIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
2023-05-04 07:45:01 +00:00
<button class="p-image-action p-link" type="button" @click="hidePreview" :aria-label="closeAriaLabel" autofocus v-bind="ptm('closeButton')">
<slot name="close">
2023-05-04 07:45:01 +00:00
<TimesIcon v-bind="ptm('closeIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
</div>
<transition name="p-image-preview" @before-enter="onBeforeEnter" @enter="onEnter" @leave="onLeave" @before-leave="onBeforeLeave" @after-leave="onAfterLeave">
2023-05-04 07:45:01 +00:00
<div v-if="previewVisible" v-bind="ptm('previewContainer')">
<slot name="preview" class="p-image-preview" :style="imagePreviewStyle" :onClick="onPreviewImageClick">
2023-05-04 07:45:01 +00:00
<img :src="$attrs.src" class="p-image-preview" :style="imagePreviewStyle" @click="onPreviewImageClick" v-bind="ptm('preview')" />
2023-04-29 18:43:56 +00:00
</slot>
2022-09-06 12:03:37 +00:00
</div>
</transition>
</div>
</Portal>
</span>
</template>
<script>
2023-05-04 07:45:01 +00:00
import BaseComponent from 'primevue/basecomponent';
2022-12-08 11:04:25 +00:00
import FocusTrap from 'primevue/focustrap';
import EyeIcon from 'primevue/icons/eye';
import RefreshIcon from 'primevue/icons/refresh';
import SearchMinusIcon from 'primevue/icons/searchminus';
import SearchPlusIcon from 'primevue/icons/searchplus';
import TimesIcon from 'primevue/icons/times';
import UndoIcon from 'primevue/icons/undo';
2022-09-06 12:03:37 +00:00
import Portal from 'primevue/portal';
2022-12-08 11:04:25 +00:00
import { DomHandler, ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'Image',
2023-05-04 07:45:01 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
inheritAttrs: false,
emits: ['show', 'hide', 'error'],
props: {
preview: {
type: Boolean,
default: false
},
2022-12-08 11:04:25 +00:00
class: {
type: null,
default: null
},
style: {
type: null,
default: null
},
imageStyle: {
type: null,
default: null
},
imageClass: {
type: null,
default: null
},
previewButtonProps: {
type: null,
default: null
2023-03-21 13:59:02 +00:00
},
indicatorIcon: {
type: String,
2023-04-06 08:21:43 +00:00
default: undefined
2022-12-08 11:04:25 +00:00
}
2022-09-06 12:03:37 +00:00
},
mask: null,
data() {
return {
maskVisible: false,
previewVisible: false,
rotate: 0,
scale: 1
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
beforeUnmount() {
if (this.mask) {
ZIndexUtils.clear(this.container);
}
},
methods: {
maskRef(el) {
this.mask = el;
},
toolbarRef(el) {
this.toolbarRef = el;
},
onImageClick() {
if (this.preview) {
this.maskVisible = true;
setTimeout(() => {
this.previewVisible = true;
}, 25);
}
},
onPreviewImageClick() {
this.previewClick = true;
},
onMaskClick() {
if (!this.previewClick) {
this.previewVisible = false;
this.rotate = 0;
this.scale = 1;
}
this.previewClick = false;
},
2022-12-08 11:04:25 +00:00
onMaskKeydown(event) {
switch (event.code) {
case 'Escape':
this.onMaskClick();
setTimeout(() => {
DomHandler.focus(this.$refs.previewButton);
}, 25);
event.preventDefault();
break;
default:
break;
}
},
2022-09-06 12:03:37 +00:00
onError() {
this.$emit('error');
},
rotateRight() {
this.rotate += 90;
this.previewClick = true;
},
rotateLeft() {
this.rotate -= 90;
this.previewClick = true;
},
zoomIn() {
this.scale = this.scale + 0.1;
this.previewClick = true;
},
zoomOut() {
this.scale = this.scale - 0.1;
this.previewClick = true;
},
onBeforeEnter() {
ZIndexUtils.set('modal', this.mask, this.$primevue.config.zIndex.modal);
},
onEnter() {
2022-12-08 11:04:25 +00:00
this.focus();
2022-09-06 12:03:37 +00:00
this.$emit('show');
},
onBeforeLeave() {
DomHandler.addClass(this.mask, 'p-component-overlay-leave');
},
onLeave() {
this.$emit('hide');
},
onAfterLeave(el) {
ZIndexUtils.clear(el);
this.maskVisible = false;
2022-12-08 11:04:25 +00:00
},
focus() {
let focusTarget = this.mask.querySelector('[autofocus]');
if (focusTarget) {
focusTarget.focus();
}
2022-09-06 12:03:37 +00:00
}
},
computed: {
containerClass() {
2022-09-14 11:26:01 +00:00
return [
'p-image p-component',
this.class,
{
'p-image-preview-container': this.preview
}
];
2022-09-06 12:03:37 +00:00
},
maskClass() {
return ['p-image-mask p-component-overlay p-component-overlay-enter'];
},
rotateClass() {
return 'p-image-preview-rotate-' + this.rotate;
},
imagePreviewStyle() {
2022-09-14 11:26:01 +00:00
return { transform: 'rotate(' + this.rotate + 'deg) scale(' + this.scale + ')' };
2022-09-06 12:03:37 +00:00
},
zoomDisabled() {
return this.scale <= 0.5 || this.scale >= 1.5;
2022-12-08 11:04:25 +00:00
},
rightAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.rotateRight : undefined;
},
leftAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.rotateLeft : undefined;
},
zoomInAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.zoomIn : undefined;
},
zoomOutAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.zoomOut : undefined;
},
closeAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.close : undefined;
2022-09-06 12:03:37 +00:00
}
},
components: {
2023-04-06 08:21:43 +00:00
Portal: Portal,
EyeIcon: EyeIcon,
RefreshIcon: RefreshIcon,
UndoIcon: UndoIcon,
SearchMinusIcon: SearchMinusIcon,
SearchPlusIcon: SearchPlusIcon,
TimesIcon: TimesIcon
2022-12-08 11:04:25 +00:00
},
directives: {
focustrap: FocusTrap
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>
<style>
.p-image-mask {
display: flex;
align-items: center;
justify-content: center;
}
.p-image-preview-container {
position: relative;
display: inline-block;
}
.p-image-preview-indicator {
position: absolute;
left: 0;
top: 0;
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
opacity: 0;
2022-09-14 11:26:01 +00:00
transition: opacity 0.3s;
2022-09-06 12:03:37 +00:00
}
.p-image-preview-container:hover > .p-image-preview-indicator {
opacity: 1;
cursor: pointer;
}
.p-image-preview-container > img {
cursor: pointer;
}
.p-image-toolbar {
position: absolute;
top: 0;
right: 0;
display: flex;
}
.p-image-action.p-link {
display: flex;
justify-content: center;
align-items: center;
}
.p-image-preview {
2022-09-14 11:26:01 +00:00
transition: transform 0.15s;
2022-09-06 12:03:37 +00:00
max-width: 100vw;
max-height: 100vh;
}
.p-image-preview-enter-active {
transition: all 150ms cubic-bezier(0, 0, 0.2, 1);
}
.p-image-preview-leave-active {
2022-09-14 11:26:01 +00:00
transition: all 150ms cubic-bezier(0.4, 0, 0.2, 1);
2022-09-06 12:03:37 +00:00
}
.p-image-preview-enter-from,
.p-image-preview-leave-to {
opacity: 0;
transform: scale(0.7);
}
</style>