|
| 1 | +// Element variables |
| 2 | + |
| 3 | +// Sidenav element ID's |
| 4 | +const mySideNav = document.getElementById("mySidenav"); |
| 5 | +const sideNavContentOne = document.getElementById("sideNavContentOne"); |
| 6 | +const sideNavContentTwo = document.getElementById("sideNavContentTwo"); |
| 7 | + |
| 8 | +// Create Task Form |
| 9 | +const createTaskForm = document.getElementById("create-task-form"); |
| 10 | +// Create Task Form <span> elemement that populates an error message when validation fails |
| 11 | +const taskFormError = document.getElementById("task-form-error"); |
| 12 | + |
| 13 | +// Create Important Task Form |
| 14 | +const createImportantTaskForm = document.getElementById( |
| 15 | + "create-important-task-form" |
| 16 | +); |
| 17 | +// Create Important Task Form <span> elemement that populates an error message when validation fails |
| 18 | +const createImportantTaskFormError = document.getElementById( |
| 19 | + "important-task-form-error" |
| 20 | +); |
| 21 | + |
| 22 | +// Login Form username field |
| 23 | +const logInFormUsername = document.getElementById("id_username"); |
| 24 | +// Login Form password field |
| 25 | +const logInFormPassword = document.getElementById("id_password"); |
| 26 | +// Login Form <span> elemement that populates an error message when validation fails |
| 27 | +const logInFormError = document.getElementById("login-alert"); |
| 28 | + |
| 29 | +// Loading button span - this contains the interior text of the button |
| 30 | +const loadingButtonSpan = document.querySelector(".loading-button-span"); |
| 31 | +// Loading button <button> element |
| 32 | +const loadingButton = document.querySelector(".loading-button"); |
| 33 | + |
| 34 | +// Toggle option for a slide-out menu |
| 35 | +const openNav = () => { |
| 36 | + // If the slide out column is open, and the 'menu' button is clicked again to close it, then shorten the bootstrap column width to minimize it |
| 37 | + if (mySideNav.classList.contains("active")) { |
| 38 | + mySideNav.classList.remove( |
| 39 | + "col-5", |
| 40 | + "col-sm-3", |
| 41 | + "col-md-2", |
| 42 | + "col-lg-2", |
| 43 | + "active" |
| 44 | + ); |
| 45 | + mySideNav.classList.add("col-2", "col-sm-1", "col-md-1", "col-lg-1"); |
| 46 | + // Hide the sidebar text upon minimizing |
| 47 | + sideNavContentOne.classList.replace("d-block", "d-none"); |
| 48 | + sideNavContentTwo.classList.replace("d-block", "d-none"); |
| 49 | + } else { |
| 50 | + // If the menu is closed and the 'menu' icon is clicked - this will add the 'active' class to it |
| 51 | + // The boostrap column classes are increased numerically to maximize the side nav |
| 52 | + mySideNav.classList.remove("col-2", "col-sm-1", "col-md-1", "col-lg-1"); |
| 53 | + mySideNav.classList.add( |
| 54 | + "col-5", |
| 55 | + "col-sm-3", |
| 56 | + "col-md-2", |
| 57 | + "col-lg-2", |
| 58 | + "active" |
| 59 | + ); |
| 60 | + // When the sidenav is maximized, the text for the content is now displayed |
| 61 | + sideNavContentOne.classList.replace("d-none", "d-block"); |
| 62 | + sideNavContentTwo.classList.replace("d-none", "d-block"); |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | +// Show the loading spinner for the register and login links on the main page |
| 67 | +// This is also used to prevent multiple click events |
| 68 | +const nonFormLoadingSpinner = e => { |
| 69 | + // Prevent default |
| 70 | + e.target.onclick = e => e.preventDefault(); |
| 71 | + e.target.classList.add("spinner-border", "text-light", "mx-auto"); |
| 72 | + e.target.innerHTML = ""; |
| 73 | +}; |
| 74 | + |
| 75 | +// Loading spinner to be used on form buttons |
| 76 | +const formSubmitLoadingSpinner = () => { |
| 77 | + loadingButtonSpan.innerHTML = ""; |
| 78 | + loadingButtonSpan.classList.add("spinner-border", "text-light"); |
| 79 | + loadingButton.setAttribute("disabled", true); |
| 80 | + loadingButton.setAttribute("aria-disabled", true); |
| 81 | + loadingButton.setAttribute("role", "status"); |
| 82 | +} |
| 83 | + |
| 84 | +// Loading spinner that replaces icons indicating a task update |
| 85 | +const checkboxSpinners = e => { |
| 86 | + // Prevent default |
| 87 | + e.target.onclick = e => e.preventDefault(); |
| 88 | + e.target.classList.remove("far", "fa-star"); |
| 89 | + e.target.classList.add("spinner-border", "fas", "fa-spinner", "p-0"); |
| 90 | +} |
| 91 | + |
| 92 | +// TODO - Need to see if it's possible to make this more DRY, since the forms are on different pages |
| 93 | +// This checks if the 'Task' form field is empty |
| 94 | +// If it's empty, set stylistic changes to indicate there is a validation error |
| 95 | +const validateTaskFormSubmit = () => { |
| 96 | + if (createTaskForm.value === "") { |
| 97 | + taskFormError.innerHTML = "You must enter a value"; |
| 98 | + taskFormError.style.color = "red"; |
| 99 | + createTaskForm.style.border = "1px solid red"; |
| 100 | + return false; |
| 101 | + } else if (createTaskForm.value !== "") { |
| 102 | + formSubmitLoadingSpinner(); |
| 103 | + } |
| 104 | + taskFormError.innerHTML = ""; |
| 105 | +}; |
| 106 | + |
| 107 | +// This checks if the 'Important Task' form field is empty |
| 108 | +// If it's empty, set stylistic changes to indicate there is a validation error |
| 109 | +const validateImportantTaskFormSubmit = () => { |
| 110 | + if (createImportantTaskForm.value === "") { |
| 111 | + createImportantTaskFormError.innerHTML = "You must enter a value"; |
| 112 | + createImportantTaskFormError.style.color = "red"; |
| 113 | + createImportantTaskForm.style.border = "1px solid red"; |
| 114 | + return false; |
| 115 | + } else if (createImportantTaskForm.value !== "") { |
| 116 | + formSubmitLoadingSpinner(); |
| 117 | + } |
| 118 | + createImportantTaskFormError.innerHTML = ""; |
| 119 | +}; |
| 120 | + |
| 121 | +// Validation check for the login form |
| 122 | +const validateLogInForm = e => { |
| 123 | + if (logInFormUsername.value === "") { |
| 124 | + logInFormError.innerHTML = "You must enter a value"; |
| 125 | + logInFormError.style.color = "red"; |
| 126 | + logInFormUsername.style.border = "1px solid red"; |
| 127 | + return false; |
| 128 | + } else if (logInFormPassword.value === "") { |
| 129 | + logInFormError.innerHTML = "You must enter a value"; |
| 130 | + logInFormError.style.color = "red"; |
| 131 | + logInFormPassword.style.border = "1px solid red"; |
| 132 | + return false; |
| 133 | + } else if (logInFormUsername.value !== "") { |
| 134 | + formSubmitLoadingSpinner(); |
| 135 | + } |
| 136 | +}; |
0 commit comments