mand-mobile/components/dialog/index.js

163 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-03-26 16:04:04 +08:00
import Vue from 'vue'
import Dialog from './dialog'
const DialogConstructor = Vue.extend(Dialog)
/* 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}
*/
2018-10-15 15:49:02 +08:00
const generate = function({title = '', icon = '', iconSvg = false, content = '', closable = false, btns = []}) {
2018-03-26 16:04:04 +08:00
const vm = new DialogConstructor({
propsData: {
value: true,
title,
icon,
2018-10-15 15:49:02 +08:00
iconSvg,
2018-03-26 16:04:04 +08:00
content,
closable,
btns,
transition: 'md-bounce',
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()
})
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 = '',
cancelText = '取消',
confirmText = '确定',
closable = false,
onConfirm = 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,
2018-03-26 16:04:04 +08:00
btns: [
{
text: cancelText,
handler: /* istanbul ignore next */ () => vm.close(),
2018-03-26 16:04:04 +08:00
},
{
text: confirmText,
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 = '',
confirmText = '确定',
closable = false,
onConfirm = 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,
2018-03-26 16:04:04 +08:00
btns: [
{
text: confirmText,
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 => {
2018-10-15 15:16:58 +08:00
props.icon = 'success'
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 => {
2018-10-15 15:16:58 +08:00
props.icon = 'fail'
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