-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg2.js
214 lines (184 loc) · 6.89 KB
/
msg2.js
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
/**
* [Msg description]
* @param {[type]} options [description]
* 弹窗
*
* type,'confirm' 确认取消弹出框,'alert' 弹出确认框
* title:'', //标题文字
* text :'', //内容文字
* btnOkText : '确定', //按钮文字
* btnNoText : '取消', //按钮文字
* maskbtn : true, //是否显示遮罩
* auto_close:false, //自动消失
* success:function(){}, //成功回调函数
* failed:function(){} //失败回调函数
*
*/
/*css*/
/*
.dialog{}
.dialog .dialog-mask{position:fixed;z-index:1000;width:100%;height:100%;top:0;left:0;background-color:rgba(62,62,62,0.8);}
.dialog .dialog-msg{width:85%;top:50%;left:50%;position:fixed;z-index:5000;overflow:hidden;-webkit-transform:translate(-50%,-50%);transform:translate(-50%,-50%);background-color:#fafafc;text-align:center;border-radius:3px;}
.dialog-msg .dialog-title{padding:1.2em 20px .5em;}
.dialog-msg .dialog-title .dialog-title-content{font-weight:400;font-size:17px;}
.dialog-msg .dialog-msg-content{padding:0 20px;text-align:center;font-size:15px;color:#888;word-wrap:break-word;word-break:break-all;}
.dialog-msg .dialog-bottom{margin-top:20px;position:relative;line-height:42px;font-size:17px;display:-webkit-box;display:-webkit-flex;display:flex;}
.dialog-bottom:after{content:" ";position:absolute;left:0;top:0;width:100%;height:1px;border-top:1px solid #d5d5d6;color:#d5d5d6;-webkit-transform-origin:0 0;transform-origin:0 0;-webkit-transform:scaleY(.5);transform:scaleY(.5);}
.dialog-msg .dialog-bottom span{position:relative;}
.dialog-bottom span{display:block;-webkit-box-flex:1;-webkit-flex:1;flex:1;color:#333;text-decoration:none;-webkit-tap-highlight-color:rgba(0,0,0,0);}
*/
/**
* msg({
* type,'confirm' 确认取消弹出框,'alert' 弹出确认框
* title:'', //标题文字
text :'', //内容文字
btnOkText : '确定', //按钮文字
btnNoText : '取消', //按钮文字
maskbtn : true, //是否显示遮罩
auto_close:false, //自动消失
success:function(){}, //成功回调函数
failed:function(){} //失败回调函数
* })
*/
class Msg {
constructor(options) {
Object.assign(this, {
type:'confirm',
title:'',
text :'',
btnOkText : '确定',
btnNoText : '取消',
maskbtn : true,
auto_close:false,
autoCloseTime: 500,
success:function(){},
failed:function(){}
},options);
this.init();
}
}
Msg.prototype.init=function(){
let doc = document,
bd = doc.body,
dialogExist = doc.querySelector('#dialog');
let dialog = '<div class="dialog" id="dialog">'
+'<div class="dialog-mask" id="dialog-mask" name="dialog-mask"></div>'
+'<div name="dialog-msg" class="dialog-msg" id="dialog-msg"></div>'
+'</div>',
dialogMsgTitle = '<div class="dialog-title"><strong class="dialog-title-content">'+this.title+'</strong></div>',
dialogMsgContent = '<div class="dialog-msg-content">'+this.text+'</div>';
if(dialogExist){
return false;
}
bd.insertAdjacentHTML('beforeEnd', dialog);
this.dialog = doc.querySelector('#dialog');
this.dialogMsg = this.dialog.querySelector('#dialog-msg');
this.dialogMsg.insertAdjacentHTML('beforeEnd', dialogMsgTitle);
this.dialogMsg.insertAdjacentHTML('beforeEnd', dialogMsgContent);
switch(this.type){
case "confirm":
//确认框
this.confirm();
break;
case "alert":
//警告框
this.alert();
break;
case "autoClose":
this.autoClose();
break;
default:
//警告框
this.alert();
break;
}
}
Msg.prototype.confirm=function(){
let dialogMsgBottom = '<div class="dialog-bottom">'
+'<a href="javascript:;" class="dialog-bottom-cancel">'+this.btnNoText+'</a>'
+'<a href="javascript:;" class="dialog-bottom-sure">'+this.btnOkText+'</a>'
+'</div>';
this.dialogMsg.insertAdjacentHTML('beforeEnd', dialogMsgBottom);
this.handlerSuccess();
this.handlerFailed();
}
Msg.prototype.alert=function(){
let dialogMsgBottom = '<div class="dialog-bottom">'
+'<a href="javascript:;" class="dialog-bottom-sure">'+this.btnOkText+'</a>'
+'</div>';
this.dialogMsg.insertAdjacentHTML('beforeEnd', dialogMsgBottom);
this.handlerSuccess();
}
Msg.prototype.autoClose=function(){
var _this = this;
var dialogMsgBottom = '<div class="dialog-bottom">'
+'<span href="javascript:;" class="dialog-bottom-sure"></span>'
+'</div>';
_this.dialogMsg.insertAdjacentHTML('beforeEnd', dialogMsgBottom);
setTimeout(function(){
_this.hide();
},_this.autoCloseTime);
}
Msg.prototype.handlerSuccess=function(){
var _this=this;
_this.dialogMsg.querySelector('.dialog-bottom-sure').addEventListener('click',function(event){
var event=event||window.event;
if(event.stopPropagation){
event.stopPropagation();
}else{
event.cancelBulle=true;
}
if(_this.success && Object.prototype.toString.call(_this.success) == "[object Function]"){
_this.success.call(this,_this,dialog);
}
setTimeout(function(){
_this.hide();
},300);
},false);
}
Msg.prototype.handlerFailed=function(){
var _this=this;
_this.dialogMsg.querySelector('.dialog-bottom-cancel').addEventListener('click',function(event){
var event=event||window.event;
if(event.stopPropagation){
event.stopPropagation();
}else{
event.cancelBulle=true;
}
if(_this.failed && Object.prototype.toString.call(_this.failed) == "[object Function]"){
_this.failed.call(this,_this.dialog);
}
setTimeout(function(){
_this.hide();
},300);
},false);
}
Msg.prototype.hide=function(){
var _this = this;
_this.dialog.parentNode.removeChild(_this.dialog);
_this.canmove();
}
Msg.prototype.nomove=function(){
var _this=this;
document.addEventListener('touchmove',function(event){
_this.move(event);
},false);
document.body.style.overflow = "hidden";
}
Msg.prototype.move=function(evnet){
var event=event||window.event;
event.preventDefault();
event.cancelBubble = true;
}
Msg.prototype.canmove=function(){
var _this=this;
document.removeEventListener('touchmove',function(event){
_this.move(event);
},false);
document.body.style.overflow = "auto";
}
const msg = (options = {}) =>{
let m = new Msg(options);
return m;
}
export default msg;