freeCodeCamp/views/stories/comments.jade

123 lines
7.2 KiB
Plaintext
Raw Normal View History

.text-left
2015-03-08 09:48:20 +00:00
div#comment-list
2015-03-03 13:52:45 +00:00
script(src="https://cdn.jsdelivr.net/ramda/0.10.0/ramda.min.js")
script(src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js")
script.
var sentinel = 0;
var renderComments = function renderComments(comments, containerSelector, level) {
var commentDetails;
2015-03-08 11:22:55 +00:00
var backgroundColorForCommentNestingLevel = level % 2 !== 0 ? 'odd' : 'even';
R.forEach(function displayComments(comment) {
2015-03-08 09:48:20 +00:00
$.ajax({
type: 'GET',
url: '/stories/comments/' + comment,
error: function (xhr, textStatus, errorThrown) {
commentDetails = {
error: true,
body: 'There seems to be a problem fetching this comment.'
}
},
success: function (data, textStatus, xhr) {
commentDetails = data;
var div = document.createElement('div');
$(div)
.html(
2015-03-08 11:22:55 +00:00
'<div class="media ' + backgroundColorForCommentNestingLevel + ' media-news">' +
2015-03-08 09:48:20 +00:00
'<div class="media-left">' +
2015-03-08 11:22:55 +00:00
"<a href='/" + commentDetails.author.username + "'>" +
'<img class="media-object img-news" src="' + commentDetails.author.picture +'" alt="' + commentDetails.author.username + '">' +
'</a>' +
2015-03-08 09:48:20 +00:00
'</div>' +
'<div class="media-body comment_' + commentDetails._id + '">' +
'<div class="media-body-wrapper">' +
'<p>' + commentDetails.body + '</p>' +
2015-03-08 11:22:55 +00:00
'<h6>' +
'<div class="clearfix comment-a-comment negative-15">' +
"commented " + moment(commentDetails.commentOn).fromNow() + " by " +
"<a href='/" + commentDetails.author.username + "'>@" + commentDetails.author.username + "</a> · " +
"<a id='" + commentDetails._id + "'>Reply</a>" +
'</div>' +
'</h6>' +
2015-03-08 09:48:20 +00:00
'</div>' +
'</div>' +
'</div>'
)
2015-03-08 09:48:20 +00:00
.addClass('comment-wrapper positive-10')
.appendTo($(containerSelector));
sentinel += commentDetails.comments.length;
renderComments(commentDetails.comments, '.comment_' + commentDetails._id, ++level);
},
complete: function () {
sentinel--;
if (!sentinel) {
$('.comment-a-comment').on('click', 'a', function () {
$(this).unbind('click');
2015-03-08 11:22:55 +00:00
$('.comment-to-comment-formgroup').empty();
var div = document.createElement('div');
var commentId = $(this).attr('id');
$(div).html(
2015-03-08 11:22:55 +00:00
"<div class='formgroup comment-to-comment-formgroup'>" +
"<textarea class='form-control' id='comment-to-comment-textarea' maxlength='140' autofocus></textarea>" +
'<div class="clearfix">' +
'<span class="pull-left" id="textarea_feedback"></span>' +
"<button class='btn btn-small btn-primary pull-right' id='submit-comment-to-comment'>Submit</button>" +
'</div>' +
"</div>"
)
2015-03-08 09:48:20 +00:00
.addClass('row')
.appendTo($(this).closest('.media-body-wrapper'));
2015-03-08 11:22:55 +00:00
var text_max = 140;
$('#textarea_feedback').html(text_max + ' characters remaining');
$('#comment-to-comment-textarea').keyup(function () {
var text_length = $('#comment-to-comment-textarea').val().length;
var text_remaining = text_max - text_length;
$('#textarea_feedback').html(text_remaining + ' characters remaining');
});
var submitCommentToCommentHandler = function submitCommentToCommentHandler() {
$('#submit-comment-to-comment').unbind('click');
$.post('/stories/comment/' + commentId + '/comment',
{
data: {
associatedPost: commentId,
body: $('#comment-to-comment-textarea').val(),
author: {
picture: user.profile.picture,
userId: user._id,
username: user.profile.username
}
}
})
.fail(function (xhr, textStatus, errorThrown) {
$('#submit-comment-to-comment').bind('click', submitCommentToCommentHandler);
})
.done(function (data, textStatus, xhr) {
window.location.reload();
});
};
$('#submit-comment-to-comment').on('click', submitCommentToCommentHandler);
});//
}
}
})
}, comments);
}
sentinel += comments.length;
2015-03-08 11:22:55 +00:00
renderComments(comments, $('#comment-list'), 0);
2015-03-03 13:52:45 +00:00