Skip to content

Commit

Permalink
Merge pull request #9 from nikitakhrulev/module8-task1
Browse files Browse the repository at this point in the history
  • Loading branch information
keksobot authored Dec 5, 2024
2 parents 6e139d2 + d08f4e7 commit 85d0866
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 13 deletions.
13 changes: 12 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { createMockPosts } from './mock.js';
import { printThumbnails } from './thumbnails.js';
import { picturesContainer } from './thumbnails.js';
import { findPostContent } from './post.js';

createMockPosts();
const initApp = () => {
const data = createMockPosts();
printThumbnails(data);
picturesContainer.addEventListener('click', (evt) => {
findPostContent(evt, data);
});
};

document.addEventListener('DOMContentLoaded', initApp);

14 changes: 4 additions & 10 deletions js/mock.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { createRandomId, getRandomInteger } from './utils';
import { printThumbnails } from './thumbnails';
import { POSTS_DATA } from './data';

import { createRandomId, getRandomInteger } from './utils.js';
import { POSTS_DATA } from './data.js';
const { POSTS_COUNT, NAMES, DESCRIPTIONS, COMMENTS } = POSTS_DATA;

const generatePhotoId = createRandomId(1, POSTS_COUNT);
Expand All @@ -27,11 +25,7 @@ const createPhotoPost = () => {
};
};

const createMockPosts = () => {
const data = Array.from({length: POSTS_COUNT}, createPhotoPost);

printThumbnails(data);
};

// const createMockPosts = () => Array.from({length: POSTS_COUNT}, createPhotoPost);
const createMockPosts = () => Array.from({length: POSTS_COUNT}, createPhotoPost);

export {createMockPosts};
57 changes: 57 additions & 0 deletions js/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { escKeypress } from './utils';

const postModal = document.querySelector('.big-picture');
const closeModalBtn = document.querySelector('.big-picture__cancel');
const postComments = document.querySelector('.social__comments');


const closePostModal = () => {
postModal.classList.add('hidden');
closeModalBtn.removeEventListener('click', closePostModal);
document.body.classList.remove('modal-open');
};


const insertPostComments = (commentsArr) => {
const commentsFragment = document.createDocumentFragment();
commentsArr.forEach((comment) => {
const commentNode = document.createElement('li');
commentNode.classList.add('social__comment');
commentNode.innerHTML = `
<img
class="social__picture"
src="${comment.avatar}"
alt="${comment.name}"
width="35" height="35">
<p class="social__text">${comment.message}</p>`;
commentsFragment.append(commentNode);
});
postComments.append(commentsFragment);
};

const openPostModal = (content) => {
postModal.classList.remove('hidden');
document.body.classList.add('modal-open');
postModal.querySelector('img').src = content.url;
postModal.querySelector('.likes-count').textContent = content.likes;
postModal.querySelector('.social__caption').textContent = content.description;
postModal.querySelector('.social__comment-total-count').textContent = content.comments.length;
postModal.querySelector('.social__comment-count').classList.add('hidden');
postModal.querySelector('.comments-loader').classList.add('hidden');
postComments.innerHTML = '';
insertPostComments(content.comments);
closeModalBtn.addEventListener('click', closePostModal);
};

const findPostContent = (evt, data) => {
const url = evt.target.closest('.picture').querySelector('img').src;
const photoId = Number(url.split('/').pop().split('.')[0]);
const postData = data.find((photo) => photo.id === photoId);
openPostModal(postData);
};

document.addEventListener('keydown', (evt) => {
escKeypress(evt, closePostModal);
});

export {findPostContent};
2 changes: 1 addition & 1 deletion js/thumbnails.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ const printThumbnails = (data) => {
picturesContainer.append(postsFragment);
};

export {printThumbnails};
export {printThumbnails, picturesContainer};
8 changes: 7 additions & 1 deletion js/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,10 @@ const createRandomId = (min, max) => {
};
};

export {createRandomId, getRandomInteger};
const escKeypress = (evt, fn) => {
if (evt.key === 'Escape') {
fn();
}
};

export {createRandomId, getRandomInteger, escKeypress};

0 comments on commit 85d0866

Please sign in to comment.