2018-07-10 13:35:43 +00:00
|
|
|
<template>
|
|
|
|
<div class="modal-container">
|
|
|
|
<modal
|
|
|
|
:key="modal.id"
|
|
|
|
v-for="modal in modals"
|
2018-07-15 11:39:40 +00:00
|
|
|
v-bind="modal.modalProps"
|
2018-07-10 13:35:43 +00:00
|
|
|
@close-modal="onModalClose(modal.id)"
|
|
|
|
></modal>
|
|
|
|
<div class="modal-backdrop show" v-show="modals.length"></div>
|
|
|
|
</div>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
import Modal from './Modal';
|
|
|
|
import Plugin from './plugin';
|
2018-09-21 05:28:53 +00:00
|
|
|
import ErrorModal from './ErrorModal';
|
2018-07-10 13:35:43 +00:00
|
|
|
export default {
|
|
|
|
name: 'ModalContainer',
|
|
|
|
components: {
|
|
|
|
Modal
|
|
|
|
},
|
|
|
|
data() {
|
|
|
|
return {
|
|
|
|
currentId: 0,
|
|
|
|
modals: []
|
|
|
|
}
|
|
|
|
},
|
|
|
|
created() {
|
|
|
|
Plugin.modalContainer = this;
|
|
|
|
Plugin.event.$on('hide', (id) => {
|
|
|
|
if (!id) {
|
|
|
|
console.warn(`id not provided in $modal.hide method, the last modal in the stack will be hidden`);
|
|
|
|
}
|
|
|
|
this.onModalClose(id);
|
|
|
|
});
|
2018-09-21 05:28:53 +00:00
|
|
|
frappe.events.on('throw', ({ message, stackTrace }) => {
|
|
|
|
this.$modal.show({
|
|
|
|
modalProps: {
|
|
|
|
title: 'Something went wrong',
|
|
|
|
noFooter: true
|
|
|
|
},
|
|
|
|
component: ErrorModal,
|
|
|
|
props: {
|
|
|
|
message,
|
|
|
|
stackTrace
|
|
|
|
}
|
2018-09-28 13:12:34 +00:00
|
|
|
});
|
2018-09-21 05:28:53 +00:00
|
|
|
});
|
2018-07-10 13:35:43 +00:00
|
|
|
},
|
|
|
|
methods: {
|
2018-07-15 11:39:40 +00:00
|
|
|
add({ component, props = {}, events = {}, modalProps = {} }) {
|
2018-07-10 13:35:43 +00:00
|
|
|
this.currentId++;
|
|
|
|
this.modals.push({
|
|
|
|
id: this.currentId,
|
2018-07-15 11:39:40 +00:00
|
|
|
modalProps: Object.assign({}, modalProps, {
|
|
|
|
component,
|
|
|
|
props,
|
|
|
|
events
|
|
|
|
})
|
2018-07-10 13:35:43 +00:00
|
|
|
});
|
|
|
|
return this.currentId;
|
|
|
|
},
|
|
|
|
removeModal(id) {
|
|
|
|
if (!id) {
|
|
|
|
id = this.currentId;
|
|
|
|
}
|
|
|
|
this.currentId--;
|
|
|
|
this.modals = this.modals.filter(modal => modal.id !== id);
|
|
|
|
},
|
|
|
|
onModalClose(id) {
|
|
|
|
if (id) {
|
|
|
|
const modal = this.modals.find(modal => modal.id === id);
|
2018-07-15 11:39:40 +00:00
|
|
|
modal.modalProps.events.onClose && modal.modalProps.events.onClose();
|
2018-07-10 13:35:43 +00:00
|
|
|
}
|
|
|
|
this.removeModal(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-21 05:28:53 +00:00
|
|
|
</script>
|