primevue-mirror/components/blockui/BlockUI.vue

116 lines
2.8 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
2022-12-08 11:04:25 +00:00
<div ref="container" class="p-blockui-container" :aria-busy="isBlocked">
2022-09-06 12:03:37 +00:00
<slot></slot>
</div>
</template>
<script>
2022-09-14 11:26:01 +00:00
import { DomHandler, ZIndexUtils } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'BlockUI',
emits: ['block', 'unblock'],
props: {
blocked: {
type: Boolean,
default: false
},
fullScreen: {
type: Boolean,
default: false
},
baseZIndex: {
type: Number,
default: 0
},
autoZIndex: {
type: Boolean,
default: true
}
},
mask: null,
2022-12-08 11:04:25 +00:00
data() {
return {
isBlocked: false
};
},
2022-09-14 11:26:01 +00:00
watch: {
blocked(newValue) {
if (newValue === true) this.block();
else this.unblock();
}
},
2022-09-06 12:03:37 +00:00
mounted() {
if (this.blocked) {
this.block();
}
},
methods: {
block() {
let styleClass = 'p-blockui p-component-overlay p-component-overlay-enter';
2022-09-14 11:26:01 +00:00
2022-09-06 12:03:37 +00:00
if (this.fullScreen) {
styleClass += ' p-blockui-document';
this.mask = document.createElement('div');
this.mask.setAttribute('class', styleClass);
document.body.appendChild(this.mask);
DomHandler.addClass(document.body, 'p-overflow-hidden');
document.activeElement.blur();
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.mask = document.createElement('div');
this.mask.setAttribute('class', styleClass);
this.$refs.container.appendChild(this.mask);
}
if (this.autoZIndex) {
ZIndexUtils.set('modal', this.mask, this.baseZIndex + this.$primevue.config.zIndex.modal);
}
2022-12-08 11:04:25 +00:00
this.isBlocked = true;
2022-09-06 12:03:37 +00:00
this.$emit('block');
},
unblock() {
DomHandler.addClass(this.mask, 'p-component-overlay-leave');
this.mask.addEventListener('animationend', () => {
this.removeMask();
});
},
removeMask() {
ZIndexUtils.clear(this.mask);
2022-09-14 11:26:01 +00:00
if (this.fullScreen) {
2022-09-06 12:03:37 +00:00
document.body.removeChild(this.mask);
DomHandler.removeClass(document.body, 'p-overflow-hidden');
2022-09-14 11:26:01 +00:00
} else {
2022-09-06 12:03:37 +00:00
this.$refs.container.removeChild(this.mask);
}
2022-12-08 11:04:25 +00:00
this.isBlocked = false;
2022-09-06 12:03:37 +00:00
this.$emit('unblock');
}
}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
</script>
<style>
.p-blockui-container {
position: relative;
}
.p-blockui {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
}
.p-blockui.p-component-overlay {
position: absolute;
}
.p-blockui-document.p-component-overlay {
position: fixed;
}
</style>