-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from harl-i/module8-task2
- Loading branch information
Showing
4 changed files
with
66 additions
and
21 deletions.
There are no files selected for viewing
This file contains 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 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,13 @@ | ||
const commentTemplateElement = document.querySelector('.social__comment'); | ||
|
||
function addComment({avatar, name, commentsList}, socialCommentsElement) { | ||
const newCommentElement = commentTemplateElement.cloneNode(true); | ||
const commentPictureElement = newCommentElement.querySelector('.social__picture'); | ||
|
||
commentPictureElement.setAttribute('src', avatar); | ||
commentPictureElement.setAttribute('alt', name); | ||
newCommentElement.querySelector('.social__text').textContent = commentsList; | ||
socialCommentsElement.appendChild(newCommentElement); | ||
} | ||
|
||
export {addComment}; |
This file contains 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 |
---|---|---|
@@ -1,18 +1,51 @@ | ||
import {addComment} from './comments-renderer.js'; | ||
|
||
const socialCommentsElement = document.querySelector('.social__comments'); | ||
const commentTemplateElement = socialCommentsElement.querySelector('.social__comment'); | ||
const bigPictureElement = document.querySelector('.big-picture'); | ||
const loadCommentElement = bigPictureElement.querySelector('.social__comment-count'); | ||
const currentCommentsCountElement = bigPictureElement.querySelector('.social__comment-shown-count'); | ||
const totalCommentsCountElement = bigPictureElement.querySelector('.social__comment-total-count'); | ||
const loadCommentsButtonElement = bigPictureElement.querySelector('.social__comments-loader'); | ||
const LOADING_STEP = 5; | ||
let commentsArray = []; | ||
let totalComments; | ||
let currentCommentsLoaded = 0; | ||
|
||
function fillCommentsList(comments) { | ||
socialCommentsElement.innerHTML = ''; | ||
comments.forEach((comment) => { | ||
const {avatar, name, commentsList} = comment; | ||
const newCommentElement = commentTemplateElement.cloneNode(true); | ||
const commentPictureElement = newCommentElement.querySelector('.social__picture'); | ||
|
||
commentPictureElement.setAttribute('src', avatar); | ||
commentPictureElement.setAttribute('alt', name); | ||
newCommentElement.querySelector('.social__text').textContent = commentsList; | ||
socialCommentsElement.appendChild(newCommentElement); | ||
}); | ||
|
||
if (comments.length > 0) { | ||
commentsArray = comments; | ||
currentCommentsLoaded = 0; | ||
totalComments = comments.length; | ||
|
||
const commentsToLoad = Math.min(LOADING_STEP, totalComments); | ||
for (let i = 0; i < commentsToLoad; i++) { | ||
addCommentAndUpdateCounter(commentsArray[i]); | ||
} | ||
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments); | ||
} else { | ||
loadCommentElement.innerHTML = ''; | ||
loadCommentElement.textContent = 'Нет комментариев'; | ||
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments); | ||
} | ||
} | ||
|
||
function loadMoreComments() { | ||
const commentsToLoad = Math.min(LOADING_STEP, totalComments - currentCommentsLoaded); | ||
|
||
for (let i = 0; i < commentsToLoad; i++) { | ||
addCommentAndUpdateCounter(commentsArray[currentCommentsLoaded]); | ||
} | ||
loadCommentsButtonElement.classList.toggle('hidden', currentCommentsLoaded >= totalComments); | ||
} | ||
|
||
function addCommentAndUpdateCounter(comment) { | ||
addComment(comment, socialCommentsElement); | ||
|
||
currentCommentsLoaded++; | ||
currentCommentsCountElement.textContent = currentCommentsLoaded; | ||
totalCommentsCountElement.textContent = totalComments; | ||
} | ||
|
||
export {fillCommentsList}; | ||
export {fillCommentsList, loadMoreComments}; |
This file contains 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