primevue-mirror/components/lib/dialog/Dialog.vue

395 lines
15 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<Portal :appendTo="appendTo">
2023-07-07 06:04:39 +00:00
<div v-if="containerVisible" :ref="maskRef" :class="cx('mask')" :style="sx('mask', true, { position, modal })" @click="onMaskClick" v-bind="ptm('mask')">
2022-09-06 12:03:37 +00:00
<transition name="p-dialog" @before-enter="onBeforeEnter" @enter="onEnter" @before-leave="onBeforeLeave" @leave="onLeave" @after-leave="onAfterLeave" appear>
2023-07-05 14:31:59 +00:00
<div v-if="visible" :ref="containerRef" v-focustrap="{ disabled: !modal }" :class="cx('root')" :style="sx('root')" role="dialog" :aria-labelledby="ariaLabelledById" :aria-modal="modal" v-bind="{ ...$attrs, ...ptm('root') }">
2023-05-29 10:59:47 +00:00
<div v-if="showHeader" :ref="headerContainerRef" :class="cx('header')" @mousedown="initDrag" v-bind="ptm('header')">
2022-09-06 12:03:37 +00:00
<slot name="header">
2023-05-29 10:59:47 +00:00
<span v-if="header" :id="ariaLabelledById" :class="cx('headerTitle')" v-bind="ptm('headerTitle')">{{ header }}</span>
2022-09-06 12:03:37 +00:00
</slot>
2023-05-29 10:59:47 +00:00
<div :class="cx('headerIcons')" v-bind="ptm('headerIcons')">
<button v-if="maximizable" :ref="maximizableRef" v-ripple :autofocus="focusableMax" :class="cx('maximizableButton')" @click="maximize" type="button" :tabindex="maximizable ? '0' : '-1'" v-bind="ptm('maximizableButton')">
<slot name="maximizeicon" :maximized="maximized">
2023-07-05 14:31:59 +00:00
<component :is="maximizeIconComponent" :class="[cx('maximizableIcon'), maximized ? minimizeIcon : maximizeIcon]" v-bind="ptm('maximizableIcon')" />
</slot>
2022-09-06 12:03:37 +00:00
</button>
<button
v-if="closable"
:ref="closeButtonRef"
v-ripple
:autofocus="focusableClose"
2023-05-29 10:59:47 +00:00
:class="cx('closeButton')"
@click="close"
:aria-label="closeAriaLabel"
type="button"
2023-05-03 14:16:11 +00:00
v-bind="{ ...closeButtonProps, ...ptm('closeButton') }"
>
<slot name="closeicon">
2023-07-05 14:31:59 +00:00
<component :is="closeIcon ? 'span' : 'TimesIcon'" :class="[cx('closeButtonIcon'), closeIcon]" v-bind="ptm('closeButtonIcon')"></component>
</slot>
2022-09-06 12:03:37 +00:00
</button>
</div>
</div>
<div :ref="contentRef" :class="[cx('content'), contentClass]" :style="contentStyle" v-bind="{ ...contentProps, ...ptm('content') }">
2022-09-06 12:03:37 +00:00
<slot></slot>
</div>
2023-05-29 10:59:47 +00:00
<div v-if="footer || $slots.footer" :ref="footerContainerRef" :class="cx('footer')" v-bind="ptm('footer')">
2022-09-14 11:26:01 +00:00
<slot name="footer">{{ footer }}</slot>
2022-09-06 12:03:37 +00:00
</div>
</div>
</transition>
</div>
</Portal>
</template>
<script>
2022-12-08 11:04:25 +00:00
import FocusTrap from 'primevue/focustrap';
import TimesIcon from 'primevue/icons/times';
import WindowMaximizeIcon from 'primevue/icons/windowmaximize';
import WindowMinimizeIcon from 'primevue/icons/windowminimize';
2022-09-06 12:03:37 +00:00
import Portal from 'primevue/portal';
2022-12-08 11:04:25 +00:00
import Ripple from 'primevue/ripple';
import { DomHandler, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
import { computed } from 'vue';
import BaseDialog from './BaseDialog.vue';
2022-09-06 12:03:37 +00:00
export default {
name: 'Dialog',
2023-05-29 10:59:47 +00:00
extends: BaseDialog,
2022-09-06 12:03:37 +00:00
inheritAttrs: false,
2022-09-14 11:26:01 +00:00
emits: ['update:visible', 'show', 'hide', 'after-hide', 'maximize', 'unmaximize', 'dragend'],
2022-09-06 12:03:37 +00:00
provide() {
return {
dialogRef: computed(() => this._instance)
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
data() {
return {
containerVisible: this.visible,
2022-12-08 11:04:25 +00:00
maximized: false,
focusableMax: null,
focusableClose: null
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
2023-04-14 14:10:51 +00:00
documentKeydownListener: null,
container: null,
mask: null,
content: null,
headerContainer: null,
footerContainer: null,
maximizableButton: null,
closeButton: null,
styleElement: null,
dragging: null,
documentDragListener: null,
documentDragEndListener: null,
lastPageX: null,
lastPageY: null,
2022-09-06 12:03:37 +00:00
updated() {
if (this.visible) {
this.containerVisible = this.visible;
}
},
beforeUnmount() {
this.unbindDocumentState();
this.unbindGlobalListeners();
this.destroyStyle();
if (this.mask && this.autoZIndex) {
ZIndexUtils.clear(this.mask);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.container = null;
this.mask = null;
},
mounted() {
if (this.breakpoints) {
this.createStyle();
}
},
methods: {
close() {
this.$emit('update:visible', false);
},
onBeforeEnter(el) {
el.setAttribute(this.attributeSelector, '');
},
onEnter() {
this.$emit('show');
this.focus();
this.enableDocumentSettings();
this.bindGlobalListeners();
if (this.autoZIndex) {
ZIndexUtils.set('modal', this.mask, this.baseZIndex + this.$primevue.config.zIndex.modal);
}
},
onBeforeLeave() {
if (this.modal) {
2023-05-29 10:59:47 +00:00
!this.isUnstyled && DomHandler.addClass(this.mask, 'p-component-overlay-leave');
2022-09-06 12:03:37 +00:00
}
},
onLeave() {
this.$emit('hide');
this.focusableClose = null;
this.focusableMax = null;
2022-09-06 12:03:37 +00:00
},
onAfterLeave() {
if (this.autoZIndex) {
ZIndexUtils.clear(this.mask);
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
this.containerVisible = false;
this.unbindDocumentState();
this.unbindGlobalListeners();
this.$emit('after-hide');
},
onMaskClick(event) {
if (this.dismissableMask && this.modal && this.mask === event.target) {
2022-09-06 12:03:37 +00:00
this.close();
}
},
focus() {
2022-12-08 11:04:25 +00:00
const findFocusableElement = (container) => {
return container.querySelector('[autofocus]');
};
let focusTarget = this.$slots.footer && findFocusableElement(this.footerContainer);
if (!focusTarget) {
focusTarget = this.$slots.header && findFocusableElement(this.headerContainer);
if (!focusTarget) {
focusTarget = this.$slots.default && findFocusableElement(this.content);
if (!focusTarget) {
if (this.maximizable) {
this.focusableMax = true;
focusTarget = this.maximizableButton;
} else {
this.focusableClose = true;
focusTarget = this.closeButton;
}
2022-12-08 11:04:25 +00:00
}
}
}
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (focusTarget) {
DomHandler.focus(focusTarget);
2022-09-06 12:03:37 +00:00
}
},
maximize(event) {
if (this.maximized) {
this.maximized = false;
this.$emit('unmaximize', event);
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.maximized = true;
this.$emit('maximize', event);
}
if (!this.modal) {
2023-05-29 10:59:47 +00:00
if (this.maximized) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
2023-05-29 10:59:47 +00:00
} else {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
2023-05-29 10:59:47 +00:00
}
2022-09-06 12:03:37 +00:00
}
},
enableDocumentSettings() {
if (this.modal || (this.maximizable && this.maximized)) {
2022-09-06 12:03:37 +00:00
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
},
unbindDocumentState() {
if (this.modal || (this.maximizable && this.maximized)) {
2022-09-06 12:03:37 +00:00
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
},
onKeyDown(event) {
2022-12-08 11:04:25 +00:00
if (event.code === 'Escape' && this.closeOnEscape) {
2022-09-06 12:03:37 +00:00
this.close();
}
},
bindDocumentKeyDownListener() {
if (!this.documentKeydownListener) {
this.documentKeydownListener = this.onKeyDown.bind(this);
window.document.addEventListener('keydown', this.documentKeydownListener);
}
},
unbindDocumentKeyDownListener() {
if (this.documentKeydownListener) {
window.document.removeEventListener('keydown', this.documentKeydownListener);
this.documentKeydownListener = null;
}
},
containerRef(el) {
this.container = el;
},
maskRef(el) {
this.mask = el;
},
2022-12-08 11:04:25 +00:00
contentRef(el) {
this.content = el;
},
headerContainerRef(el) {
this.headerContainer = el;
},
footerContainerRef(el) {
this.footerContainer = el;
},
maximizableRef(el) {
this.maximizableButton = el;
},
closeButtonRef(el) {
this.closeButton = el;
},
2022-09-06 12:03:37 +00:00
createStyle() {
if (!this.styleElement && !this.isUnstyled) {
2022-09-14 11:26:01 +00:00
this.styleElement = document.createElement('style');
this.styleElement.type = 'text/css';
document.head.appendChild(this.styleElement);
2022-09-06 12:03:37 +00:00
let innerHTML = '';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
for (let breakpoint in this.breakpoints) {
innerHTML += `
@media screen and (max-width: ${breakpoint}) {
.p-dialog[${this.attributeSelector}] {
width: ${this.breakpoints[breakpoint]} !important;
}
}
2022-09-14 11:26:01 +00:00
`;
2022-09-06 12:03:37 +00:00
}
this.styleElement.innerHTML = innerHTML;
2022-09-14 11:26:01 +00:00
}
},
2022-09-06 12:03:37 +00:00
destroyStyle() {
if (this.styleElement) {
document.head.removeChild(this.styleElement);
this.styleElement = null;
}
},
initDrag(event) {
2023-05-29 10:59:47 +00:00
if (DomHandler.findSingle(event.target, '[data-pc-section="headeraction"]') || DomHandler.findSingle(event.target.parentElement, '[data-pc-section="headeraction"]')) {
2022-09-06 12:03:37 +00:00
return;
}
if (this.draggable) {
this.dragging = true;
this.lastPageX = event.pageX;
this.lastPageY = event.pageY;
this.container.style.margin = '0';
2023-05-29 10:59:47 +00:00
!this.isUnstyled && DomHandler.addClass(document.body, 'p-unselectable-text');
2022-09-06 12:03:37 +00:00
}
},
bindGlobalListeners() {
if (this.draggable) {
this.bindDocumentDragListener();
this.bindDocumentDragEndListener();
}
if (this.closeOnEscape && this.closable) {
this.bindDocumentKeyDownListener();
}
},
unbindGlobalListeners() {
this.unbindDocumentDragListener();
this.unbindDocumentDragEndListener();
this.unbindDocumentKeyDownListener();
},
bindDocumentDragListener() {
this.documentDragListener = (event) => {
if (this.dragging) {
let width = DomHandler.getOuterWidth(this.container);
let height = DomHandler.getOuterHeight(this.container);
let deltaX = event.pageX - this.lastPageX;
let deltaY = event.pageY - this.lastPageY;
let offset = this.container.getBoundingClientRect();
let leftPos = offset.left + deltaX;
let topPos = offset.top + deltaY;
let viewport = DomHandler.getViewport();
this.container.style.position = 'fixed';
if (this.keepInViewport) {
2022-09-14 11:26:01 +00:00
if (leftPos >= this.minX && leftPos + width < viewport.width) {
2022-09-06 12:03:37 +00:00
this.lastPageX = event.pageX;
this.container.style.left = leftPos + 'px';
}
2022-09-14 11:26:01 +00:00
if (topPos >= this.minY && topPos + height < viewport.height) {
2022-09-06 12:03:37 +00:00
this.lastPageY = event.pageY;
this.container.style.top = topPos + 'px';
}
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.lastPageX = event.pageX;
this.container.style.left = leftPos + 'px';
this.lastPageY = event.pageY;
this.container.style.top = topPos + 'px';
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
window.document.addEventListener('mousemove', this.documentDragListener);
},
unbindDocumentDragListener() {
if (this.documentDragListener) {
window.document.removeEventListener('mousemove', this.documentDragListener);
this.documentDragListener = null;
}
},
bindDocumentDragEndListener() {
this.documentDragEndListener = (event) => {
if (this.dragging) {
this.dragging = false;
2023-05-29 10:59:47 +00:00
!this.isUnstyled && DomHandler.removeClass(document.body, 'p-unselectable-text');
2022-09-06 12:03:37 +00:00
this.$emit('dragend', event);
}
};
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
window.document.addEventListener('mouseup', this.documentDragEndListener);
},
unbindDocumentDragEndListener() {
if (this.documentDragEndListener) {
window.document.removeEventListener('mouseup', this.documentDragEndListener);
this.documentDragEndListener = null;
}
}
},
computed: {
maximizeIconComponent() {
return this.maximized ? (this.minimizeIcon ? 'span' : 'WindowMinimizeIcon') : this.maximizeIcon ? 'span' : 'WindowMaximizeIcon';
},
2022-09-06 12:03:37 +00:00
ariaId() {
return UniqueComponentId();
},
ariaLabelledById() {
2022-12-08 11:04:25 +00:00
return this.header != null || this.$attrs['aria-labelledby'] !== null ? this.ariaId + '_header' : null;
},
closeAriaLabel() {
return this.$primevue.config.locale.aria ? this.$primevue.config.locale.aria.close : undefined;
2022-09-06 12:03:37 +00:00
},
attributeSelector() {
return UniqueComponentId();
},
contentStyleClass() {
return ['p-dialog-content', this.contentClass];
}
},
directives: {
2022-12-08 11:04:25 +00:00
ripple: Ripple,
focustrap: FocusTrap
2022-09-06 12:03:37 +00:00
},
components: {
Portal: Portal,
WindowMinimizeIcon,
WindowMaximizeIcon,
TimesIcon
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>