-
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 #7 from ClaireFotina/module8-task2
- Loading branch information
Showing
4 changed files
with
114 additions
and
4 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,34 @@ | ||
import { isValid, reset as resetValidation } from "./validation.js"; | ||
|
||
|
||
const uploadForm = document.querySelector('.img-upload__form'); | ||
const uploadFileInput = uploadForm.querySelector('.img-upload__input'); | ||
const uploadOverlay = uploadForm.querySelector('.img-upload__overlay'); | ||
const uploadCancelInput = uploadForm.querySelector('.img-upload__cancel'); | ||
const body = document.body; | ||
|
||
const openForm = () => { | ||
uploadOverlay.classList.remove('hidden'); | ||
body.classList.add('modal-open'); | ||
} | ||
|
||
uploadFileInput.addEventListener('change', () => { | ||
openForm() | ||
}) | ||
|
||
const closeForm = () => { | ||
uploadOverlay.classList.add('hidden'); | ||
body.classList.remove('modal-open'); | ||
} | ||
|
||
uploadCancelInput.addEventListener('click', (evt) => { | ||
evt.preventDefault(); | ||
closeForm() | ||
}) | ||
|
||
document.addEventListener('keydown', (evt) => { | ||
if(evt.key === "Escape"){ | ||
evt.preventDefault() | ||
closeForm() | ||
} | ||
}) |
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,73 @@ | ||
const MAX_SYMBOL = 140; | ||
const SPACE = /\s+/g; | ||
const HASHTAG_FORMULA = /^#[a-za-яё0-9]{1,19}$/; | ||
const MAX_HASHTAGS = 5; | ||
|
||
const uploadForm = document.querySelector('.img-upload__form'); | ||
const inputHashtags = uploadForm.querySelector('.text__hashtags'); | ||
const inputComment = uploadForm.querySelector('.text__description'); | ||
|
||
|
||
const pristine = new Pristine(uploadForm, { | ||
classTo: 'img-upload__field-wrapper', | ||
errorClass: 'img-upload__field-wrapper--error', | ||
errorTextParent: 'img-upload__field-wrapper' | ||
}) | ||
|
||
const createHashtags = (value) => value | ||
.toLowerCase() | ||
.replaceAll(SPACE, ' ') | ||
.trim() | ||
.split(' ') | ||
.filter((item) => item !== ''); | ||
|
||
const checkDescription = (value) => value.length <= MAX_SYMBOL; | ||
const checkHashtags = (value) => { | ||
if(!value.length) { | ||
return true; | ||
} | ||
const hashTags = createHashtags(value); | ||
return hashTags.every((hashtag) => HASHTAG_FORMULA.test(hashtag)) | ||
}; | ||
const checkHashtagsCount = (value) => { | ||
if(!value.length) { | ||
return true; | ||
} | ||
const hashTags = createHashtags(value); | ||
return hashTags.length <= MAX_HASHTAGS; | ||
}; | ||
const checkUniqueHashtags = (value) => { | ||
if(!value.length) { | ||
return true; | ||
} | ||
const hashTags = createHashtags(value); | ||
const uniques = [...new Set(hashTags)]; | ||
return hashTags.length === uniques.length; | ||
} | ||
|
||
pristine.addValidator( | ||
inputHashtags, | ||
checkHashtags, | ||
'Хэштэг содержит неверные символы' | ||
) | ||
|
||
pristine.addValidator( | ||
inputHashtags, | ||
checkHashtagsCount, | ||
`Количество хэштэгов не должно превышать ${MAX_HASHTAGS}` | ||
) | ||
|
||
pristine.addValidator( | ||
inputHashtags, | ||
checkUniqueHashtags, | ||
'Хэштэги не должны повторяться' | ||
) | ||
|
||
pristine.addValidator( | ||
inputComment, | ||
checkDescription, | ||
`Максимальная длина - ${MAX_SYMBOL} символов` | ||
) | ||
|
||
export const isValid = () => pristine.validate(); | ||
export const reset = () => pristine.reset(); |