2019-01-12 11:33:27 +08:00
|
|
|
import Vue from 'vue';
|
|
|
|
import ConfirmDialog from './ConfirmDialog';
|
2019-04-17 10:21:28 +08:00
|
|
|
import { destroyFns } from './Modal';
|
2019-08-28 10:50:19 +08:00
|
|
|
import Base from '../base';
|
2020-01-15 21:03:36 +08:00
|
|
|
import Omit from 'omit.js';
|
2018-05-04 16:02:31 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
export default function confirm(config) {
|
|
|
|
const div = document.createElement('div');
|
|
|
|
const el = document.createElement('div');
|
|
|
|
div.appendChild(el);
|
|
|
|
document.body.appendChild(div);
|
2020-01-15 21:03:36 +08:00
|
|
|
let currentConfig = { ...Omit(config, ['parentContext']), close, visible: true };
|
2018-03-07 21:36:15 +08:00
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
let confirmDialogInstance = null;
|
|
|
|
const confirmDialogProps = { props: {} };
|
|
|
|
function close(...args) {
|
|
|
|
destroy(...args);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
function update(newConfig) {
|
2018-12-09 17:34:27 +08:00
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
...newConfig,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
|
|
|
confirmDialogProps.props = currentConfig;
|
2018-12-09 17:34:27 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
function destroy(...args) {
|
2018-03-06 19:14:41 +08:00
|
|
|
if (confirmDialogInstance && div.parentNode) {
|
2019-01-12 11:33:27 +08:00
|
|
|
confirmDialogInstance.$destroy();
|
|
|
|
confirmDialogInstance = null;
|
|
|
|
div.parentNode.removeChild(div);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2019-01-12 11:33:27 +08:00
|
|
|
const triggerCancel = args.some(param => param && param.triggerCancel);
|
2018-03-06 19:14:41 +08:00
|
|
|
if (config.onCancel && triggerCancel) {
|
2019-01-12 11:33:27 +08:00
|
|
|
config.onCancel(...args);
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
2019-04-17 10:21:28 +08:00
|
|
|
for (let i = 0; i < destroyFns.length; i++) {
|
|
|
|
const fn = destroyFns[i];
|
|
|
|
if (fn === close) {
|
|
|
|
destroyFns.splice(i, 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
function render(props) {
|
|
|
|
confirmDialogProps.props = props;
|
2019-08-28 10:50:19 +08:00
|
|
|
const V = Base.Vue || Vue;
|
|
|
|
return new V({
|
2020-03-18 14:52:02 +08:00
|
|
|
el,
|
2020-01-15 21:03:36 +08:00
|
|
|
parent: config.parentContext,
|
2019-01-12 11:33:27 +08:00
|
|
|
data() {
|
|
|
|
return { confirmDialogProps };
|
|
|
|
},
|
|
|
|
render() {
|
2018-12-09 17:34:27 +08:00
|
|
|
// 先解构,避免报错,原因不详
|
2019-01-12 11:33:27 +08:00
|
|
|
const cdProps = { ...this.confirmDialogProps };
|
|
|
|
return <ConfirmDialog {...cdProps} />;
|
2018-03-06 19:14:41 +08:00
|
|
|
},
|
2019-01-12 11:33:27 +08:00
|
|
|
});
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 11:33:27 +08:00
|
|
|
confirmDialogInstance = render(currentConfig);
|
2019-04-17 10:21:28 +08:00
|
|
|
destroyFns.push(close);
|
2018-03-06 19:14:41 +08:00
|
|
|
return {
|
|
|
|
destroy: close,
|
2018-12-09 17:34:27 +08:00
|
|
|
update,
|
2019-01-12 11:33:27 +08:00
|
|
|
};
|
2018-03-06 19:14:41 +08:00
|
|
|
}
|