-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommonModal.js
90 lines (84 loc) · 1.48 KB
/
CommonModal.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
class CommonModal {
/**
* @include createNodesByClasss
* @param {object} [op]
* @param {object} [op.contents = {}]
* @param {string} [op.title]
* @param {string} [op.content]
*/
constructor(op = {}){
this.doms = createNodesByClasss(
{
wrap: {
header: {
title: null,
close: null,
},
body: {
content: null,
},
footer: {
buttons: null,
},
}
},
{
attrName: 'modal-role',
}
);
this.dom = this.doms.wrap;
['title', 'content'].forEach((key)=>{
if(op[key] != null){
this.doms[key].innerHTML = op[key];
}
});
}
attr(key, value){
if(value === undefined){
return this[key];
}else{
this[key] = value;
if(this.dom[key]){
this.doms[key].innerHTML = op[key];
}
return this;
}
}
/**
* @include apply, appendStyleNode
*/
show(p){
if(!this.dom.parentNode){
document.body.appendChild(this.dom);
}
if(!this.styleNode){
this.styleNode = appendStyleNode(`
[modal-role=wrap]{
position: absolute;
overflow: hidden;
border: 1px #989898 solid;
border-radius: 3px;
}
[modal-role=body]{
padding: 5px 8px;
}
`, 'modal-style');
document.body.appendChild(this.dom);
}
if(p){
['left', 'top'].forEach((key, i)=>{
this.dom.style[key] = p[i] + 'px';
});
}
apply(this.onshow, [this]);
}
/**
* @include apply
*/
close(){
if(this.dom.parentNode){
this.dom.parentNode.removeChild(this.dom);
}
apply(this.onclose, [this]);
}
}