-
Notifications
You must be signed in to change notification settings - Fork 1
Перламутровые пуговицы #14
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
Merged
keksobot
merged 9 commits into
htmlacademy-javascript:master
from
Musa-Ismailov:module12-task1
Dec 23, 2025
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
df032cc
правки по пунктам
Musa-Ismailov 10c5717
выполняет 12 дз 1
Musa-Ismailov a366654
обновляет
Musa-Ismailov e494846
обновляет 2
Musa-Ismailov 3ddfc4d
правки 3
Musa-Ismailov 5245334
изменят правки 4
Musa-Ismailov 719e87c
правит ссылку
Musa-Ismailov a54b4a0
итоговые правки
Musa-Ismailov 65d0ece
добовляет debounce
Musa-Ismailov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| const BASE_URL = 'https://31.javascript.htmlacademy.pro/kekstagram'; | ||
| const Route = { | ||
| GET_DATA: '/data', | ||
| SEND_DATA: '/', | ||
| }; | ||
| const Method = { | ||
| GET: 'GET', | ||
| POST: 'POST', | ||
| }; | ||
| const ErrorText = { | ||
| GET_DATA: 'Не удалось загрузить данные. Попробуйте обновить страницу', | ||
| SEND_DATA: 'Не удалось отправить форму. Попробуйте ещё раз', | ||
| }; | ||
|
|
||
| const load = (route, errorText, method = Method.GET, body = null) => | ||
| fetch(`${BASE_URL}${route}`, {method, body}) | ||
| .then((response) => { | ||
| if (!response.ok) { | ||
| throw new Error(); | ||
| } | ||
| return response.json(); | ||
| }) | ||
| .catch(() => { | ||
| throw new Error(errorText); | ||
| }); | ||
|
|
||
| export const getPhotos = () => load(Route.GET_DATA, ErrorText.GET_DATA); | ||
| export const sendPhoto = (body) => load(Route.SEND_DATA, ErrorText.SEND_DATA, Method.POST, body); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| import { isEscapeKey } from './utils.js'; | ||
|
|
||
| const ERROR_DISPLAY_DURATION = 5000; | ||
|
|
||
| let currentDialog = null; | ||
|
|
||
| const errorTemplate = document.querySelector('#error'); | ||
| const successTemplate = document.querySelector('#success'); | ||
| const dataErrorTemplate = document.querySelector('#data-error'); | ||
|
|
||
| const onCloseKeydown = (evt) => { | ||
| if (isEscapeKey(evt.key) && currentDialog) { | ||
| evt.preventDefault(); | ||
| closeDialog(); | ||
| evt.stopPropagation(); | ||
| } | ||
| }; | ||
|
|
||
| function closeDialog () { | ||
| if (currentDialog) { | ||
| currentDialog.remove(); | ||
| currentDialog = null; | ||
|
|
||
| document.removeEventListener('keydown', onCloseKeydown, true); | ||
| } | ||
| } | ||
|
|
||
| const onCloseClick = (evt) => { | ||
| if (evt.target.closest('[data-dialog-close]') || !evt.target.closest('[data-dialog-content]')) { | ||
| closeDialog(); | ||
| } | ||
| }; | ||
|
|
||
| const openDialog = (template) => { | ||
| const dialogElement = template.content.firstElementChild.cloneNode(true); | ||
|
|
||
| document.body.appendChild(dialogElement); | ||
|
|
||
| currentDialog = dialogElement; | ||
|
|
||
| document.addEventListener('keydown', onCloseKeydown, true); | ||
| }; | ||
|
|
||
| document.addEventListener('click', onCloseClick); | ||
|
|
||
| export const showError = () => { | ||
| openDialog(errorTemplate); | ||
| }; | ||
|
|
||
| export const showSuccess = () => { | ||
Musa-Ismailov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| openDialog(successTemplate); | ||
| }; | ||
|
|
||
| export const showDataError = () => { | ||
| const errorElement = dataErrorTemplate.content.firstElementChild.cloneNode(true); | ||
|
|
||
| document.body.appendChild(errorElement); | ||
|
|
||
| setTimeout(() => { | ||
| if (errorElement) { | ||
| errorElement.remove(); | ||
| } | ||
| }, ERROR_DISPLAY_DURATION); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import { renderGallery, clearGallery, getGalleryData } from './gallery.js'; | ||
| import { debounce } from './utils.js'; | ||
|
|
||
| const RERENDER_DELAY = 500; | ||
|
|
||
| const filterName = { | ||
| FILTER_RANDOM: 'filter-random', | ||
| FILTER_DISCUSSED: 'filter-discussed', | ||
| FILTER_DEFAULT: 'filter-default' | ||
| }; | ||
|
|
||
| const filtersContainer = document.querySelector('.img-filters'); | ||
| let activeFilterButton = filtersContainer.querySelector('.img-filters__button--active'); | ||
|
|
||
| const shuffleArray = (data) => | ||
| data.toSorted(() => Math.random() - 0.5); | ||
|
|
||
| const sortByCommentsCount = (data) => | ||
| data.toSorted((a, b) => b.comments.length - a.comments.length); | ||
|
|
||
| const getFilteredData = (filterId) => { | ||
| const galleryData = getGalleryData(); | ||
|
|
||
| switch (filterId) { | ||
| case filterName.FILTER_RANDOM: | ||
| return shuffleArray(galleryData); | ||
|
|
||
| case filterName.FILTER_DISCUSSED: | ||
| return sortByCommentsCount(galleryData); | ||
|
|
||
| case filterName.FILTER_DEFAULT: | ||
| default: | ||
| return galleryData; | ||
| } | ||
| }; | ||
|
|
||
| const handleFilterClick = (evt) => { | ||
| const button = evt.target.closest('.img-filters__button'); | ||
|
|
||
| if (!button) { | ||
| return; | ||
| } | ||
|
|
||
| if (activeFilterButton) { | ||
| activeFilterButton.classList.remove('img-filters__button--active'); | ||
| } | ||
|
|
||
| button.classList.add('img-filters__button--active'); | ||
| activeFilterButton = button; | ||
|
|
||
| const filteredData = getFilteredData(button.id); | ||
|
|
||
| clearGallery(); | ||
|
|
||
| debounce(() => renderGallery(filteredData), RERENDER_DELAY)(); | ||
| }; | ||
|
|
||
| export const initFilters = () => { | ||
| filtersContainer.classList.remove('img-filters--inactive'); | ||
| filtersContainer.addEventListener('click', handleFilterClick); | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Этот модуль нужно переделать:
1 - функция открытия диалогов и закрытия у тебя очень похожи, не считая errorMessageGetPhotos, поэтому вынеси общую логику, постарайся оставить 2 функции openDialog / closeDialog, ты можешь указывать параметром template который открыть, явно передавая его, за них отвечает messageElement
2 - все поиски элементов сделай за переделами функций
const messageElement = document.querySelector('.error');и подобное, нам не нужно каждый раз при открытии искать элемент, ты их знаешь заранее, достаточно клонировать
3 - функции - это глагол, сейчас у тебя это не так: errorMessageSendPhoto, errorMessageGetPhotos и тд
4 - ты можешь запоминать последний открытый dialog в переменную, и потом этим воспользоваться в onOutsideClick, тебе не нужно искать постоянно элемент, ты его знаешь, а по закрытию очищаешь
5 - чтобы не делать проверку isErrorMessageOpen, ты можешь в onDocumentKeydown добавить вызов дополнительной функции, которая отвечает за отмену всплытия события