Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions js/const.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const DESCRIPTION = [
const DESCRIPTIONS = [
'Тяжелый день на болоте. Заказ пиццы – единственная активность на вечер.',
'Этот вазон сам бросился под меня. Я лишь попытался его спасти.',
'Моё лицо, когда ты в третий раз рассказываешь эту историю.',
Expand Down Expand Up @@ -59,4 +59,4 @@ const SURNAMES = [
'Тапочкин'
];

export {DESCRIPTION, COMMENTS, NAMES, SURNAMES};
export {DESCRIPTIONS, COMMENTS, NAMES, SURNAMES};
24 changes: 12 additions & 12 deletions js/filter.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {container, photosData, loadAndRenderPhotos, renderPhotos} from './picture-from-api.js';
import {container, getPhotosData, onLoadRenderPhotos, renderPhotos} from './picture-from-api.js';
import {getUniqueRandom} from './utils.js';
import {debounce} from './main.js';

Expand All @@ -20,40 +20,40 @@ buttons.forEach((button) => {
});
});

function clearAll() {
const clearAll = () => {
const photoElements = container.querySelectorAll('.picture');
photoElements.forEach((element) => element.remove());
}
};

function showDefault() {
const showDefault = () => {
clearAll();
loadAndRenderPhotos().catch((error) => {
onLoadRenderPhotos().catch((error) => {
throw new Error(error);
});
}
};

function showRandom() {
const showRandom = () => {
clearAll();
const randomSet = new Set();

for (let i = 0; i < 10; i++) {

const number = getUniqueRandom(randomSet, 0, 24, 'Не могу придумать новое число');
const picture = photosData[number];
const picture = getPhotosData()[number];
renderPhotos(picture);
randomSet.add(number);
}
}
};

function showDiscussed() {
const showDiscussed = () => {
clearAll();
const descendingCommentsPhotos = photosData.sort((a, b) =>
const descendingCommentsPhotos = getPhotosData().sort((a, b) =>
b.comments.length - a.comments.length
);
descendingCommentsPhotos.forEach((picture) => {
renderPhotos(picture);
});
}
};


const debouncedShowDefault = debounce(showDefault);
Expand Down
104 changes: 54 additions & 50 deletions js/form.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,42 @@
import { BASE_URL, Route, Method, ErrorText } from './api.js';
import {sendTemplate, sendErrorTemplate} from './templates.js';

const HASH_REGULAR = new RegExp('^#[a-zа-яё0-9]{1,19}$', 'i');

const form = document.querySelector('.img-upload__form');
const formOpener = document.querySelector('.img-upload__start');
const formWindow = document.querySelector('.img-upload__overlay');
const formCloseBtn = document.querySelector('.img-upload__cancel');
const submitButton = document.querySelector('.img-upload__submit');

const hashRegular = new RegExp('^#[a-zа-яё0-9]{1,19}$', 'i');
const hashtagField = document.querySelector('.text__hashtags');
const commentField = document.querySelector('.text__description');

const submitButtonText = submitButton.textContent;

const successModal = sendTemplate.content.cloneNode(true).querySelector('.success');
const errorElement = sendErrorTemplate.content.cloneNode(true);
const errorModal = errorElement.querySelector('.error');
const errorButton = errorModal.querySelector('.error__button');

function blockSubmitButton() {
const blockSubmitButton = () => {
submitButton.disabled = true;
submitButton.textContent = 'Публикую...';
}
};

function unblockSubmitButton() {
const unblockSubmitButton = () => {
submitButton.disabled = false;
submitButton.textContent = submitButtonText;
}
function closeSuccessModal() {
// const successModal = sendTemplate.content.cloneNode(true).querySelector('.success');
};

const onCloseSuccessModal = () => {
successModal.remove();
changeEventListeners('remove');

Check failure on line 31 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

'changeEventListeners' was used before it was defined
}
function handleEscapeKey(evt) {
};

const onCloseErrorModal = () => {
errorModal.remove();
changeEventListeners('remove');

Check failure on line 36 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

'changeEventListeners' was used before it was defined
};

const onHandleEscapeKey = (evt) => {
if (evt.key === 'Escape') {
const excludedFields = [hashtagField, commentField];
const activeElement = document.activeElement;
Expand All @@ -42,37 +49,43 @@
});

if (shouldClose) {
closeForm();
onCloseForm();

Check failure on line 52 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

'onCloseForm' was used before it was defined
}
}
}
function onEscPress(evt) {
};

const onEscPress = (evt) => {
if (evt.key === 'Escape') {
closeSuccessModal();

onCloseSuccessModal();
onCloseErrorModal();
}
}
function onOutsideClick(evt) {
};

const onOutsideClick = (evt) => {
if (!evt.target.closest('.success__inner')) {
closeSuccessModal();
onCloseSuccessModal();
}
}
function switchForm(param1, param2) {
};

const switchForm = (param1, param2) => {
formWindow.classList[param1]('hidden');
document.querySelector('body').classList[param2]('modal-open');
}
function changeEventListeners(action) {
};

const changeEventListeners = (action) => {
const method = `${action}EventListener`;
document[method]('keydown', onEscPress);
document[method]('click', onOutsideClick);
}
};

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

function resetForm() {
const resetForm = () => {
form.reset();
pristine.reset();

Expand All @@ -92,40 +105,40 @@
if (effectLevel) {
effectLevel.classList.add('hidden');
}
}

Check failure on line 108 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

function closeForm() {
const onCloseForm = () => {
switchForm('add', 'remove');
resetForm();
}
};


formOpener.addEventListener('click', () => {
switchForm('remove', 'add');
});

function validateHashContent(value) {
const validateHashContent = (value) => {
if (value.trim() === '') {
return true;
}
const hashtags = value.split(' ');
for (const hashtag of hashtags) {
if (!hashRegular.test(hashtag)) {
if (!HASH_REGULAR.test(hashtag)) {
return false;
}
}
return true;
}
};

function validateHashAmount(value) {
const validateHashAmount = (value) => {
if (!value.trim()) {
return true;
}
const hashtags = value.split(' ');
return hashtags.length <= 5;
}
};

function validateHashRepeat(value) {
const validateHashRepeat = (value) => {
if (!value.trim()) {
return true;
}
Expand All @@ -139,7 +152,7 @@
}
}
return true;
}

Check failure on line 155 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

pristine.addValidator(
commentField,
Expand All @@ -162,30 +175,21 @@
'Хештеги не должны повторяться'
);

function showSuccessMessage() {
const showSuccessMessage = () => {
const successButton = successModal.querySelector('.success__button');

document.body.appendChild(successModal);

successButton.addEventListener('click', closeSuccessModal);
successButton.addEventListener('click', onCloseSuccessModal);
changeEventListeners('add');
}

Check failure on line 185 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

function showErrorMessage() {
const errorElement = sendErrorTemplate.content.cloneNode(true);
const errorModal = errorElement.querySelector('.error');
const errorButton = errorModal.querySelector('.error__button');

const showErrorMessage = () => {
document.body.appendChild(errorModal);

function closeErrorModal() {
errorModal.remove();
changeEventListeners('remove');
}

errorButton.addEventListener('click', closeErrorModal);
errorButton.addEventListener('click', onCloseErrorModal);
changeEventListeners('add');
}

Check failure on line 192 in js/form.js

View workflow job for this annotation

GitHub Actions / Check

Missing semicolon

form.addEventListener('submit', async (evt) => {
evt.preventDefault();
Expand All @@ -208,7 +212,7 @@
throw new Error(ErrorText[Method.POST]);
}

closeForm();
onCloseForm();
showSuccessMessage();

} catch (error) {
Expand All @@ -219,10 +223,10 @@
}
});

formCloseBtn.addEventListener('click', closeForm);
document.addEventListener('keydown', handleEscapeKey);
formCloseBtn.addEventListener('click', onCloseForm);
document.addEventListener('keydown', onHandleEscapeKey);

const cancelButton = document.querySelector('.img-upload__cancel');
if (cancelButton) {
cancelButton.addEventListener('click', closeForm);
cancelButton.addEventListener('click', onCloseForm);
}
4 changes: 2 additions & 2 deletions js/photo-preview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const FILE_TYPES = ['jpg', 'jpeg', 'png'];
const fileChooser = document.querySelector('.img-upload__input');
const preview = document.querySelector('.img-upload__preview img');
const effectPreview = document.querySelectorAll('.effects__preview');
const effectPreviews = document.querySelectorAll('.effects__preview');

fileChooser.addEventListener('change', () => {
const file = fileChooser.files[0];
Expand All @@ -10,7 +10,7 @@ fileChooser.addEventListener('change', () => {
if (FILE_TYPES.some((it) => fileName.endsWith(it))) {
const url = URL.createObjectURL(file);
preview.src = url;
effectPreview.forEach((picture) => {
effectPreviews.forEach((picture) => {
picture.style.backgroundImage = `url(${url})`;
});
}
Expand Down
Loading