Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Правда или действие #10

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<link rel="stylesheet" href="css/normalize.css">
<link rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<script src="./vendor/pristine/pristine.min.js" defer></script>
<script src="./js/functions.js" defer></script>
<script src="./js/main.js" type="module" defer></script>

Expand All @@ -33,7 +34,7 @@ <h2 class="pictures__title visually-hidden">Фотографии других
<section class="img-upload">
<div class="img-upload__wrapper">
<h2 class="img-upload__title visually-hidden">Загрузка фотографии</h2>
<form class="img-upload__form" id="upload-select-image" autocomplete="off">
<form class="img-upload__form" id="upload-select-image" action="https://31.javascript.htmlacademy.pro/kekstagram" method="post" enctype="multipart/form-data" autocomplete="off">

<!-- Изначальное состояние поля для загрузки изображения -->
<fieldset class="img-upload__start">
Expand Down Expand Up @@ -122,7 +123,7 @@ <h2 class="img-upload__title visually-hidden">Загрузка фотограф
<input class="text__hashtags" name="hashtags" placeholder="#ХэшТег">
</div>
<div class="img-upload__field-wrapper">
<textarea class="text__description" name="description" placeholder="Ваш комментарий..."></textarea>
<textarea class="text__description" name="description" placeholder="Ваш комментарий..." maxlength="140"></textarea>
</div>
</fieldset>

Expand Down
1 change: 1 addition & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { photoslist } from './modules/photo-template.js';
import { displaysPictures } from './modules/photo-template.js';
import './modules/upload-form.js';

displaysPictures(photoslist);

105 changes: 105 additions & 0 deletions js/modules/upload-form.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { isEscapeKey } from './util';

const regexp = /^#[a-zф-яё0-9]{1,19}$/i;
const HASHTAGS_COUNT = 5;
const COMMENT_LENGTH = 140;
Comment on lines +3 to +4
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

должны быть сразу после import


const formUpload = document.querySelector('.img-upload__form');
const imageUploadInput = formUpload.querySelector('.img-upload__input');
const imageOverlay = formUpload.querySelector('.img-upload__overlay');
const imageUploadCancle = formUpload.querySelector('.img-upload__cancel');
const uploadHashtag = formUpload.querySelector('.text__hashtags');
const uploadComment = formUpload.querySelector('.text__description');


const pristine = new Pristine(formUpload, {
classTo: 'img-upload__field-wrapper',
errorTextParent: 'img-upload__field-wrapper',
errorTextTag: 'div',
errorTextClass: 'img-upload__field-wrapper--error'
});

const onDocumentKeyDown = (evt) => {
if(isEscapeKey(evt)){
evt.preventDefault();
closeUploadWindow();
}
};

const onCancleKeyDown = (evt) => {
if(isEscapeKey(evt)){
evt.stopPropagation();
}
};

const openUploadWindow = () => {
imageOverlay.classList.remove('hidden');
document.body.classList.add('modal-open');
uploadHashtag.addEventListener('keydown', onCancleKeyDown);
uploadComment.addEventListener('keydown', onCancleKeyDown);
document.addEventListener('keydown', onDocumentKeyDown);
};

function closeUploadWindow() {
imageUploadInput.value = '';
uploadHashtag.value = '';
uploadComment.value = '';
imageOverlay.classList.add('hidden');
document.body.classList.remove('modal-open');
document.removeEventListener('keydown', onDocumentKeyDown);
uploadHashtag.removeEventListener('focus', onCancleKeyDown);
uploadComment.removeEventListener('focus', onCancleKeyDown);
}

imageUploadInput.addEventListener('change', openUploadWindow);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

именование обработчиков


imageUploadCancle.addEventListener('click', closeUploadWindow);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

именование обработчиков


const getHashtagsArray = (element) => {
const hashtagsArray = element.toLowerCase().split(/\s+/).filter((item) => item !== '');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

const hashtagsArray = element.toLowerCase().split(/\s+/).filter(Boolean);

return hashtagsArray;
};

const getHashtagsName = (element) => {
const hashtagsName = getHashtagsArray(element);
const newArray = [];

hashtagsName.forEach((hashtag) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(regexp.test(hashtag) === true) {
newArray.push(hashtag);
}
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(regexp.test(hashtag)) {


return hashtagsName.length === newArray.length;
};

const getHashtagsCount = (element) => {
const hashtagsArray = getHashtagsArray(element);
return hashtagsArray.length <= HASHTAGS_COUNT;
};

const getHashtagsDuplicate = (element) => {
const hashtagsArray = getHashtagsArray(element);
const newArray = [];
for (let i = 0; i < hashtagsArray.length; i++) {
if(!newArray.includes(hashtagsArray[i])){
newArray.push(hashtagsArray[i]);
}
}
if(newArray.length === hashtagsArray.length) {
return newArray;
}
};

Copy link
Collaborator

@Kartaviy-arteom Kartaviy-arteom Mar 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new Set(hashtagsArray ).size === hashtagsArray .length
https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Global_Objects/Set

const getCommentLength = (element) => element.length < COMMENT_LENGTH;


pristine.addValidator(uploadHashtag, getHashtagsName, 'введён невалидный хэштег');
pristine.addValidator(uploadHashtag, getHashtagsCount, 'превышено количество хэштегов');
pristine.addValidator(uploadHashtag, getHashtagsDuplicate, 'хэштеги повторяются');
pristine.addValidator(uploadComment, getCommentLength, 'длина комментария больше 140 символов');

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ошибки в константы
const ErrorMessage = { }

formUpload.addEventListener('submit', (evt) => {
evt.preventDefault();
pristine.validate();
});