-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Ayronhayd/module9-task1
- Loading branch information
Showing
3 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
|
||
const body = document.querySelector('body'); | ||
const form = body.querySelector('.img-upload__form'); | ||
const overlay = form.querySelector('.img-upload__overlay'); | ||
const cancelButton = form.querySelector('#upload-cancel'); | ||
const fileField = form.querySelector('#upload-file'); | ||
const hashtagField = form.querySelector('.text__hashtags'); | ||
const commentField = form.querySelector('.text__description'); | ||
|
||
const MAX_HASHTAG_COUNT = 5; | ||
const HASHTAG_LENGTH = { | ||
min: 2, | ||
max: 20, | ||
}; | ||
const characterComment = 140; | ||
|
||
const UNVALID_SYMBOLS = /[^a-zA-Z0-9а-яА-ЯёЁ]/g; | ||
|
||
const pristine = new Pristine(form, { | ||
classTo: 'img-upload__field-wrapper', | ||
errorTextParent: 'img-upload__field-wrapper', | ||
errorTextClass: 'img-upload__field-wrapper--error', | ||
}, | ||
false | ||
); | ||
|
||
const showModal = () => { | ||
overlay.classList.remove('hidden'); | ||
body.classList.add('modal-open'); | ||
document.addEventListener('keydown', onEscKeyDown); | ||
}; | ||
|
||
const hideModal = () => { | ||
form.reset(); | ||
pristine.reset(); | ||
overlay.classList.add('hidden'); | ||
body.classList.remove('modal-open'); | ||
document.removeEventListener('keydown', onEscKeyDown); | ||
}; | ||
|
||
const isTextFieldFocused = () => | ||
document.activeElement === hashtagField || | ||
document.activeElement === commentField; | ||
|
||
function onEscKeyDown(evt) { | ||
if (evt.key === 'Escape' && !isTextFieldFocused()) { | ||
evt.preventDefault(); | ||
hideModal(); | ||
} | ||
} | ||
|
||
const onCancelButtonClick = () => { | ||
hideModal(); | ||
}; | ||
|
||
const onFileInputChange = () => { | ||
showModal(); | ||
}; | ||
|
||
const startsWithHash = (string) => string[0] === '#'; | ||
|
||
const hasValidLength = (string) => | ||
string.length >= HASHTAG_LENGTH.min && string.length <= HASHTAG_LENGTH.max; | ||
|
||
const hasValidSymbols = (string) => !UNVALID_SYMBOLS.test(string.slice(1)); | ||
|
||
const isValidTag = (tag) => | ||
startsWithHash(tag) && hasValidLength(tag) && hasValidSymbols(tag); | ||
|
||
const hasValidCount = (tags) => tags.length <= MAX_HASHTAG_COUNT; | ||
|
||
const hasUniqueTags = (tags) => { | ||
const lowerCaseTags = tags.map((tag) => tag.toLowerCase()); | ||
return lowerCaseTags.length === new Set(lowerCaseTags).size; | ||
}; | ||
|
||
const validateTags = (value) => { | ||
const tags = value | ||
.trim() | ||
.split(' ') | ||
.filter((tag) => tag.trim().length); | ||
return hasValidCount(tags) && hasUniqueTags(tags) && tags.every(isValidTag); | ||
}; | ||
|
||
const hascommentLength = (value) => value.length <= characterComment; | ||
|
||
|
||
pristine.addValidator( | ||
hashtagField, | ||
validateTags, | ||
'Неправильно заполнены хэштеги' | ||
); | ||
|
||
pristine.addValidator( | ||
commentField, | ||
hascommentLength, | ||
'Максимум 140 символов' | ||
); | ||
|
||
|
||
const onFormSubmit = (evt) => { | ||
evt.preventDefault(); | ||
pristine.validate(); | ||
}; | ||
|
||
|
||
fileField.addEventListener('change', onFileInputChange); | ||
cancelButton.addEventListener('click', onCancelButtonClick); | ||
form.addEventListener('submit', onFormSubmit); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
import { | ||
generatedObjectArrays | ||
} from './data/generated-object.js'; | ||
|
||
import { | ||
renderPictures | ||
} from './components/render-pictures.js'; | ||
import './components/form.js'; | ||
|
||
renderPictures(generatedObjectArrays()); |