-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodal-builder.js
105 lines (82 loc) · 3.59 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
/* Copyright © 1016-2016 Néhány jog fenntartva. */
var ModalBuilder = function(modal_builder_form) {
var modal = '';
var modal_body_template_id = 'modal-body-template';
var modal_body_id = 'modal-body';
var modal_buttons_id = 'modal-buttons';
var modal_title_template_id = 'modal-title-template';
var modal_content_id = 'modal-content';
var modal_container_id = 'modal-container'
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks on the button, open the modal
btn.onclick = function() {
// create modal container
var modal_container = document.createElement('div');
modal_container.setAttribute('id', modal_container_id);
modal_container.setAttribute('class', 'modal');
document.body.appendChild(modal_container);
// create modal content
appendElement('div', modal_content_id, modal_container_id, 'modal-content');
// create h1 tag
appendElement('h1', modal_title_template_id, modal_content_id);
// create span for closing the modal
var span_element = appendElement('span', 'close', modal_content_id, 'close');
span_element.innerHTML = 'x';
// When the user clicks on <span> (x), close the modal
span_element.onclick = function() {
closeModal();
}
appendElement('div', modal_body_template_id, modal_content_id);
// Get the modal cotainer
modal = document.getElementById(modal_container_id);
modal.style.display = "block";
var modal_title = document.getElementById('modal-title').value;
document.getElementById(modal_title_template_id).innerHTML = modal_title;
var modal_body = document.getElementById(modal_body_id).value;
document.getElementById(modal_body_template_id).innerHTML = modal_body + '<br /><br />';
renderButtons();
return false;
}
// append element to DOM
function appendElement(element_type, element_id, append_to, element_class) {
var element = document.createElement(element_type);
element.setAttribute('id', element_id);
if ('' != element_class) {
element.setAttribute('class', element_class);
}
document.getElementById(append_to).appendChild(element);
return element;
}
// Render buttons
function renderButtons() {
var modal_buttons = document.getElementById(modal_buttons_id).value;
if ('' != modal_buttons) {
var modal_buttons_array = modal_buttons.split(',');
modal_buttons_array.forEach(function(text_node) {
var btn = document.createElement('button'); // Create a <button> element
var t = document.createTextNode(text_node); // Create a text node
btn.appendChild(t); // Append the text to <button>
document.getElementById(modal_body_template_id).appendChild(btn);
});
}
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
// catch the escape character
window.onkeydown = function( event ) {
if ( event.keyCode === 27 ) { // 27 = escape char
closeModal();
}
};
// close modal
function closeModal() {
modal.style.display = "none";
}
}