modal.js
9.47 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
define('modal', ['layer', 'components/common', 'css!layer_css'], function(layer, common) {
var modal = layer;
layer.config({
skin: 'layer-e9'
});
var message = {
modals: [],
open: function (icon, content, duration, onClose) {
if (common.isFunction(duration)) {
onClose = duration;
duration = 0;
}
var _end = function () {
var modals = message.modals;
var prevModal = modals[0];
var index = 0;
modals.remove(index);
modals.forEach(function (m, i) {
if (i < index) return;
var $dom = $(m.dom);
var top = $dom.css("top");
$dom.animate({
top: parseInt(top) - 20 - prevModal.height
});
});
};
var _close = function () { };
var len = this.modals.length;
var top = 20; // 20 为弹窗的间距
var time = 2000;
var closeBtn = false;
var prevModal;
if (len) {
prevModal = this.modals[len - 1];
top += len * (top + prevModal.height);
}
if (duration === 0) {
_close = function (i) {
onClose && onClose();
message.remove(i);
};
_end = function () {};
time = duration;
closeBtn = 3;
}
return modal.open({
type: 0,
className: 'layer-e9-msg',
time: time,
icon: icon,
shade: false,
title: false,
resize: false,
fullresize: false,
single: false,
offset: top,
maxWidth: 400,
content: content,
closeBtn: closeBtn,
btn: false,
anim: 7,
success: function ($dom, i) {
message.modals.push({
dom: $dom[0],
index: i,
height: $dom.outerHeight()
});
},
cancel: _close,
end: _end
});
},
remove: function (index) {
var modals = this.modals;
modals.every(function (m, i) {
if (m.index !== index) return true;
modals.remove(i);
});
},
close: function (index) {
var len = this.modals.length;
var dom = this.modals[len - 1].dom;
index = index || parseInt(dom.id.replace("layui-layer", ""));
modal.close(index);
this.remove(index);
}
};
["info", "success", "error", "warn", "loading"].forEach(function (type) {
message[type] = function (content, duration, onClose) {
if (type === "loading") {
duration = 0;
}
return message.open(type, content, duration, onClose);
};
});
return {
list: {},
open: function(settings, onOpen, onClose) { // 内嵌组件形式,html片段
var that = this,
id = settings.mId, // 针对需要单例的窗口 设置mId 通过restore方法打开原窗口
mIndex = that.list[id],
de = {
type: 1, // 允许嵌入html的模态窗
maxmin: true, //放大缩小窗口
scrollbar: false,
success: function(m, index) {
id && (that.list[id] = index);
onOpen && onOpen.apply(null, arguments);
$(document).on('keydown.openmodal' + index, function (event) {
if (event.keyCode === 27) { //ESC 退出弹框
modal.close(index);
return false;
}
});
},
end: function() {
onClose && onClose.apply(null, arguments);
id && delete that.list[id];
$(document).off('keydown.openmodal' + modal.index);
}
};
if(mIndex >= 0 ) return modal.restore(mIndex);
settings = $.extend(de, settings);
return modal.open(settings);
},
restore: function(index) {
modal.restore(index || modal.index);
},
tips: function(content, el, placement) {
var settings = {};
var getPlacement = function(placement) {
switch (placement) {
case 'right':
return 2;
case 'left':
return 4;
default:
return 3;
}
};
if(common.isObject(placement)) {
settings = placement;
placement = settings.placement;
}
return layer.tips(content, el, $.extend({
tips: [getPlacement(placement), settings.bgColor || '#111'], // 表示出现方向为下 背景色为#111
time: 0, //表示不自动关闭
shift: 7, // 表示不使用动画
tipsMore: (settings.tipsMore === undefined) ? true : settings.tipsMore // 支持与错误提示框共同存在
}, settings));
},
errorTips: function(content, el, placement) {
return this.tips(content, el, {
className: 'wea-mm-has-error',
placement: 'right' || placement,
bgColor: '#ffc6c6',
tipsMore: true // 支持多个错误提示存在
});
},
iframe: function(src, settings) {
settings = settings || {};
settings.content = [src, 'no'];
settings.type = 2;
return modal.open(settings);
},
getModalUIId: function(id) {
return "layui-layer" + this.list[id];
},
getModal$Dom: function (index) {
return $("#layui-layer" + index);
},
initFore9: function (){
layer.config({
lang: common.getLanguage(),
skin: 'layer-e9'
});
},
close: function(index) {
index = index || modal.index; // 默认关闭当前窗口
modal.close(index);
},
closeAll: modal.closeAll,
confirm: function () {
var that = this;
var args = [].slice.call(arguments);
var options = {
success: function (layero, index) {
$(document).on('keydown.confirm' + index, function (event) {
if (event.keyCode === 13 || event.keyCode === 32) {
that.getModal$Dom(index).find(".layui-layer-btn0").click();
return false; //阻止系统默认回车事件
}
if (event.keyCode === 27) { //ESC 退出弹框
that.close(index);
}
});
},
end: function () {
$(document).off('keydown.confirm' + modal.index);
}
};
if (common.isObject(args[1])) {
args[1] = $.extend(options, args[1]);
}else{
args.splice(1,0,options);
}
modal.confirm.apply(null, args);
},
alert: function(msg, yes, cancel) {
var that = this;
var options = {
success: function (layero, index) {
$(document).on('keydown.alert' + index, function (event) {
if (event.keyCode === 27) { //ESC 退出弹框
that.close(index);
}
});
},
end: function () {
$(document).off('keydown.alert' + modal.index);
},
cancel: cancel
};
modal.alert(msg, options, yes);
},
load: function(type) {
modal.load(type, {
shade: [0.5, '#111'],
btn: false,
icon: false
});
},
index: modal.index,
prompt: function(value, title,fn) {
var op = {};
if(common.isString(value)) {
op.value = value;
op.title = title;
} else {
op = value;
}
return modal.prompt(op, function(value, index) {
if (common.isFunction(fn)) {
fn.apply(this, arguments);
}
modal.close(index);
});
},
changeScope: function(scope) {
modal = scope.layer;
},
info: message.info,
success: message.success,
error: message.error,
warn: message.warn,
closeMsg: message.close.bind(message),
loading: message.loading,
getFrameIndex: modal.getFrameIndex, // 指定iframe层自适应
iframeSrc: modal.iframeSrc // 重置iframe的url
}
});