Skip to content

Commit

Permalink
Исправляет по замечаниям
Browse files Browse the repository at this point in the history
  • Loading branch information
olissotib committed Dec 4, 2024
1 parent fb83530 commit 2f4deeb
Showing 1 changed file with 25 additions and 24 deletions.
49 changes: 25 additions & 24 deletions js/form.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { isEscapeKey } from './utils.js';

const MAX_COMMENT_LENGTH = 140;
const MAX_HASHTAG_COUNT = 5;
const HASHTAG_FORMAT_REGEX = /^#[a-zа-яё0-9]{1,19}$/i;
const form = document.querySelector('.img-upload__form');
const uploadInput = form.querySelector('.img-upload__input');
const uploadOverlay = form.querySelector('.img-upload__overlay');
const resetButton = form.querySelector('.img-upload__cancel');
const hashtagInput = form.querySelector('.text__hashtags');
const commentInput = form.querySelector('.text__description');
const MAX_COMMENT_LENGTH = 140;
const MAX_HASHTAG_COUNT = 5;
const hastagRegex = /^#[a-zа-яё0-9]{1,19}$/i;

const onFormModalKeydown = (evt) => {
const disableCloseOnFocusInput = !(document.activeElement === hashtagInput || document.activeElement === commentInput);
const isTextInputActive = document.activeElement === hashtagInput || document.activeElement === commentInput;

if (isEscapeKey(evt.key) && disableCloseOnFocusInput) {
if (isEscapeKey(evt.key) && !isTextInputActive) {
evt.preventDefault();
onResetButtonClose();
closeform();
}
};

Expand All @@ -24,19 +24,19 @@ function closeform () {
document.body.classList.remove('modal-open');
uploadInput.value = '';

resetButton.removeEventListener('click', onResetButtonClose);
resetButton.removeEventListener('click', onResetButtonClick);
document.removeEventListener('keydown', onFormModalKeydown);
}

function onResetButtonClose () {
function onResetButtonClick () {
closeform();
}

uploadInput.addEventListener('change', () => {
uploadOverlay.classList.remove('hidden');
document.body.classList.add('modal-open');

resetButton.addEventListener('click', onResetButtonClose);
resetButton.addEventListener('click', onResetButtonClick);
document.addEventListener('keydown', onFormModalKeydown);
});

Expand All @@ -51,29 +51,30 @@ const validateComment = (value) => value.length <= MAX_COMMENT_LENGTH;

pristine.addValidator(commentInput, validateComment, `Не более ${MAX_COMMENT_LENGTH} символов`);

const isValidCount = (hashtags) => hashtags.length <= MAX_HASHTAG_COUNT;

const hasDuplicate = (hashtags) => new Set(hashtags).size === hashtags.length;

const isValidFormat = (hashtags) => {
for (const hashtag of hashtags) {
if (!HASHTAG_FORMAT_REGEX.test(hashtag)) {
return false;
}
}

return true;
};

const validateHashtag = (input) => {
const trimmedInput = input.trim();

if (trimmedInput === '') {
return true;
}
const hashtags = trimmedInput.split(/\s+/);

const isValidCount = () => hashtags.length <= MAX_HASHTAG_COUNT;

const hasDuplicate = () => new Set(hashtags).size === hashtags.length;

const isValidFormat = () => {
for (const hashtag of hashtags) {
if (!hastagRegex.test(hashtag)) {
return false;
}
}

return true;
};
const hashtags = trimmedInput.split(/\s+/);

return isValidCount() && hasDuplicate() && isValidFormat();
return isValidCount(hashtags) && hasDuplicate(hashtags) && isValidFormat(hashtags);
};

pristine.addValidator(hashtagInput, validateHashtag, 'Хештеги не валидны');
Expand Down

0 comments on commit 2f4deeb

Please sign in to comment.