app.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. eventListeners()
  2. function eventListeners() {
  3. const ui = new UI()
  4. // preloader
  5. window.addEventListener('load', function () {
  6. ui.hidePreloader()
  7. })
  8. // nav btn
  9. document.querySelector('.navBtn').addEventListener('click', function () {
  10. ui.showNav()
  11. })
  12. // control the video
  13. document.querySelector('.video__switch').addEventListener('click', function () {
  14. ui.videoControls()
  15. })
  16. // submit the form
  17. document.querySelector('.drink-form').addEventListener('submit', function (event) {
  18. event.preventDefault()
  19. const name = document.querySelector('.input-name').value
  20. const lastName = document.querySelector('.input-lastname').value
  21. const email = document.querySelector('.input-email').value
  22. let value = ui.checkEmpty(name, lastName, email)
  23. if (value) {
  24. let customer = new Customer(name, lastName, email)
  25. console.log(customer)
  26. ui.addCustomer(customer)
  27. ui.showFeedback('custumer added to the list', 'success')
  28. ui.clearFields()
  29. }
  30. else {
  31. ui.showFeedback('some form values empty', 'error')
  32. }
  33. })
  34. // display modal
  35. const links = document.querySelectorAll('.work-item__icon');
  36. links.forEach(function (item) {
  37. item.addEventListener('click', function (event) {
  38. ui.showModal(event)
  39. })
  40. })
  41. // hide modal
  42. document.querySelector('.work-modal__close').addEventListener('click', function () {
  43. ui.closeModal()
  44. })
  45. }
  46. function UI() { }
  47. UI.prototype.hidePreloader = function () {
  48. document.querySelector('.preloader').style.display = "none"
  49. }
  50. UI.prototype.showNav = function () {
  51. document.querySelector('.nav').classList.toggle('nav--show')
  52. }
  53. // play/pause the video
  54. UI.prototype.videoControls = function () {
  55. let btn = document.querySelector('.video__switch-btn')
  56. if (!btn.classList.contains('btnSlide')) {
  57. btn.classList.add('btnSlide')
  58. document.querySelector('.video__item').pause()
  59. }
  60. else {
  61. btn.classList.remove('btnSlide')
  62. document.querySelector('.video__item').play()
  63. }
  64. }
  65. // check for empty values
  66. UI.prototype.checkEmpty = function (name, lastname, email) {
  67. let result;
  68. if (name === '' || lastname === '' || email === '') {
  69. result = false;
  70. }
  71. else {
  72. result = true;
  73. }
  74. return result;
  75. }
  76. UI.prototype.showFeedback = function (text, type) {
  77. const feedback = document.querySelector('.drink-form__feedback');
  78. if (type === 'success') {
  79. feedback.classList.add('success');
  80. feedback.innerText = text;
  81. this.removeAlert('success');
  82. }
  83. else if (type === 'error') {
  84. feedback.classList.add('error');
  85. feedback.innerText = text;
  86. this.removeAlert('error');
  87. }
  88. }
  89. // remove alert
  90. UI.prototype.removeAlert = function (type) {
  91. setTimeout(function () {
  92. document.querySelector('.drink-form__feedback').classList.remove(type)
  93. }, 3000)
  94. }
  95. // add customer
  96. UI.prototype.addCustomer = function (customer) {
  97. const images = [1, 2, 3, 4, 5];
  98. let random = Math.floor(Math.random() * images.length);
  99. const div = document.createElement('div');
  100. div.classList.add('person');
  101. div.innerHTML = `<img src="https://raw.githubusercontent.com/john-smilga/grid-coffee-project-final/master/img/person-${random}.jpeg" alt="person" class="person__thumbnail">
  102. <h4 class="person__name">${customer.name}</h4>
  103. <h4 class="person__last-name">${customer.lastname}</h4>`
  104. document.querySelector('.drink-card__list').appendChild(div)
  105. }
  106. // clear fields
  107. UI.prototype.clearFields = function () {
  108. document.querySelector('.input-name').value = '';
  109. document.querySelector('.input-lastname').value = '';
  110. document.querySelector('.input-email').value = '';
  111. }
  112. // show modal
  113. UI.prototype.showModal = function (event) {
  114. event.preventDefault();
  115. if (event.target.parentElement.classList.contains('work-item__icon')) {
  116. let id = event.target.parentElement.dataset.id
  117. const modal = document.querySelector('.work-modal');
  118. const modalItem = document.querySelector('.work-modal__item');
  119. modal.classList.add('work-modal--show');
  120. modalItem.style.backgroundImage = `url(https://raw.githubusercontent.com/john-smilga/grid-coffee-project-final/master/img/work-${id}.jpeg)`
  121. }
  122. }
  123. // hide modal
  124. UI.prototype.closeModal = function () {
  125. document.querySelector('.work-modal').classList.remove('work-modal--show')
  126. }
  127. // customer
  128. function Customer(name, lastname, email) {
  129. this.name = name,
  130. this.lastname = lastname,
  131. this.email = email;
  132. }