2022-09-06 12:03:37 +00:00
|
|
|
<template>
|
|
|
|
<Portal>
|
2024-02-11 23:48:46 +00:00
|
|
|
<div ref="container" :class="cx('root')" :style="sx('root', true, { position })" v-bind="ptmi('root')">
|
2024-05-26 09:41:32 +00:00
|
|
|
<transition-group name="p-toast-message" tag="div" @enter="onEnter" @leave="onLeave" v-bind="{ ...ptm('transition') }">
|
2023-04-14 12:03:58 +00:00
|
|
|
<ToastMessage
|
|
|
|
v-for="msg of messages"
|
|
|
|
:key="msg.id"
|
|
|
|
:message="msg"
|
|
|
|
:templates="$slots"
|
|
|
|
:closeIcon="closeIcon"
|
|
|
|
:infoIcon="infoIcon"
|
|
|
|
:warnIcon="warnIcon"
|
|
|
|
:errorIcon="errorIcon"
|
|
|
|
:successIcon="successIcon"
|
|
|
|
:closeButtonProps="closeButtonProps"
|
|
|
|
@close="remove($event)"
|
2023-04-28 13:39:19 +00:00
|
|
|
:pt="pt"
|
2023-04-14 12:03:58 +00:00
|
|
|
/>
|
2022-09-06 12:03:37 +00:00
|
|
|
</transition-group>
|
|
|
|
</div>
|
|
|
|
</Portal>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2022-12-08 11:04:25 +00:00
|
|
|
import Portal from 'primevue/portal';
|
2022-09-06 12:03:37 +00:00
|
|
|
import ToastEventBus from 'primevue/toasteventbus';
|
2023-08-11 01:34:02 +00:00
|
|
|
import { DomHandler, ObjectUtils, UniqueComponentId, ZIndexUtils } from 'primevue/utils';
|
2023-06-05 11:24:13 +00:00
|
|
|
import BaseToast from './BaseToast.vue';
|
2022-09-06 12:03:37 +00:00
|
|
|
import ToastMessage from './ToastMessage.vue';
|
|
|
|
|
|
|
|
var messageIdx = 0;
|
|
|
|
|
|
|
|
export default {
|
|
|
|
name: 'Toast',
|
2023-05-25 21:53:07 +00:00
|
|
|
extends: BaseToast,
|
2022-09-06 12:03:37 +00:00
|
|
|
inheritAttrs: false,
|
2023-03-07 12:36:38 +00:00
|
|
|
emits: ['close', 'life-end'],
|
2022-09-06 12:03:37 +00:00
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
messages: []
|
2022-09-14 11:26:01 +00:00
|
|
|
};
|
2022-09-06 12:03:37 +00:00
|
|
|
},
|
|
|
|
styleElement: null,
|
|
|
|
mounted() {
|
|
|
|
ToastEventBus.on('add', this.onAdd);
|
2023-10-05 12:54:37 +00:00
|
|
|
ToastEventBus.on('remove', this.onRemove);
|
2022-09-06 12:03:37 +00:00
|
|
|
ToastEventBus.on('remove-group', this.onRemoveGroup);
|
|
|
|
ToastEventBus.on('remove-all-groups', this.onRemoveAllGroups);
|
|
|
|
|
|
|
|
if (this.breakpoints) {
|
|
|
|
this.createStyle();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
beforeUnmount() {
|
|
|
|
this.destroyStyle();
|
|
|
|
|
|
|
|
if (this.$refs.container && this.autoZIndex) {
|
|
|
|
ZIndexUtils.clear(this.$refs.container);
|
|
|
|
}
|
|
|
|
|
|
|
|
ToastEventBus.off('add', this.onAdd);
|
2023-10-05 12:54:37 +00:00
|
|
|
ToastEventBus.off('remove', this.onRemove);
|
2022-09-06 12:03:37 +00:00
|
|
|
ToastEventBus.off('remove-group', this.onRemoveGroup);
|
|
|
|
ToastEventBus.off('remove-all-groups', this.onRemoveAllGroups);
|
|
|
|
},
|
|
|
|
methods: {
|
|
|
|
add(message) {
|
|
|
|
if (message.id == null) {
|
|
|
|
message.id = messageIdx++;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.messages = [...this.messages, message];
|
|
|
|
},
|
2023-03-07 12:36:38 +00:00
|
|
|
remove(params) {
|
2024-04-24 09:23:32 +00:00
|
|
|
const index = this.messages.findIndex((m) => m.id === params.message.id);
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2024-04-24 09:23:32 +00:00
|
|
|
if (index !== -1) {
|
|
|
|
this.messages.splice(index, 1);
|
|
|
|
this.$emit(params.type, { message: params.message });
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
onAdd(message) {
|
|
|
|
if (this.group == message.group) {
|
|
|
|
this.add(message);
|
|
|
|
}
|
|
|
|
},
|
2023-10-05 12:54:37 +00:00
|
|
|
onRemove(message) {
|
|
|
|
this.remove({ message, type: 'close' });
|
|
|
|
},
|
2022-09-06 12:03:37 +00:00
|
|
|
onRemoveGroup(group) {
|
|
|
|
if (this.group === group) {
|
|
|
|
this.messages = [];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onRemoveAllGroups() {
|
|
|
|
this.messages = [];
|
|
|
|
},
|
|
|
|
onEnter() {
|
|
|
|
this.$refs.container.setAttribute(this.attributeSelector, '');
|
|
|
|
|
|
|
|
if (this.autoZIndex) {
|
|
|
|
ZIndexUtils.set('modal', this.$refs.container, this.baseZIndex || this.$primevue.config.zIndex.modal);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
onLeave() {
|
|
|
|
if (this.$refs.container && this.autoZIndex && ObjectUtils.isEmpty(this.messages)) {
|
2022-12-08 11:04:25 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
ZIndexUtils.clear(this.$refs.container);
|
|
|
|
}, 200);
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
createStyle() {
|
2023-06-05 11:24:13 +00:00
|
|
|
if (!this.styleElement && !this.isUnstyled) {
|
2022-09-06 12:03:37 +00:00
|
|
|
this.styleElement = document.createElement('style');
|
|
|
|
this.styleElement.type = 'text/css';
|
2023-08-11 01:34:02 +00:00
|
|
|
DomHandler.setAttribute(this.styleElement, 'nonce', this.$primevue?.config?.csp?.nonce);
|
2022-09-06 12:03:37 +00:00
|
|
|
document.head.appendChild(this.styleElement);
|
|
|
|
|
|
|
|
let innerHTML = '';
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
for (let breakpoint in this.breakpoints) {
|
|
|
|
let breakpointStyle = '';
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
for (let styleProp in this.breakpoints[breakpoint]) {
|
2023-12-21 07:56:38 +00:00
|
|
|
breakpointStyle += styleProp + ':' + this.breakpoints[breakpoint][styleProp] + '!important;';
|
2022-09-06 12:03:37 +00:00
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
|
2022-09-06 12:03:37 +00:00
|
|
|
innerHTML += `
|
|
|
|
@media screen and (max-width: ${breakpoint}) {
|
|
|
|
.p-toast[${this.attributeSelector}] {
|
|
|
|
${breakpointStyle}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.styleElement.innerHTML = innerHTML;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
destroyStyle() {
|
|
|
|
if (this.styleElement) {
|
|
|
|
document.head.removeChild(this.styleElement);
|
|
|
|
this.styleElement = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
computed: {
|
|
|
|
attributeSelector() {
|
|
|
|
return UniqueComponentId();
|
|
|
|
}
|
2022-09-14 11:26:01 +00:00
|
|
|
},
|
|
|
|
components: {
|
|
|
|
ToastMessage: ToastMessage,
|
|
|
|
Portal: Portal
|
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>
|