MessageBox: fix a callback overwriting bug

pull/716/head
Leopoldthecoder 2016-10-29 14:10:55 +08:00
parent ef90ce3cda
commit 4cff9bc197
1 changed files with 55 additions and 50 deletions

View File

@ -1,9 +1,9 @@
import { $t } from 'element-ui/src/locale'; import { $t } from 'element-ui/src/locale';
var CONFIRM_TEXT = $t('el.messagebox.confirm'); const CONFIRM_TEXT = $t('el.messagebox.confirm');
var CANCEL_TEXT = $t('el.messagebox.cancel'); const CANCEL_TEXT = $t('el.messagebox.cancel');
var defaults = { const defaults = {
title: '提示', title: '提示',
message: '', message: '',
type: '', type: '',
@ -31,12 +31,12 @@ var defaults = {
import Vue from 'vue'; import Vue from 'vue';
import msgboxVue from './main.vue'; import msgboxVue from './main.vue';
var merge = function(target) { const merge = function(target) {
for (var i = 1, j = arguments.length; i < j; i++) { for (let i = 1, j = arguments.length; i < j; i++) {
var source = arguments[i]; let source = arguments[i];
for (var prop in source) { for (let prop in source) {
if (source.hasOwnProperty(prop)) { if (source.hasOwnProperty(prop)) {
var value = source[prop]; let value = source[prop];
if (value !== undefined) { if (value !== undefined) {
target[prop] = value; target[prop] = value;
} }
@ -47,47 +47,49 @@ var merge = function(target) {
return target; return target;
}; };
var MessageBoxConstructor = Vue.extend(msgboxVue); const MessageBoxConstructor = Vue.extend(msgboxVue);
var currentMsg, instance; let currentMsg, instance;
var msgQueue = []; let msgQueue = [];
var initInstance = function() { const defaultCallback = action => {
if (currentMsg) {
let callback = currentMsg.callback;
if (typeof callback === 'function') {
if (instance.showInput) {
callback(instance.inputValue, action);
} else {
callback(action);
}
}
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);
}
} else if (action === 'cancel' && currentMsg.reject) {
currentMsg.reject(action);
}
} else {
currentMsg.resolve(action);
}
}
}
};
const initInstance = () => {
instance = new MessageBoxConstructor({ instance = new MessageBoxConstructor({
el: document.createElement('div') el: document.createElement('div')
}); });
instance.callback = function(action) { instance.callback = defaultCallback;
if (currentMsg) {
var callback = currentMsg.callback;
if (typeof callback === 'function') {
if (instance.showInput) {
callback(instance.inputValue, action);
} else {
callback(action);
}
}
if (currentMsg.resolve) {
var $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);
}
} else if (action === 'cancel' && currentMsg.reject) {
currentMsg.reject(action);
}
} else {
currentMsg.resolve(action);
}
}
}
};
}; };
var showNextMsg = function() { const showNextMsg = () => {
if (!instance) { if (!instance) {
initInstance(); initInstance();
} }
@ -96,12 +98,15 @@ var showNextMsg = function() {
if (msgQueue.length > 0) { if (msgQueue.length > 0) {
currentMsg = msgQueue.shift(); currentMsg = msgQueue.shift();
var options = currentMsg.options; let options = currentMsg.options;
for (var prop in options) { for (let prop in options) {
if (options.hasOwnProperty(prop)) { if (options.hasOwnProperty(prop)) {
instance[prop] = options[prop]; instance[prop] = options[prop];
} }
} }
if (options.callback === undefined) {
instance.callback = defaultCallback;
}
['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape'].forEach(prop => { ['modal', 'showClose', 'closeOnClickModal', 'closeOnPressEscape'].forEach(prop => {
if (instance[prop] === undefined) { if (instance[prop] === undefined) {
instance[prop] = true; instance[prop] = true;
@ -116,7 +121,7 @@ var showNextMsg = function() {
} }
}; };
var MessageBox = function(options, callback) { const MessageBox = function(options, callback) {
if (typeof options === 'string') { if (typeof options === 'string') {
options = { options = {
title: options title: options
@ -132,7 +137,7 @@ var MessageBox = function(options, callback) {
} }
if (typeof Promise !== 'undefined') { if (typeof Promise !== 'undefined') {
return new Promise(function(resolve, reject) { // eslint-disable-line return new Promise((resolve, reject) => { // eslint-disable-line
msgQueue.push({ msgQueue.push({
options: merge({}, defaults, MessageBox.defaults || {}, options), options: merge({}, defaults, MessageBox.defaults || {}, options),
callback: callback, callback: callback,
@ -152,11 +157,11 @@ var MessageBox = function(options, callback) {
} }
}; };
MessageBox.setDefaults = function(defaults) { MessageBox.setDefaults = defaults => {
MessageBox.defaults = defaults; MessageBox.defaults = defaults;
}; };
MessageBox.alert = function(message, title, options) { MessageBox.alert = (message, title, options) => {
if (typeof title === 'object') { if (typeof title === 'object') {
options = title; options = title;
title = ''; title = '';
@ -170,7 +175,7 @@ MessageBox.alert = function(message, title, options) {
}, options)); }, options));
}; };
MessageBox.confirm = function(message, title, options) { MessageBox.confirm = (message, title, options) => {
if (typeof title === 'object') { if (typeof title === 'object') {
options = title; options = title;
title = ''; title = '';
@ -183,7 +188,7 @@ MessageBox.confirm = function(message, title, options) {
}, options)); }, options));
}; };
MessageBox.prompt = function(message, title, options) { MessageBox.prompt = (message, title, options) => {
if (typeof title === 'object') { if (typeof title === 'object') {
options = title; options = title;
title = ''; title = '';
@ -197,7 +202,7 @@ MessageBox.prompt = function(message, title, options) {
}, options)); }, options));
}; };
MessageBox.close = function() { MessageBox.close = () => {
instance.value = false; instance.value = false;
msgQueue = []; msgQueue = [];
currentMsg = null; currentMsg = null;