2018-03-06 11:14:41 +00:00
|
|
|
import Vue from 'vue'
|
|
|
|
import ConfirmDialog from './ConfirmDialog'
|
2018-05-04 08:02:31 +00:00
|
|
|
|
2018-03-06 11:14:41 +00:00
|
|
|
export default function confirm (config) {
|
|
|
|
const div = document.createElement('div')
|
2018-03-07 13:36:15 +00:00
|
|
|
const el = document.createElement('div')
|
|
|
|
div.appendChild(el)
|
2018-03-06 11:14:41 +00:00
|
|
|
document.body.appendChild(div)
|
2018-12-09 09:34:27 +00:00
|
|
|
let currentConfig = { ...config, close, visible: true }
|
2018-03-07 13:36:15 +00:00
|
|
|
|
2018-03-06 11:14:41 +00:00
|
|
|
let confirmDialogInstance = null
|
2018-12-09 09:34:27 +00:00
|
|
|
const confirmDialogProps = { props: {}}
|
2018-03-06 11:14:41 +00:00
|
|
|
function close (...args) {
|
|
|
|
destroy(...args)
|
|
|
|
}
|
2018-12-09 09:34:27 +00:00
|
|
|
function update (newConfig) {
|
|
|
|
currentConfig = {
|
|
|
|
...currentConfig,
|
|
|
|
...newConfig,
|
|
|
|
}
|
|
|
|
confirmDialogProps.props = currentConfig
|
|
|
|
}
|
2018-03-06 11:14:41 +00:00
|
|
|
function destroy (...args) {
|
|
|
|
if (confirmDialogInstance && div.parentNode) {
|
|
|
|
confirmDialogInstance.$destroy()
|
|
|
|
confirmDialogInstance = null
|
|
|
|
div.parentNode.removeChild(div)
|
|
|
|
}
|
|
|
|
const triggerCancel = args && args.length &&
|
|
|
|
args.some(param => param && param.triggerCancel)
|
|
|
|
if (config.onCancel && triggerCancel) {
|
|
|
|
config.onCancel(...args)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function render (props) {
|
2018-12-09 09:34:27 +00:00
|
|
|
confirmDialogProps.props = props
|
2018-03-06 11:14:41 +00:00
|
|
|
return new Vue({
|
2018-03-07 13:36:15 +00:00
|
|
|
el: el,
|
2018-12-09 09:34:27 +00:00
|
|
|
data () { return { confirmDialogProps } },
|
2018-03-06 11:14:41 +00:00
|
|
|
render () {
|
2018-12-09 09:34:27 +00:00
|
|
|
// 先解构,避免报错,原因不详
|
|
|
|
const cdProps = { ...this.confirmDialogProps }
|
|
|
|
return <ConfirmDialog {...cdProps} />
|
2018-03-06 11:14:41 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-12-09 09:34:27 +00:00
|
|
|
confirmDialogInstance = render(currentConfig)
|
2018-03-06 11:14:41 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
destroy: close,
|
2018-12-09 09:34:27 +00:00
|
|
|
update,
|
2018-03-06 11:14:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|