vant/packages/dialog/index.js
2018-12-14 14:24:23 +08:00

85 lines
1.5 KiB
JavaScript

import Vue from 'vue';
import VanDialog from './Dialog';
import { isServer } from '../utils';
let instance;
const initInstance = () => {
instance = new (Vue.extend(VanDialog))({
el: document.createElement('div')
});
instance.$on('input', value => {
instance.value = value;
});
document.body.appendChild(instance.$el);
};
const Dialog = options => {
/* istanbul ignore if */
if (isServer) {
return Promise.resolve();
}
return new Promise((resolve, reject) => {
if (!instance) {
initInstance();
}
Object.assign(instance, Dialog.currentOptions, options, {
resolve,
reject
});
});
};
Dialog.defaultOptions = {
value: true,
title: '',
message: '',
overlay: true,
className: '',
lockScroll: true,
beforeClose: null,
messageAlign: '',
confirmButtonText: '',
cancelButtonText: '',
showConfirmButton: true,
showCancelButton: false,
closeOnClickOverlay: false,
callback: action => {
instance[action === 'confirm' ? 'resolve' : 'reject'](action);
}
};
Dialog.alert = Dialog;
Dialog.confirm = options => Dialog({
showCancelButton: true,
...options
});
Dialog.close = () => {
if (instance) {
instance.value = false;
}
};
Dialog.setDefaultOptions = options => {
Object.assign(Dialog.currentOptions, options);
};
Dialog.resetDefaultOptions = () => {
Dialog.currentOptions = { ...Dialog.defaultOptions };
};
Dialog.install = () => {
Vue.use(VanDialog);
};
Vue.prototype.$dialog = Dialog;
Dialog.resetDefaultOptions();
export default Dialog;