-
Notifications
You must be signed in to change notification settings - Fork 1
/
modal.js
81 lines (60 loc) · 1.66 KB
/
modal.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
(function($){
function createModalObject(){
var m= '<div class="modal" id="modal">'+
'<div class="modal-content"></div>'+
'</div>'
var div = document.createElement('div')
div.innerHTML = m
m = div.firstChild
return m
}
function removeCurrentModal(id){
$(id).remove()
}
function removeCurrentOverlay(){
$('#modal-overlay').remove()
}
function createOverlay(){
var overlay = '<div class="modal-overlay" id="modal-overlay"></div>'
var div = document.createElement('div')
div.innerHTML = overlay
overlay = div.firstChild
return overlay
}
$.modal = function(text,options){
var defaults = {
parent:'body',
overlay:false,
id:'.modal',
closeOnClick: true,
placeholder: '.modal-content',
closeBtn: '',
animateClass: 'modal-animate',
},
o = Object.assign({},defaults, options)
removeCurrentOverlay()
removeCurrentModal(o.id)
var parent = document.querySelector('body')
var modal = createModalObject()
var overlay = createOverlay()
o.overlay && parent.appendChild(overlay)
parent.appendChild(modal)
modal.querySelector(o.placeholder).innerHTML = text
setTimeout( function(){
modal.classList.add(o.animateClass)
}, 0)
function handleDocumentOnClick(e){
if(!o.closeOnClick) return
for (var element = e.target; element; element = element.parentNode) {
if (element === modal) return
}
modal.classList.remove(o.animateClass)
modal.addEventListener('transitionend', function(){
this.remove()
document.removeEventListener('mouseup', handleDocumentOnClick)
}, false)
}
document.addEventListener('mouseup', handleDocumentOnClick, false)
return modal
}
})(jQuery)