Skip to content

Commit f3d82b1

Browse files
authored
Merge pull request #15 from NoaVel/master
Fix critical issues and cleanup project
2 parents fea06a1 + c803848 commit f3d82b1

12 files changed

Lines changed: 87 additions & 284 deletions

js/check-hashtag-validity.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ const hasValidHashtagCount = (value) =>
1414
const hasValidHashtagLength = (value) =>
1515
normalizeHashtags(value).every((hashtag) => hashtag.length <= MAX_HASHTAG_LENGTH);
1616

17-
const hasValidHashtagFormat = (value) =>
18-
normalizeHashtags(value).every((hashtag) =>
19-
hashtag.startsWith('#') &&
20-
hashtag.length > 1 &&
21-
HASHTAG_REGEXP.test(hashtag)
22-
);
17+
const hasValidHashtagFormat = (value) => {
18+
const hashtags = normalizeHashtags(value);
19+
if (hashtags.length === 0) {
20+
return true;
21+
}
22+
return hashtags.every((hashtag) => HASHTAG_REGEXP.test(hashtag));
23+
};
2324

2425
const hasUniqueHashtags = (value) => {
2526
const hashtags = normalizeHashtags(value);

js/create-array-photos.js

Lines changed: 0 additions & 75 deletions
This file was deleted.

js/data.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

js/filters.js

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,41 @@ const FilterId = {
1111
DISCUSSED: 'filter-discussed',
1212
};
1313

14-
const filtersSection = document.querySelector('.img-filters');
15-
const filtersForm = document.querySelector('.img-filters__form');
14+
const filtersSectionNode = document.querySelector('.img-filters');
15+
const filtersFormNode = document.querySelector('.img-filters__form');
1616

1717
let currentFilter = FilterId.DEFAULT;
18-
let photos = [];
18+
let loadedPhotos = [];
1919

20-
const getRandomPhotos = (photosArray) => {
21-
const shuffled = [...photosArray].sort(() => Math.random() - 0.5);
20+
const getRandomPhotos = (photos) => {
21+
const shuffled = [...photos];
22+
for (let i = shuffled.length - 1; i > 0; i--) {
23+
const j = Math.floor(Math.random() * (i + 1));
24+
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
25+
}
2226
return shuffled.slice(0, RANDOM_PHOTOS_COUNT);
2327
};
2428

25-
const getDiscussedPhotos = (photosArray) =>
26-
[...photosArray].sort((a, b) => b.comments.length - a.comments.length);
29+
const getDiscussedPhotos = (photos) =>
30+
[...photos].sort((a, b) => b.comments.length - a.comments.length);
2731

2832
const filterPhotos = () => {
2933
switch (currentFilter) {
3034
case FilterId.RANDOM:
31-
return getRandomPhotos(photos);
35+
return getRandomPhotos(loadedPhotos);
3236
case FilterId.DISCUSSED:
33-
return getDiscussedPhotos(photos);
37+
return getDiscussedPhotos(loadedPhotos);
3438
default:
35-
return [...photos];
39+
return [...loadedPhotos];
3640
}
3741
};
3842

39-
const updateActiveButton = (clickedButton) => {
40-
const activeButton = filtersForm.querySelector(`.${ACTIVE_BUTTON_CLASS}`);
41-
if (activeButton) {
42-
activeButton.classList.remove(ACTIVE_BUTTON_CLASS);
43+
const updateActiveButton = (clickedButtonNode) => {
44+
const activeButtonNode = filtersFormNode.querySelector(`.${ACTIVE_BUTTON_CLASS}`);
45+
if (activeButtonNode) {
46+
activeButtonNode.classList.remove(ACTIVE_BUTTON_CLASS);
4347
}
44-
clickedButton.classList.add(ACTIVE_BUTTON_CLASS);
48+
clickedButtonNode.classList.add(ACTIVE_BUTTON_CLASS);
4549
};
4650

4751
const debouncedRenderThumbnails = debounce(
@@ -66,13 +70,13 @@ const onFilterButtonClick = (evt) => {
6670
};
6771

6872
const showFilters = () => {
69-
filtersSection.classList.remove('img-filters--inactive');
73+
filtersSectionNode.classList.remove('img-filters--inactive');
7074
};
7175

72-
const initFilters = (loadedPhotos) => {
73-
photos = loadedPhotos;
76+
const initFilters = (photos) => {
77+
loadedPhotos = photos;
7478
showFilters();
75-
filtersForm.addEventListener('click', onFilterButtonClick);
79+
filtersFormNode.addEventListener('click', onFilterButtonClick);
7680
};
7781

7882
export { initFilters };

js/full-size-picture.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,26 @@ const likesCountNode = bigPictureNode.querySelector('.likes-count');
1414
const commentsCaptionNode = bigPictureNode.querySelector('.social__caption');
1515
const bigPictureCancelNode = bigPictureNode.querySelector('.big-picture__cancel');
1616

17-
const onEscKeydown = (evt) => {
17+
const handlers = {};
18+
19+
handlers.onEscKeydown = (evt) => {
1820
if (evt.key === 'Escape') {
1921
evt.preventDefault();
20-
clearComments();
21-
bigPictureNode.classList.add('hidden');
22-
document.body.classList.remove('modal-open');
23-
document.removeEventListener('keydown', onEscKeydown);
22+
handlers.closeBigPicture();
2423
}
2524
};
2625

27-
const closeBigPicture = () => {
26+
handlers.closeBigPicture = () => {
2827
clearComments();
2928
bigPictureNode.classList.add('hidden');
3029
document.body.classList.remove('modal-open');
31-
document.removeEventListener('keydown', onEscKeydown);
30+
document.removeEventListener('keydown', handlers.onEscKeydown);
3231
};
3332

33+
3434
const onBigPictureCancelClick = (evt) => {
3535
evt.preventDefault();
36-
closeBigPicture();
36+
handlers.closeBigPicture();
3737
};
3838

3939
const openBigPicture = (pictureId) => {
@@ -51,7 +51,7 @@ const openBigPicture = (pictureId) => {
5151

5252
bigPictureNode.classList.remove('hidden');
5353
document.body.classList.add('modal-open');
54-
document.addEventListener('keydown', onEscKeydown);
54+
document.addEventListener('keydown', handlers.onEscKeydown);
5555
};
5656

5757
bigPictureCancelNode.addEventListener('click', onBigPictureCancelClick);

js/functions.js

Lines changed: 0 additions & 68 deletions
This file was deleted.

js/image-editor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ const onEffectsListChange = (evt) => {
121121
return;
122122
}
123123

124-
updateSliderOptions(evt.target.value);
124+
const effectName = evt.target.value;
125+
updateSliderOptions(effectName);
126+
applyEffect(effectName, effectsConfig[effectName].start);
125127
};
126128

127129
const onEffectLevelSliderUpdate = () => {

0 commit comments

Comments
 (0)