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;
|
2012-09-17 10:32:40 +09:00
|
|
|
// 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();
|
2012-09-17 12:19:01 +09:00
|
|
|
$prev=$comment.prev();
|
|
|
|
$next=$comment.next();
|
2012-09-17 10:28:42 +09:00
|
|
|
$queuedAncestors=$comment.parents(".comment-queued");
|
|
|
|
if($queuedAncestors.exists()){
|
2012-09-20 08:09:45 +09:00
|
|
|
// console.log("----------- case 1: Queued Ancestor -----------");
|
2012-09-17 10:28:42 +09:00
|
|
|
// 1.
|
|
|
|
// our comment has one or more queued ancestor, so we look for the root-most
|
|
|
|
// ancestor's queue container
|
|
|
|
$container=$queuedAncestors.last().data("queue");
|
2012-09-17 12:19:01 +09:00
|
|
|
}else if($prev.hasClass("queue-container")){
|
2012-09-20 08:09:45 +09:00
|
|
|
// console.log("----------- case 2: Queued Brother -----------");
|
2012-09-17 10:28:42 +09:00
|
|
|
// 2.
|
2012-09-17 12:19:01 +09:00
|
|
|
// 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")){
|
2012-09-20 08:09:45 +09:00
|
|
|
// console.log("----------- case 3: Queued Cousin -----------");
|
2012-09-17 12:19:01 +09:00
|
|
|
// 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")){
|
2012-09-20 08:09:45 +09:00
|
|
|
// console.log("----------- case 4: Queued Sister -----------");
|
2012-09-17 10:28:42 +09:00
|
|
|
// 3.
|
2012-09-17 12:19:01 +09:00
|
|
|
// 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()){
|
2012-09-20 08:09:45 +09:00
|
|
|
// console.log("----------- case 5: No Queue -----------");
|
2012-09-17 10:28:42 +09:00
|
|
|
// 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
|
|
|
|
$container=$('<div class="queue-container"><ul></ul></div>').insertBefore($comment);
|
|
|
|
$container.click(function(){
|
|
|
|
$(this).find("a").each(function(){
|
|
|
|
var target=$(this).attr("href");
|
|
|
|
$(target).removeClass("comment-queued").addClass("comment-displayed");
|
|
|
|
});
|
|
|
|
$(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;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
Template.comment_item.rendered=function(){
|
2012-09-19 10:05:02 +09:00
|
|
|
// t("comment_item");
|
2012-09-17 10:28:42 +09:00
|
|
|
if(this.data){
|
|
|
|
var comment=this.data;
|
|
|
|
var $comment=$("#"+comment._id);
|
|
|
|
|
2012-09-17 12:51:35 +09:00
|
|
|
if(Meteor.user() && Meteor.user()._id==comment.user_id){
|
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")){
|
|
|
|
// 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
|
|
|
|
if(Meteor.users.findOne(comment.user_id)){
|
|
|
|
// get comment author name
|
2012-09-18 08:29:03 +09:00
|
|
|
var user=Meteor.users.findOne(comment.user_id);
|
|
|
|
var author=user.username;
|
|
|
|
|
|
|
|
// TODO: add gravatar support
|
|
|
|
// var email=user.email;
|
2012-09-18 12:21:43 +09:00
|
|
|
var imgURL=Gravatar.getGravatar(user, {
|
2012-09-18 11:24:27 +09:00
|
|
|
d: 'http://telescope.herokuapp.com/img/default_avatar.png',
|
|
|
|
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>';
|
2012-09-17 10:28:42 +09:00
|
|
|
$(comment_link).appendTo($container.find("ul")).hide().fadeIn("slow");
|
|
|
|
$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 .goto-comment': function(event){
|
|
|
|
event.preventDefault();
|
|
|
|
var href=event.target.href.replace(/^(?:\/\/|[^\/]+)*\//, "");
|
2012-08-23 00:35:31 -04:00
|
|
|
|
2012-09-13 14:57:57 +09:00
|
|
|
Session.set('selected_comment', this);
|
|
|
|
// Session.set('state', 'reply');
|
|
|
|
Router.navigate(href, {trigger: true});
|
|
|
|
},
|
|
|
|
'click .open-comment-link': function(e){
|
|
|
|
e.preventDefault();
|
|
|
|
$(event.target).closest(".comment").removeClass("queued");
|
|
|
|
},
|
|
|
|
'click .queue-comment': function(e){
|
|
|
|
e.preventDefault();
|
|
|
|
var current_comment_id=$(event.target).closest(".comment").attr("id");
|
|
|
|
var now = new Date();
|
|
|
|
console.log("now: ", now.toString());
|
|
|
|
var comment_id = Comments.update(current_comment_id,
|
|
|
|
{
|
|
|
|
$set: {
|
|
|
|
submitted: new Date().getTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// $(event.target).closest(".comment").addClass("queued");
|
2012-09-18 09:29:31 +10:00
|
|
|
},
|
|
|
|
'click .upvote': function(e) {
|
|
|
|
e.preventDefault();
|
2012-09-20 08:09:45 +09:00
|
|
|
Meteor.call('upvoteComment', this._id);
|
|
|
|
},
|
|
|
|
'click .downvote': function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
Meteor.call('downvoteComment', this._id);
|
2012-09-13 14:57:57 +09:00
|
|
|
}
|
|
|
|
};
|
2012-09-06 09:59:03 +09:00
|
|
|
|
2012-09-13 14:57:57 +09:00
|
|
|
Template.comment_item.full_date = function(){
|
|
|
|
var submitted = new Date(this.submitted);
|
|
|
|
return submitted.toString();
|
|
|
|
};
|
2012-09-06 09:59:03 +09:00
|
|
|
|
2012-09-13 14:57:57 +09:00
|
|
|
Template.comment_item.child_comments = function(){
|
|
|
|
var post_id = Session.get('selected_post_id');
|
|
|
|
var comments = Comments.find({ post: post_id, parent: this._id });
|
|
|
|
return comments;
|
|
|
|
};
|
2012-09-08 10:23:58 +09:00
|
|
|
|
2012-09-13 14:57:57 +09:00
|
|
|
Template.comment_item.author = function(){
|
|
|
|
if(Meteor.users.findOne(this.user_id)){
|
|
|
|
return Meteor.users.findOne(this.user_id).username;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-09-19 10:05:02 +09:00
|
|
|
Template.comment_item.can_edit = function(){
|
|
|
|
if(this.user_id){
|
|
|
|
if(Meteor.user() && (isAdmin(Meteor.user()) || Meteor.user()._id==this.user_id)){
|
|
|
|
return true;
|
|
|
|
}
|
2012-09-13 14:57:57 +09:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
};
|
|
|
|
|
|
|
|
Template.comment_item.body_formatted = function(){
|
|
|
|
if(this.body){
|
|
|
|
var converter = new Markdown.Converter();
|
|
|
|
var html_body=converter.makeHtml(this.body);
|
|
|
|
return html_body.autoLink();
|
|
|
|
}
|
2012-09-08 12:11:26 +09:00
|
|
|
}
|
2012-09-19 10:05:02 +09:00
|
|
|
|
|
|
|
Template.comment_item.is_admin = function(){
|
|
|
|
return currentUserIsAdmin();
|
|
|
|
};
|
|
|
|
|
2012-09-13 14:57:57 +09:00
|
|
|
})();
|
2012-09-09 13:38:56 +09:00
|
|
|
|
2012-09-11 11:09:16 +09:00
|
|
|
|
2012-09-19 10:05:02 +09:00
|
|
|
|
2012-09-09 13:38:56 +09:00
|
|
|
Template.comment_item.helpers({
|
2012-09-15 18:41:08 +09:00
|
|
|
// isQueued: function() {
|
|
|
|
// commentIsNew(this);
|
|
|
|
// },
|
2012-09-10 22:35:35 +09:00
|
|
|
repress_recursion: function(){
|
|
|
|
if(window.repress_recursion){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2012-09-13 14:57:57 +09:00
|
|
|
},
|
|
|
|
ago: function(){
|
2012-09-18 15:43:37 +09:00
|
|
|
return moment(this.submitted).fromNow();
|
2012-09-20 08:20:39 +09:00
|
|
|
},
|
|
|
|
upvoted: function(){
|
|
|
|
var user = Meteor.user();
|
|
|
|
if(!user) return false;
|
|
|
|
return _.include(this.upvoters, user._id);
|
|
|
|
},
|
|
|
|
downvoted: function(){
|
|
|
|
var user = Meteor.user();
|
|
|
|
if(!user) return false;
|
|
|
|
return _.include(this.downvoters, user._id);
|
2012-09-09 13:38:56 +09:00
|
|
|
}
|
|
|
|
});
|