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

Надо подкачаться #13

Merged
merged 2 commits into from
Dec 26, 2024
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
68 changes: 68 additions & 0 deletions js/alerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import {isEscKeyDown} from './util.js';

const successUploadContainer = document.querySelector('#success').content.querySelector('.success').cloneNode(true);
const errorUploadContainer = document.querySelector('#error').content.querySelector('.error').cloneNode(true);
const dataAlertContainer = document.querySelector('#data-error').content.querySelector('.data-error').cloneNode(true);
const successButton = successUploadContainer.querySelector('.success__button');
const errorButton = errorUploadContainer.querySelector('.error__button');

successUploadContainer.addEventListener('keydown', (evt) =>
onKeyDownEsc(evt, successUploadContainer));

successUploadContainer.addEventListener('click', (evt) =>
onClick(evt, successUploadContainer, 'success'));

successButton.addEventListener('click', () => {
successUploadContainer.remove();
});

successButton.addEventListener('blur', () => {
successButton.focus();
});

errorUploadContainer.addEventListener('keydown', (evt) =>
onKeyDownEsc(evt, errorUploadContainer));

errorUploadContainer.addEventListener('click', (evt) =>
onClick(evt, errorUploadContainer, 'error'));

errorButton.addEventListener('click', () => {
errorUploadContainer.remove();
});

errorButton.addEventListener('blur', () => {
errorButton.focus();
});

function onKeyDownEsc(evt, container) {
if (isEscKeyDown(evt)) {
evt.stopPropagation();
container.remove();
}
}

function onClick(evt, container, evtClass) {
if (evt.target.classList.contains(evtClass)) {
container.remove();
}
}

function showSuccessAlert() {
addAlertElement(successUploadContainer, successButton);
}

function showErrorAlert() {
addAlertElement(errorUploadContainer, errorButton);
}

function showDataAlert() {
document.body.appendChild(dataAlertContainer);
setTimeout(() => dataAlertContainer.remove(), 5000);
}

function addAlertElement(element, elementButton) {
document.body.appendChild(element);
elementButton.focus();
}

export {showSuccessAlert, showErrorAlert, showDataAlert};
37 changes: 37 additions & 0 deletions js/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {showDataAlert} from './alerts.js';

function getData() {
return fetch('https://31.javascript.htmlacademy.pro/kekstagram/data')
.then((response) => {
if (response.ok) {
return response.json();
} else {
throw new Error('Неуспешный ответ от сервера');
}
})
.catch(() => {
showDataAlert();
});
}

function sendData(onSuccess, onFail, body) {
fetch('https://31.javascript.htmlacademy.pro/kekstagram', {
method: 'POST',
body: body
}).then((response) => {
if (response.ok) {
onSuccess();
} else {
throw new Error('Неуспешный ответ от сервера');
}
}).catch(() => onFail());
}

function showData(onSuccess) {
getData().then((data) => {
onSuccess(data);
});
}

export {getData, sendData, showData};

69 changes: 37 additions & 32 deletions js/showBigPicture.js → js/big-picture.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {photos} from './miniatureCreate.js';
import {isEscKeyDown} from './util.js';
import {addHidden, removeHidden} from './util.js';
import {getData} from './api.js';

const COMMENTS_AMOUNT = 5;

Expand All @@ -12,6 +12,16 @@ const buttonCansel = document.querySelector('.big-picture__cancel');
const buttonLoad = document.querySelector('.social__comments-loader');
let shownCommentsCounter = 0;

picturesContainer.addEventListener('click', (evt) => {
if (evt.target.classList.contains('picture__img')) {
openBigPicture(evt);
}
});

buttonCansel.addEventListener('click', () => {
closeBigPicture();
});

