-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
337 lines (243 loc) · 9.16 KB
/
index.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { isElementValid } from "./static/js/utils.js";
const navQuoteLink = document.querySelector("#quote")
const quoteBox = document.querySelector(".quote-box")
const quotePopupBox = document.querySelector(".popup");
const sideBar = document.querySelector(".testimonal-sidebar");
const closeIcon = document.querySelector(".testimonal-close-icon");
const readMorespinner = document.querySelector(".readmore-spinner")
const ERROR_MSG = "The selector was not found"
/**
*
* 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) {
const navSpinner = document.querySelector(".spinner");
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";
}
function minimumCharactersToUse() {
const contactFormTextArea = document.querySelector("#description");
if (!contactFormTextArea) {
console.error("The element was not found!!");
return;
}
contactFormTextArea.addEventListener("input", handleEvent, { passive: true });
function handleEvent(e) {
const MIN_CHARACTERS_TO_USE = 50;
const noOfCharsUsed = e.target.value.length;
const minCharsUsed = remainingMinimumCharacters(MIN_CHARACTERS_TO_USE, noOfCharsUsed);
const minCharString = document.querySelector(".minimum-char-string");
handleMinimumCharString(minCharString, minCharsUsed, MIN_CHARACTERS_TO_USE);
}
}
function remainingMinimumCharacters(minCharactersToUse, noOfCharsUsed) {
return minCharactersToUse - noOfCharsUsed;
}
function handleMinimumCharString(stringElement, remaingMinChars, minCharactersToUse) {
const EMPTY_VALUE = ''
stringElement.classList.remove("light-red", "black-color", "dark-green");
updateMinimumCharacterString(remaingMinChars, stringElement)
switch (true) {
case remaingMinChars === EMPTY_VALUE:
stringElement.classList.add("black-color");
break;
case remaingMinChars <= EMPTY_VALUE:
stringElement.classList.add("dark-green");
break;
case remaingMinChars < minCharactersToUse:
stringElement.classList.add("light-red");
break;
}
}
function updateMinimumCharacterString(remaingMinChars, stringElement) {
if (remaingMinChars >= 0) {
stringElement.textContent = `Minimum characters to use: ${remaingMinChars}`;
}
}
function closeTestimonalSideBar() {
const isCloseIconValid = isElementValid(closeIcon, ERROR_MSG);
const isSideBarValid = isElementValid(sideBar, ERROR_MSG);
if (isCloseIconValid && isSideBarValid) {
closeIcon.classList.toggle("rotateIcon");
closeIcon.addEventListener("click", () => {
handleCloseSideBar(closeIcon, sideBar);
});
}
}
function handleCloseSideBar(closeIcon, sideBar) {
const animationDuration = 100;
const closeWidth = 0;
setTimeout(() => {
sideBar.style.width = closeWidth;
closeIcon.style.display = "none";
readMorespinner.style.display = "none";
}, animationDuration);
}
function openReadMoreSideBar() {
const readMoreButton = document.querySelector(".read-more-btn");
const WIDTH_SIZE = "60%"
const isREadMoreValid = isElementValid(readMoreButton, ERROR_MSG);
const isSideBarValid = isElementValid(sideBar, ERROR_MSG);
const isCloseIconValid = isElementValid(closeIcon, ERROR_MSG);
const isReadMoreSpinnerValid = isElementValid(readMorespinner, ERROR_MSG)
if (isREadMoreValid && isSideBarValid && isCloseIconValid && isReadMoreSpinnerValid) {
readMoreButton.addEventListener("click", (e) => {
e.preventDefault();
readMorespinner.style.display = "inline-flex";
setTimeout(() => {
sideBar.style.width = WIDTH_SIZE;
closeIcon.style.display = "flex";
}, 2000)
})
}
}
closeQuotePopupBox();
showQuotePopupBox();
submitSubscriptionForm();
minimumCharactersToUse();
closeTestimonalSideBar();
openReadMoreSideBar();