freeCodeCamp/views/stories/comments.jade

134 lines
7.9 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;
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(
'<div class="media 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">' +
2015-03-17 20:01:14 +00:00
"<a class='btn btn-no-shadow btn-primary btn-xs btn-primary-ghost' id='" + commentDetails._id + "'>Reply</a> · " +
2015-03-08 11:22:55 +00:00
"commented " + moment(commentDetails.commentOn).fromNow() + " by " +
"<a href='/" + commentDetails.author.username + "'>@" + commentDetails.author.username + "</a>" +
2015-03-08 11:22:55 +00:00
'</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 () {
if (typeof user == "undefined" || !user) {
window.location.href = '/signin';
return;
}
$(this).unbind('click');
2015-03-08 11:22:55 +00:00
$('.comment-to-comment-formgroup').empty();
$('#initial-comment-submit').addClass('hidden-element');
var div = document.createElement('div');
var commentId = $(this).attr('id');
$(div).html(
"<div class='formgroup comment-to-comment-formgroup control-label-story-submission'>" +
'<div class="col-xs-12">' +
"<div class='input-group'>" +
"<input class='form-control big-text-field field-responsive' id='comment-to-comment-textinput' type='text' maxlength='140' autofocus />" +
"<span class='input-group-btn'>" +
"<button id='submit-comment-to-comment' class='btn btn-big btn-primary btn-responsive'>Send</button>" +
"</span>" +
"</div>" +
"</div>" +
"<div id='textarea-comment-feedback'></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-comment-feedback').html(text_max + ' characters remaining');
$('#comment-to-comment-textinput').keyup(function (e) {
if (e.which === 13 || e === 13) {
$('#submit-comment-to-comment').click();
}
var text_length = $('#comment-to-comment-textinput').val().length;
2015-03-08 11:22:55 +00:00
var text_remaining = text_max - text_length;
$('#textarea-comment-feedback').html(text_remaining + ' characters remaining');
2015-03-08 11:22:55 +00:00
});
var submitCommentToCommentHandler = function submitCommentToCommentHandler() {
$('#submit-comment-to-comment').unbind('click');
$.post('/stories/comment/' + commentId + '/comment',
{
data: {
associatedPost: commentId,
body: $('#comment-to-comment-textinput').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