-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
188 lines (177 loc) · 4.29 KB
/
index.js
File metadata and controls
188 lines (177 loc) · 4.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Created by yejiaming on 2017/3/15.
*/
import Vue from 'vue';
import msgboxVue from './index.vue';
var CONFIRM_TEXT = '确定';
var CANCEL_TEXT = '取消';
var defaults = {
title: '提示',
message: '',
type: '',
showInput: false,
showClose: true,
modalFade: false,
lockScroll: false,
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: ''
};
var currentMsg, instance;
var msgQueue = [];
// merge方法一个工具方法,是为了检查对象key是否存在,存在即用,不存在使用默认代替
var merge = function (target) {
for (var i = 1, j = arguments.length; i < j; i++) {
var source = arguments[i];
for (var prop in source) {
if (source.hasOwnProperty(prop)) {
var value = source[prop];
if (value !== undefined) {
target[prop] = value;
}
}
}
}
return target;
};
var MessageBoxConstructor = Vue.extend(msgboxVue);
// 初始化实例
var initInstance = function () {
instance = new MessageBoxConstructor({
el: document.createElement('div')
});
instance.callback = defaultCallback;
};
// 默认回调
const defaultCallback = (action) => {
if (currentMsg) {
var callback = currentMsg.callback;
if (typeof callback === 'function') {
callback(action);
}
if (currentMsg.resolve) {
var $type = currentMsg.options.type;
if ($type === 'confirm') {
if (action === 'confirm') {
currentMsg.resolve(action);
} else if (action === 'cancel' && currentMsg.reject) {
currentMsg.reject(action);
}
} else {
currentMsg.resolve(action);
}
}
}
};
// 显示弹窗
var showNextMsg = function () {
if (!instance) {
initInstance();
}
if (!instance.value) {
if (msgQueue.length > 0) {
currentMsg = msgQueue.shift();
var options = currentMsg.options;
for (var prop in options) {
if (options.hasOwnProperty(prop)) {
instance[prop] = options[prop];
}
}
if (options.callback === undefined) {
instance.callback = defaultCallback;
}
document.body.appendChild(instance.$el);
Vue.nextTick(() => {
instance.value = true;
});
}
}
};
// 弹窗对象
var MessageBox = function (options, callback) {
// 纠错
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(function (resolve, reject) { // eslint-disable-line
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 = function (defaults) {
MessageBox.defaults = defaults;
};
/**
* 提示信息alert弹窗
* @param message 提示信息,必传
* @param title 提示标题
* @param options
*/
MessageBox.alert = function (message, title, options) {
if (typeof title === 'object') {
options = title;
title = '';
}
return MessageBox(merge({
title: title,
message: message,
type: 'alert',
timeOutClose: 500
}, options));
};
// 确认弹窗
MessageBox.confirm = function (message, title, options) {
if (typeof title === 'object') {
options = title;
title = '';
}
return MessageBox(merge({
title: title,
message: message,
type: 'confirm'
}, options));
};
// 关闭所有的弹窗
MessageBox.closeAll = function () {
instance.value = false;
msgQueue = [];
currentMsg = null;
};
export default MessageBox;
export {MessageBox};