2015-01-18 23:55:39 -08:00
|
|
|
findQueueContainer = function($comment) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// go up and down the DOM until we find either A) a queue container or B) an unqueued comment
|
2015-01-18 23:55:39 -08:00
|
|
|
$up = $comment.prevAll(".queue-container, .comment-displayed").first();
|
|
|
|
$down = $comment.nextAll(".queue-container, .comment-displayed").first();
|
|
|
|
$prev = $comment.prev();
|
|
|
|
$next = $comment.next();
|
|
|
|
$queuedAncestors = $comment.parents(".comment-queued");
|
|
|
|
if ($queuedAncestors.exists()) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// console.log("----------- case 1: Queued Ancestor -----------");
|
|
|
|
// 1.
|
|
|
|
// our comment has one or more queued ancestor, so we look for the root-most
|
|
|
|
// ancestor's queue container
|
2015-01-18 23:55:39 -08:00
|
|
|
$container = $queuedAncestors.last().data("queue");
|
|
|
|
} else if ($prev.hasClass("queue-container")) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// console.log("----------- case 2: Queued Brother -----------");
|
|
|
|
// 2.
|
|
|
|
// the comment just above is queued, so we use the same queue container as him
|
2015-01-18 23:55:39 -08:00
|
|
|
$container = $prev.data("queue");
|
|
|
|
} else if ($prev.find(".comment").last().hasClass("comment-queued")) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// console.log("----------- case 3: Queued Cousin -----------");
|
|
|
|
// 3.
|
|
|
|
// there are no queued comments going up on the same level,
|
|
|
|
// but the bottom-most child of the comment directly above is queued
|
2015-01-18 23:55:39 -08:00
|
|
|
$container = $prev.find(".comment").last().data("queue");
|
|
|
|
} else if ($down.hasClass("queue-container")) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// console.log("----------- case 4: Queued Sister -----------");
|
|
|
|
// 3.
|
|
|
|
// the comment just below is queued, so we use the same queue container as him
|
2015-01-18 23:55:39 -08:00
|
|
|
$container = $next.data("queue");
|
|
|
|
} else if ($up.hasClass('comment-displayed') || !$up.exists()) {
|
2013-10-21 23:07:36 +08:00
|
|
|
// console.log("----------- case 5: No Queue -----------");
|
|
|
|
// 4.
|
|
|
|
// we've found containers neither above or below, but
|
|
|
|
// A) we've hit a displayed comment or
|
|
|
|
// B) we've haven't found any comments (i.e. we're at the beginning of the list)
|
|
|
|
// so we put our queue container just before the comment
|
2015-01-18 23:55:39 -08:00
|
|
|
$container = $('<div class="queue-container"><ul></ul></div>').insertBefore($comment);
|
2013-10-21 23:07:36 +08:00
|
|
|
$container.click(function(e){
|
|
|
|
e.preventDefault();
|
2015-01-18 23:55:39 -08:00
|
|
|
var links = $(this).find("a");
|
2013-10-21 23:07:36 +08:00
|
|
|
links.each(function(){
|
2015-01-18 23:55:39 -08:00
|
|
|
var target = $(this).attr("href");
|
2013-10-21 23:07:36 +08:00
|
|
|
$(target).removeClass("comment-queued").addClass("comment-displayed");
|
2013-10-23 10:40:29 +08:00
|
|
|
// add comment ID to global array to avoid queuing it again
|
|
|
|
window.openedComments.push(target.substr(1));
|
2012-09-17 10:28:42 +09:00
|
|
|
});
|
2015-04-20 13:57:37 +09:00
|
|
|
// Telescope.utils.scrollPageTo(links.first().attr("href"));
|
2013-10-21 23:07:36 +08:00
|
|
|
$(this).hide("slow").remove();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// console.log("comment", $comment);
|
|
|
|
// console.log("up", $up);
|
|
|
|
// console.log("down", $down);
|
|
|
|
// console.log("queuedAncestors", $queuedAncestors);
|
|
|
|
// console.log("container", $container);
|
|
|
|
return $container;
|
|
|
|
};
|
2012-09-25 10:51:43 +09:00
|
|
|
|
2015-04-13 09:19:42 +09:00
|
|
|
Template.comment_item.created = function() {
|
2013-10-23 10:40:29 +08:00
|
|
|
// if comments are supposed to be queued, then queue this comment on create
|
|
|
|
this.isQueued = window.queueComments;
|
2014-07-13 11:15:58 +09:00
|
|
|
window.openedComments = [];
|
2014-09-16 15:18:27 -04:00
|
|
|
};
|
2012-09-17 10:28:42 +09:00
|
|
|
|
2015-04-13 09:19:42 +09:00
|
|
|
Template.comment_item.helpers({
|
2015-03-13 09:41:15 +09:00
|
|
|
commentClass: function () {
|
|
|
|
// if this comment was made by the post author
|
|
|
|
if (Posts.findOne(this.postId).userId == this.userId) {
|
|
|
|
return 'author-comment';
|
|
|
|
}
|
|
|
|
},
|
2013-10-09 20:11:58 +09:00
|
|
|
full_date: function(){
|
2014-07-03 10:15:19 +09:00
|
|
|
return this.createdAt.toString();
|
2013-10-09 20:11:58 +09:00
|
|
|
},
|
2015-01-20 16:18:27 +09:00
|
|
|
childComments: function(){
|
2013-10-09 20:23:20 +09:00
|
|
|
// return only child comments
|
2015-01-18 23:55:39 -08:00
|
|
|
return Comments.find({parentCommentId: this._id});
|
2013-10-09 20:11:58 +09:00
|
|
|
},
|
|
|
|
author: function(){
|
|
|
|
return Meteor.users.findOne(this.userId);
|
|
|
|
},
|
|
|
|
authorName: function(){
|
2015-04-20 13:57:37 +09:00
|
|
|
return Users.getAuthorName(this);
|
2013-10-09 20:11:58 +09:00
|
|
|
},
|
|
|
|
showChildComments: function(){
|
|
|
|
// TODO: fix this
|
|
|
|
// return Session.get('showChildComments');
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
ago: function(){
|
2014-11-20 14:55:34 +09:00
|
|
|
return this.createdAt;
|
2013-10-09 20:11:58 +09:00
|
|
|
},
|
|
|
|
upvoted: function(){
|
|
|
|
return Meteor.user() && _.include(this.upvoters, Meteor.user()._id);
|
|
|
|
},
|
|
|
|
downvoted: function(){
|
|
|
|
return Meteor.user() && _.include(this.downvoters, Meteor.user()._id);
|
2013-10-26 10:37:32 +09:00
|
|
|
},
|
|
|
|
profileUrl: function(){
|
|
|
|
var user = Meteor.users.findOne(this.userId);
|
2015-01-18 23:55:39 -08:00
|
|
|
if (user) {
|
2015-04-20 13:57:37 +09:00
|
|
|
return Users.getProfileUrl(user);
|
2015-01-18 23:55:39 -08:00
|
|
|
}
|
2014-09-26 23:06:12 +03:00
|
|
|
}
|
2013-10-09 20:11:58 +09:00
|
|
|
});
|
2012-09-17 10:28:42 +09:00
|
|
|
|
2015-01-18 23:55:39 -08:00
|
|
|
var handleVoteClick = function (meteorMethodName, eventName, e, instance) {
|
|
|
|
e.preventDefault();
|
2015-03-31 14:06:58 +09:00
|
|
|
e.stopImmediatePropagation(); // needed to prevent the handler running multiple times in nested comments
|
2015-01-18 23:55:39 -08:00
|
|
|
if (!Meteor.user()){
|
|
|
|
Router.go('atSignIn');
|
2015-03-27 16:24:21 +08:00
|
|
|
Messages.flash(i18n.t('please_log_in_first'), 'info');
|
2015-01-18 23:55:39 -08:00
|
|
|
} else {
|
|
|
|
Meteor.call(meteorMethodName, this, function(error, result){
|
2015-04-22 11:49:42 +09:00
|
|
|
Events.track(eventName, {
|
2015-01-18 23:55:39 -08:00
|
|
|
'commentId': instance.data._id,
|
|
|
|
'postId': instance.data.post,
|
|
|
|
'authorId': instance.data.userId
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2014-09-16 15:18:27 -04:00
|
|
|
};
|
2014-07-05 11:42:28 +09:00
|
|
|
|
2015-04-13 09:19:42 +09:00
|
|
|
Template.comment_item.events({
|
2015-01-18 23:55:39 -08:00
|
|
|
'click .not-upvoted .upvote': _.partial(handleVoteClick, 'upvoteComment', 'post upvoted'),
|
|
|
|
'click .upvoted .upvote': _.partial(handleVoteClick, 'cancelUpvoteComment', 'post upvote cancelled'),
|
|
|
|
'click .not-downvoted .downvote': _.partial(handleVoteClick, 'downvoteComment', 'post downvoted'),
|
|
|
|
'click .downvoted .downvote': _.partial(handleVoteClick, 'cancelDownvoteComment', 'post downvote cancelled')
|
2014-09-08 01:52:09 +08:00
|
|
|
});
|