-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnav.js
213 lines (160 loc) · 5.59 KB
/
nav.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
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
import { isElementValid } from "./static/js/utils.js";
const navQuoteLink = document.querySelector("#quote")
const quoteBox = document.querySelector(".quote-box")
const quotePopupBox = document.querySelector(".popup");
const navSpinner = document.querySelector(".spinner");
/**
*
* When the close icon is clicked the quote popup box's visibility is toggled,
* and a navigation spinner is triggered for a duration of X-milliseconds.
*
* @example
* // Call the function to close the open quote popup box.
* closeQuotePopupBox();
*/
function closeQuotePopupBox() {
/**
* Handles the click event on the quote popup close icon.
* @param {Event} e - The click event object.
*/
function handlePopupClose(e) {
const DURATION = 2000;
const errorMsg = "The quote box element was not found!!!";
const quotePopupCloseIcon = document.querySelector("#popup-close-icon");
if (isValid(quotePopupCloseIcon, errorMsg)) {
quotePopupBox.classList.toggle('show-popup');
handleNavSpinner(DURATION);
}
}
/**
* Runs an event listener on the quote popup close icon, triggering a specified action when clicked
*/
function runEventListener() {
const errorMsg = "The quote box icon element was not found!!!"
const quotePopupCloseIcon = document.querySelector("#popup-close-icon");
if (isValid(quotePopupCloseIcon, errorMsg)) {
quotePopupCloseIcon.addEventListener("click", handlePopupClose);
}
}
/**
* Takes a selector element and checks the validity of the element.
* @returns {boolean} Returns true if the element is valid, otherwise returns false.
*/
function isValid(selectorElement, errorMsg) {
return isElementValid(selectorElement, errorMsg);
}
runEventListener();
}
/**
* Submits the subscription form but ensures the validity of the subscription form
*
* @example
* submitSubscriptionForm();
*/
function submitSubscriptionForm() {
/**
* Sets up an event listener for the subscription form submission.
* Adds a submit event listener to the subscribeForm element, invoking handleSubmission.
*/
function runEventListener() {
const subscribeForm = document.querySelector("#popup-form")
const errorMsg = "The subscription form element was not found!!!";
if (isValid(subscribeForm, errorMsg)) {
subscribeForm.addEventListener("submit", handleSubmission);
}
}
/**
* Handles the submission of the form
* @param {e} event parameter
*/
function handleSubmission(e) {
e.preventDefault();
Swal.fire("Subscription Successful", "You've successfully subscribed to our mailing list!", "success");
e.target.reset();
}
/**
* Checks the validity of the specified selector element.
*
* @param {HTMLElement} selectorElement - The HTML element to validate.
* @param {string} errorMsg - The error message to be logged if the element is not valid.
* @returns {boolean} Returns true if the element is valid, otherwise returns false.
*/
function isValid(selectorElement, errorMsg) {
return isElementValid(selectorElement, errorMsg);
}
runEventListener();
}
function showQuotePopupBox() {
navQuoteLink.addEventListener("click", handleQuotePopupDisplay);
}
function handleQuotePopupDisplay(e) {
e.preventDefault();
const DURATION = 3000;
quotePopupBox.classList.add('show-popup');
handleNavSpinner(DURATION)
}
function handleNavSpinner(duration) {
let isSpinnrVisible = true;
hideQuoteButton();
toggleSpinnerVisibility(navSpinner, true);
setTimeout(() => {
toggleSpinnerVisibility(navSpinner, false)
isSpinnrVisible = false;
if (!isSpinnrVisible) {
showQuoteButton();
}
}, duration);
}
function toggleSpinnerVisibility(spinnerElement, shouldShowSpinner = false) {
/**
* Shows the spinner by setting display style to "flex".
*/
function showSpinner() {
updateSpinnerVisibility("flex");
}
/**
* Hides the spinner by setting display style to "none".
*/
function hideSpinner() {
updateSpinnerVisibility("none");
}
/**
* Updates the spinner visibility based on the provided display style.
* @param {string} displayStyle - The display style for the spinner.
*/
function updateSpinnerVisibility(displayStyle) {
if (isValid()) {
spinnerElement.style.display = displayStyle;
};
}
/**
* Checks if the spinner element is valid.
* Throws an error if the spinner element is not found.
* @param {HTMLElement} spinnerElement - The spinner element.
* @returns {boolean} - True if the spinner element is valid.
*/
function isValid() {
const errorMsg = "The spinner element was not found";
return isElementValid(spinnerElement, errorMsg)
}
function run() {
switch (shouldShowSpinner) {
case true:
showSpinner();
break;
case false:
hideSpinner();
break;
}
}
run();
}
function showQuoteButton() {
quoteBox.style.display = "flex";
}
function hideQuoteButton() {
quoteBox.style.display = "none";
}
closeQuotePopupBox();
showQuotePopupBox();
submitSubscriptionForm();