-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodebit-modal.js
More file actions
151 lines (123 loc) · 4.85 KB
/
Copy pathcodebit-modal.js
File metadata and controls
151 lines (123 loc) · 4.85 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
function createCbModalSkeleton() {
var backdrop = document.createElement("div");
backdrop.className = "cb-modal-backdrop";
var table = document.createElement("div");
backdrop.appendChild(table);
var tableCell = document.createElement("div");
table.appendChild(tableCell);
var modal = document.createElement("div");
modal.className = "cb-modal";
tableCell.appendChild(modal);
var modalHead = document.createElement("div");
modalHead.className = "cb-modal-head";
modal.appendChild(modalHead);
var modalBody = document.createElement("div");
modalBody.className = "cb-modal-body";
modal.appendChild(modalBody);
var modalFoot = document.createElement("div");
modalFoot.className = "cb-modal-foot";
modal.appendChild(modalFoot);
return backdrop;
}
var showingCbModals = [];
function CbModal(element) {
this.backdrop = element || createCbModalSkeleton();
this.animationTime = 200; // same as CSS transition (.show)
this.disattachWhenHidden = true;
this.onShown = null;
this.onHidden = null;
var self = this;
this.backdrop.modal = this;
this.modal = this.backdrop.getElementsByClassName("cb-modal")[0];
this.modalHead = this.backdrop.getElementsByClassName("cb-modal-head")[0];
this.modalBody = this.backdrop.getElementsByClassName("cb-modal-body")[0];
this.modalFoot = this.backdrop.getElementsByClassName("cb-modal-foot")[0];
this.guid = function () {
function s4() {
return Math.floor((1 + Math.random()) * 0x10000)
.toString(16)
.substring(1);
}
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
};
this.modalHead.id = this.guid();
this.modalBody.id = this.guid();
this.modal.setAttribute("role", "modal");
this.modal.setAttribute("aria-modal", "true");
this.modal.setAttribute("aria-labelledby", this.modalHead.id);
this.modal.setAttribute("aria-describedby", this.modalBody.id);
this.modalBody.tabIndex = -1;
this.blurBackdrop = function (blur) {
var sameLevelElements = this.backdrop.parentElement.children;
for (var i = 0; i < sameLevelElements.length; i++) {
var e = sameLevelElements[i];
if (e === this.backdrop || (e.offsetWidth == 0 && e.offsetHeight == 0)) continue;
if (blur) {
e.classList.add("cb-modal-blur");
} else {
e.classList.remove("cb-modal-blur");
}
}
};
this.removeFromParent = function () {
var parent = this.backdrop.parentElement;
if (parent != null) parent.removeChild(this.backdrop);
};
this.show = function (childOf) {
var cbModal = this;
this.backdrop.style.display = "block";
var zIndex = 5;
if (childOf != null) {
if (childOf.backdrop == null || childOf.backdrop.style == null || childOf.backdrop.style.zIndex == null) {
console.warn('The object passed as "childOf" is not a valid CbModal.');
} else {
this.childOf = childOf;
zIndex = parseInt(childOf.backdrop.style.zIndex) + 1;
}
}
this.backdrop.style.zIndex = zIndex || '5';
document.body.classList.add('has-modal-showing');
// insert element on root
if (this.backdrop.parentElement !== document.body) {
this.removeFromParent();
document.body.appendChild(this.backdrop);
}
setTimeout(function () {
cbModal.backdrop.classList.add("show");
setTimeout(function () {
if (cbModal.onShown != null) cbModal.onShown();
}, this.animationTime);
showingCbModals.push(self);
}, 50);
cbModal.blurBackdrop(true);
if (document.activeElement != null) {
document.activeElement.blur();
}
};
this.setZIndex = function (zIndex) {
this.backdrop.style.zIndex = zIndex;
};
this.hide = function () {
this.backdrop.classList.remove("show");
var cbModal = this;
setTimeout(function () {
cbModal.backdrop.style.display = "none";
cbModal.blurBackdrop(false);
if (cbModal.disattachWhenHidden) cbModal.removeFromParent();
if (cbModal.onHidden != null) cbModal.onHidden();
showingCbModals.splice(showingCbModals.indexOf(self));
if (showingCbModals.length == 0) {
document.body.classList.remove('has-modal-showing')
}
}, this.animationTime);
};
}
document.addEventListener('keydown', function (e) {
if (e.key == "Escape") {
if (showingCbModals.length != 0) {
var modal = showingCbModals[showingCbModals.length - 1];
modal.hide();
e.stopPropagation();
}
}
});