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

464 lines
14 KiB
Vue
Raw Normal View History

2019-01-31 12:15:32 +00:00
<template>
2020-02-04 07:46:11 +00:00
<div ref="mask" :class="maskClass" v-if="maskVisible">
<transition name="p-dialog" @before-enter="onBeforeEnter" @enter="onEnter" @leave="onLeave" @after-leave="onAfterLeave" @appear="onAppear">
2020-02-04 07:46:11 +00:00
<div ref="dialog" :class="dialogClass" :style="dialogStyle" v-if="visible" v-bind="$attrs" v-on="listeners" role="dialog" :aria-labelledby="ariaLabelledById" :aria-modal="modal">
<div class="p-dialog-titlebar" v-if="showHeader">
<slot name="header">
<span :id="ariaLabelledById" class="p-dialog-title" v-if="header" >{{header}}</span>
</slot>
<div class="p-dialog-titlebar-icons">
<button class="p-dialog-titlebar-icon p-dialog-titlebar-maximize p-link" @click="maximize" v-if="maximizable" type="button">
<span :class="maximizeIconClass"></span>
</button>
2020-01-13 10:06:08 +00:00
<button class="p-dialog-titlebar-icon p-dialog-titlebar-close p-link" @click="close" v-if="closable" :aria-label="ariaCloseLabel" type="button">
<span class="p-dialog-titlebar-close-icon pi pi-times"></span>
</button>
</div>
</div>
<div class="p-dialog-content" :style="contentStyle">
<slot></slot>
</div>
<div class="p-dialog-footer" v-if="footer || $slots.footer">
<slot name="footer">{{footer}}</slot>
2019-01-31 12:15:32 +00:00
</div>
</div>
</transition>
</div>
2019-01-31 12:15:32 +00:00
</template>
<script>
import UniqueComponentId from '../utils/UniqueComponentId';
2019-01-31 12:15:32 +00:00
import DomHandler from '../utils/DomHandler';
export default {
inheritAttrs: false,
2019-01-31 12:15:32 +00:00
props: {
header: null,
footer: null,
2019-01-31 12:15:32 +00:00
visible: Boolean,
modal: Boolean,
contentStyle: null,
2019-01-31 12:15:32 +00:00
rtl: Boolean,
maximizable: Boolean,
2019-01-31 12:15:32 +00:00
closable: {
type: Boolean,
default: true
},
showHeader: {
type: Boolean,
default: true
},
baseZIndex: {
type: Number,
default: 0
},
autoZIndex: {
type: Boolean,
default: true
},
ariaCloseLabel: {
type: String,
default: 'close'
},
position: {
type: String,
default: 'center'
2019-01-31 15:42:58 +00:00
}
2019-01-31 12:15:32 +00:00
},
data() {
return {
dialogClasses: null,
2020-01-17 13:27:16 +00:00
dialogStyles: null,
maskVisible: this.visible,
maximized: false
}
},
documentKeydownListener: null,
updated() {
this.removeStylesFromMask();
2020-01-30 07:13:26 +00:00
if (this.visible && !this.maskVisible) {
this.maskVisible = true;
}
},
mounted() {
this.removeStylesFromMask();
},
beforeDestroy() {
2020-02-03 09:44:20 +00:00
this.disableDocumentSettings();
},
2019-01-31 12:15:32 +00:00
methods: {
2019-01-31 15:42:58 +00:00
close() {
this.$emit('update:visible', false);
2019-01-31 12:15:32 +00:00
},
onBeforeEnter(el) {
2019-01-31 12:15:32 +00:00
if (this.autoZIndex) {
el.style.zIndex = String(this.baseZIndex + DomHandler.generateZIndex());
2019-01-31 12:15:32 +00:00
}
},
onEnter() {
2020-02-04 07:46:11 +00:00
this.$refs.mask.style.zIndex = String(parseInt(this.$refs.dialog.style.zIndex, 10) - 1);
this.$emit('show');
this.focus();
2020-02-03 09:44:20 +00:00
this.enableDocumentSettings();
2019-01-31 12:15:32 +00:00
},
onLeave() {
2019-01-31 12:15:32 +00:00
this.$emit('hide');
},
onAfterLeave() {
2020-01-30 07:13:26 +00:00
this.maskVisible = false;
2020-02-03 09:44:20 +00:00
this.disableDocumentSettings();
2019-01-31 12:15:32 +00:00
},
2019-12-09 14:48:23 +00:00
onAppear() {
if (this.visible) {
2019-12-09 14:48:23 +00:00
this.onEnter();
}
},
2019-01-31 12:15:32 +00:00
focus() {
2020-02-04 07:46:11 +00:00
let focusable = DomHandler.findSingle(this.$refs.dialog, 'input,button');
2019-01-31 12:15:32 +00:00
if (focusable) {
focusable.focus();
}
},
2020-02-03 09:44:20 +00:00
maximize() {
this.maximized = !this.maximized;
if (!this.modal) {
if (this.maximized)
DomHandler.addClass(document.body, 'p-overflow-hidden');
else
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
},
enableDocumentSettings() {
if (this.modal) {
2019-01-31 12:15:32 +00:00
DomHandler.addClass(document.body, 'p-overflow-hidden');
this.bindDocumentKeydownListener();
2019-01-31 12:15:32 +00:00
}
2020-02-03 09:44:20 +00:00
else if (this.maximizable && this.maximized) {
DomHandler.addClass(document.body, 'p-overflow-hidden');
}
2019-01-31 12:15:32 +00:00
},
2020-02-03 09:44:20 +00:00
disableDocumentSettings() {
if (this.modal) {
2019-01-31 12:15:32 +00:00
DomHandler.removeClass(document.body, 'p-overflow-hidden');
this.unbindDocumentKeydownListener();
}
2020-02-03 09:44:20 +00:00
else if (this.maximizable && this.maximized) {
DomHandler.removeClass(document.body, 'p-overflow-hidden');
}
},
onKeyDown(event) {
if (event.which === 9) {
event.preventDefault();
2020-02-04 07:46:11 +00:00
let focusableElements = DomHandler.getFocusableElements(this.$refs.dialog);
if (focusableElements && focusableElements.length > 0) {
if (!document.activeElement) {
focusableElements[0].focus();
}
else {
let focusedIndex = focusableElements.indexOf(document.activeElement);
if (event.shiftKey) {
if (focusedIndex == -1 || focusedIndex === 0)
focusableElements[focusableElements.length - 1].focus();
else
focusableElements[focusedIndex - 1].focus();
}
else {
if (focusedIndex == -1 || focusedIndex === (focusableElements.length - 1))
focusableElements[0].focus();
else
focusableElements[focusedIndex + 1].focus();
}
}
}
}
},
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;
2019-01-31 12:15:32 +00:00
}
},
getPositionClass() {
2020-02-06 11:57:20 +00:00
const positions = ['left', 'right', 'top', 'topleft', 'topright', 'bottom', 'bottomleft', 'bottomright'];
const pos = positions.find(item => item === this.position);
return pos ? `p-dialog-${pos}` : '';
},
removeStylesFromMask() {
if (this.$refs.mask) {
this.dialogStyles = this.$vnode.data.style;
if (this.dialogStyles) {
Object.keys(this.dialogStyles).forEach((key) => {
this.$refs.mask.style[key] = '';
});
}
this.dialogClasses = this.$vnode.data.class || this.$vnode.data.staticClass;
if (this.dialogClasses) {
2020-02-04 07:46:11 +00:00
this.$refs.mask.classList = 'p-dialog-mask' + (this.modal && ' p-component-overlay ') + this.getPositionClass();
}
}
2019-01-31 12:15:32 +00:00
}
},
computed: {
listeners() {
return {
...this.$listeners
};
},
2020-02-04 07:46:11 +00:00
maskClass() {
return ['p-dialog-mask', {
'p-component-overlay': this.modal,
}, this.getPositionClass()];
},
2020-02-04 07:46:11 +00:00
dialogClass() {
2019-01-31 12:15:32 +00:00
return ['p-dialog p-component', {
'p-dialog-rtl': this.rtl,
'p-dialog-maximized': this.maximizable && this.maximized
}, this.dialogClasses];
},
maximizeIconClass() {
return ['p-dialog-titlebar-maximize-icon pi', {
'pi-window-maximize': !this.maximized,
'pi-window-minimize': this.maximized
}];
},
2020-02-04 07:46:11 +00:00
dialogStyle() {
return this.dialogStyles;
},
ariaId() {
return UniqueComponentId();
},
ariaLabelledById() {
return this.header != null ? this.ariaId + '_header' : null;
2019-01-31 12:15:32 +00:00
}
}
}
</script>
2019-05-25 21:13:31 +00:00
<style>
2020-02-04 07:46:11 +00:00
.p-dialog-mask {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
pointer-events: none;
}
2020-02-04 07:46:11 +00:00
.p-dialog-mask.p-component-overlay {
pointer-events: auto;
}
2019-05-25 21:13:31 +00:00
.p-dialog {
2020-01-31 13:34:35 +00:00
display: flex;
flex-direction: column;
2019-05-25 21:13:31 +00:00
padding: 0;
pointer-events: auto;
2020-01-23 08:27:24 +00:00
max-height: 90%;
transform: scale(1);
2019-05-25 21:13:31 +00:00
}
.p-dialog .p-dialog-titlebar {
padding: .5em .75em;
2019-05-25 21:13:31 +00:00
position: relative;
border: 0;
flex-shrink: 0;
2019-05-25 21:13:31 +00:00
}
.p-dialog .p-dialog-content {
position: relative;
border: 0;
padding: .5em .75em;
background: none;
zoom: 1;
2020-01-31 13:34:35 +00:00
overflow-y: auto;
2019-05-25 21:13:31 +00:00
}
.p-dialog-resizable .p-dialog-content {
overflow: auto;
}
.p-dialog .p-resizable-handle {
width: 14px;
height: 14px;
right: 3px;
bottom: 3px;
position: absolute;
font-size: .1px;
display: block;
cursor: se-resize;
}
.p-draggable .p-dialog-titlebar {
cursor: move;
}
.p-dialog .p-dialog-titlebar-icons {
float: right;
}
.p-dialog .p-dialog-titlebar-icons:after {
content: "";
display: table;
clear: both;
}
.p-dialog .p-dialog-titlebar-icon {
text-decoration: none;
padding: .125em;
cursor: pointer;
display: inline-block;
vertical-align: middle;
border: 1px solid transparent;
}
.p-dialog .p-dialog-titlebar-icon span {
display: block;
margin: 0;
}
.p-dialog-footer {
padding: 1em;
text-align: right;
flex-shrink: 0;
2019-05-25 21:13:31 +00:00
}
/* ConfirmDialog */
.p-confirmdialog {
width: 30em;
}
.p-confirmdialog.p-dialog .p-dialog-content {
padding: 1em 2em;
}
.p-confirmdialog .p-dialog-content .p-confirmdialog-icon {
font-size: 1.5em;
vertical-align: middle;
margin-right: .5em;
}
.p-confirmdialog .p-dialog-content .p-confirmdialog-message {
vertical-align: middle;
}
/* Fluid */
.p-fluid .p-dialog-footer .p-button {
width: auto;
}
/* RTL */
.p-rtl .p-dialog .p-dialog-titlebar-close {
float: left;
}
.p-rtl .p-dialog .p-dialog-footer {
text-align: left;
}
@media screen and (max-width: 40em) {
.p-confirmdialog {
width: 90%;
}
}
/* Animation */
2020-02-04 07:46:11 +00:00
/* Center */
2019-05-25 21:13:31 +00:00
.p-dialog-enter-active {
transition: all 150ms cubic-bezier(0, 0, 0.2, 1);
}
.p-dialog-leave-active {
2020-01-30 08:05:19 +00:00
transition: all 150ms cubic-bezier(0.4, 0.0, 0.2, 1);
2019-05-25 21:13:31 +00:00
}
.p-dialog-enter,
.p-dialog-leave-to {
opacity: 0;
2020-01-23 08:27:24 +00:00
transform: scale(0.7);
2019-05-25 21:13:31 +00:00
}
2020-02-04 07:46:11 +00:00
/* Top, Bottom, Left, Right, Top* and Bottom* */
.p-dialog-top .p-dialog,
.p-dialog-bottom .p-dialog,
.p-dialog-left .p-dialog,
.p-dialog-right .p-dialog,
2020-02-06 11:57:20 +00:00
.p-dialog-topleft .p-dialog,
.p-dialog-topright .p-dialog,
.p-dialog-bottomleft .p-dialog,
.p-dialog-bottomright .p-dialog {
2020-02-04 07:46:11 +00:00
margin: .75em;
transform: translate3d(0px, 0px, 0px);
}
.p-dialog-top .p-dialog-enter-active,
.p-dialog-top .p-dialog-leave-active,
.p-dialog-bottom .p-dialog-enter-active,
.p-dialog-bottom .p-dialog-leave-active,
.p-dialog-left .p-dialog-enter-active,
.p-dialog-left .p-dialog-leave-active,
.p-dialog-right .p-dialog-enter-active,
.p-dialog-right .p-dialog-leave-active,
2020-02-06 11:57:20 +00:00
.p-dialog-topleft .p-dialog-enter-active,
.p-dialog-topleft .p-dialog-leave-active,
.p-dialog-topright .p-dialog-enter-active,
.p-dialog-topright .p-dialog-leave-active,
.p-dialog-bottomleft .p-dialog-enter-active,
.p-dialog-bottomleft .p-dialog-leave-active,
.p-dialog-bottomright .p-dialog-enter-active,
.p-dialog-bottomright .p-dialog-leave-active {
2020-02-04 07:46:11 +00:00
transition: all .3s ease-out;
}
.p-dialog-top .p-dialog-enter,
.p-dialog-top .p-dialog-leave-to {
transform: translate3d(0px, -100%, 0px);
}
.p-dialog-bottom .p-dialog-enter,
.p-dialog-bottom .p-dialog-leave-to {
transform: translate3d(0px, 100%, 0px);
}
.p-dialog-left .p-dialog-enter,
.p-dialog-left .p-dialog-leave-to,
2020-02-06 11:57:20 +00:00
.p-dialog-topleft .p-dialog-enter,
.p-dialog-topleft .p-dialog-leave-to,
.p-dialog-bottomleft .p-dialog-enter,
.p-dialog-bottomleft .p-dialog-leave-to {
2020-02-04 07:46:11 +00:00
transform: translate3d(-100%, 0px, 0px);
}
.p-dialog-right .p-dialog-enter,
.p-dialog-right .p-dialog-leave-to,
2020-02-06 11:57:20 +00:00
.p-dialog-topright .p-dialog-enter,
.p-dialog-topright .p-dialog-leave-to,
.p-dialog-bottomright .p-dialog-enter,
.p-dialog-bottomright .p-dialog-leave-to {
2020-02-04 07:46:11 +00:00
transform: translate3d(100%, 0px, 0px);
}
2019-05-25 21:13:31 +00:00
/* Maximize */
.p-dialog-maximized {
-webkit-transition: none;
transition: none;
transform: none;
width: 100vw !important;
2020-01-23 08:39:52 +00:00
max-height: 100%;
height: 100%;
2019-05-25 21:13:31 +00:00
}
.p-dialog-maximized .p-dialog-content {
flex-grow: 1;
2019-05-25 21:13:31 +00:00
}
/* Position */
.p-dialog-left {
justify-content: flex-start;
}
.p-dialog-right {
justify-content: flex-end;
}
.p-dialog-top {
align-items: flex-start;
}
.p-dialog-topleft {
justify-content: flex-start;
align-items: flex-start;
}
.p-dialog-topright {
justify-content: flex-end;
align-items: flex-start;
}
.p-dialog-bottom {
align-items: flex-end;
}
.p-dialog-bottomleft {
justify-content: flex-start;
align-items: flex-end;
}
.p-dialog-bottomright {
justify-content: flex-end;
align-items: flex-end;
}
2020-01-17 13:27:16 +00:00
</style>