|
| 1 | +import { isEscapeKey } from './util.js'; |
| 2 | +import { validateDescription, validateHashtags, getError } from './validation.js'; |
| 3 | + |
| 4 | +const GET_ERROR_TAGS = 'tags'; |
| 5 | +const GET_ERROR_DESCRIPTION = 'description'; |
| 6 | + |
| 7 | +const body = document.querySelector('body'); |
| 8 | +const overlayForm = document.querySelector('.img-upload__overlay'); |
| 9 | +const closeBtnForm = overlayForm.querySelector('.img-upload__cancel'); |
| 10 | +const uploadForm = document.querySelector('.img-upload__form'); |
| 11 | +const inputDescription = uploadForm.querySelector('.text__description'); |
| 12 | +const inputHashtags = uploadForm.querySelector('.text__hashtags'); |
| 13 | +const uploadSubmit = uploadForm.querySelector('.img-upload__submit'); |
| 14 | +const uploadFile = document.querySelector('#upload-file'); |
| 15 | + |
| 16 | +const pristine = new Pristine(uploadForm, { |
| 17 | + classTo: 'img-upload__field-wrapper', |
| 18 | + errorTextParent: 'img-upload__field-wrapper', |
| 19 | + errorTextClass: 'img-upload__field-wrapper--error' |
| 20 | +}); |
| 21 | + |
| 22 | +const onHashtagsInput = () => { |
| 23 | + if (pristine.validate()) { |
| 24 | + uploadSubmit.disabled = false; |
| 25 | + return; |
| 26 | + } |
| 27 | + |
| 28 | + uploadSubmit.disabled = true; |
| 29 | +}; |
| 30 | + |
| 31 | +const onDescriptionInput = () => { |
| 32 | + if (pristine.validate()) { |
| 33 | + uploadSubmit.disabled = false; |
| 34 | + return; |
| 35 | + } uploadSubmit.disabled = true; |
| 36 | +}; |
| 37 | + |
| 38 | +const onDocumentKeydown = (event) => { |
| 39 | + if (isEscapeKey(event) && (document.activeElement === inputHashtags || document.activeElement === inputDescription)){ |
| 40 | + return; |
| 41 | + } |
| 42 | + if (isEscapeKey(event)){ |
| 43 | + /* eslint-disable no-use-before-define*/ |
| 44 | + closeFormUpload(); |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +const onClickFormUpload = () => { |
| 49 | + body.classList.add('modal-open'); |
| 50 | + overlayForm.classList.remove('hidden'); |
| 51 | + document.addEventListener('keydown', onDocumentKeydown); |
| 52 | + closeBtnForm.addEventListener('click', closeFormUpload); |
| 53 | +}; |
| 54 | + |
| 55 | +const closeFormUpload = () => { |
| 56 | + overlayForm.classList.add('hidden'); |
| 57 | + body.classList.remove('modal-open'); |
| 58 | + document.removeEventListener('keydown', onDocumentKeydown); |
| 59 | + closeBtnForm.removeEventListener('click', closeFormUpload); |
| 60 | +}; |
| 61 | + |
| 62 | +uploadFile.addEventListener('change', onClickFormUpload); |
| 63 | + |
| 64 | +pristine.addValidator(inputHashtags, validateHashtags, getError(GET_ERROR_TAGS), false); |
| 65 | +inputHashtags.addEventListener('input', onHashtagsInput); |
| 66 | + |
| 67 | +pristine.addValidator(inputDescription, validateDescription, getError(GET_ERROR_DESCRIPTION), false); |
| 68 | +inputDescription.addEventListener('input', onDescriptionInput); |
0 commit comments