primevue-mirror/components/lib/confirmpopup/ConfirmPopup.vue

309 lines
11 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal>
2023-08-02 12:00:18 +00:00
<transition name="p-confirm-popup" @enter="onEnter" @after-enter="onAfterEnter" @leave="onLeave" @after-leave="onAfterLeave" v-bind="ptm('transition')">
2024-02-12 13:02:20 +00:00
<div v-if="visible" :ref="containerRef" v-focustrap role="alertdialog" :class="cx('root')" :aria-modal="visible" @click="onOverlayClick" @keydown="onOverlayKeydown" v-bind="ptmi('root')">
<slot v-if="$slots.container" name="container" :message="confirmation" :onAccept="accept" :onReject="reject" :acceptCallback="accept" :rejectCallback="reject"></slot>
2023-09-19 11:14:41 +00:00
<template v-else>
<template v-if="!$slots.message">
<div :class="cx('content')" v-bind="ptm('content')">
<slot name="icon">
<component v-if="$slots.icon" :is="$slots.icon" :class="cx('icon')" />
<span v-else-if="confirmation.icon" :class="[confirmation.icon, cx('icon')]" v-bind="ptm('icon')" />
2023-09-19 11:14:41 +00:00
</slot>
<span :class="cx('message')" v-bind="ptm('message')">{{ confirmation.message }}</span>
</div>
</template>
<component v-else :is="$slots.message" :message="confirmation"></component>
<div :class="cx('footer')" v-bind="ptm('footer')">
<CPButton :label="rejectLabel" @click="reject()" @keydown="onRejectKeydown" :autofocus="autoFocusReject" :class="[cx('rejectButton'), confirmation.rejectClass]" :unstyled="unstyled" :pt="ptm('rejectButton')">
2023-09-19 11:14:41 +00:00
<template v-if="rejectIcon || $slots.rejecticon" #icon="iconProps">
<slot name="rejecticon">
<span :class="[rejectIcon, iconProps.class]" v-bind="ptm('rejectButton')['icon']" data-pc-section="rejectbuttonicon" />
2023-09-19 11:14:41 +00:00
</slot>
</template>
</CPButton>
<CPButton :label="acceptLabel" @click="accept()" @keydown="onAcceptKeydown" :autofocus="autoFocusAccept" :class="[cx('acceptButton'), confirmation.acceptClass]" :unstyled="unstyled" :pt="ptm('acceptButton')">
2023-09-19 11:14:41 +00:00
<template v-if="acceptIcon || $slots.accepticon" #icon="iconProps">
<slot name="accepticon">
<span :class="[acceptIcon, iconProps.class]" v-bind="ptm('acceptButton')['icon']" data-pc-section="acceptbuttonicon" />
2023-09-19 11:14:41 +00:00
</slot>
</template>
</CPButton>
2022-09-06 12:03:37 +00:00
</div>
</template>
</div>
</transition>
</Portal>
</template>
<script>
2022-12-08 11:04:25 +00:00
import Button from 'primevue/button';
2022-09-06 12:03:37 +00:00
import ConfirmationEventBus from 'primevue/confirmationeventbus';
2022-12-08 11:04:25 +00:00
import FocusTrap from 'primevue/focustrap';
2022-09-06 12:03:37 +00:00
import OverlayEventBus from 'primevue/overlayeventbus';
import Portal from 'primevue/portal';
2022-12-08 11:04:25 +00:00
import { ConnectedOverlayScrollHandler, DomHandler, ZIndexUtils } from 'primevue/utils';
import BaseConfirmPopup from './BaseConfirmPopup.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'ConfirmPopup',
2023-05-29 18:41:50 +00:00
extends: BaseConfirmPopup,
2022-09-06 12:03:37 +00:00
inheritAttrs: false,
data() {
return {
visible: false,
confirmation: null,
autoFocusAccept: null,
autoFocusReject: null,
2024-06-10 07:43:41 +00:00
focusElementOnHide: null
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
target: null,
outsideClickListener: null,
scrollHandler: null,
resizeListener: null,
container: null,
confirmListener: null,
closeListener: null,
mounted() {
this.confirmListener = (options) => {
if (!options) {
return;
}
if (options.group === this.group) {
this.confirmation = options;
this.target = options.target;
2022-12-08 11:04:25 +00:00
if (this.confirmation.onShow) {
this.confirmation.onShow();
}
2022-09-06 12:03:37 +00:00
this.visible = true;
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.closeListener = () => {
this.visible = false;
this.confirmation = null;
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
ConfirmationEventBus.on('confirm', this.confirmListener);
ConfirmationEventBus.on('close', this.closeListener);
},
beforeUnmount() {
ConfirmationEventBus.off('confirm', this.confirmListener);
ConfirmationEventBus.off('close', this.closeListener);
this.unbindOutsideClickListener();
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.scrollHandler) {
this.scrollHandler.destroy();
this.scrollHandler = null;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.unbindResizeListener();
if (this.container) {
ZIndexUtils.clear(this.container);
this.container = null;
}
this.target = null;
this.confirmation = null;
},
methods: {
2022-09-14 11:26:01 +00:00
accept() {
2022-09-06 12:03:37 +00:00
if (this.confirmation.accept) {
this.confirmation.accept();
}
this.visible = false;
},
reject() {
if (this.confirmation.reject) {
this.confirmation.reject();
}
this.visible = false;
},
2022-12-08 11:04:25 +00:00
onHide() {
if (this.confirmation.onHide) {
this.confirmation.onHide();
}
this.visible = false;
},
onAcceptKeydown(event) {
if (event.code === 'Space' || event.code === 'Enter' || event.code === 'NumpadEnter') {
2022-12-08 11:04:25 +00:00
this.accept();
DomHandler.focus(this.target);
event.preventDefault();
}
},
onRejectKeydown(event) {
if (event.code === 'Space' || event.code === 'Enter' || event.code === 'NumpadEnter') {
2022-12-08 11:04:25 +00:00
this.reject();
DomHandler.focus(this.target);
event.preventDefault();
}
},
2022-09-06 12:03:37 +00:00
onEnter(el) {
this.autoFocusAccept = this.confirmation.defaultFocus === undefined || this.confirmation.defaultFocus === 'accept' ? true : false;
this.autoFocusReject = this.confirmation.defaultFocus === 'reject' ? true : false;
this.focusElementOnHide = document.activeElement;
2022-09-06 12:03:37 +00:00
this.bindOutsideClickListener();
this.bindScrollListener();
this.bindResizeListener();
ZIndexUtils.set('overlay', el, this.$primevue.config.zIndex.overlay);
},
onAfterEnter() {
this.focus();
},
2022-09-06 12:03:37 +00:00
onLeave() {
this.autoFocusAccept = null;
this.autoFocusReject = null;
DomHandler.focus(this.focusElementOnHide);
this.focusElementOnHide = null;
2022-09-06 12:03:37 +00:00
this.unbindOutsideClickListener();
this.unbindScrollListener();
this.unbindResizeListener();
},
onAfterLeave(el) {
ZIndexUtils.clear(el);
},
alignOverlay() {
2024-01-21 10:43:57 +00:00
DomHandler.absolutePosition(this.container, this.target, false);
2022-09-06 12:03:37 +00:00
const containerOffset = DomHandler.getOffset(this.container);
const targetOffset = DomHandler.getOffset(this.target);
let arrowLeft = 0;
if (containerOffset.left < targetOffset.left) {
arrowLeft = targetOffset.left - containerOffset.left;
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.container.style.setProperty('--overlayArrowLeft', `${arrowLeft}px`);
if (containerOffset.top < targetOffset.top) {
this.container.setAttribute('data-p-confirm-popup-flipped', 'true');
!this.isUnstyled && DomHandler.addClass(this.container, 'p-confirm-popup-flipped');
2022-09-06 12:03:37 +00:00
}
},
bindOutsideClickListener() {
if (!this.outsideClickListener) {
this.outsideClickListener = (event) => {
if (this.visible && this.container && !this.container.contains(event.target) && !this.isTargetClicked(event)) {
2022-12-08 11:04:25 +00:00
if (this.confirmation.onHide) {
this.confirmation.onHide();
}
2022-09-06 12:03:37 +00:00
this.visible = false;
} else {
this.alignOverlay();
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
document.addEventListener('click', this.outsideClickListener);
}
},
unbindOutsideClickListener() {
if (this.outsideClickListener) {
document.removeEventListener('click', this.outsideClickListener);
this.outsideClickListener = null;
}
},
bindScrollListener() {
if (!this.scrollHandler) {
this.scrollHandler = new ConnectedOverlayScrollHandler(this.target, () => {
if (this.visible) {
this.visible = false;
}
});
}
this.scrollHandler.bindScrollListener();
},
unbindScrollListener() {
if (this.scrollHandler) {
this.scrollHandler.unbindScrollListener();
}
},
bindResizeListener() {
if (!this.resizeListener) {
this.resizeListener = () => {
if (this.visible && !DomHandler.isTouchDevice()) {
this.visible = false;
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
window.addEventListener('resize', this.resizeListener);
}
},
unbindResizeListener() {
if (this.resizeListener) {
window.removeEventListener('resize', this.resizeListener);
this.resizeListener = null;
}
},
2022-12-08 11:04:25 +00:00
focus() {
let focusTarget = this.container.querySelector('[autofocus]');
if (focusTarget) {
focusTarget.focus({ preventScroll: true }); // Firefox requires preventScroll
2022-12-08 11:04:25 +00:00
}
},
2022-09-06 12:03:37 +00:00
isTargetClicked(event) {
return this.target && (this.target === event.target || this.target.contains(event.target));
},
containerRef(el) {
this.container = el;
},
onOverlayClick(event) {
OverlayEventBus.emit('overlay-click', {
originalEvent: event,
target: this.target
});
2022-12-08 11:04:25 +00:00
},
onOverlayKeydown(event) {
if (event.code === 'Escape') {
ConfirmationEventBus.emit('close', this.closeListener);
DomHandler.focus(this.target);
}
2023-05-29 18:41:50 +00:00
},
getCXOptions(icon, iconProps) {
return { contenxt: { icon, iconClass: iconProps.class } };
2022-09-06 12:03:37 +00:00
}
},
computed: {
message() {
return this.confirmation ? this.confirmation.message : null;
},
acceptLabel() {
2022-09-14 11:26:01 +00:00
return this.confirmation ? this.confirmation.acceptLabel || this.$primevue.config.locale.accept : null;
2022-09-06 12:03:37 +00:00
},
rejectLabel() {
2022-09-14 11:26:01 +00:00
return this.confirmation ? this.confirmation.rejectLabel || this.$primevue.config.locale.reject : null;
2022-09-06 12:03:37 +00:00
},
acceptIcon() {
return this.confirmation ? this.confirmation.acceptIcon : null;
},
rejectIcon() {
return this.confirmation ? this.confirmation.rejectIcon : null;
}
},
components: {
2022-09-14 11:26:01 +00:00
CPButton: Button,
Portal: Portal
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>