forked from heaven7github/modal-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodal-builder.js
105 lines (84 loc) · 3.43 KB
/
modal-builder.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var ModalBuilder = function(formId) {
var modalUniqueId = 'pszt_modal_' + Math.round(Math.random() * 1000);
var form = document.getElementById(formId);
var validateInputs = function (inputsObject) {
var errorMessages = [];
for(var i in inputsObject) {
if (!inputsObject[i].value.trim().length) {
errorMessages.push('Please fill ' + i + ' value');
}
}
return errorMessages.length ? errorMessages : true;
};
var windowClickEvent = function(event){
if (event.target == document.getElementById(modalUniqueId)) {
event.preventDefault();
closePopup();
}
};
var windowKeyUpEvent = function(event){
if (event.keyCode == 27) {
event.preventDefault();
closePopup();
}
};
var closePopup = function() {
var modalDiv = document.getElementById(modalUniqueId);
if (modalDiv) {
form.removeChild(modalDiv);
window.removeEventListener('click', windowClickEvent);
window.removeEventListener('keyup', windowKeyUpEvent);
}
};
var createModalDomObject = function(inputsObject) {
closePopup();
var modalDiv = document.createElement('div');
modalDiv.id = modalUniqueId;
modalDiv.className = 'modal';
var modalContentDiv = document.createElement('div');
modalContentDiv.className = 'modal-content';
var modalHeaderDiv = document.createElement('div');
modalHeaderDiv.className = 'modal-header';
modalHeaderDiv.innerHTML = '<span class="close">x</span><h2>' + inputsObject.title.value + '</h2>';
modalContentDiv.appendChild(modalHeaderDiv);
var modalBodyDiv = document.createElement('div');
modalBodyDiv.className = 'modal-body';
modalBodyDiv.innerHTML = '<p>' + inputsObject.body.value + '</p>';
modalContentDiv.appendChild(modalBodyDiv);
var modalFooterDiv = document.createElement('div');
modalFooterDiv.className = 'modal-footer';
var buttons = inputsObject.buttons.value.split(',');
for(var i in buttons) {
var buttonTitle = buttons[i];
if (buttonTitle.trim().length) {
modalFooterDiv.innerHTML += '<button type="button">' + buttonTitle + '</button>';
}
}
modalContentDiv.appendChild(modalFooterDiv);
modalDiv.appendChild(modalContentDiv);
form.appendChild(modalDiv);
var closeBtn = document.getElementsByClassName("close")[0];
closeBtn.addEventListener('click', function(event){
event.preventDefault();
closePopup();
});
window.addEventListener('click', windowClickEvent);
window.addEventListener('keyup', windowKeyUpEvent);
};
if (form) {
form.addEventListener('submit', function(event){
event.preventDefault();
var inputsObject = {
title : document.getElementById('modal-title'),
body : document.getElementById('modal-body'),
buttons : document.getElementById('modal-buttons')
};
var validationResponse = validateInputs(inputsObject);
if (validationResponse === true) {
createModalDomObject(inputsObject);
} else {
alert(validationResponse.join("\n"));
}
});
}
};