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

Фото на память #16

Merged
Merged
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
2 changes: 1 addition & 1 deletion js/effects-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from './constant.js';

const modalElement = document.querySelector('.img-upload');
const imgElement = modalElement.querySelector('.img-upload__preview');
const imgElement = modalElement.querySelector('.img-upload__preview img');
const effectsElement = modalElement.querySelector('.effects');
const slider = modalElement.querySelector('.effect-level__slider');
const sliderContainer = modalElement.querySelector('.img-upload__effect-level');
Expand Down
11 changes: 6 additions & 5 deletions js/form-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const closeRedactForm = () => {
imgUploadInput.value = '';
document.removeEventListener('keydown', onDocumentKeydown);
resetValidation();
imgUploadForm.reset();
};

const toggleSubmitButton = (isDisabled) => {
Expand All @@ -52,14 +53,14 @@ imgUploadCancelButton.addEventListener('click', () => {

const isErrorMessageShown = () => Boolean(document.querySelector('.error'));

function onDocumentKeydown(evt) {
if(evt.isEscapeKey && !isErrorMessageShown()) {
function onDocumentKeydown (evt) {
if (isEscapeKey(evt) && !isErrorMessageShown()) {
evt.preventDefault();
closeRedactForm();
}
}

fieldComments.addEventListener('.keydown', (evt) => {
fieldComments.addEventListener('keydown', (evt) => {
if(isEscapeKey(evt)) {
evt.stopPropagation();
}
Expand All @@ -75,12 +76,12 @@ const setOnFormSubmit = (callback) => {
imgUploadForm.addEventListener('submit', async (evt) => {
evt.preventDefault();

if (isValid) {
if (isValid()) {
toggleSubmitButton(true);
await callback(new FormData(imgUploadForm));
toggleSubmitButton();
}
});
};

export { setOnFormSubmit, imgUploadForm };
export { setOnFormSubmit, closeRedactForm };
2 changes: 1 addition & 1 deletion js/form-validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const resetValidation = () => {
pristine.reset();
};

const isValid = pristine.validate();
const isValid = () => pristine.validate();
initEffect();

export { isValid, resetValidation };
18 changes: 18 additions & 0 deletions js/load-pictures.js
Original file line number Diff line number Diff line change
@@ -1 +1,19 @@
import { FILE_TYPES } from './constant.js';

const imgUploadPreview = document.querySelector('.img-upload__preview img');
const fileChooser = document.querySelector('.img-upload__start input[type=file]');

const loadingPicture = () => {
fileChooser.addEventListener('change', () => {
const file = fileChooser.files[0];
const fileName = file.name.toLowerCase();

const matches = FILE_TYPES.some((it) => fileName.endsWith(it));

if (matches) {
imgUploadPreview.src = URL.createObjectURL(file);
}
});
};

export { loadingPicture };
4 changes: 2 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { generateThumbnails } from './thumbnail.js';
import { getData, sendData } from './api.js';
import { showAlert, debounce } from './utils.js';
import { setOnFormSubmit, imgUploadForm } from './form-modal.js';
import { setOnFormSubmit, closeRedactForm } from './form-modal.js';
import { showMessageSuccess, showMessageError } from './success-error.js';
import { init as initFilter, getFilteredPictures } from './filters.js';

setOnFormSubmit(async (data) => {
try {
await sendData(data);
imgUploadForm();
closeRedactForm();
showMessageSuccess();
} catch {
showMessageError();
Expand Down
44 changes: 20 additions & 24 deletions js/success-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,46 @@ const errorFragment = document.createDocumentFragment();

const body = document.querySelector('body');

const onEscapeKeydownSuccess = (evt) => {
if (isEscapeKey(evt)) {
document.querySelector('.success').remove();
document.removeEventListener('keydown', onEscapeKeydownSuccess);
}
};

const showMessageSuccess = () => {
const messageSuccess = successTemplate.cloneNode(true);
successFragment.appendChild(messageSuccess);
body.appendChild(successFragment);
const buttonSuccess = document.querySelector('.success__button');
const sectionSuccess = document.querySelector('.success');
const successInner = document.querySelector('.success__inner');
document.addEventListener('keydown', (evt) => {
if (isEscapeKey(evt)) {
sectionSuccess.remove();
}
});
sectionSuccess.addEventListener(('click'), (evt) => {
if (evt.target === buttonSuccess) {
sectionSuccess.remove();
}
});
document.addEventListener('keydown', onEscapeKeydownSuccess);
sectionSuccess.addEventListener(('click'), (evt) => {
if (evt.target === successInner) {
if (evt.target === buttonSuccess || evt.target.classList.contains('success')) {
sectionSuccess.remove();
document.removeEventListener('keydown', onEscapeKeydownSuccess);
}
});
};

const onEscapeKeydownError = (evt) => {
if (isEscapeKey(evt)) {
document.querySelector('.error').remove();
document.removeEventListener('keydown', onEscapeKeydownError);
}
};

const showMessageError = () => {
const messageError = errorTemplate.cloneNode(true);
errorFragment.appendChild(messageError);
body.appendChild(errorFragment);
const buttonError = document.querySelector('.error__button');
const sectionError = document.querySelector('.error');
const errorInner = document.querySelector('.error__inner');
document.addEventListener('keydown', (evt) => {
if (isEscapeKey(evt)) {
sectionError.remove();
}
});
sectionError.addEventListener(('click'), (evt) => {
if (evt.target === buttonError) {
sectionError.remove();
}
});
document.addEventListener('keydown', onEscapeKeydownError);
sectionError.addEventListener(('click'), (evt) => {
if (evt.target === errorInner) {
if (evt.target === buttonError || evt.target.classList.contains('error')) {
sectionError.remove();
document.removeEventListener('keydown', onEscapeKeydownError);
}
});
};
Expand Down
2 changes: 2 additions & 0 deletions js/thumbnail.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const pictureTemplate = document.querySelector('#picture')

const generateThumbnails = (picture) => {
picturesContainer.querySelectorAll('.picture').forEach((element) => element.remove());

const similarListFragment = document.createDocumentFragment();

picture.forEach(({ id, url, description, likes, comments }) => {
const pictureElement = pictureTemplate.cloneNode(true);
pictureElement.querySelector('.picture__img').dataset.id = id;
Expand Down
Loading