mand-mobile/components/dialog/index.js

206 lines
3.3 KiB
JavaScript
Raw Normal View History

2018-03-26 16:04:04 +08:00
import Vue from 'vue'
2021-03-02 14:18:30 +08:00
import {t} from '../_locale'
import Dialog from './dialog'
2018-03-26 16:04:04 +08:00
/* istanbul ignore next */
2018-03-26 16:04:04 +08:00
const noop = function() {}
// all active instances
const instances = []
/**
* Dialog factory
*
* @param {Object} props
* @return {Dialog}
*/
const generate = function({
title = '',
icon = '',
iconSvg = false,
content = '',
closable = false,
transition = 'md-bounce',
btns = [],
onShow = noop,
onHide = noop,
}) {
const DialogConstructor = Vue.extend(Dialog)
2018-03-26 16:04:04 +08:00
const vm = new DialogConstructor({
propsData: {
value: false,
2018-03-26 16:04:04 +08:00
title,
icon,
2018-10-15 15:49:02 +08:00
iconSvg,
2018-03-26 16:04:04 +08:00
content,
closable,
btns,
transition,
preventScroll: true,
2018-03-26 16:04:04 +08:00
},
}).$mount()
instances.push(vm)
2018-03-26 16:04:04 +08:00
vm.$on('input', val => {
/* istanbul ignore else */
2018-03-26 16:04:04 +08:00
if (!val) {
vm.value = false
}
})
vm.$on('hide', () => {
const index = instances.indexOf(vm)
/* istanbul ignore else */
if (index >= 0) {
instances.splice(index, 1)
}
2018-03-26 16:04:04 +08:00
vm.$destroy()
onHide()
})
vm.$on('show', () => {
onShow()
2018-03-26 16:04:04 +08:00
})
vm.value = true
2018-03-26 16:04:04 +08:00
return vm
}
/**
* Dynamically create a confirm dialog
*
* @param {Object} props
* @return {Dialog}
*/
Dialog.confirm = ({
title = '',
icon = '',
2018-10-15 15:49:02 +08:00
iconSvg = false,
content = '',
2021-03-02 14:18:30 +08:00
cancelText = t('md.dialog.cancel'),
cancelWarning = false,
2021-03-02 14:18:30 +08:00
confirmText = t('md.dialog.confirm'),
confirmWarning = false,
closable = false,
transition,
onConfirm = noop,
2018-11-30 19:33:46 +08:00
onCancel = noop,
onShow = noop,
onHide = noop,
}) => {
2018-03-26 16:04:04 +08:00
const vm = generate({
title,
icon,
2018-10-15 15:49:02 +08:00
iconSvg,
2018-03-26 16:04:04 +08:00
content,
closable,
transition,
onShow,
onHide,
2018-03-26 16:04:04 +08:00
btns: [
{
text: cancelText,
warning: cancelWarning,
2018-11-30 19:33:46 +08:00
handler: /* istanbul ignore next */ () => {
if (onCancel() !== false) {
vm.close()
}
},
2018-03-26 16:04:04 +08:00
},
{
text: confirmText,
warning: confirmWarning,
handler: /* istanbul ignore next */ () => {
2018-03-26 16:04:04 +08:00
if (onConfirm() !== false) {
vm.close()
}
},
},
],
})
return vm
}
/**
* Dynamically create a alert dialog
*
* @param {Object} props
* @return {Dialog}
*/
2018-10-15 15:49:02 +08:00
Dialog.alert = ({
title = '',
icon = '',
iconSvg = false,
content = '',
2021-03-02 14:18:30 +08:00
confirmText = t('md.dialog.confirm'),
2018-10-15 15:49:02 +08:00
closable = false,
warning = false,
transition,
2018-10-15 15:49:02 +08:00
onConfirm = noop,
onShow = noop,
onHide = noop,
2018-10-15 15:49:02 +08:00
}) => {
2018-03-26 16:04:04 +08:00
const vm = generate({
title,
icon,
2018-10-15 15:49:02 +08:00
iconSvg,
2018-03-26 16:04:04 +08:00
content,
closable,
transition,
onShow,
onHide,
2018-03-26 16:04:04 +08:00
btns: [
{
text: confirmText,
warning,
handler: /* istanbul ignore next */ () => {
2018-03-26 16:04:04 +08:00
if (onConfirm() !== false) {
vm.close()
}
},
},
],
})
return vm
}
/**
* Dynamically create a succeed dialog
*
* @param {Object} props
* @return {Dialog}
*/
2018-03-26 16:04:04 +08:00
Dialog.succeed = props => {
props.icon = 'success-color'
2024-01-04 21:01:58 +08:00
props.iconSvg = true
2018-03-26 16:04:04 +08:00
return Dialog.confirm(props)
}
/**
* Dynamically create a failed dialog
*
* @param {Object} props
* @return {Dialog}
*/
2018-03-26 16:04:04 +08:00
Dialog.failed = props => {
props.icon = 'warn-color'
2024-01-04 21:01:58 +08:00
props.iconSvg = true
2018-03-26 16:04:04 +08:00
return Dialog.confirm(props)
}
/**
* Close all actived static dialogs
*
* @static
* @return void
*/
Dialog.closeAll = () => {
instances.forEach(instance => {
instance.close()
})
}
2018-03-26 16:04:04 +08:00
export default Dialog