element/packages/message-box/src/main.js

216 lines
4.9 KiB
JavaScript
Raw Normal View History

const defaults = {
title: null,
2016-07-27 06:15:02 +00:00
message: '',
type: '',
2018-06-06 04:19:24 +00:00
iconClass: '',
2016-07-27 06:15:02 +00:00
showInput: false,
showClose: true,
2016-09-23 10:03:01 +00:00
modalFade: true,
2016-10-02 13:14:43 +00:00
lockScroll: true,
2016-07-27 06:15:02 +00:00
closeOnClickModal: true,
2017-02-22 07:15:16 +00:00
closeOnPressEscape: true,
closeOnHashChange: true,
2016-07-27 06:15:02 +00:00
inputValue: null,
inputPlaceholder: '',
2017-10-23 12:01:49 +00:00
inputType: 'text',
2016-07-27 06:15:02 +00:00
inputPattern: null,
inputValidator: null,
inputErrorMessage: '',
showConfirmButton: true,
showCancelButton: false,
confirmButtonPosition: 'right',
confirmButtonHighlight: false,
cancelButtonHighlight: false,
2016-11-17 07:37:04 +00:00
confirmButtonText: '',
cancelButtonText: '',
2016-07-27 06:15:02 +00:00
confirmButtonClass: '',
2017-01-06 05:53:06 +00:00
cancelButtonClass: '',
customClass: '',
2017-08-02 11:06:59 +00:00
beforeClose: null,
dangerouslyUseHTMLString: false,
center: false,
roundButton: false
2016-07-27 06:15:02 +00:00
};
import Vue from 'vue';
import msgboxVue from './main.vue';
import merge from 'element-ui/src/utils/merge';
import { isVNode } from 'element-ui/src/utils/vdom';
2016-07-27 06:15:02 +00:00
const MessageBoxConstructor = Vue.extend(msgboxVue);
2016-07-27 06:15:02 +00:00
let currentMsg, instance;
let msgQueue = [];
2016-07-27 06:15:02 +00:00
const defaultCallback = action => {
if (currentMsg) {
let callback = currentMsg.callback;
if (typeof callback === 'function') {
if (instance.showInput) {
callback(instance.inputValue, action);
} else {
callback(action);
2016-07-27 06:15:02 +00:00
}
}
if (currentMsg.resolve) {
2017-07-02 11:56:25 +00:00
if (action === 'confirm') {
if (instance.showInput) {
currentMsg.resolve({ value: instance.inputValue, action });
} else {
currentMsg.resolve(action);
2016-07-27 06:15:02 +00:00
}
2017-07-02 11:56:25 +00:00
} else if (action === 'cancel' && currentMsg.reject) {
currentMsg.reject(action);
2016-07-27 06:15:02 +00:00
}
}
}
2016-07-27 06:15:02 +00:00
};
const initInstance = () => {
instance = new MessageBoxConstructor({
el: document.createElement('div')
});
instance.callback = defaultCallback;
};
const showNextMsg = () => {
2016-07-27 06:15:02 +00:00
if (!instance) {
initInstance();
}
2017-02-22 07:15:16 +00:00
instance.action = '';
2016-07-27 06:15:02 +00:00
if (!instance.visible || instance.closeTimer) {
2016-07-27 06:15:02 +00:00
if (msgQueue.length > 0) {
currentMsg = msgQueue.shift();
let options = currentMsg.options;
for (let prop in options) {
2016-07-27 06:15:02 +00:00
if (options.hasOwnProperty(prop)) {
instance[prop] = options[prop];
}
}
if (options.callback === undefined) {
instance.callback = defaultCallback;
}
2017-01-12 09:34:11 +00:00
let oldCb = instance.callback;
instance.callback = (action, instance) => {
oldCb(action, instance);
2017-01-12 09:34:11 +00:00
showNextMsg();
};
if (isVNode(instance.message)) {
instance.$slots.default = [instance.message];
instance.message = null;
2017-05-13 14:34:50 +00:00
} else {
delete instance.$slots.default;
}
['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape', 'closeOnHashChange'].forEach(prop => {
2016-08-10 10:11:56 +00:00
if (instance[prop] === undefined) {
instance[prop] = true;
}
});
document.body.appendChild(instance.$el);
2016-07-27 06:15:02 +00:00
Vue.nextTick(() => {
instance.visible = true;
2016-07-27 06:15:02 +00:00
});
}
}
};
const MessageBox = function(options, callback) {
2016-12-26 02:45:20 +00:00
if (Vue.prototype.$isServer) return;
if (typeof options === 'string' || isVNode(options)) {
2016-07-27 06:15:02 +00:00
options = {
message: options
2016-07-27 06:15:02 +00:00
};
if (typeof arguments[1] === 'string') {
options.title = arguments[1];
2016-07-27 06:15:02 +00:00
}
} else if (options.callback && !callback) {
callback = options.callback;
}
if (typeof Promise !== 'undefined') {
return new Promise((resolve, reject) => { // eslint-disable-line
2016-07-27 06:15:02 +00:00
msgQueue.push({
options: merge({}, defaults, MessageBox.defaults, options),
2016-07-27 06:15:02 +00:00
callback: callback,
resolve: resolve,
reject: reject
});
showNextMsg();
});
} else {
msgQueue.push({
options: merge({}, defaults, MessageBox.defaults, options),
2016-07-27 06:15:02 +00:00
callback: callback
});
showNextMsg();
}
};
MessageBox.setDefaults = defaults => {
2016-07-27 06:15:02 +00:00
MessageBox.defaults = defaults;
};
MessageBox.alert = (message, title, options) => {
2016-07-27 06:15:02 +00:00
if (typeof title === 'object') {
options = title;
title = '';
2017-08-18 06:36:39 +00:00
} else if (title === undefined) {
title = '';
2016-07-27 06:15:02 +00:00
}
return MessageBox(merge({
title: title,
message: message,
$type: 'alert',
2016-09-02 05:56:47 +00:00
closeOnPressEscape: false,
2016-07-27 06:15:02 +00:00
closeOnClickModal: false
}, options));
};
MessageBox.confirm = (message, title, options) => {
2016-07-27 06:15:02 +00:00
if (typeof title === 'object') {
options = title;
title = '';
2017-08-18 06:36:39 +00:00
} else if (title === undefined) {
title = '';
2016-07-27 06:15:02 +00:00
}
return MessageBox(merge({
title: title,
message: message,
$type: 'confirm',
showCancelButton: true
}, options));
};
MessageBox.prompt = (message, title, options) => {
2016-07-27 06:15:02 +00:00
if (typeof title === 'object') {
options = title;
title = '';
2017-08-18 06:36:39 +00:00
} else if (title === undefined) {
title = '';
2016-07-27 06:15:02 +00:00
}
return MessageBox(merge({
title: title,
message: message,
showCancelButton: true,
showInput: true,
$type: 'prompt'
}, options));
};
MessageBox.close = () => {
instance.doClose();
instance.visible = false;
2016-07-27 06:15:02 +00:00
msgQueue = [];
currentMsg = null;
};
export default MessageBox;
export { MessageBox };