primevue-mirror/components/lib/dynamicdialog/DynamicDialog.vue

78 lines
2.9 KiB
Vue
Raw Normal View History

2022-09-06 12:03:37 +00:00
<template>
<template v-for="(instance, key) in instanceMap" :key="key">
2023-05-29 19:00:59 +00:00
<DDialog v-model:visible="instance.visible" :_instance="instance" v-bind="instance.options.props" :pt="pt" :unstyled="unstyled" @hide="onDialogHide(instance)" @after-hide="onDialogAfterHide">
2022-09-14 11:26:01 +00:00
<template v-if="instance.options.templates && instance.options.templates.header" #header>
<component v-for="(header, index) in getTemplateItems(instance.options.templates.header)" :is="header" :key="index + '_header'" v-bind="instance.options.emits"></component>
2022-09-06 12:03:37 +00:00
</template>
<component :is="instance.content" v-bind="instance.options.emits"></component>
2022-09-14 11:26:01 +00:00
<template v-if="instance.options.templates && instance.options.templates.footer" #footer>
<component v-for="(footer, index) in getTemplateItems(instance.options.templates.footer)" :is="footer" :key="index + '_footer'" v-bind="instance.options.emits"></component>
2022-09-06 12:03:37 +00:00
</template>
</DDialog>
</template>
</template>
<script>
2023-04-25 10:23:19 +00:00
import BaseComponent from 'primevue/basecomponent';
2022-09-06 12:03:37 +00:00
import Dialog from 'primevue/dialog';
2023-03-07 12:36:48 +00:00
import DynamicDialogEventBus from 'primevue/dynamicdialogeventbus';
import { UniqueComponentId } from 'primevue/utils';
2022-09-06 12:03:37 +00:00
export default {
name: 'DynamicDialog',
2023-04-25 10:23:19 +00:00
extends: BaseComponent,
2022-09-06 12:03:37 +00:00
inheritAttrs: false,
data() {
return {
instanceMap: {}
2022-09-14 11:26:01 +00:00
};
2022-09-06 12:03:37 +00:00
},
openListener: null,
closeListener: null,
currentInstance: null,
mounted() {
this.openListener = ({ instance }) => {
const key = UniqueComponentId() + '_dynamic_dialog';
instance.visible = true;
instance.key = key;
this.instanceMap[key] = instance;
};
this.closeListener = ({ instance, params }) => {
const key = instance.key;
const currentInstance = this.instanceMap[key];
if (currentInstance) {
currentInstance.visible = false;
2022-09-14 11:26:01 +00:00
currentInstance.options.onClose && currentInstance.options.onClose({ data: params, type: 'config-close' });
2022-09-06 12:03:37 +00:00
this.currentInstance = currentInstance;
}
};
DynamicDialogEventBus.on('open', this.openListener);
DynamicDialogEventBus.on('close', this.closeListener);
},
beforeUnmount() {
DynamicDialogEventBus.off('open', this.openListener);
DynamicDialogEventBus.off('close', this.closeListener);
},
methods: {
onDialogHide(instance) {
!this.currentInstance && instance.options.onClose && instance.options.onClose({ type: 'dialog-close' });
},
onDialogAfterHide() {
2022-09-14 11:26:01 +00:00
this.currentInstance && delete this.currentInstance;
2022-09-06 12:03:37 +00:00
this.currentInstance = null;
},
getTemplateItems(template) {
return Array.isArray(template) ? template : [template];
}
},
components: {
2022-09-14 11:26:01 +00:00
DDialog: Dialog
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>