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

213 lines
4.7 KiB
JavaScript
Raw Normal View History

2016-10-27 12:14:31 +00:00
import { $t } from 'element-ui/src/locale';
const CONFIRM_TEXT = $t('el.messagebox.confirm');
const CANCEL_TEXT = $t('el.messagebox.cancel');
2016-07-27 06:15:02 +00:00
const defaults = {
2016-07-27 06:15:02 +00:00
title: '提示',
message: '',
type: '',
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,
inputValue: null,
inputPlaceholder: '',
inputPattern: null,
inputValidator: null,
inputErrorMessage: '',
showConfirmButton: true,
showCancelButton: false,
confirmButtonPosition: 'right',
confirmButtonHighlight: false,
cancelButtonHighlight: false,
confirmButtonText: CONFIRM_TEXT,
cancelButtonText: CANCEL_TEXT,
confirmButtonClass: '',
cancelButtonClass: ''
};
import Vue from 'vue';
import msgboxVue from './main.vue';
const merge = function(target) {
for (let i = 1, j = arguments.length; i < j; i++) {
let source = arguments[i];
for (let prop in source) {
2016-07-27 06:15:02 +00:00
if (source.hasOwnProperty(prop)) {
let value = source[prop];
2016-07-27 06:15:02 +00:00
if (value !== undefined) {
target[prop] = value;
}
}
}
}
return target;
};
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) {
let $type = currentMsg.options.$type;
if ($type === 'confirm' || $type === 'prompt') {
if (action === 'confirm') {
if (instance.showInput) {
currentMsg.resolve({ value: instance.inputValue, action });
} else {
currentMsg.resolve(action);
2016-07-27 06:15:02 +00:00
}
} else if (action === 'cancel' && currentMsg.reject) {
currentMsg.reject(action);
2016-07-27 06:15:02 +00:00
}
} else {
currentMsg.resolve(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();
}
2016-08-10 10:11:56 +00:00
if (!instance.value || 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;
}
2016-08-10 10:11:56 +00:00
['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape'].forEach(prop => {
if (instance[prop] === undefined) {
instance[prop] = true;
}
});
document.body.appendChild(instance.$el);
2016-07-27 06:15:02 +00:00
Vue.nextTick(() => {
2016-08-10 10:11:56 +00:00
instance.value = true;
2016-07-27 06:15:02 +00:00
});
}
}
};
const MessageBox = function(options, callback) {
2016-07-27 06:15:02 +00:00
if (typeof options === 'string') {
options = {
title: options
};
if (arguments[1]) {
options.message = arguments[1];
}
if (arguments[2]) {
options.type = arguments[2];
}
} 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),
callback: callback,
resolve: resolve,
reject: reject
});
showNextMsg();
});
} else {
msgQueue.push({
options: merge({}, defaults, MessageBox.defaults || {}, options),
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 = '';
}
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 = '';
}
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 = '';
}
return MessageBox(merge({
title: title,
message: message,
showCancelButton: true,
showInput: true,
$type: 'prompt'
}, options));
};
MessageBox.close = () => {
2016-08-10 10:11:56 +00:00
instance.value = false;
2016-07-27 06:15:02 +00:00
msgQueue = [];
currentMsg = null;
};
export default MessageBox;
export { MessageBox };