function onBigPictureEscKeyDown(evt) {
if (isEscKeyDown(evt)) {
addHidden(bigPicture);
Expand All @@ -21,31 +31,37 @@ function onBigPictureEscKeyDown(evt) {
}

function onButtonLoadClick() {
const photoId = document.querySelector('.big-picture img').dataset.photoId;
const currentPhoto = getPhotoById(photoId);
addComments(currentPhoto, COMMENTS_AMOUNT + shownCommentsCounter);
getCommentsShown();
getData().then((photos) => {
const photoId = document.querySelector('.big-picture img').dataset.photoId;
const currentPhoto = getPhotoById(photos, photoId);
addComments(currentPhoto, COMMENTS_AMOUNT + shownCommentsCounter);
getCommentsShown();
});
}

function createBigPictureData(evt) {
const miniature = evt.target.closest('.picture');
if (miniature.classList.contains('picture')) {
const photoId = miniature.querySelector('.picture__img').dataset.photoId;
const currentPhoto = getPhotoById(photoId);
bigPicture.querySelector('.big-picture__img').querySelector('img').src =
currentPhoto.url;
bigPicture.querySelector('.big-picture__img').querySelector('img').dataset.photoId = currentPhoto.id;
bigPicture.querySelector('.likes-count').textContent =
currentPhoto.likes;
bigPicture.querySelector('.social__comment-total-count').textContent =
currentPhoto.comments.length.toString();
function createBigPicture(evt) {
getData().then((photos) => {
const photoId = evt.target.dataset.photoId;
const currentPhoto = getPhotoById(photos, photoId);
updateBigPicture(currentPhoto);
addComments(currentPhoto, COMMENTS_AMOUNT);
getCommentsShown();
}
});
}

function updateBigPicture(currentPhoto) {
bigPicture.querySelector('.big-picture__img').querySelector('img').src =
currentPhoto.url;
bigPicture.querySelector('.big-picture__img').querySelector('img').dataset.photoId =
currentPhoto.id;
bigPicture.querySelector('.likes-count').textContent =
currentPhoto.likes;
bigPicture.querySelector('.social__comment-total-count').textContent =
currentPhoto.comments.length.toString();
}

function openBigPicture(evt) {
createBigPictureData(evt);
createBigPicture(evt);
removeHidden(bigPicture);
buttonLoad.addEventListener('click', onButtonLoadClick);
document.addEventListener('keydown', onBigPictureEscKeyDown);
Expand Down Expand Up @@ -89,17 +105,6 @@ function getCommentsShown() {
shownCommentsCounter.toString();
}

function getPhotoById(id) {
return photos.find((photo) => photo.id === Number(id));
function getPhotoById(data, id) {
return data.find((photo) => photo.id === Number(id));
}

picturesContainer.addEventListener('click', (evt) => {
if(evt.target.classList.contains('picture__img')) {
openBigPicture(evt);
}
});

buttonCansel.addEventListener('click', () => {
closeBigPicture();
});

48 changes: 0 additions & 48 deletions js/data.js

This file was deleted.

79 changes: 79 additions & 0 deletions js/effects.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import {createSlider, resetSlider, updateSlider, onUpdateSlider} from './slider-bar.js';

const effectLevelValue = document.querySelector('.effect-level__value');
const imgUploadEffects = document.querySelector('.img-upload__effects');
const imgUploadPreview = document.querySelector('.img-upload__preview');
const sliderBar = document.querySelector('.effect-level__slider');
const sliderContainer = document.querySelector('.img-upload__effect-level');

const effects = {
none: {filter: 'none', start: 1, minSliderValue: 0, maxSliderValue: 1, step: 0.1},
chrome: {filter: 'grayscale', start: 1, minSliderValue: 0, maxSliderValue: 1, step: 0.1},
sepia: {filter: 'sepia', start: 1, minSliderValue: 0, maxSliderValue: 1, step: 0.1},
marvin: {filter: 'invert', start: 100, minSliderValue: 0, maxSliderValue: 100, step: 1},
phobos: {filter: 'blur', start: 3, minSliderValue: 0, maxSliderValue: 3, step: 0.1},
heat: {filter: 'brightness', start: 3, minSliderValue: 0, maxSliderValue: 3, step: 0.1},
};

createSlider(sliderBar,
effects.none.start,
effects.none.minSliderValue,
effects.none.maxSliderValue,
effects.none.step);

onUpdateSlider(sliderBar, () => {
effectLevelValue.value = sliderBar.noUiSlider.get();
});

sliderContainer.style.visibility = 'hidden';

imgUploadEffects.addEventListener('change', (evt) => {
if (evt.target.value === 'none') {
sliderContainer.style.visibility = 'hidden';
} else {
sliderContainer.style.visibility = 'visible';
}
resetSlider(sliderBar);
applyEffectLevel(imgUploadPreview, evt.target.value);
});

function applyEffectLevel(image, effectName) {
updateSliderOptions(effectName);
if (effectName === 'none') {
image.style.filter = effects[effectName].filter;
} else {
onUpdateSlider(sliderBar, () => {
image.style.filter = `${effects[effectName].filter}(${getFilterValue(effectName)})`;
});
}
}

function updateSliderOptions(effectName) {
const sliderParameters = {...effects[effectName]};
delete sliderParameters['filter'];
switch (effectName) {
case 'marvin': {
updateSlider(sliderBar, ...Object.values(sliderParameters));
}
break;
case 'phobos':
case 'heat': {
updateSlider(sliderBar, ...Object.values(sliderParameters));
}
break;
default: {
updateSlider(sliderBar, ...Object.values(sliderParameters));
}
}
}

function getFilterValue(effectName) {
let value = effectLevelValue.value;
if (effectName === 'marvin') {
value += '%';
} else if (effectName === 'phobos') {
value += 'px';
}
return value;
}

Loading
Loading