Vulcan/client/templates/comment_item.js

143 lines
4.9 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;
2012-09-13 14:57:57 +09:00
console.log("body: "+comment.body+" | comment submission date: "+d+" | newCommentTimestamp: "+window.newCommentTimestamp+" | isNew: "+commentIsNew);
return commentIsNew;
};
2012-08-23 00:15:48 -04: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-13 14:57:57 +09:00
Template.comment_item.full_date = function(){
var submitted = new Date(this.submitted);
return submitted.toString();
};
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-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;
}
};
Template.comment_item.is_my_comment = function(){
if(this.user_id && Meteor.user() && Meteor.user()._id==this.user_id){
return true;
}
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-13 14:57:57 +09:00
})();
Template.comment_item.rendered=function(){
2012-09-13 14:57:57 +09:00
// t("comment_item");
if(this.data){
var comment=this.data;
if(Meteor.users.findOne(comment.user_id)){
var author=Meteor.users.findOne(comment.user_id).username;
}
console.log(comment);
var $comment=$("#"+comment._id);
var $commentParentList=$comment.closest(".comment-list");
2012-09-15 18:41:08 +09:00
if(false && Meteor.user() && Meteor.user()._id==comment.user_id){
// if user is logged in, and the comment belongs to the user, then never queue it
}else{
if(commentIsNew(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();
if($up.hasClass("queue-container")){
console.log("case 1", $up);
// we've found a container above our comment, so we use it to store our queue
$container=$up;
}else if($down.hasClass("queue-container")){
console.log("case 2", $down);
// we've found a container below our comment, so we use it to store our queue
$container=$down;
}else if($up.hasClass('comment-displayed') || !$up.exists()){
console.log("case 3", $up);
// 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(){
console.log($container);
$(this).find("a").each(function(){
var target=$(this).attr("href");
console.log("target: ", target);
$(target).removeClass("comment-queued").addClass("comment-displayed");
});
$(this).hide("slow").remove();
});
}else{
console.log("case 4");
}
console.log("up", $up);
console.log("down", $down);
console.log("container", $container);
var comment_link='<li class="icon-user"><a href="#'+comment._id+'" class="has-tooltip"><span class="tooltip"><span>'+author+'</span></span></a></li>';
$(comment_link).appendTo($container.find("ul")).hide().fadeIn("slow");
$comment.removeClass("comment-displayed").addClass("comment-queued");
2012-09-13 14:57:57 +09:00
}
}
}
}
Template.comment_item.helpers({
2012-09-15 18:41:08 +09:00
// isQueued: function() {
// commentIsNew(this);
// },
repress_recursion: function(){
if(window.repress_recursion){
return true;
}
return false;
2012-09-13 14:57:57 +09:00
},
ago: function(){
var submitted = new Date(this.submitted);
var timeAgo=jQuery.timeago(submitted);
return timeAgo;
}
});