Repository navigation
Expand file tree
/
Copy pathmodalbox.js
More file actions
250 lines (234 loc) · 10.1 KB
/
Copy pathmodalbox.js
File metadata and controls
250 lines (234 loc) · 10.1 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
class ModalBox {
constructor(options) {
//Checking for options object
this.options = options;
if (typeof this.options !== 'object') {
throw new Error('options is not defined as an Object')
return
}
//Checking for options.width
if(this.options.width === undefined) {
this.width = '40%'
} else if(typeof this.options.width === 'number'){
this.width = this.options.width + 'px'
} else if (typeof this.options.width === 'string') {
if(this.options.width.slice(-2) === 'px' || this.options.width.slice(-1) === '%' || this.options.width.slice(-2) === 'vw') {
this.width = this.options.width
} else {
throw new Error('Width is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('Width must be a Number or String')
return
}
//Checking for options.height
if(this.options.height === undefined) {
this.height = 'auto'
} else if(typeof this.options.height === 'number'){
this.height = this.options.height + 'px'
} else if (typeof this.options.height === 'string') {
if(this.options.height.slice(-2) === 'px' || this.options.height.slice(-1) === '%' || this.options.height.slice(-2) === 'vw' || this.options.height.slice(-2) === 'vh') {
this.height = this.options.height
} else {
throw new Error('Height is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('Height must be a Number or String')
return
}
//Checking for options.maxWidth
if(this.options.maxWidth === undefined) {
this.maxWidth = '500px'
} else if(typeof this.options.maxWidth === 'number'){
this.maxWidth = this.options.maxWidth + 'px'
} else if (typeof this.options.maxWidth === 'string') {
if(this.options.maxWidth.slice(-2) === 'px' || this.options.maxWidth.slice(-1) === '%' || this.options.maxWidth.slice(-2) === 'vw') {
this.maxWidth = this.options.maxWidth
} else {
throw new Error('maxWidth is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('maxWidth must be a Number or String')
return
}
//Checking for options.maxHeight
if(this.options.maxHeight === undefined) {
this.maxHeight = '100vh'
} else if(typeof this.options.maxHeight === 'number'){
this.maxHeight = this.options.maxHeight + 'px'
} else if (typeof this.options.maxHeight === 'string') {
if(this.options.maxHeight.slice(-2) === 'px' || this.options.maxHeight.slice(-1) === '%' || this.options.maxHeight.slice(-2) === 'vw') {
this.maxHeight = this.options.maxHeight
} else {
throw new Error('maxHeight is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('maxHeight must be a Number or String')
return
}
//Checking for options.minWidth
if(this.options.minWidth === undefined) {
this.minWidth = '350px'
} else if(typeof this.options.minWidth === 'number'){
this.minWidth = this.options.minWidth + 'px'
} else if (typeof this.options.minWidth === 'string') {
if(this.options.minWidth.slice(-2) === 'px' || this.options.minWidth.slice(-1) === '%' || this.options.minWidth.slice(-2) === 'vw') {
this.minWidth = this.options.minWidth
} else {
throw new Error('minWidth is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('minWidth must be a Number or String')
return
}
//Checking for options.minHeight
if(this.options.minHeight === undefined) {
this.minHeight = 'auto'
} else if(typeof this.options.minHeight === 'number'){
this.minHeight = this.options.minHeight + 'px'
} else if (typeof this.options.minHeight === 'string') {
if(this.options.minHeight.slice(-2) === 'px' || this.options.minHeight.slice(-1) === '%' || this.options.minHeight.slice(-2) === 'vw') {
this.minHeight = this.options.minHeight
} else {
throw new Error('minHeight is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('minHeight must be a Number or String')
return
}
//Checking for options.padding
if(this.options.padding === undefined) {
this.padding = '30px'
} else if(typeof this.options.padding === 'number'){
this.padding = this.options.padding + 'px'
} else if (typeof this.options.padding === 'string') {
if(this.options.padding.slice(-2) === 'px' || this.options.padding.slice(-1) === '%' || this.options.padding.slice(-2) === 'vw') {
this.padding = this.options.padding
} else {
throw new Error('Height is not the correct unit. Choose between px, % or vw')
return
}
} else {
throw new Error('Height must be a Number or String')
return
}
//Checking for template
if(this.options.template === undefined) {
throw new Error('Template is undefined')
return
}
if (typeof this.options.template === 'string') {
this.modal = document.createElement('div');
this.modal.classList.add('mb-modal')
this.modalBg = document.createElement('div');
this.modalBg.classList.add('mb-modalBg')
this.close = document.createElement('div');
this.close.classList.add('mb-close')
this.modal.innerHTML = this.options.template
} else {
throw new Error('Template is required a String')
return
}
//Checking for animation
if(this.options.animation) {
switch(this.options.animation) {
case 'fade':
this.modal.style.opacity = '0'
this.modalBg.style.opacity = '0'
this.modal.style.transform = 'translate(-50%, -50%)'
this.modal.style.transition = 'opacity .3s ease-out'
this.modalBg.style.transition = 'opacity .3s ease-out'
break;
case 'slide':
this.modal.style.opacity = '0'
this.modalBg.style.opacity = '0'
this.modal.style.transform = 'translate(-50%, -25%)'
this.modal.style.transition = 'opacity .3s ease-out, transform .3s ease-out'
this.modalBg.style.transition = 'opacity .3s ease-out'
break;
default:
throw new Error('Animation does not exist, choose between fade or slide')
return
}
} else {
this.modal.style.transform = 'translate(-50%, -50%)'
}
//Styling the elements
this.modalBg.style.position = 'fixed'
this.modalBg.style.display = 'none'
this.modalBg.style.top = '0'
this.modalBg.style.left = '0'
this.modalBg.style.right = '0'
this.modalBg.style.bottom = '0'
this.modalBg.style.width = '100vw'
this.modalBg.style.height = '100vh'
this.modalBg.style.maxWidth = '100%'
this.modalBg.style.zIndex = '9999'
this.modalBg.style.backgroundColor = 'rgba(0,0,0,0.6)'
this.modalBg.addEventListener('click', (e)=>{
if(e.target === this.modalBg) {
this.closeModal()
}
})
this.close.innerHTML = '×'
this.close.style.color = 'rgba(0,0,0,0.7)'
this.close.style.fontSize = '33px'
this.close.style.position = 'absolute'
this.close.style.top = '5px'
this.close.style.right = '12px'
this.close.style.cursor = 'pointer'
this.close.addEventListener('click', ()=>{
this.closeModal()
})
this.modal.style.position = 'absolute'
this.modal.style.display = 'none'
this.modal.style.width = this.width
this.modal.style.height = this.height
this.modal.style.maxWidth = this.maxWidth
this.modal.style.maxHeight = this.maxHeight
this.modal.style.minWidth = this.minWidth
this.modal.style.minHeight = this.minHeight
this.modal.style.padding = this.padding
this.modal.style.top = '50%'
this.modal.style.left = '50%'
this.modal.style.backgroundColor = '#fff'
this.modal.style.borderRadius = '4px'
this.modalBg.appendChild(this.modal)
this.modal.appendChild(this.close)
document.body.appendChild(this.modalBg)
}
openModal() {
this.modalBg.style.display = 'block'
this.modal.style.display = 'block'
if(this.options.animation) {
setTimeout(()=>{
this.modal.style.transform = 'translate(-50%, -50%)'
this.modal.style.opacity = '1'
this.modalBg.style.opacity = '1'
},50)
} else {
this.modal.style.opacity = '1'
this.modalBg.style.opacity = '1'
}
}
closeModal () {
this.modal.style.opacity = '0'
this.modalBg.style.opacity = '0'
setTimeout(()=>{
this.modalBg.style.display = 'none'
this.modal.style.display = 'none'
},350)
if(this.options.animation === 'slide') {
this.modal.style.transform = 'translate(-50%, -75%)'
setTimeout(()=>{
this.modal.style.transform = 'translate(-50%, -25%)'
},350)
}
}
}