|
| 1 | +let firstTimeClick = true; |
| 2 | +let tasks = 0; |
| 3 | +let completed = 0; |
| 4 | + |
| 5 | +//selectors of the dom nodes even in the dosument manipulation in this project |
| 6 | + |
| 7 | +let uiNodes = { |
| 8 | + mainItem: 'main', |
| 9 | + presentation: '.presentation', |
| 10 | + topHeading: '.top__section__heading', |
| 11 | + topInput: '.top__section__input', |
| 12 | + inputButton: '.input__type__button', |
| 13 | + parentList: 'ul', |
| 14 | + mainHeader: '.main__header', |
| 15 | + mainHeaderCompleted: '.completed', |
| 16 | + ballon: '.ballon', |
| 17 | +}; |
| 18 | + |
| 19 | +//function responsible for readin input value text |
| 20 | +let readInputValue = () => document.querySelector(uiNodes.topInput).value; |
| 21 | + |
| 22 | +//function responsible for formatting header text |
| 23 | +let updateTaskMessage = () => { |
| 24 | + document.querySelector(uiNodes.mainHeader).textContent = `${ |
| 25 | + tasks > 0 ? tasks : 'No' |
| 26 | + } task${tasks <= 1 ? '' : 's'} recorded`; |
| 27 | + document.querySelector(uiNodes.mainHeaderCompleted).textContent = `${ |
| 28 | + completed > 0 ? completed : 'No' |
| 29 | + } task${completed <= 1 ? '' : 's'} completed`; |
| 30 | +}; |
| 31 | + |
| 32 | +//function responsible for addition of new task |
| 33 | +let addNewTaskInterface = (task) => { |
| 34 | + let listSnippet = `<li class="main__list__item"> |
| 35 | + ${task} |
| 36 | + <div class="btn-wrapper"> |
| 37 | + <button class="button-marker check"> |
| 38 | + <i class="fas fa-check"></i> |
| 39 | + </button> |
| 40 | + <button class="button-marker button-marker--modified"> |
| 41 | + <i class="fas fa-times"></i> |
| 42 | + </button> |
| 43 | + </div> |
| 44 | +</li>`; |
| 45 | + document |
| 46 | + .querySelector(uiNodes.parentList) |
| 47 | + .insertAdjacentHTML('beforeend', listSnippet); |
| 48 | +}; |
| 49 | + |
| 50 | +//function responsible for updating task number |
| 51 | +let updateTasks = () => { |
| 52 | + tasks++; |
| 53 | +}; |
| 54 | + |
| 55 | +//function responsible for clearing input field |
| 56 | +let clearInput = () => { |
| 57 | + document.querySelector(uiNodes.topInput).value = ''; |
| 58 | +}; |
| 59 | + |
| 60 | +//event listener for trigerring animation and handlin button input |
| 61 | +document.querySelector(uiNodes.inputButton).addEventListener('click', (e) => { |
| 62 | + if (firstTimeClick) { |
| 63 | + Array.from( |
| 64 | + document.querySelectorAll( |
| 65 | + `${uiNodes.mainItem},${uiNodes.presentation},${uiNodes.topHeading},${uiNodes.topInput}` |
| 66 | + ) |
| 67 | + ).forEach((e) => { |
| 68 | + e.style.animationPlayState = 'running'; |
| 69 | + }); |
| 70 | + firstTimeClick = false; |
| 71 | + updateTaskMessage(); |
| 72 | + } |
| 73 | + let input = readInputValue(); |
| 74 | + if (input) { |
| 75 | + addNewTaskInterface(input); |
| 76 | + updateTasks(); |
| 77 | + updateTaskMessage(); |
| 78 | + clearInput(); |
| 79 | + } |
| 80 | +}); |
| 81 | + |
| 82 | +let date = () => { |
| 83 | + let months = [ |
| 84 | + ' January', |
| 85 | + ' February', |
| 86 | + 'March', |
| 87 | + ' April', |
| 88 | + 'May', |
| 89 | + ' June', |
| 90 | + 'July', |
| 91 | + ' August', |
| 92 | + ' September', |
| 93 | + ' October', |
| 94 | + 'November', |
| 95 | + ' December', |
| 96 | + ]; |
| 97 | + let daysWeek = [ |
| 98 | + 'Monday', |
| 99 | + 'Tuesday', |
| 100 | + 'Wednesday', |
| 101 | + 'Thursday', |
| 102 | + 'Friday', |
| 103 | + 'Saturday', |
| 104 | + 'Sunday', |
| 105 | + ]; |
| 106 | + |
| 107 | + let currentDate = new Date(); |
| 108 | + console.log(currentDate.getUTCDate()); |
| 109 | + let htmlSnippet = `<h2 class="top__section__heading">${ |
| 110 | + daysWeek[currentDate.getDay() - 1] |
| 111 | + },${currentDate.getUTCDate()}th ${months[currentDate.getUTCMonth()]}</h2>`; |
| 112 | + console.log(htmlSnippet); |
| 113 | + document |
| 114 | + .querySelector('.top__section') |
| 115 | + .insertAdjacentHTML('afterbegin', htmlSnippet); |
| 116 | +}; |
| 117 | +date(); |
| 118 | +//event listener for completing and deleting tasks |
| 119 | + |
| 120 | +document.querySelector('ul').addEventListener('click', (e) => { |
| 121 | + if (e.target.closest('.check')) { |
| 122 | + document |
| 123 | + .querySelector('.check') |
| 124 | + .parentNode.parentNode.parentNode.removeChild( |
| 125 | + document.querySelector('.check').parentNode.parentNode |
| 126 | + ); |
| 127 | + completed += 1; |
| 128 | + tasks -= 1; |
| 129 | + updateTaskMessage(); |
| 130 | + } else if (e.target.closest('.button-marker--modified')) { |
| 131 | + document |
| 132 | + .querySelector('.button-marker--modified') |
| 133 | + .parentNode.parentNode.parentNode.removeChild( |
| 134 | + document.querySelector('.button-marker--modified').parentNode.parentNode |
| 135 | + ); |
| 136 | + tasks -= 1; |
| 137 | + updateTaskMessage(); |
| 138 | + } |
| 139 | +}); |
0 commit comments