Skip to content

Commit

Permalink
Merge pull request #5 from vikkont79/module7-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Dec 19, 2024
2 parents 736b2b5 + 7d7f085 commit 7981753
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 93 deletions.
4 changes: 2 additions & 2 deletions js/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {
getRandomInteger,
getRandomUniqueInteger,
getRandomArrayElement,
} from './functions.js';
} from './utils.js';

// Массив описаний

Expand Down Expand Up @@ -105,7 +105,7 @@ const createGallery = (quantity) => {
const uniquePhotoUrl = getRandomUniqueInteger(1, quantity);
return Array.from({ length: quantity }).map(() => ({
id: uniquePhotoId(),
url: `photos/${uniquePhotoUrl()}.jpeg`,
url: `photos/${uniquePhotoUrl()}.jpg`,
description: getRandomArrayElement(DESCRIPTIONS),
likes: getRandomInteger(LikesRange.MIN, LikesRange.MAX),
comments: Array.from(
Expand Down
90 changes: 0 additions & 90 deletions js/function.js

This file was deleted.

5 changes: 4 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { createGallery } from './data.js';
import { renderGallery } from './view.js';

createGallery();
const photosPreview = createGallery(25);

renderGallery(photosPreview);
35 changes: 35 additions & 0 deletions js/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Проверка на максимальную длину строки

const checkStringLength = (string, maxLength) => string.length <= maxLength;

// Определяем рандомное значение

const getRandomInteger = (min, max) =>
Math.floor(Math.random() * (max - min + 1) + min);

// Проверяем его на уникальность (возвращаем рандомное значение, ранее не использованое в блоке)

const getRandomUniqueInteger = (min, max) => {
const uniqueIntArr = [];
return () => {
while (uniqueIntArr.length < max - min + 1) {
const randomInt = getRandomInteger(min, max);
if (!uniqueIntArr.includes(randomInt)) {
uniqueIntArr.push(randomInt);
return randomInt;
}
}
};
};

// Получаем рандомный элемент любого массива

const getRandomArrayElement = (elements) =>
elements[getRandomInteger(0, elements.length - 1)];

export {
getRandomInteger,
checkStringLength,
getRandomUniqueInteger,
getRandomArrayElement,
};
25 changes: 25 additions & 0 deletions js/view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const renderGallery = (gallery) => {
const picturesContainerElement = document.querySelector('.pictures');
const pictureTemplateElement = document
.querySelector('#picture')
.content.querySelector('.picture');
const pictureFragment = document.createDocumentFragment();

gallery.forEach(({ id, url, description, likes, comments }) => {
const pictureElement = pictureTemplateElement.cloneNode(true);
const imgElement = pictureElement.querySelector('.picture__img');
const likesElement = pictureElement.querySelector('.picture__likes');
const commentsElement = pictureElement.querySelector('.picture__comments');
pictureElement.href = url;
pictureElement.dataset.id = id;
imgElement.src = url;
imgElement.alt = description;
likesElement.textContent = likes;
commentsElement.textContent = comments.length;
pictureFragment.appendChild(pictureElement);
});

picturesContainerElement.appendChild(pictureFragment);
};

export { renderGallery };

0 comments on commit 7981753

Please sign in to comment.