Vulcan/client/templates/comment_item.js

213 lines
8.2 KiB
JavaScript
Raw Normal View History

2012-09-13 14:57:57 +09:00
(function(){
2012-09-05 11:55:06 +09:00
2012-09-13 14:57:57 +09:00
commentIsNew=function(comment){
var d=new Date(comment.submitted);
2012-09-15 18:41:08 +09:00
var commentIsNew=d > window.newCommentTimestamp;
// console.log("body: "+comment.body+" | comment submission date: "+d+" | newCommentTimestamp: "+window.newCommentTimestamp+" | isNew: "+commentIsNew);
2012-09-13 14:57:57 +09:00
return commentIsNew;
};
2012-08-23 00:15:48 -04:00
2012-09-17 10:28:42 +09:00
findQueueContainer=function($comment){
// go up and down the DOM until we find either A) a queue container or B) an unqueued comment
$up=$comment.prevAll(".queue-container, .comment-displayed").first();
$down=$comment.nextAll(".queue-container, .comment-displayed").first();
$prev=$comment.prev();
$next=$comment.next();
2012-09-17 10:28:42 +09:00
$queuedAncestors=$comment.parents(".comment-queued");
if($queuedAncestors.exists()){
// console.log("----------- case 1: Queued Ancestor -----------");
// 1.
2012-09-17 10:28:42 +09:00
// our comment has one or more queued ancestor, so we look for the root-most
// ancestor's queue container
$container=$queuedAncestors.last().data("queue");
}else if($prev.hasClass("queue-container")){
// console.log("----------- case 2: Queued Brother -----------");
// 2.
// the comment just above is queued, so we use the same queue container as him
$container=$prev.data("queue");
}else if($prev.find(".comment").last().hasClass("comment-queued")){
// 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
$container=$prev.find(".comment").last().data("queue");
2012-09-17 10:28:42 +09:00
}else if($down.hasClass("queue-container")){
// console.log("----------- case 4: Queued Sister -----------");
// 3.
// the comment just below is queued, so we use the same queue container as him
$container=$next.data("queue");
2012-09-17 10:28:42 +09:00
}else if($up.hasClass('comment-displayed') || !$up.exists()){
// console.log("----------- case 5: No Queue -----------");
// 4.
// we've found containers neither above or below, but
2012-09-17 10:28:42 +09:00
// 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
$container=$('<div class="queue-container"><ul></ul></div>').insertBefore($comment);
$container.click(function(e){
e.preventDefault();
var links=$(this).find("a");
links.each(function(){
2012-09-17 10:28:42 +09:00
var target=$(this).attr("href");
$(target).removeClass("comment-queued").addClass("comment-displayed");
var openedComments=sessionGetObject('openedComments') || [];
openedComments.push(target.substr(1));
sessionSetObject('openedComments', openedComments);
2012-09-17 10:28:42 +09:00
});
// scrollPageTo(links.first().attr("href"));
2012-09-17 10:28:42 +09: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);
2012-09-17 10:28:42 +09:00
return $container;
};
Template.comment_item.helpers({
full_date: function(){
var submitted = new Date(this.submitted);
return submitted.toString();
}
,child_comments: function(){
var post_id = Session.get('selectedPostId');
var comments = Comments.find({ post: post_id, parent: this._id });
return comments;
}
,author: function(){
2012-10-01 16:08:30 +09:00
return Meteor.users.findOne(this.userId);
}
,authorName: function(){
return getAuthorName(this);
}
,can_edit: function(){
2012-10-01 16:08:30 +09:00
if(this.userId && Meteor.userId())
return Meteor.user().isAdmin || (Meteor.userId() === this.userId);
2012-09-27 23:18:02 -07:00
else
return false;
}
,body_formatted: function(){
if(this.body){
var converter = new Markdown.Converter();
var html_body=converter.makeHtml(this.body);
return html_body.autoLink();
}
}
,repress_recursion: function(){
2012-09-27 16:56:55 -07:00
return window.repress_recursion;
}
,ago: function(){
return moment(this.submitted).fromNow();
}
,upvoted: function(){
2012-09-27 16:56:55 -07:00
return Meteor.user() && _.include(this.upvoters, Meteor.user()._id);
}
,downvoted: function(){
2012-09-27 16:56:55 -07:00
return Meteor.user() && _.include(this.downvoters, Meteor.user()._id);
}
});
Template.comment_item.created=function(){
2012-09-28 17:20:49 +09:00
// this.firstRender=true;
}
2012-09-17 10:28:42 +09:00
Template.comment_item.rendered=function(){
// t("comment_item");
2012-09-17 10:28:42 +09:00
if(this.data){
var comment=this.data;
var $comment=$("#"+comment._id);
var openedComments=sessionGetObject('openedComments') || [];
2012-09-17 10:28:42 +09:00
2012-10-01 16:08:30 +09:00
if(Meteor.user() && Meteor.user()._id==comment.userId){
2012-09-17 10:28:42 +09:00
// if user is logged in, and the comment belongs to the user, then never queue it
}else{
if(commentIsNew(comment) && !$comment.hasClass("comment-queued") && openedComments.indexOf(comment._id)==-1){
2012-09-17 10:28:42 +09:00
// if comment is new and has not already been previously queued
// note: testing on the class works because Meteor apparently preserves newly assigned CSS classes
// across template renderings
// TODO: save scroll position
2012-10-01 16:08:30 +09:00
if(Meteor.users.findOne(comment.userId)){
2012-09-17 10:28:42 +09:00
// get comment author name
2012-10-01 16:08:30 +09:00
var user=Meteor.users.findOne(comment.userId);
2012-09-18 08:29:03 +09:00
var author=user.username;
2012-09-18 12:21:43 +09:00
var imgURL=Gravatar.getGravatar(user, {
d: 'http://telesc.pe/img/default_avatar.png',
2012-09-18 11:24:27 +09:00
s: 30
});
2012-09-17 10:28:42 +09:00
var $container=findQueueContainer($comment);
2012-09-18 12:21:43 +09:00
var comment_link='<li class="icon-user"><a href="#'+comment._id+'" class="has-tooltip" style="background-image:url('+imgURL+')"><span class="tooltip"><span>'+author+'</span></span></a></li>';
if(this.firstRender){
2012-09-28 17:20:49 +09:00
// TODO: fix re-rendering problem with timer
$(comment_link).appendTo($container.find("ul")).hide().fadeIn("slow");
this.firstRender=false;
}else{
$(comment_link).appendTo($container.find("ul"));
}
2012-09-17 10:28:42 +09:00
$comment.removeClass("comment-displayed").addClass("comment-queued");
$comment.data("queue", $container);
// TODO: take the user back to their previous scroll position
2012-09-18 08:29:03 +09:00
}
}
2012-09-17 10:28:42 +09:00
}
}
}
2012-09-13 14:57:57 +09:00
Template.comment_item.events = {
'click .queue-comment': function(e){
e.preventDefault();
var current_comment_id=$(event.target).closest(".comment").attr("id");
var now = new Date();
var comment_id = Comments.update(current_comment_id,
{
$set: {
submitted: new Date().getTime()
}
2012-09-13 14:57:57 +09:00
}
);
},
'click .not-upvoted .upvote': function(e, instance){
2012-09-18 09:29:31 +10:00
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
}
Meteor.call('upvoteComment', this._id, function(error, result){
2012-10-01 16:08:30 +09:00
trackEvent("post upvoted", {'commentId':instance.data._id, 'postId': instance.data.post, 'authorId':instance.data.userId});
});
},
'click .upvoted .upvote': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
}
Meteor.call('cancelUpvoteComment', this._id, function(error, result){
2012-10-01 16:08:30 +09:00
trackEvent("post upvote cancelled", {'commentId':instance.data._id, 'postId': instance.data.post, 'authorId':instance.data.userId});
});
},
'click .not-downvoted .downvote': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
}
Meteor.call('downvoteComment', this._id, function(error, result){
2012-10-01 16:08:30 +09:00
trackEvent("post downvoted", {'commentId':instance.data._id, 'postId': instance.data.post, 'authorId':instance.data.userId});
});
},
'click .downvoted .downvote': function(e, instance){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
}
Meteor.call('cancelDownvoteComment', this._id, function(error, result){
2012-10-01 16:08:30 +09:00
trackEvent("post downvote cancelled", {'commentId':instance.data._id, 'postId': instance.data.post, 'authorId':instance.data.userId});
});
2012-09-13 14:57:57 +09:00
}
};
})();