forked from bjtqti/study
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdialog.js
69 lines (65 loc) · 1.77 KB
/
dialog.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
/**
* 对话框
* @param message 消息内容
* @param [options] 可选的配置信息
*/
function Dialog(message,options){
var _default = {
confirmText:"确定",
cancelText:"取消",
onlyConfirm:false,
onConfrim:function(){}
};
options = options || {};
for(var i in _default){
if(options[i]!== undefined){
this[i] = options[i];
}else{
this[i] = _default[i];
}
}
this.createBox(message);
}
Dialog.prototype.handleEvent = function(e){
var button = e.target.innerHTML;
e.preventDefault();
if(e.target.tagName.toLowerCase()==='button'){
this.container.removeEventListener('click',this,false);
document.body.removeChild(this.container);
}
if(button ===this.cancelText){
return false;
}
if(button===this.confirmText){
this.onConfrim();
return false;
}
}
Dialog.prototype.createBox = function(message){
var container = document.createElement('div');
container.setAttribute('class','dialog-container');
var html = '<div class="dialog">'+
'<div class="dialog-content">'+message+'</div>'+
'<div class="dialog-btns">'+
(this.onlyConfirm ? '<button>'+this.confirmText+'</button>':
'<button>'+this.cancelText+'</button><button>'+this.confirmText+'</button>')+
'</div>'+
'</div>';
container.innerHTML = html;
container.addEventListener('click',this,false);
this.container = container;
document.body.appendChild(container);
setTimeout(function(){
container.classList.add('active');
},50)
}
//-----------
Dialog.tips = function(message,sec){
var container = document.createElement('div');
container.setAttribute('class','tips-wrap');
container.innerHTML = '<span class="text">'+message+'</span>';
document.body.appendChild(container);
setTimeout(function(){
document.body.removeChild(container);
},sec||3000);
}