forked from abhisack/FModal.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmodal.js
More file actions
133 lines (93 loc) · 4.32 KB
/
Copy pathfmodal.js
File metadata and controls
133 lines (93 loc) · 4.32 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
//DOM Elements Creation and appending
var modal= document.createElement("div");
var modalHeader= document.createElement("header");
modal.appendChild(modalHeader);
var header_title= document.createElement("h2");
var header_btn= document.createElement("button");
header_btn.textContent= "X";
header_btn.classList.add("btn-close");
modalHeader.appendChild(header_title);
modalHeader.appendChild(header_btn);
var modal_section= document.createElement("section");
modal.appendChild(modal_section);
var modal_section_text= document.createElement("p");
modal_section.appendChild(modal_section_text);
var modal_section_btn= document.createElement("a");
modal_section_btn.textContent= "Know more";
modal_section_btn.classList.add("section-btn");
// THE MAIN APPENDING!!!
document.body.appendChild(modal);
modal.classList.add("modal");
$(document).on("click", "[data-entrance]", openModal);
function openModal(e) {
var item= this;
/*** fetching data to be set to modalTitle and ModalText of header from attributes of clicked element(this) ***/
var modalTitle= this.getAttribute("data-title");
var modalText= this.getAttribute("data-text");
var btnText= this.getAttribute("data-btn-text");
var btnLink= this.getAttribute("data-btn-link");
header_title.textContent= modalTitle;
modal_section_text.textContent= modalText;
/*** get the value of 'data-entrance attribute, add and remove appropriate classes accroding to the attribute ***/
var entrance= this.getAttribute("data-entrance");
if(entrance==="modal-scale") {
modal.classList.remove("modal-unscaled");
modal.classList.remove("modal-untranslated");
modal.classList.remove("modal-unfaded");
modal.classList.add("modal-scaled");
}
else if((entrance==="modal-translate")) {
modal.classList.remove("modal-untranslated");
modal.classList.remove("modal-unfaded");
modal.classList.remove("modal-unscaled");
modal.classList.add("modal-translated");
}
else if((entrance==="modal-fade")) {
modal.classList.remove("modal-unfaded");
modal.classList.remove("modal-untranslated");
modal.classList.remove("modal-unscaled");
modal.classList.add("modal-faded");
}
modal.classList.add("modal-is-opened");
modal.style.pointerEvents= "auto";
//create new variable for header_btn for ease
var close= header_btn;
close.addEventListener("click", closeModal, false); //add event listener to close to handle closeModal function at the last
//close modal when "esc" key is pressed
window.addEventListener("keyup", function(e) {
if(e.keyCode===27) {
closeModal();
}
});
/*** If 'data-btn' is set to 'yes', assign textContent and 'href' for button(modal_section_btn) we created
earliner. And finally append button to modal_section***/
/*** CAUTION:: Keep this peice of code at the last but before 'e.preventDefault()' of openModal function or it would become buggy ***/
var button= this.getAttribute("data-btn");
if(button==="yes") {
modal_section_btn.textContent= btnText;
modal_section_btn.setAttribute("href", "http://"+btnLink);
modal_section.appendChild(modal_section_btn);
} else {
if(modal_section.childNodes[1]===modal_section_btn) {
modal_section.removeChild(modal_section_btn);
}
}
e.preventDefault();
}
// Event handle to be executed when close(header>.btn-close) is clicked
function closeModal(e) {
modal.classList.remove("modal-is-opened");
modal.style.pointerEvents= "none";
if(modal.classList.contains("modal-scaled")) {
modal.classList.remove("modal-scaled");
modal.classList.add("modal-unscaled");
}
else if(modal.classList.contains("modal-translated")) {
modal.classList.remove("modal-translated");
modal.classList.add("modal-untranslated");
}
else if(modal.classList.contains("modal-faded")) {
modal.classList.remove("modal-faded");
modal.classList.add("modal-unfaded");
}
}