Skip to content

Commit 4032f55

Browse files
authored
Merge pull request #12 from suntira/module12-task1
2 parents 3762d0e + 40b9ae7 commit 4032f55

File tree

5 files changed

+111
-2
lines changed

5 files changed

+111
-2
lines changed

index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ <h2 class="img-upload__title visually-hidden">Загрузка фотограф
3535

3636
<!-- Изначальное состояние поля для загрузки изображения -->
3737
<fieldset class="img-upload__start">
38-
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename" required>
38+
<input type="file" id="upload-file" class="img-upload__input visually-hidden" name="filename" required accept=".jpg,.jpeg,.png">
3939
<label for="upload-file" class="img-upload__label img-upload__control">Загрузить</label>
4040
</fieldset>
4141

js/filters.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { renderGallery } from './gallery.js';
2+
import { debounce } from './util.js';
3+
4+
const filtersContainer = document.querySelector('.img-filters');
5+
const filtersForm = filtersContainer.querySelector('.img-filters__form');
6+
7+
const RANDOM_PHOTOS_COUNT = 10;
8+
9+
let photos = [];
10+
11+
const clearGallery = () => {
12+
document.querySelectorAll('.picture').forEach((picture) => {
13+
picture.remove();
14+
});
15+
};
16+
17+
const getDefaultPhotos = () => photos.slice();
18+
19+
const getRandomPhotos = () => {
20+
const shuffled = photos.slice().sort(() => Math.random() - 0.5);
21+
return shuffled.slice(0, RANDOM_PHOTOS_COUNT);
22+
};
23+
24+
const getDiscussedPhotos = () =>
25+
photos
26+
.slice()
27+
.sort((a, b) => b.comments.length - a.comments.length);
28+
29+
const filterHandlers = {
30+
default: getDefaultPhotos,
31+
random: getRandomPhotos,
32+
discussed: getDiscussedPhotos,
33+
};
34+
35+
const applyFilter = (filter) => {
36+
clearGallery();
37+
renderGallery((filterHandlers[filter] || filterHandlers.default)());
38+
};
39+
40+
const debouncedApplyFilter = debounce(applyFilter);
41+
42+
const onFilterClick = (evt) => {
43+
if (!evt.target.matches('button')) {
44+
return;
45+
}
46+
47+
const activeButton = filtersForm.querySelector('.img-filters__button--active');
48+
activeButton.classList.remove('img-filters__button--active');
49+
evt.target.classList.add('img-filters__button--active');
50+
51+
const filter = evt.target.id.replace('filter-', '');
52+
debouncedApplyFilter(filter);
53+
};
54+
55+
const initFilters = (loadedPhotos) => {
56+
photos = loadedPhotos.slice();
57+
58+
filtersContainer.classList.remove('img-filters--inactive');
59+
filtersForm.addEventListener('click', onFilterClick);
60+
};
61+
62+
export { initFilters };

js/main.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
import { renderGallery } from './gallery.js';
22
import { initPhotoUploadForm } from './photo-upload-form.js';
33
import { getData } from './api.js';
4+
import { initFilters } from './filters.js';
45
import { showDataError } from './messages.js';
6+
import { initPhotoPreview } from './view-the-uploaded-photo.js';
57

68
getData()
79
.then((photos) => {
810
renderGallery(photos);
11+
initFilters(photos);
912
})
1013
.catch(() => {
1114
showDataError();
1215
});
1316

1417
initPhotoUploadForm();
18+
initPhotoPreview();
19+

js/util.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const TIMEOUT_DELAY = 500;
12
const body = document.body;
23

34
const isEscapeKey = (evt) => evt.key === 'Escape';
@@ -13,4 +14,12 @@ const showModal = (modal, isShown = true) => {
1314
body.classList.remove('modal-open');
1415
};
1516

16-
export { isEscapeKey, showModal};
17+
const debounce = (callback, timeoutDelay = TIMEOUT_DELAY) => {
18+
let timeoutId;
19+
return (...rest) => {
20+
clearTimeout(timeoutId);
21+
timeoutId = setTimeout(() => callback.apply(this, rest), timeoutDelay);
22+
};
23+
};
24+
25+
export { isEscapeKey, showModal, debounce};

js/view-the-uploaded-photo.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
const FILE_TYPES = ['jpg', 'jpeg', 'png'];
2+
3+
const fileChooser = document.querySelector('.img-upload__input');
4+
const preview = document.querySelector('.img-upload__preview img');
5+
6+
const effectPreviews = document.querySelectorAll('.effects__preview');
7+
8+
const initPhotoPreview = () => {
9+
fileChooser.addEventListener('change', () => {
10+
const file = fileChooser.files[0];
11+
12+
if (!file) {
13+
return;
14+
}
15+
16+
const fileName = file.name.toLowerCase();
17+
const matches = FILE_TYPES.some((type) => fileName.endsWith(type));
18+
19+
if (!matches) {
20+
return;
21+
}
22+
23+
const imageUrl = URL.createObjectURL(file);
24+
25+
preview.src = imageUrl;
26+
27+
effectPreviews.forEach((effectPreview) => {
28+
effectPreview.style.backgroundImage = `url(${imageUrl})`;
29+
});
30+
});
31+
};
32+
33+
export { initPhotoPreview };

0 commit comments

Comments
 (0)