Vulcan/client/templates/post_item.js

125 lines
3.2 KiB
JavaScript
Raw Normal View History

2012-09-06 10:18:26 +09:00
Template.post_item.events = {
'click .go-to-comments': function(e){
e.preventDefault();
2012-09-04 18:59:59 +09:00
// Session.set('selected_post', this);
Session.set('state', 'view_post');
2012-09-04 18:59:59 +09:00
Router.navigate('posts/'+this._id, {trigger: true});
}
2012-09-21 19:11:47 +09:00
, 'click .upvote-link': function(e){
e.preventDefault();
if(!Meteor.user()){
throwError("Please log in first");
return false;
}
2012-09-18 10:04:26 +10:00
Meteor.call('upvotePost', this._id);
}
, 'click .share-link': function(e){
var $this = $(e.target);
e.preventDefault();
$(".share-link").not($this).next().addClass("hidden");
$this.next().toggleClass("hidden");
2012-09-24 15:56:56 +09:00
console.log($this);
$this.next().find('.share-replace').sharrre(SharrreOptions);
// $(".overlay").toggleClass("hidden");
}
};
Template.post_item.created = function(){
2012-09-13 10:55:05 +09:00
if(this.data){
this.current_distance = (getRank(this.data)-1) * 80;
}
}
2012-09-06 10:18:26 +09:00
Template.post_item.rendered = function(){
// t("post_item");
2012-09-13 10:55:05 +09:00
if(this.data){
var new_distance=(getRank(this.data)-1)*80;
var old_distance=this.current_distance;
var $this=$(this.find(".post"));
var instance=this;
2012-09-13 10:55:05 +09:00
// at rendering time, move posts to their old place
$this.css("top", old_distance+"px");
2012-09-18 10:00:53 +09:00
Meteor.setTimeout(function() {
2012-09-13 10:55:05 +09:00
// then a few milliseconds after, move the to their new spot
$this.css("top", new_distance+"px");
// we don't want elements to be animated the first ever time they load, so we only set the class "animate" after that
$this.addClass("animate");
instance.current_distance=new_distance;
2012-09-13 10:55:05 +09:00
}, 100);
}
2012-09-12 08:53:13 +09:00
2012-09-24 15:56:56 +09:00
};
2012-09-12 08:53:13 +09:00
Template.post_item.preserve({
2012-09-13 11:05:49 +09:00
'.post': function (node) {return node.id; }
2012-09-12 08:53:13 +09:00
});
2012-09-06 10:18:26 +09:00
Template.post_item.ago = function(){
2012-09-18 15:43:37 +09:00
return moment(this.submitted).fromNow();
};
2012-09-06 10:18:26 +09:00
Template.post_item.voted = function(){
var user = Meteor.user();
if(!user) return false;
2012-09-11 18:41:14 +10:00
2012-09-18 10:04:26 +10:00
return _.include(this.upvoters, user._id);
};
2012-09-12 08:53:13 +09:00
var getRank = function(post){
2012-09-18 11:49:17 +09:00
if(window.sortBy=="score"){
var filter = {$or: [
{score: {$gt: post.score}},
{$and: [{score: post.score}, {submitted: {$lt: post.submitted}}]}
]};
}else{
var filter = {$or: [
{submitted: {$gt: post.submitted}},
{$and: [{submitted: post.submitted}, {score: {$lt: post.score}}]}
]};
}
2012-09-12 08:53:13 +09:00
return Posts.find(filter).count()+1;
}
Template.post_item.rank = function() {
return getRank(this);
}
2012-09-06 10:18:26 +09:00
Template.post_item.domain = function(){
var a = document.createElement('a');
a.href = this.url;
return a.hostname;
};
2012-09-18 10:25:15 +09:00
Template.post_item.current_domain = function(){
return "http://"+document.domain;
}
Template.post_item.can_edit = function(){
2012-09-29 12:48:37 +09:00
if(Meteor.user() && (Meteor.user().isAdmin || Meteor.userId() === this.user_id))
2012-09-29 12:47:40 +09:00
return true;
2012-09-27 23:18:02 -07:00
else
return false;
};
2012-09-27 16:56:55 -07:00
Template.post_item.authorName = function(){
2012-09-06 12:27:59 +09:00
if(this.user_id && Meteor.users.findOne(this.user_id)){
return Meteor.users.findOne(this.user_id).username;
}else{
return this.author;
}
2012-09-08 11:54:08 +09:00
};
Template.post_item.short_score = function(){
return Math.floor(this.score*1000)/1000;
}
2012-09-08 11:54:08 +09:00
Template.post_item.body_formatted = function(){
var converter = new Markdown.Converter();
var html_body=converter.makeHtml(this.body);
return html_body.autoLink();
}